pangea-server 1.0.3 → 1.0.5
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.
- package/dist/authentication/base-auth.class.d.ts +1 -0
- package/dist/authentication/base-auth.class.js +3 -0
- package/dist/database/db-client.js +10 -7
- package/dist/database/db.class.d.ts +1 -1
- package/dist/database/db.class.js +10 -18
- package/dist/database/seed.helpers.d.ts +1 -0
- package/dist/database/seed.helpers.js +9 -5
- package/package.json +1 -1
|
@@ -4,6 +4,7 @@ export declare abstract class BaseAuth<AM extends AuthMap> {
|
|
|
4
4
|
constructor(authUsers: AuthUsers<AM>);
|
|
5
5
|
free(): void;
|
|
6
6
|
notSetYet(): void;
|
|
7
|
+
notAllowed(): void;
|
|
7
8
|
isUserAuth<T extends UserType<AM>>(type: T): NonNullable<AuthUsers<AM>[T]>;
|
|
8
9
|
isAnyUserAuth<const T extends readonly UserType<AM>[]>(types: T): { [I in keyof T]: InstanceType<AM[T[I]]> | null; };
|
|
9
10
|
}
|
|
@@ -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
|
+
}
|
|
@@ -3,7 +3,7 @@ type AttributeField = string;
|
|
|
3
3
|
type AttributeOp = 'COUNT' | 'SUM';
|
|
4
4
|
type AttributeColumn = AttributeField | [AttributeOp, AttributeField];
|
|
5
5
|
type AttributeAlias = string;
|
|
6
|
-
type Attribute =
|
|
6
|
+
type Attribute = AttributeField | Record<AttributeAlias, AttributeColumn>;
|
|
7
7
|
type IncludeItem = {
|
|
8
8
|
relation?: string;
|
|
9
9
|
active?: boolean;
|
|
@@ -48,13 +48,9 @@ class Db {
|
|
|
48
48
|
const deletedAtWhere = paranoid === 'deleted' ? { deletedAt: { [_1.Ops.not]: null } } : {};
|
|
49
49
|
const baseOptions = {
|
|
50
50
|
attributes: getFinalAttributes(attributes),
|
|
51
|
-
...getIncludeAndWhere(model, include, {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...deletedAtWhere,
|
|
55
|
-
}),
|
|
56
|
-
...(group && { group }),
|
|
57
|
-
order: [...getFinalOrder(order), ['createdAt', 'DESC']],
|
|
51
|
+
...getIncludeAndWhere(model, include, { ...where, ...searchWhere, ...deletedAtWhere }),
|
|
52
|
+
...(group && { group: group.map(pangea_helpers_1.camelToSnake) }),
|
|
53
|
+
order: [...getFinalOrder(order), ...(!group ? [['createdAt', 'DESC']] : [])],
|
|
58
54
|
paranoid: paranoid === 'active',
|
|
59
55
|
subQuery: false,
|
|
60
56
|
transaction: this.__tx,
|
|
@@ -71,10 +67,7 @@ class Db {
|
|
|
71
67
|
limit: pageSize,
|
|
72
68
|
});
|
|
73
69
|
const ids = idRows.map((row) => row.id);
|
|
74
|
-
return model.findAll({
|
|
75
|
-
...baseOptions,
|
|
76
|
-
where: { id: { [_1.Ops.in]: ids } },
|
|
77
|
-
});
|
|
70
|
+
return model.findAll({ ...baseOptions, where: { id: { [_1.Ops.in]: ids } } });
|
|
78
71
|
})(),
|
|
79
72
|
model.count({ ...baseOptions, col: 'id', distinct: true }),
|
|
80
73
|
]);
|
|
@@ -92,11 +85,7 @@ class Db {
|
|
|
92
85
|
}
|
|
93
86
|
insertMany(model, data, options = {}) {
|
|
94
87
|
return handleDbError(async () => {
|
|
95
|
-
const newInstances = await model.bulkCreate(data, {
|
|
96
|
-
...options,
|
|
97
|
-
validate: true,
|
|
98
|
-
transaction: this.__tx,
|
|
99
|
-
});
|
|
88
|
+
const newInstances = await model.bulkCreate(data, { ...options, validate: true, transaction: this.__tx });
|
|
100
89
|
return newInstances.length;
|
|
101
90
|
});
|
|
102
91
|
}
|
|
@@ -174,9 +163,12 @@ exports.Db = Db;
|
|
|
174
163
|
function getFinalAttributes(attributes) {
|
|
175
164
|
if (!attributes)
|
|
176
165
|
return;
|
|
177
|
-
return attributes.map((
|
|
166
|
+
return attributes.map((attribute) => {
|
|
167
|
+
if (typeof attribute === 'string')
|
|
168
|
+
return [(0, sequelize_1.col)((0, pangea_helpers_1.camelToSnake)(attribute)), attribute];
|
|
169
|
+
const [alias, column] = Object.entries(attribute)[0];
|
|
178
170
|
if (typeof column === 'string')
|
|
179
|
-
return [(0, sequelize_1.col)(column), alias];
|
|
171
|
+
return [(0, sequelize_1.col)((0, pangea_helpers_1.camelToSnake)(column)), alias];
|
|
180
172
|
const [op, field] = column;
|
|
181
173
|
return [(0, sequelize_1.fn)(op, (0, sequelize_1.col)(field)), alias];
|
|
182
174
|
});
|
|
@@ -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
|
}
|