budibase/packages/server/initialise/createMasterDb.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-06-07 15:18:10 +02:00
const {initialiseData, setupDatastore,
getTemplateApi} = require("budibase-core");
2019-06-14 11:05:46 +02:00
const getDatabaseManager = require("../utilities/databaseManager");
const {getApisForUser, getApisWithFullAccess} = require("../utilities/budibaseApi");
2019-06-07 15:18:10 +02:00
const masterDbAppDefinition = require("../appPackages/master/appDefinition.json");
const masterDbAccessLevels = require("../appPackages/master/access_levels.json");
module.exports = async (datastoreModule, rootConfig, username, password) => {
try {
const databaseManager = getDatabaseManager(datastoreModule, rootConfig);
await databaseManager.createEmptyMasterDb();
const masterDbConfig = databaseManager.masterDatastoreConfig;
const datastore = setupDatastore(
datastoreModule.getDatastore(masterDbConfig)
);
const templateApi = getTemplateApi({datastore});
await initialiseData(datastore, {
heirarchy:templateApi.constructHeirarchy(masterDbAppDefinition.hierarchy),
actions:masterDbAppDefinition.actions,
triggers:masterDbAppDefinition.triggers
});
const bbMaster = await getApisWithFullAccess(datastore);
await bbMaster.authApi.saveAccessLevels(masterDbAccessLevels);
const user = bbMaster.authApi.getNewUser();
user.name = username;
user.accessLevels= ["owner"];
await bbMaster.authApi.createUser(user, password);
return await getApisForUser(datastore, username, password);
} catch(e) {
throw e;
}
};