budibase/packages/server/initialise/createMasterDb.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

const { initialiseData, setupDatastore } = require("@budibase/core")
const constructHierarchy = require("../utilities/constructHierarchy")
const getDatabaseManager = require("../utilities/databaseManager")
const {
getApisForUser,
getApisWithFullAccess,
} = require("../utilities/budibaseApi")
const masterDbAppDefinition = require("../appPackages/_master/appDefinition.json")
const masterDbAccessLevels = require("../appPackages/_master/access_levels.json")
const { masterAppPackage } = require("../utilities/createAppPackage")
2019-06-07 15:18:10 +02:00
2019-07-09 08:29:50 +02:00
module.exports = async (context, datastoreModule, username, password) => {
try {
const { config } = context
const databaseManager = getDatabaseManager(
datastoreModule,
config.datastoreConfig
)
2019-06-07 15:18:10 +02:00
await databaseManager.createEmptyMasterDb()
const masterDbConfig = databaseManager.masterDatastoreConfig
const datastore = setupDatastore(
datastoreModule.getDatastore(masterDbConfig)
)
2019-06-07 15:18:10 +02:00
await initialiseData(datastore, constructHierarchy(masterDbAppDefinition))
2019-06-07 15:18:10 +02:00
const masterPackage = masterAppPackage(context)
const bbMaster = await getApisWithFullAccess(datastore, masterPackage)
await bbMaster.authApi.saveAccessLevels(masterDbAccessLevels)
const user = bbMaster.authApi.getNewUser()
user.name = username
user.accessLevels = ["owner"]
await bbMaster.authApi.createUser(user, password)
2019-06-07 15:18:10 +02:00
return await getApisForUser(datastore, masterPackage, username, password)
} catch (e) {
throw e
}
}