pangea-server 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -26,13 +26,9 @@ function initDatabase(models, seeds, config = {}) {
|
|
|
26
26
|
const dbClient = getDbClient();
|
|
27
27
|
await dbClient.authenticate();
|
|
28
28
|
(0, helpers_1.printSuccess)('database', 'database connected');
|
|
29
|
-
if ((0, helpers_1.getEnvStr)('ENVIRONMENT') !== '
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
await (0, seed_helpers_1.syncTables)();
|
|
33
|
-
if ((0, helpers_1.getEnvBool)('DB_SEED_TABLES'))
|
|
34
|
-
await (0, seed_helpers_1.seedTables)(models, seeds);
|
|
35
|
-
}
|
|
29
|
+
if ((0, helpers_1.getEnvStr)('ENVIRONMENT') !== 'development')
|
|
30
|
+
return;
|
|
31
|
+
await buildDatabase(models, seeds);
|
|
36
32
|
}
|
|
37
33
|
catch (err) {
|
|
38
34
|
(0, helpers_1.printDanger)('database', err);
|
|
@@ -67,3 +63,10 @@ function logQuery(sql, timing) {
|
|
|
67
63
|
const level = timing <= 50 ? 'success' : timing <= 200 ? 'warning' : 'danger';
|
|
68
64
|
logFns[level]('timing', `${timing} ms`);
|
|
69
65
|
}
|
|
66
|
+
async function buildDatabase(models, seeds) {
|
|
67
|
+
if ((0, helpers_1.getEnvBool)('DB_DROP_TABLES'))
|
|
68
|
+
await (0, seed_helpers_1.dropTables)();
|
|
69
|
+
await (0, seed_helpers_1.syncTables)();
|
|
70
|
+
if ((0, helpers_1.getEnvBool)('DB_SEED_TABLES'))
|
|
71
|
+
await (0, seed_helpers_1.seedTables)(models, seeds);
|
|
72
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Models, Seeds } from './database.types';
|
|
2
2
|
export declare function dropTables(): Promise<void>;
|
|
3
|
+
export declare function emptyTables(): Promise<void>;
|
|
3
4
|
export declare function syncTables(): Promise<void>;
|
|
4
5
|
export declare function seedTables<M extends Models>(models: M, seeds: Seeds<M>): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.seedTables = exports.syncTables = exports.dropTables = void 0;
|
|
3
|
+
exports.seedTables = exports.syncTables = exports.emptyTables = exports.dropTables = void 0;
|
|
4
4
|
const db_client_1 = require("./db-client");
|
|
5
5
|
const db_class_1 = require("./db.class");
|
|
6
6
|
// helpers
|
|
@@ -12,6 +12,13 @@ async function dropTables() {
|
|
|
12
12
|
(0, helpers_1.printSuccess)('database', 'tables dropped');
|
|
13
13
|
}
|
|
14
14
|
exports.dropTables = dropTables;
|
|
15
|
+
async function emptyTables() {
|
|
16
|
+
(0, helpers_1.printInfo)('database', 'emptying tables...');
|
|
17
|
+
const dbClient = (0, db_client_1.getDbClient)();
|
|
18
|
+
await dbClient.truncate({ truncate: true, cascade: true });
|
|
19
|
+
(0, helpers_1.printSuccess)('database', 'tables emptied');
|
|
20
|
+
}
|
|
21
|
+
exports.emptyTables = emptyTables;
|
|
15
22
|
async function syncTables() {
|
|
16
23
|
const dbClient = (0, db_client_1.getDbClient)();
|
|
17
24
|
await dbClient.sync();
|
|
@@ -27,10 +34,7 @@ async function seedTables(models, seeds) {
|
|
|
27
34
|
const db = new db_class_1.Db(tx);
|
|
28
35
|
try {
|
|
29
36
|
await db.disableForeignKeyChecks();
|
|
30
|
-
await Promise.all(modelsWithData.map(
|
|
31
|
-
const data = seeds[modelName];
|
|
32
|
-
await db.insertMany(models[modelName], data);
|
|
33
|
-
}));
|
|
37
|
+
await Promise.all(modelsWithData.map((modelName) => db.insertMany(models[modelName], seeds[modelName])));
|
|
34
38
|
await db.enableForeignKeyChecks();
|
|
35
39
|
await tx.commit();
|
|
36
40
|
}
|