strapi/packages/core/database.old/lib/database-manager.js

178 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-09-20 12:44:24 +02:00
'use strict';
const _ = require('lodash');
const { createQuery } = require('./queries');
const createConnectorRegistry = require('./connector-registry');
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
this.connectors = createConnectorRegistry({
connections: strapi.config.get('database.connections'),
defaultConnection: strapi.config.get('database.defaultConnection'),
});
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;
this.connectors.load();
2019-09-20 12:44:24 +02:00
validateModelSchemas({ strapi: this.strapi, manager: this });
2019-11-15 11:49:32 +01:00
this.initializeModelsMap();
await this.connectors.initialize();
2019-09-20 12:44:24 +02:00
return this;
}
async destroy() {
2020-12-29 17:44:14 +01:00
await Promise.all(this.connectors.getAll().map(connector => connector.destroy()));
}
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`);
}
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)
.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;
}
getModelFromStrapi(name, plugin) {
2019-09-20 12:44:24 +02:00
const key = _.toLower(name);
if (plugin === 'admin') {
return _.get(strapi.admin, ['models', key]);
}
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
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);
}
}
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;
});
}
getModelByGlobalId(globalId) {
return Array.from(this.models.values()).find(model => {
return model.globalId === globalId;
});
}
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 [];
}
getModelsByPluginName(pluginName) {
if (!pluginName) {
return strapi.models;
}
return pluginName === 'admin' ? strapi.admin.models : strapi.plugins[pluginName].models;
}
getReservedNames() {
return {
models: constants.RESERVED_MODEL_NAMES,
attributes: [
...constants.RESERVED_ATTRIBUTE_NAMES,
...(strapi.db.connectors.default.defaultTimestamps || []),
],
};
}
2019-09-20 12:44:24 +02:00
}
function createDatabaseManager(strapi) {
return new DatabaseManager(strapi);
}
module.exports = {
createDatabaseManager,
};