budibase/packages/server/initialise/createInstanceDb.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-06-28 23:59:27 +02:00
const {
initialiseData,
2019-07-07 10:03:37 +02:00
setupDatastore,
common
2019-09-10 07:14:45 +02:00
} = require("@budibase/core");
2019-07-01 23:49:13 +02:00
const getDatabaseManager = require("../utilities/databaseManager");
2019-07-07 10:03:37 +02:00
const { applictionVersionPackage } = require("../utilities/createAppPackage");
const { last } = require("lodash/fp");
const {$,splitKey} = common;
2019-06-28 23:59:27 +02:00
2019-07-07 10:03:37 +02:00
2019-07-09 08:29:50 +02:00
module.exports = async (context, datastoreModule, app, instance) => {
2019-06-28 23:59:27 +02:00
try {
const databaseManager = getDatabaseManager(
2019-07-09 08:29:50 +02:00
datastoreModule,
context.config.datastoreConfig);
2019-06-28 23:59:27 +02:00
await databaseManager.createEmptyInstanceDb(
2019-07-07 10:03:37 +02:00
app.id, instance.id);
2019-06-28 23:59:27 +02:00
const dbConfig = databaseManager.getInstanceDatastoreConfig(
2019-07-07 10:03:37 +02:00
app.id, instance.id);
2019-06-28 23:59:27 +02:00
const datastore = setupDatastore(
2019-07-07 10:03:37 +02:00
datastoreModule.getDatastore(dbConfig));
const versionId = $(instance.version.key, [
splitKey,
last
]);
2019-07-09 08:29:50 +02:00
const appPackage = await applictionVersionPackage(
context, app.name, versionId, instance.key
2019-07-07 10:03:37 +02:00
);
await initialiseData(
datastore,
appPackage.appDefinition,
appPackage.accessLevels);
2019-06-28 23:59:27 +02:00
return dbConfig;
} catch(e) {
throw e;
}
};