@zenstackhq/orm 3.8.0-beta.3 → 3.8.1
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/index.cjs +58 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -1
- package/dist/index.d.mts +17 -1
- package/dist/index.mjs +58 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.d.cts
CHANGED
|
@@ -3407,7 +3407,7 @@ declare class ORMError extends Error {
|
|
|
3407
3407
|
sqlParams?: readonly unknown[];
|
|
3408
3408
|
}
|
|
3409
3409
|
declare namespace query_utils_d_exports {
|
|
3410
|
-
export { TEMP_ALIAS_PREFIX, aggregate, buildJoinPairs, ensureArray, extractFieldName, extractIdFields, extractModelName, fieldHasDefaultValue, flattenCompoundUniqueFilters, getDelegateDescendantModels, getDelegateDiscriminatorValue, getDiscriminatorField, getEnum, getField, getIdFields, getIdValues, getManyToManyRelation, getModel, getModelFields, getRelationForeignKeyFieldPairs, getTypeDef, getUniqueFields, hasModel, isEnum, isForeignKeyField, isInheritedField, isRelationField, isScalarField, isTypeDef, isUnsupportedField, makeDefaultOrderBy, requireField, requireIdFields, requireModel, requireTypeDef, stripAlias, tmpAlias };
|
|
3410
|
+
export { ManyToManyJoinTableEndpoints, TEMP_ALIAS_PREFIX, aggregate, buildJoinPairs, ensureArray, extractFieldName, extractIdFields, extractModelName, fieldHasDefaultValue, flattenCompoundUniqueFilters, getDelegateDescendantModels, getDelegateDiscriminatorValue, getDiscriminatorField, getEnum, getField, getIdFields, getIdValues, getManyToManyJoinTable, getManyToManyRelation, getModel, getModelFields, getRelationForeignKeyFieldPairs, getTypeDef, getUniqueFields, hasModel, isEnum, isForeignKeyField, isInheritedField, isRelationField, isScalarField, isTypeDef, isUnsupportedField, makeDefaultOrderBy, requireField, requireIdFields, requireModel, requireTypeDef, stripAlias, tmpAlias };
|
|
3411
3411
|
}
|
|
3412
3412
|
declare function hasModel(schema: SchemaDef, model: string): boolean;
|
|
3413
3413
|
declare function getModel(schema: SchemaDef, model: string): ModelDef | undefined;
|
|
@@ -3467,6 +3467,22 @@ declare function getManyToManyRelation(schema: SchemaDef, model: string, field:
|
|
|
3467
3467
|
otherPKName: string;
|
|
3468
3468
|
joinTable: string;
|
|
3469
3469
|
} | undefined;
|
|
3470
|
+
/**
|
|
3471
|
+
* Endpoints of an implicit many-to-many relation, identified by its join table name.
|
|
3472
|
+
*/
|
|
3473
|
+
interface ManyToManyJoinTableEndpoints {
|
|
3474
|
+
model: string;
|
|
3475
|
+
field: string;
|
|
3476
|
+
otherModel: string;
|
|
3477
|
+
otherField: string;
|
|
3478
|
+
}
|
|
3479
|
+
/**
|
|
3480
|
+
* Resolve the relation endpoints for an implicit many-to-many join table by its table
|
|
3481
|
+
* name, or `undefined` if the table is not an implicit m2m join table. The join-table
|
|
3482
|
+
* index is built once per schema (single pass) and reused, making this an O(1) lookup.
|
|
3483
|
+
* See issue #2715.
|
|
3484
|
+
*/
|
|
3485
|
+
declare function getManyToManyJoinTable(schema: SchemaDef, joinTableName: string): ManyToManyJoinTableEndpoints | undefined;
|
|
3470
3486
|
/**
|
|
3471
3487
|
* Convert filter like `{ id1_id2: { id1: 1, id2: 1 } }` to `{ id1: 1, id2: 1 }`
|
|
3472
3488
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -3407,7 +3407,7 @@ declare class ORMError extends Error {
|
|
|
3407
3407
|
sqlParams?: readonly unknown[];
|
|
3408
3408
|
}
|
|
3409
3409
|
declare namespace query_utils_d_exports {
|
|
3410
|
-
export { TEMP_ALIAS_PREFIX, aggregate, buildJoinPairs, ensureArray, extractFieldName, extractIdFields, extractModelName, fieldHasDefaultValue, flattenCompoundUniqueFilters, getDelegateDescendantModels, getDelegateDiscriminatorValue, getDiscriminatorField, getEnum, getField, getIdFields, getIdValues, getManyToManyRelation, getModel, getModelFields, getRelationForeignKeyFieldPairs, getTypeDef, getUniqueFields, hasModel, isEnum, isForeignKeyField, isInheritedField, isRelationField, isScalarField, isTypeDef, isUnsupportedField, makeDefaultOrderBy, requireField, requireIdFields, requireModel, requireTypeDef, stripAlias, tmpAlias };
|
|
3410
|
+
export { ManyToManyJoinTableEndpoints, TEMP_ALIAS_PREFIX, aggregate, buildJoinPairs, ensureArray, extractFieldName, extractIdFields, extractModelName, fieldHasDefaultValue, flattenCompoundUniqueFilters, getDelegateDescendantModels, getDelegateDiscriminatorValue, getDiscriminatorField, getEnum, getField, getIdFields, getIdValues, getManyToManyJoinTable, getManyToManyRelation, getModel, getModelFields, getRelationForeignKeyFieldPairs, getTypeDef, getUniqueFields, hasModel, isEnum, isForeignKeyField, isInheritedField, isRelationField, isScalarField, isTypeDef, isUnsupportedField, makeDefaultOrderBy, requireField, requireIdFields, requireModel, requireTypeDef, stripAlias, tmpAlias };
|
|
3411
3411
|
}
|
|
3412
3412
|
declare function hasModel(schema: SchemaDef, model: string): boolean;
|
|
3413
3413
|
declare function getModel(schema: SchemaDef, model: string): ModelDef | undefined;
|
|
@@ -3467,6 +3467,22 @@ declare function getManyToManyRelation(schema: SchemaDef, model: string, field:
|
|
|
3467
3467
|
otherPKName: string;
|
|
3468
3468
|
joinTable: string;
|
|
3469
3469
|
} | undefined;
|
|
3470
|
+
/**
|
|
3471
|
+
* Endpoints of an implicit many-to-many relation, identified by its join table name.
|
|
3472
|
+
*/
|
|
3473
|
+
interface ManyToManyJoinTableEndpoints {
|
|
3474
|
+
model: string;
|
|
3475
|
+
field: string;
|
|
3476
|
+
otherModel: string;
|
|
3477
|
+
otherField: string;
|
|
3478
|
+
}
|
|
3479
|
+
/**
|
|
3480
|
+
* Resolve the relation endpoints for an implicit many-to-many join table by its table
|
|
3481
|
+
* name, or `undefined` if the table is not an implicit m2m join table. The join-table
|
|
3482
|
+
* index is built once per schema (single pass) and reused, making this an O(1) lookup.
|
|
3483
|
+
* See issue #2715.
|
|
3484
|
+
*/
|
|
3485
|
+
declare function getManyToManyJoinTable(schema: SchemaDef, joinTableName: string): ManyToManyJoinTableEndpoints | undefined;
|
|
3470
3486
|
/**
|
|
3471
3487
|
* Convert filter like `{ id1_id2: { id1: 1, id2: 1 } }` to `{ id1: 1, id2: 1 }`
|
|
3472
3488
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -195,6 +195,7 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
|
195
195
|
getField: () => getField,
|
|
196
196
|
getIdFields: () => getIdFields,
|
|
197
197
|
getIdValues: () => getIdValues,
|
|
198
|
+
getManyToManyJoinTable: () => getManyToManyJoinTable,
|
|
198
199
|
getManyToManyRelation: () => getManyToManyRelation,
|
|
199
200
|
getModel: () => getModel,
|
|
200
201
|
getModelFields: () => getModelFields,
|
|
@@ -220,8 +221,25 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
|
220
221
|
function hasModel(schema, model) {
|
|
221
222
|
return Object.keys(schema.models).map((k) => k.toLowerCase()).includes(model.toLowerCase());
|
|
222
223
|
}
|
|
224
|
+
const schemaLookupCache = /* @__PURE__ */ new WeakMap();
|
|
225
|
+
function getSchemaLookupCache(schema) {
|
|
226
|
+
let cache = schemaLookupCache.get(schema);
|
|
227
|
+
if (!cache) {
|
|
228
|
+
cache = {
|
|
229
|
+
model: /* @__PURE__ */ new Map(),
|
|
230
|
+
m2mRelation: /* @__PURE__ */ new Map()
|
|
231
|
+
};
|
|
232
|
+
schemaLookupCache.set(schema, cache);
|
|
233
|
+
}
|
|
234
|
+
return cache;
|
|
235
|
+
}
|
|
223
236
|
function getModel(schema, model) {
|
|
224
|
-
|
|
237
|
+
const cache = getSchemaLookupCache(schema);
|
|
238
|
+
const key = model.toLowerCase();
|
|
239
|
+
if (cache.model.has(key)) return cache.model.get(key);
|
|
240
|
+
const result = Object.values(schema.models).find((m) => m.name.toLowerCase() === key);
|
|
241
|
+
cache.model.set(key, result);
|
|
242
|
+
return result;
|
|
225
243
|
}
|
|
226
244
|
function getTypeDef(schema, type) {
|
|
227
245
|
return schema.typeDefs?.[type];
|
|
@@ -362,6 +380,37 @@ function makeDefaultOrderBy(schema, model) {
|
|
|
362
380
|
return requireIdFields(schema, model).map((f) => ({ [f]: "asc" }));
|
|
363
381
|
}
|
|
364
382
|
function getManyToManyRelation(schema, model, field) {
|
|
383
|
+
const cache = getSchemaLookupCache(schema);
|
|
384
|
+
const key = `${model} ${field}`;
|
|
385
|
+
if (cache.m2mRelation.has(key)) return cache.m2mRelation.get(key);
|
|
386
|
+
const result = computeManyToManyRelation(schema, model, field);
|
|
387
|
+
cache.m2mRelation.set(key, result);
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Resolve the relation endpoints for an implicit many-to-many join table by its table
|
|
392
|
+
* name, or `undefined` if the table is not an implicit m2m join table. The join-table
|
|
393
|
+
* index is built once per schema (single pass) and reused, making this an O(1) lookup.
|
|
394
|
+
* See issue #2715.
|
|
395
|
+
*/
|
|
396
|
+
function getManyToManyJoinTable(schema, joinTableName) {
|
|
397
|
+
const cache = getSchemaLookupCache(schema);
|
|
398
|
+
if (!cache.m2mJoinTable) {
|
|
399
|
+
const map = /* @__PURE__ */ new Map();
|
|
400
|
+
for (const model of Object.values(schema.models)) for (const field of Object.values(model.fields)) {
|
|
401
|
+
const m2m = getManyToManyRelation(schema, model.name, field.name);
|
|
402
|
+
if (m2m?.joinTable && !map.has(m2m.joinTable)) map.set(m2m.joinTable, {
|
|
403
|
+
model: model.name,
|
|
404
|
+
field: field.name,
|
|
405
|
+
otherModel: m2m.otherModel,
|
|
406
|
+
otherField: m2m.otherField
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
cache.m2mJoinTable = map;
|
|
410
|
+
}
|
|
411
|
+
return cache.m2mJoinTable.get(joinTableName);
|
|
412
|
+
}
|
|
413
|
+
function computeManyToManyRelation(schema, model, field) {
|
|
365
414
|
const fieldDef = requireField(schema, model, field);
|
|
366
415
|
if (!fieldDef.array || !fieldDef.relation?.opposite) return;
|
|
367
416
|
const realModel = fieldDef.originModel ?? model;
|
|
@@ -1437,7 +1486,10 @@ var LateralJoinDialectBase = class extends BaseCrudDialect {
|
|
|
1437
1486
|
return { [field]: fieldValue };
|
|
1438
1487
|
}
|
|
1439
1488
|
}));
|
|
1440
|
-
if (typeof payload === "object" && payload.include && typeof payload.include === "object") Object.assign(objArgs, ...Object.entries(payload.include).filter(([, value]) => value).map(([field]) =>
|
|
1489
|
+
if (typeof payload === "object" && payload.include && typeof payload.include === "object") Object.assign(objArgs, ...Object.entries(payload.include).filter(([, value]) => value).map(([field, value]) => {
|
|
1490
|
+
if (field === "_count") return { [field]: this.buildCountJson(relationModel, eb, relationModelAlias, value) };
|
|
1491
|
+
return { [field]: eb.ref(`${parentResultName}$${field}.$data`) };
|
|
1492
|
+
}));
|
|
1441
1493
|
return objArgs;
|
|
1442
1494
|
}
|
|
1443
1495
|
buildRelationJoins(query, relationModel, relationModelAlias, payload, parentResultName) {
|
|
@@ -2112,6 +2164,10 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2112
2164
|
} else return [sql.lit(field), this.fieldRef(relationModel, field, subQueryName, false)];
|
|
2113
2165
|
}).flatMap((v) => v));
|
|
2114
2166
|
if (typeof payload === "object" && payload.include && typeof payload.include === "object") objArgs.push(...Object.entries(payload.include).filter(([, value]) => value).map(([field, value]) => {
|
|
2167
|
+
if (field === "_count") {
|
|
2168
|
+
const subJson = this.buildCountJson(relationModel, eb, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2169
|
+
return [sql.lit(field), subJson];
|
|
2170
|
+
}
|
|
2115
2171
|
const subJson = this.buildRelationJSON(relationModel, eb, field, tmpAlias(`${parentAlias}$${relationField}`), value);
|
|
2116
2172
|
return [sql.lit(field), subJson];
|
|
2117
2173
|
}).flatMap((v) => v));
|