2019-09-20 12:44:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-05-18 10:16:03 +02:00
|
|
|
const knex = require('knex');
|
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
const { getDialect } = require('./dialects');
|
2021-05-18 10:16:03 +02:00
|
|
|
const createSchemaProvider = require('./schema');
|
|
|
|
const createMetadata = require('./metadata');
|
2021-06-17 16:17:15 +02:00
|
|
|
const { createEntityManager } = require('./entity-manager');
|
|
|
|
|
|
|
|
// TODO: move back into strapi
|
|
|
|
const { transformContentTypes } = require('./utils/content-types');
|
2021-05-18 10:16:03 +02:00
|
|
|
// const Configuration = require('./configuration');
|
|
|
|
// const { resolveConnector } = require('./connector');
|
2021-05-10 15:36:09 +02:00
|
|
|
|
|
|
|
class Database {
|
|
|
|
constructor(config) {
|
2021-06-02 15:53:22 +02:00
|
|
|
this.metadata = createMetadata(config.models);
|
2021-05-18 10:16:03 +02:00
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
// TODO:; validate meta
|
2021-06-02 15:53:22 +02:00
|
|
|
// this.metadata.validate();
|
2021-05-18 10:16:03 +02:00
|
|
|
|
|
|
|
// this.connector = resolveConnector(this.config);
|
|
|
|
|
|
|
|
this.connection = knex(config.connection);
|
2021-06-17 16:17:15 +02:00
|
|
|
this.dialect = getDialect(this.connection);
|
2021-05-18 10:16:03 +02:00
|
|
|
|
2021-06-02 15:53:22 +02:00
|
|
|
this.schema = createSchemaProvider(this);
|
2021-05-18 10:16:03 +02:00
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
// TODO: migrations -> allow running them through cli before startup
|
2021-05-10 15:36:09 +02:00
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
this.entityManager = createEntityManager(this);
|
2021-05-10 15:36:09 +02:00
|
|
|
}
|
2021-05-17 16:34:19 +02:00
|
|
|
|
2021-05-18 10:16:03 +02:00
|
|
|
query(uid) {
|
2021-06-17 16:17:15 +02:00
|
|
|
return this.entityManager.getRepository(uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroy() {
|
|
|
|
await this.connection.destroy();
|
2021-05-17 16:34:19 +02:00
|
|
|
}
|
2021-05-10 15:36:09 +02:00
|
|
|
}
|
2019-09-20 12:44:24 +02:00
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
Database.transformContentTypes = transformContentTypes;
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
module.exports = {
|
2021-05-10 15:36:09 +02:00
|
|
|
Database,
|
2019-09-20 12:44:24 +02:00
|
|
|
};
|