2019-09-10 07:14:45 +02:00
|
|
|
const {initialiseData, setupDatastore} = require("@budibase/core");
|
2019-06-28 23:59:27 +02:00
|
|
|
const constructHierarchy = require("../utilities/constructHierarchy");
|
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");
|
2019-06-25 23:48:22 +02:00
|
|
|
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) => {
|
2019-06-07 15:18:10 +02:00
|
|
|
try {
|
2019-07-09 08:29:50 +02:00
|
|
|
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-28 23:59:27 +02:00
|
|
|
await initialiseData(datastore,
|
|
|
|
constructHierarchy(masterDbAppDefinition));
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2019-07-09 08:29:50 +02:00
|
|
|
const masterPackage = masterAppPackage(context);
|
2019-06-25 23:48:22 +02:00
|
|
|
const bbMaster = await getApisWithFullAccess(
|
2019-07-09 08:29:50 +02:00
|
|
|
datastore, masterPackage);
|
2019-06-07 15:18:10 +02:00
|
|
|
await bbMaster.authApi.saveAccessLevels(masterDbAccessLevels);
|
|
|
|
const user = bbMaster.authApi.getNewUser();
|
|
|
|
user.name = username;
|
|
|
|
user.accessLevels= ["owner"];
|
|
|
|
await bbMaster.authApi.createUser(user, password);
|
|
|
|
|
2019-07-09 08:29:50 +02:00
|
|
|
return await getApisForUser(
|
|
|
|
datastore,
|
|
|
|
masterPackage,
|
|
|
|
username,
|
|
|
|
password);
|
2019-06-07 15:18:10 +02:00
|
|
|
} catch(e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|