pangea-server 1.0.62 → 1.0.64
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.
|
@@ -12,7 +12,8 @@ type FindOneOptions = {
|
|
|
12
12
|
order?: Order;
|
|
13
13
|
paranoid?: boolean;
|
|
14
14
|
};
|
|
15
|
-
type AggregateOneOptions = {
|
|
15
|
+
type AggregateOneOptions<BM extends BaseModel> = {
|
|
16
|
+
where?: Where<BM>;
|
|
16
17
|
paranoid?: boolean;
|
|
17
18
|
};
|
|
18
19
|
type FindManyOptions<BM extends BaseModel> = {
|
|
@@ -36,7 +37,7 @@ export declare class Db {
|
|
|
36
37
|
constructor(tx: Tx);
|
|
37
38
|
findOneOrNull<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM | null>;
|
|
38
39
|
findOne<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM>;
|
|
39
|
-
aggregateOne<BM extends BaseModel>(model: BaseModelCtor<BM>, attributes: Attributes, options?: AggregateOneOptions): Promise<BM | null>;
|
|
40
|
+
aggregateOne<BM extends BaseModel>(model: BaseModelCtor<BM>, attributes: Attributes, options?: AggregateOneOptions<BM>): Promise<BM | null>;
|
|
40
41
|
private __getFindOneBaseOptions;
|
|
41
42
|
findMany<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyPagedOptions<BM>): Promise<BM[]>;
|
|
42
43
|
count<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyOptions<BM>): Promise<number>;
|
|
@@ -37,7 +37,7 @@ class Db {
|
|
|
37
37
|
return model.findOne({
|
|
38
38
|
...baseOptions,
|
|
39
39
|
attributes: getFinalAttributes(model, attributes),
|
|
40
|
-
...getInclude(model, { attributes }),
|
|
40
|
+
...getInclude(model, { attributes, where: options.where }),
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
__getFindOneBaseOptions(paranoid) {
|
|
@@ -78,9 +78,7 @@ class Db {
|
|
|
78
78
|
return model.findAll({
|
|
79
79
|
...baseOptions,
|
|
80
80
|
attributes: getFinalAttributes(model, config.attributes),
|
|
81
|
-
...(config.group && {
|
|
82
|
-
group: config.group.map((field) => (field.includes('.') ? field : `${model.name}.${(0, pangea_helpers_1.camelToSnake)(field)}`)),
|
|
83
|
-
}),
|
|
81
|
+
...(config.group && { group: config.group.map((field) => formatField(model.name, field)) }),
|
|
84
82
|
});
|
|
85
83
|
}
|
|
86
84
|
__getFindManyBaseOptions(model, options = {}) {
|
|
@@ -198,17 +196,22 @@ class Db {
|
|
|
198
196
|
}
|
|
199
197
|
exports.Db = Db;
|
|
200
198
|
// internal functions
|
|
199
|
+
function formatField(modelName, field) {
|
|
200
|
+
if (field.includes('.')) {
|
|
201
|
+
const parts = field.split('.');
|
|
202
|
+
const last = parts.pop();
|
|
203
|
+
return [...parts, (0, pangea_helpers_1.camelToSnake)(last)].join('.');
|
|
204
|
+
}
|
|
205
|
+
return `${modelName}.${(0, pangea_helpers_1.camelToSnake)(field)}`;
|
|
206
|
+
}
|
|
201
207
|
function getFinalAttributes(model, attributes) {
|
|
202
208
|
if (!attributes)
|
|
203
209
|
return;
|
|
204
210
|
return Object.entries(attributes).map(([alias, column]) => {
|
|
205
|
-
if (typeof column === 'string')
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const [op, fieldName] = column;
|
|
210
|
-
const field = fieldName.includes('.') ? fieldName : `${model.name}.${(0, pangea_helpers_1.camelToSnake)(fieldName)}`;
|
|
211
|
-
return [(0, sequelize_1.fn)(op, (0, sequelize_1.col)(field)), alias];
|
|
211
|
+
if (typeof column === 'string')
|
|
212
|
+
return [(0, sequelize_1.col)(formatField(model.name, column)), alias];
|
|
213
|
+
const [op, field] = column;
|
|
214
|
+
return [(0, sequelize_1.fn)(op, (0, sequelize_1.col)(formatField(model.name, field))), alias];
|
|
212
215
|
});
|
|
213
216
|
}
|
|
214
217
|
function convertWhereKeys(obj) {
|
|
@@ -231,7 +234,7 @@ function convertWhereKeys(obj) {
|
|
|
231
234
|
const parts = inner.split('.');
|
|
232
235
|
if (parts.length >= 2) {
|
|
233
236
|
const last = parts.pop();
|
|
234
|
-
newKey = `$${[...parts
|
|
237
|
+
newKey = `$${[...parts, (0, pangea_helpers_1.camelToSnake)(last)].join('.')}$`;
|
|
235
238
|
}
|
|
236
239
|
}
|
|
237
240
|
newObj[newKey] = convertWhereKeys(value);
|
|
@@ -258,7 +261,7 @@ function getInclude(model, config = {}, relDepth = new Map()) {
|
|
|
258
261
|
const relModel = rel.getModelFn();
|
|
259
262
|
includeOptions.push({
|
|
260
263
|
model: relModel,
|
|
261
|
-
as:
|
|
264
|
+
as: relName,
|
|
262
265
|
attributes: !config.attributes ? undefined : [],
|
|
263
266
|
required: rel.required,
|
|
264
267
|
paranoid: rel.paranoid,
|
|
@@ -25,8 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.HasMany = exports.HasOne = exports.BelongsTo = void 0;
|
|
27
27
|
const seq = __importStar(require("sequelize-typescript"));
|
|
28
|
-
// helpers
|
|
29
|
-
const pangea_helpers_1 = require("pangea-helpers");
|
|
30
28
|
exports.BelongsTo = getRelation(seq.BelongsTo);
|
|
31
29
|
exports.HasOne = getRelation(seq.HasOne);
|
|
32
30
|
exports.HasMany = getRelation(seq.HasMany);
|
|
@@ -35,11 +33,10 @@ function getRelation(relationFn) {
|
|
|
35
33
|
return function (getModelFn, options = {}) {
|
|
36
34
|
return function (target, propertyName) {
|
|
37
35
|
const { foreignKey, ...relOptions } = options;
|
|
38
|
-
|
|
39
|
-
relationFn(getModelFn, { as: alias, ...(foreignKey && { foreignKey }) })(target, propertyName);
|
|
36
|
+
relationFn(getModelFn, { as: propertyName, ...(foreignKey && { foreignKey }) })(target, propertyName);
|
|
40
37
|
const model = target.constructor;
|
|
41
38
|
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, {
|
|
39
|
+
model.AddRelation(propertyName, { getModelFn, eager, required, paranoid, joinDepth, ...restOptions });
|
|
43
40
|
};
|
|
44
41
|
};
|
|
45
42
|
}
|