Alexandre Bodin 0f3f984ea7 Init migration v4
- Hooks registry
- D&P CT migrations
- i18N CT migrations
- Umzug with js / sql migrations
- Eslint updates
2021-09-13 12:03:12 +02:00

60 lines
1.4 KiB
JavaScript

'use strict';
const knex = require('knex');
const { getDialect } = require('./dialects');
const createSchemaProvider = require('./schema');
const createMigrationProvider = require('./migration');
const createMetadata = require('./metadata');
const { createEntityManager } = require('./entity-manager');
const { createLifecyclesManager } = require('./lifecycles');
// TODO: move back into strapi
const { transformContentTypes } = require('./utils/content-types');
class Database {
constructor(config) {
this.metadata = createMetadata(config.models);
this.config = config;
this.dialect = getDialect(this);
this.dialect.configure();
this.connection = knex(this.config.connection);
this.dialect.initialize();
this.schema = createSchemaProvider(this);
this.migration = createMigrationProvider(this);
this.lifecycles = createLifecyclesManager(this);
this.entityManager = createEntityManager(this);
}
query(uid) {
if (!this.metadata.has(uid)) {
throw new Error(`Model ${uid} not found`);
}
return this.entityManager.getRepository(uid);
}
queryBuilder(uid) {
return this.entityManager.createQueryBuilder(uid);
}
async destroy() {
await this.lifecycles.clear();
await this.connection.destroy();
}
}
// TODO: move into strapi
Database.transformContentTypes = transformContentTypes;
Database.init = async config => new Database(config);
module.exports = {
Database,
};