pangea-server 1.0.58 → 1.0.59
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.
|
@@ -4,30 +4,23 @@ type AttributeOp = 'COUNT' | 'SUM';
|
|
|
4
4
|
type AttributeColumn = AttributeField | [AttributeOp, AttributeField];
|
|
5
5
|
type AttributeAlias = string;
|
|
6
6
|
type Attributes = Record<AttributeAlias, AttributeColumn>;
|
|
7
|
-
type IncludeItem = {
|
|
8
|
-
relation?: string;
|
|
9
|
-
active?: boolean;
|
|
10
|
-
include?: IncludeItem[];
|
|
11
|
-
};
|
|
12
7
|
type Filters<BM extends BaseModel> = ModelId | Where<BM> | undefined;
|
|
13
8
|
type Group = string[];
|
|
14
9
|
type Order = Record<string, 'asc' | 'desc'>;
|
|
15
10
|
type FindOneOptions = {
|
|
16
11
|
scopes?: string[];
|
|
17
12
|
attributes?: Attributes;
|
|
18
|
-
include?: IncludeItem[];
|
|
19
13
|
order?: Order;
|
|
20
14
|
paranoid?: boolean;
|
|
21
15
|
};
|
|
22
16
|
type FindManyOptions<BM extends BaseModel> = {
|
|
23
17
|
attributes?: Attributes;
|
|
24
|
-
include?: IncludeItem[];
|
|
25
18
|
where?: Where<BM>;
|
|
26
19
|
searchFields?: string[];
|
|
27
20
|
search?: string | null;
|
|
28
21
|
group?: Group;
|
|
29
22
|
order?: Order;
|
|
30
|
-
paranoid?:
|
|
23
|
+
paranoid?: boolean;
|
|
31
24
|
};
|
|
32
25
|
type FindManyPagedOptions<BM extends BaseModel> = FindManyOptions<BM> & {
|
|
33
26
|
page?: number | null;
|
|
@@ -13,8 +13,8 @@ class Db {
|
|
|
13
13
|
}
|
|
14
14
|
// find methods
|
|
15
15
|
findOneOrNull(model, filters, options = {}) {
|
|
16
|
-
const { scopes
|
|
17
|
-
const scopedModel = scopes
|
|
16
|
+
const { scopes, attributes, order, paranoid = true } = options;
|
|
17
|
+
const scopedModel = scopes?.length ? model.scope(...scopes) : model;
|
|
18
18
|
const baseOptions = {
|
|
19
19
|
attributes: getFinalAttributes(attributes),
|
|
20
20
|
paranoid,
|
|
@@ -22,11 +22,11 @@ class Db {
|
|
|
22
22
|
transaction: this.__tx,
|
|
23
23
|
};
|
|
24
24
|
if (typeof filters === 'number') {
|
|
25
|
-
return scopedModel.findByPk(filters, { ...baseOptions, ...
|
|
25
|
+
return scopedModel.findByPk(filters, { ...baseOptions, ...getInclude(model) });
|
|
26
26
|
}
|
|
27
27
|
return scopedModel.findOne({
|
|
28
28
|
...baseOptions,
|
|
29
|
-
...
|
|
29
|
+
...getInclude(model, { attributes, where: filters }),
|
|
30
30
|
order: getFinalOrder(order),
|
|
31
31
|
});
|
|
32
32
|
}
|
|
@@ -67,7 +67,7 @@ class Db {
|
|
|
67
67
|
return { instances, totalCount: instances.length };
|
|
68
68
|
}
|
|
69
69
|
__getFindManyBaseOptions(model, options = {}) {
|
|
70
|
-
const { attributes,
|
|
70
|
+
const { attributes, where, searchFields, search, group, order, paranoid = true } = options;
|
|
71
71
|
let searchWhere;
|
|
72
72
|
if (searchFields?.length && search) {
|
|
73
73
|
const searchWords = search.split(/\s+/).filter(Boolean);
|
|
@@ -80,13 +80,12 @@ class Db {
|
|
|
80
80
|
})),
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
const deletedAtWhere = paranoid === 'deleted' ? { deletedAt: { [_1.Ops.not]: null } } : {};
|
|
84
83
|
return {
|
|
85
84
|
attributes: getFinalAttributes(attributes),
|
|
86
|
-
...
|
|
85
|
+
...getInclude(model, { attributes, where: { ...where, ...searchWhere } }),
|
|
87
86
|
...(group && { group: group.map(pangea_helpers_1.camelToSnake) }),
|
|
88
|
-
order:
|
|
89
|
-
paranoid
|
|
87
|
+
order: getFinalOrder(order),
|
|
88
|
+
paranoid,
|
|
90
89
|
subQuery: false,
|
|
91
90
|
transaction: this.__tx,
|
|
92
91
|
};
|
|
@@ -223,17 +222,16 @@ function convertWhereKeys(obj) {
|
|
|
223
222
|
}
|
|
224
223
|
return obj;
|
|
225
224
|
}
|
|
226
|
-
function
|
|
225
|
+
function getInclude(model, config = {}, relDepth = new Map()) {
|
|
227
226
|
const includeOptions = [];
|
|
228
|
-
const cleanWhere = { ...where };
|
|
227
|
+
const cleanWhere = { ...config.where };
|
|
229
228
|
for (const [relName, rel] of Object.entries(model.Relations)) {
|
|
230
229
|
const relKey = `${model.name}.${relName}`;
|
|
231
230
|
const currentDepth = relDepth.get(relKey) || 0;
|
|
232
231
|
const maxDepth = rel.joinDepth;
|
|
233
232
|
if (currentDepth >= maxDepth)
|
|
234
233
|
continue;
|
|
235
|
-
|
|
236
|
-
if (!rel.eager || !active)
|
|
234
|
+
if (!rel.eager)
|
|
237
235
|
continue;
|
|
238
236
|
const relWhere = cleanWhere[relName];
|
|
239
237
|
delete cleanWhere[relName];
|
|
@@ -242,11 +240,11 @@ function getIncludeAndWhere(model, includeItems, config = {}, where, relDepth =
|
|
|
242
240
|
const relModel = rel.getModelFn();
|
|
243
241
|
includeOptions.push({
|
|
244
242
|
model: relModel,
|
|
245
|
-
as: rel.
|
|
246
|
-
attributes: !config.
|
|
243
|
+
as: rel.alias,
|
|
244
|
+
attributes: !config.attributes ? undefined : [],
|
|
247
245
|
required: rel.required,
|
|
248
246
|
paranoid: rel.paranoid,
|
|
249
|
-
...
|
|
247
|
+
...getInclude(relModel, { ...config, where: relWhere }, newRelDepth),
|
|
250
248
|
});
|
|
251
249
|
}
|
|
252
250
|
return {
|
|
@@ -254,12 +252,15 @@ function getIncludeAndWhere(model, includeItems, config = {}, where, relDepth =
|
|
|
254
252
|
...(Reflect.ownKeys(cleanWhere).length && { where: convertWhereKeys(cleanWhere) }),
|
|
255
253
|
};
|
|
256
254
|
}
|
|
257
|
-
function getFinalOrder(order) {
|
|
258
|
-
|
|
255
|
+
function getFinalOrder(order, config = {}) {
|
|
256
|
+
if (!order)
|
|
257
|
+
return;
|
|
258
|
+
const finalOrder = Object.entries(order).map(([key, value]) => {
|
|
259
259
|
const parts = key.split('.');
|
|
260
260
|
const direction = value.toUpperCase();
|
|
261
261
|
return [...parts, direction];
|
|
262
262
|
});
|
|
263
|
+
return [...finalOrder, ...(!config.attributes ? [['createdAt', 'DESC']] : [])];
|
|
263
264
|
}
|
|
264
265
|
async function handleDbError(operation) {
|
|
265
266
|
try {
|
|
@@ -35,11 +35,11 @@ function getRelation(relationFn) {
|
|
|
35
35
|
return function (getModelFn, options = {}) {
|
|
36
36
|
return function (target, propertyName) {
|
|
37
37
|
const { foreignKey, ...relOptions } = options;
|
|
38
|
-
const
|
|
39
|
-
relationFn(getModelFn, { as, ...(foreignKey && { foreignKey }) })(target, propertyName);
|
|
38
|
+
const alias = (0, pangea_helpers_1.camelToSnake)(propertyName);
|
|
39
|
+
relationFn(getModelFn, { as: alias, ...(foreignKey && { foreignKey }) })(target, propertyName);
|
|
40
40
|
const model = target.constructor;
|
|
41
41
|
const { eager = relationFn !== seq.HasMany, required = relationFn === seq.BelongsTo ? !model.Columns[`${propertyName}Id`].allowNull : false, paranoid = relationFn !== seq.BelongsTo, joinDepth = 1, ...restOptions } = relOptions;
|
|
42
|
-
model.AddRelation(propertyName, {
|
|
42
|
+
model.AddRelation(propertyName, { alias, getModelFn, eager, required, paranoid, joinDepth, ...restOptions });
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
45
|
}
|
|
@@ -35,17 +35,13 @@ function Table(tableName, options = {}) {
|
|
|
35
35
|
const allIndexes = [...tableIndexes, ...columnIndexes];
|
|
36
36
|
seq.Table({
|
|
37
37
|
tableName,
|
|
38
|
-
// modelName: camelToSnake(target.name), // 'as' in SQL query
|
|
39
38
|
charset: 'utf8mb4',
|
|
40
39
|
collate: 'utf8mb4_general_ci',
|
|
41
40
|
underscored: true,
|
|
42
41
|
timestamps: true,
|
|
43
42
|
paranoid: true,
|
|
44
43
|
indexes: allIndexes.map(({ field, fields, ...restIndex }) => {
|
|
45
|
-
return {
|
|
46
|
-
...restIndex,
|
|
47
|
-
fields: fields ? fields.map(pangea_helpers_1.camelToSnake) : field ? [(0, pangea_helpers_1.camelToSnake)(field)] : [],
|
|
48
|
-
};
|
|
44
|
+
return { ...restIndex, fields: fields ? fields.map(pangea_helpers_1.camelToSnake) : field ? [(0, pangea_helpers_1.camelToSnake)(field)] : [] };
|
|
49
45
|
}),
|
|
50
46
|
})(target);
|
|
51
47
|
const excludedAttributes = target.ExcludedAttributes;
|