2019-09-20 12:44:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const { createQuery } = require('./queries');
|
2020-04-28 15:32:29 +02:00
|
|
|
const createConnectorRegistry = require('./connector-registry');
|
2020-04-27 20:39:34 +02:00
|
|
|
const constants = require('./constants');
|
|
|
|
const { validateModelSchemas } = require('./validation');
|
2021-02-12 12:52:29 +01:00
|
|
|
const createMigrationManager = require('./migration-manager');
|
2021-02-16 12:08:35 +01:00
|
|
|
const createLifecycleManager = require('./lifecycle-manager');
|
2019-09-20 12:44:24 +02:00
|
|
|
|
|
|
|
class DatabaseManager {
|
|
|
|
constructor(strapi) {
|
|
|
|
this.strapi = strapi;
|
|
|
|
|
|
|
|
this.initialized = false;
|
2019-12-10 14:21:20 +01:00
|
|
|
|
2020-04-28 15:32:29 +02:00
|
|
|
this.connectors = createConnectorRegistry({
|
2020-04-29 16:11:11 +02:00
|
|
|
connections: strapi.config.get('database.connections'),
|
|
|
|
defaultConnection: strapi.config.get('database.defaultConnection'),
|
2020-04-28 15:32:29 +02:00
|
|
|
});
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
this.queries = new Map();
|
2019-11-15 11:49:32 +01:00
|
|
|
this.models = new Map();
|
2021-02-12 12:52:29 +01:00
|
|
|
|
|
|
|
this.migrations = createMigrationManager(this);
|
2021-02-17 15:51:47 +01:00
|
|
|
this.lifecycles = createLifecycleManager();
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async initialize() {
|
|
|
|
if (this.initialized === true) {
|
|
|
|
throw new Error('Database manager already initialized');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
2020-04-28 15:32:29 +02:00
|
|
|
this.connectors.load();
|
2019-09-20 12:44:24 +02:00
|
|
|
|
2020-04-28 15:32:29 +02:00
|
|
|
validateModelSchemas({ strapi: this.strapi, manager: this });
|
2020-03-30 12:50:32 +02:00
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
this.initializeModelsMap();
|
|
|
|
|
2021-04-08 18:05:12 +02:00
|
|
|
await this.connectors.initialize();
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
async destroy() {
|
2020-12-29 17:44:14 +01:00
|
|
|
await Promise.all(this.connectors.getAll().map(connector => connector.destroy()));
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
initializeModelsMap() {
|
|
|
|
Object.keys(this.strapi.models).forEach(modelKey => {
|
|
|
|
const model = this.strapi.models[modelKey];
|
|
|
|
this.models.set(model.uid, model);
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(this.strapi.admin.models).forEach(modelKey => {
|
|
|
|
const model = this.strapi.admin.models[modelKey];
|
|
|
|
this.models.set(model.uid, model);
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(this.strapi.plugins).forEach(pluginKey => {
|
2021-07-08 11:20:13 +02:00
|
|
|
Object.keys(this.strapi.plugins[pluginKey].models || {}).forEach(modelKey => {
|
2019-11-15 11:49:32 +01:00
|
|
|
const model = this.strapi.plugins[pluginKey].models[modelKey];
|
|
|
|
this.models.set(model.uid, model);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
query(entity, plugin) {
|
|
|
|
if (!entity) {
|
|
|
|
throw new Error(`argument entity is required`);
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:05:12 +02:00
|
|
|
const model = this.getModel(entity, plugin);
|
2019-09-20 12:44:24 +02:00
|
|
|
|
|
|
|
if (!model) {
|
|
|
|
throw new Error(`The model ${entity} can't be found.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.queries.has(model.uid)) {
|
|
|
|
return this.queries.get(model.uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const connectorQuery = this.connectors
|
|
|
|
.get(model.orm)
|
2019-12-17 14:05:15 +01:00
|
|
|
.queries({ model, modelKey: model.modelName, strapi });
|
2019-09-20 12:44:24 +02:00
|
|
|
|
2019-12-17 20:59:57 +01:00
|
|
|
const query = createQuery({
|
|
|
|
connectorQuery,
|
|
|
|
model,
|
|
|
|
});
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
this.queries.set(model.uid, query);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:05:12 +02:00
|
|
|
getModelFromStrapi(name, plugin) {
|
2019-09-20 12:44:24 +02:00
|
|
|
const key = _.toLower(name);
|
|
|
|
if (plugin === 'admin') {
|
|
|
|
return _.get(strapi.admin, ['models', key]);
|
|
|
|
}
|
|
|
|
|
2020-05-05 20:38:41 +03:00
|
|
|
if (plugin) {
|
|
|
|
return _.get(strapi.plugins, [plugin, 'models', key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.get(strapi, ['models', key]) || _.get(strapi, ['components', key]);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
2019-12-10 16:21:21 +01:00
|
|
|
|
2021-04-08 18:05:12 +02:00
|
|
|
getModel(name, plugin) {
|
|
|
|
const key = _.toLower(name);
|
|
|
|
|
|
|
|
if (this.models.has(key)) {
|
|
|
|
const { modelName, plugin: pluginName } = this.models.get(key);
|
|
|
|
return this.getModelFromStrapi(modelName, pluginName);
|
|
|
|
} else {
|
|
|
|
return this.getModelFromStrapi(key, plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 11:09:17 +02:00
|
|
|
getModelByAssoc(assoc) {
|
|
|
|
return this.getModel(assoc.collection || assoc.model, assoc.plugin);
|
|
|
|
}
|
|
|
|
|
2019-12-10 16:21:21 +01:00
|
|
|
getModelByCollectionName(collectionName) {
|
|
|
|
return Array.from(this.models.values()).find(model => {
|
|
|
|
return model.collectionName === collectionName;
|
|
|
|
});
|
|
|
|
}
|
2020-03-19 16:46:27 +01:00
|
|
|
|
|
|
|
getModelByGlobalId(globalId) {
|
|
|
|
return Array.from(this.models.values()).find(model => {
|
|
|
|
return model.globalId === globalId;
|
|
|
|
});
|
|
|
|
}
|
2020-04-27 20:39:34 +02:00
|
|
|
|
2020-09-22 12:31:26 +02:00
|
|
|
getModelsByAttribute(attr) {
|
|
|
|
if (attr.type === 'component') {
|
|
|
|
return [this.getModel(attr.component)];
|
|
|
|
}
|
|
|
|
if (attr.type === 'dynamiczone') {
|
|
|
|
return attr.components.map(compoName => this.getModel(compoName));
|
|
|
|
}
|
|
|
|
if (attr.model || attr.collection) {
|
|
|
|
return [this.getModelByAssoc(attr)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:20:10 +02:00
|
|
|
getModelsByPluginName(pluginName) {
|
|
|
|
if (!pluginName) {
|
|
|
|
return strapi.models;
|
|
|
|
}
|
|
|
|
return pluginName === 'admin' ? strapi.admin.models : strapi.plugins[pluginName].models;
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:32:29 +02:00
|
|
|
getReservedNames() {
|
2020-04-27 20:39:34 +02:00
|
|
|
return {
|
2020-04-29 10:55:47 +02:00
|
|
|
models: constants.RESERVED_MODEL_NAMES,
|
2020-04-27 20:39:34 +02:00
|
|
|
attributes: [
|
|
|
|
...constants.RESERVED_ATTRIBUTE_NAMES,
|
2020-04-28 15:32:29 +02:00
|
|
|
...(strapi.db.connectors.default.defaultTimestamps || []),
|
2020-04-27 20:39:34 +02:00
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDatabaseManager(strapi) {
|
|
|
|
return new DatabaseManager(strapi);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createDatabaseManager,
|
|
|
|
};
|