vona-module-a-orm 5.1.13 → 5.1.18
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.js +48 -27
- package/dist/index.js.map +1 -1
- package/dist/lib/dto/dtoMutate.d.ts.map +1 -1
- package/dist/service/relations_.d.ts +1 -0
- package/dist/service/relations_.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/lib/dto/dtoGet.ts +6 -2
- package/src/lib/dto/dtoMutate.ts +13 -4
- package/src/lib/utils.ts +1 -1
- package/src/service/relations_.ts +37 -16
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import knex from 'knex';
|
|
|
9
9
|
import { BeanMutateBase } from 'vona-module-a-beanmutate';
|
|
10
10
|
import { swapDeps } from '@cabloy/deps';
|
|
11
11
|
import { $tableNameFromEntity, $tableDefaults, $columnsAll } from 'vona-module-a-ormutils';
|
|
12
|
-
import { prepareClassType, Api, v, schemaRenderVisible, getSchemaDynamic, SymbolSchemaDynamicRefId, addSchemaDynamic, mergeDtoFieldsOpenapiMetadata } from 'vona-module-a-openapiutils';
|
|
12
|
+
import { prepareClassType, Api, v, schemaRenderVisible, getSchemaDynamic, SymbolSchemaDynamicRefId, addSchemaDynamic, getTargetDecoratorRuleColumns, mergeDtoFieldsOpenapiMetadata } from 'vona-module-a-openapiutils';
|
|
13
13
|
import z from 'zod';
|
|
14
14
|
import { Broadcast, BeanBroadcastBase } from 'vona-module-a-broadcast';
|
|
15
15
|
import { Event, BeanEventBase } from 'vona-module-a-event';
|
|
@@ -2320,7 +2320,7 @@ function handleRelationsCollection(relationsStatic, includeWrapper) {
|
|
|
2320
2320
|
let withReal;
|
|
2321
2321
|
let autoload;
|
|
2322
2322
|
if (relationCur === false) {
|
|
2323
|
-
|
|
2323
|
+
relationReal = false;
|
|
2324
2324
|
} else if (relationCur === true) {
|
|
2325
2325
|
relationReal = relationDef;
|
|
2326
2326
|
autoload = relationDef.options?.autoload;
|
|
@@ -2403,6 +2403,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2403
2403
|
}
|
|
2404
2404
|
async __handleRelationOne(entity, relation, methodOptions) {
|
|
2405
2405
|
const [relationName, relationReal, includeReal, withReal] = relation;
|
|
2406
|
+
if (relationReal === false) return;
|
|
2406
2407
|
const {
|
|
2407
2408
|
type,
|
|
2408
2409
|
modelMiddle,
|
|
@@ -2505,6 +2506,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2505
2506
|
}
|
|
2506
2507
|
async __handleRelationMany(entities, relation, methodOptions) {
|
|
2507
2508
|
const [relationName, relationReal, includeReal, withReal] = relation;
|
|
2509
|
+
if (relationReal === false) return;
|
|
2508
2510
|
const {
|
|
2509
2511
|
type,
|
|
2510
2512
|
modelMiddle,
|
|
@@ -2539,7 +2541,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2539
2541
|
}
|
|
2540
2542
|
for (const entity of entities) {
|
|
2541
2543
|
entity[relationName] = items.find(item => {
|
|
2542
|
-
if (item[key]
|
|
2544
|
+
if (this.__sameTableIdentity(item[key], cast(entity).id)) {
|
|
2543
2545
|
if (!withKey) delete item[key];
|
|
2544
2546
|
return true;
|
|
2545
2547
|
}
|
|
@@ -2551,7 +2553,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2551
2553
|
const options2 = deepExtend({}, methodOptionsReal, optionsReal);
|
|
2552
2554
|
const items = await modelTarget.mget(idsTo, options2);
|
|
2553
2555
|
for (const entity of entities) {
|
|
2554
|
-
entity[relationName] = items.find(item => cast(item).id
|
|
2556
|
+
entity[relationName] = items.find(item => this.__sameTableIdentity(cast(item).id, cast(entity)[key]));
|
|
2555
2557
|
}
|
|
2556
2558
|
} else if (type === 'hasMany') {
|
|
2557
2559
|
const idsFrom = entities.map(item => cast(item).id).filter(id => !isNil(id));
|
|
@@ -2567,7 +2569,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2567
2569
|
for (const entity of entities) {
|
|
2568
2570
|
entity[relationName] = [];
|
|
2569
2571
|
for (const item of items) {
|
|
2570
|
-
if (item[key]
|
|
2572
|
+
if (this.__sameTableIdentity(item[key], cast(entity).id)) {
|
|
2571
2573
|
delete item[key];
|
|
2572
2574
|
entity[relationName].push(item);
|
|
2573
2575
|
}
|
|
@@ -2582,7 +2584,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2582
2584
|
});
|
|
2583
2585
|
const items = await modelTarget.group(options2, methodOptionsReal);
|
|
2584
2586
|
for (const entity of entities) {
|
|
2585
|
-
const item = items.find(item => item[key]
|
|
2587
|
+
const item = items.find(item => this.__sameTableIdentity(item[key], cast(entity).id));
|
|
2586
2588
|
if (item) {
|
|
2587
2589
|
delete item[key];
|
|
2588
2590
|
}
|
|
@@ -2600,7 +2602,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2600
2602
|
for (const entity of entities) {
|
|
2601
2603
|
entity[relationName] = [];
|
|
2602
2604
|
for (const item of items) {
|
|
2603
|
-
if (item[key]
|
|
2605
|
+
if (this.__sameTableIdentity(item[key], cast(entity).id)) {
|
|
2604
2606
|
if (!withKey) delete item[key];
|
|
2605
2607
|
entity[relationName].push(item);
|
|
2606
2608
|
}
|
|
@@ -2622,7 +2624,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2622
2624
|
}
|
|
2623
2625
|
if (optionsReal.groups) {
|
|
2624
2626
|
for (const entity of entities) {
|
|
2625
|
-
const idsTo = itemsMiddle.filter(item => item[keyFrom]
|
|
2627
|
+
const idsTo = itemsMiddle.filter(item => this.__sameTableIdentity(item[keyFrom], cast(entity).id)).map(item => item[keyTo]);
|
|
2626
2628
|
const options2 = deepExtend({}, optionsReal, {
|
|
2627
2629
|
groups: optionsReal.groups,
|
|
2628
2630
|
where: {
|
|
@@ -2633,7 +2635,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2633
2635
|
}
|
|
2634
2636
|
} else if (optionsReal.aggrs) {
|
|
2635
2637
|
for (const entity of entities) {
|
|
2636
|
-
const idsTo = itemsMiddle.filter(item => item[keyFrom]
|
|
2638
|
+
const idsTo = itemsMiddle.filter(item => this.__sameTableIdentity(item[keyFrom], cast(entity).id)).map(item => item[keyTo]);
|
|
2637
2639
|
const options2 = deepExtend({}, optionsReal, {
|
|
2638
2640
|
where: {
|
|
2639
2641
|
id: idsTo
|
|
@@ -2648,8 +2650,8 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2648
2650
|
for (const entity of entities) {
|
|
2649
2651
|
entity[relationName] = [];
|
|
2650
2652
|
for (const itemMiddle of itemsMiddle) {
|
|
2651
|
-
if (itemMiddle[keyFrom]
|
|
2652
|
-
entity[relationName].push(items.find(item => cast(item).id
|
|
2653
|
+
if (this.__sameTableIdentity(itemMiddle[keyFrom], cast(entity).id)) {
|
|
2654
|
+
entity[relationName].push(items.find(item => this.__sameTableIdentity(cast(item).id, cast(itemMiddle)[keyTo])));
|
|
2653
2655
|
}
|
|
2654
2656
|
}
|
|
2655
2657
|
}
|
|
@@ -2658,6 +2660,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2658
2660
|
}
|
|
2659
2661
|
async __handleRelationMutate(entitiesResult, entities, relation, methodOptions) {
|
|
2660
2662
|
const [relationName, relationReal, includeReal, withReal] = relation;
|
|
2663
|
+
if (relationReal === false) return entitiesResult;
|
|
2661
2664
|
const {
|
|
2662
2665
|
type,
|
|
2663
2666
|
modelMiddle,
|
|
@@ -2713,9 +2716,9 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2713
2716
|
if (entity[relationName] && entity[relationName].length > 0) {
|
|
2714
2717
|
const entityId = cast(entity).id;
|
|
2715
2718
|
const idsTo = entity[relationName].map(item => item.id).filter(id => !isNil(id));
|
|
2716
|
-
let
|
|
2719
|
+
let idsTargetSet;
|
|
2717
2720
|
if (idsTo.length === 0) {
|
|
2718
|
-
|
|
2721
|
+
idsTargetSet = new Set();
|
|
2719
2722
|
} else {
|
|
2720
2723
|
const itemsTarget = await cast(modelTarget).__select_raw(undefined, {
|
|
2721
2724
|
where: {
|
|
@@ -2723,10 +2726,10 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2723
2726
|
id: idsTo
|
|
2724
2727
|
}
|
|
2725
2728
|
}, methodOptionsReal);
|
|
2726
|
-
|
|
2729
|
+
idsTargetSet = new Set(itemsTarget.map(item => String(item.id)));
|
|
2727
2730
|
}
|
|
2728
2731
|
for (const child of entity[relationName]) {
|
|
2729
|
-
if (!isNil(child.id) && !
|
|
2732
|
+
if (!isNil(child.id) && !idsTargetSet.has(String(child.id))) {
|
|
2730
2733
|
throw new Error(`invalid id: ${child.id}`);
|
|
2731
2734
|
}
|
|
2732
2735
|
children.push(Object.assign({}, child, {
|
|
@@ -2743,7 +2746,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2743
2746
|
if (entity[relationName]) {
|
|
2744
2747
|
entityResult[relationName] = [];
|
|
2745
2748
|
for (const child of children) {
|
|
2746
|
-
if (child[key]
|
|
2749
|
+
if (this.__sameTableIdentity(child[key], cast(entity).id)) {
|
|
2747
2750
|
entityResult[relationName].push(child);
|
|
2748
2751
|
}
|
|
2749
2752
|
}
|
|
@@ -2769,7 +2772,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2769
2772
|
}, methodOptionsReal);
|
|
2770
2773
|
}
|
|
2771
2774
|
for (const child of entity[relationName]) {
|
|
2772
|
-
const itemMiddle = itemsMiddle.find(item => item[keyTo]
|
|
2775
|
+
const itemMiddle = itemsMiddle.find(item => this.__sameTableIdentity(item[keyTo], child.id));
|
|
2773
2776
|
if (!itemMiddle) {
|
|
2774
2777
|
if (!child.deleted) {
|
|
2775
2778
|
// add
|
|
@@ -2798,7 +2801,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2798
2801
|
if (entity[relationName]) {
|
|
2799
2802
|
entityResult[relationName] = [];
|
|
2800
2803
|
for (const child of children) {
|
|
2801
|
-
if (child[keyFrom]
|
|
2804
|
+
if (this.__sameTableIdentity(child[keyFrom], cast(entity).id)) {
|
|
2802
2805
|
entityResult[relationName].push({
|
|
2803
2806
|
id: child[keyTo]
|
|
2804
2807
|
});
|
|
@@ -2813,6 +2816,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2813
2816
|
}
|
|
2814
2817
|
async __handleRelationDelete(ids, relation, methodOptions) {
|
|
2815
2818
|
const [_relationName, relationReal, includeReal, withReal] = relation;
|
|
2819
|
+
if (relationReal === false) return;
|
|
2816
2820
|
const {
|
|
2817
2821
|
type,
|
|
2818
2822
|
modelMiddle,
|
|
@@ -2847,6 +2851,10 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2847
2851
|
await modelTargetMiddle.deleteBulk(idsMiddle, methodOptionsReal);
|
|
2848
2852
|
}
|
|
2849
2853
|
}
|
|
2854
|
+
__sameTableIdentity(id1, id2) {
|
|
2855
|
+
if (isNil(id1) || isNil(id2)) return false;
|
|
2856
|
+
return String(id1) === String(id2);
|
|
2857
|
+
}
|
|
2850
2858
|
__prepareColumnsAndKey(columns, key) {
|
|
2851
2859
|
if (!columns) return [columns, true];
|
|
2852
2860
|
columns = Array.isArray(columns) ? columns : [columns];
|
|
@@ -2862,6 +2870,7 @@ let ServiceRelations = class ServiceRelations extends BeanBase {
|
|
|
2862
2870
|
const refKeys = [];
|
|
2863
2871
|
for (const relation of relations) {
|
|
2864
2872
|
const [_relationName, relationReal] = relation;
|
|
2873
|
+
if (relationReal === false) continue;
|
|
2865
2874
|
const {
|
|
2866
2875
|
type,
|
|
2867
2876
|
key
|
|
@@ -3251,8 +3260,8 @@ function _DtoGet_raw(modelLike, params) {
|
|
|
3251
3260
|
// entity
|
|
3252
3261
|
let entityClass = params?.dtoClass ?? getClassEntityFromClassModel(modelClass);
|
|
3253
3262
|
// columns
|
|
3254
|
-
const columns = prepareColumns(params?.columns);
|
|
3255
|
-
// always create a new class, no matter if columns empty
|
|
3263
|
+
const columns = params?.dtoClass ? undefined : prepareColumns(params?.columns);
|
|
3264
|
+
// always create a new class, no matter if columns empty: dtoClass keeps own fields
|
|
3256
3265
|
entityClass = $Class.pick(entityClass, columns);
|
|
3257
3266
|
// relations
|
|
3258
3267
|
_DtoGet_relations(modelClass, entityClass, params);
|
|
@@ -3268,6 +3277,10 @@ function _DtoGet_relations(modelClass, entityClass, includeWrapper, mutateTypeTo
|
|
|
3268
3277
|
}
|
|
3269
3278
|
function _DtoGet_relation_handle(entityClass, relation, mutateTypeTopLevel) {
|
|
3270
3279
|
const [relationName, relationReal, includeReal, withReal, autoload] = relation;
|
|
3280
|
+
if (relationReal === false) {
|
|
3281
|
+
Api.field(schemaRenderVisible(false), v.optional())(entityClass.prototype, relationName);
|
|
3282
|
+
return;
|
|
3283
|
+
}
|
|
3271
3284
|
const {
|
|
3272
3285
|
type,
|
|
3273
3286
|
model,
|
|
@@ -3381,7 +3394,13 @@ function _DtoMutate_raw(modelLike, params, mutateTypeTopLevel, columnsOmitDefaul
|
|
|
3381
3394
|
// entity
|
|
3382
3395
|
let entityClass = params?.dtoClass ?? getClassEntityFromClassModel(modelClass);
|
|
3383
3396
|
// columns
|
|
3384
|
-
let columns
|
|
3397
|
+
let columns;
|
|
3398
|
+
// if dtoClass, use dtoClass fields
|
|
3399
|
+
if (params?.dtoClass) {
|
|
3400
|
+
columns = getTargetDecoratorRuleColumns(params.dtoClass.prototype);
|
|
3401
|
+
} else {
|
|
3402
|
+
columns = prepareColumns(params?.columns);
|
|
3403
|
+
}
|
|
3385
3404
|
if (columns) {
|
|
3386
3405
|
if (!topLevel) {
|
|
3387
3406
|
if (mutateTypeTopLevel === 'create') {
|
|
@@ -3413,12 +3432,14 @@ function _DtoMutate_raw(modelLike, params, mutateTypeTopLevel, columnsOmitDefaul
|
|
|
3413
3432
|
}
|
|
3414
3433
|
if (!topLevel && relation) {
|
|
3415
3434
|
const [_relationName, relationReal] = relation;
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3435
|
+
if (relationReal !== false) {
|
|
3436
|
+
const {
|
|
3437
|
+
type,
|
|
3438
|
+
key
|
|
3439
|
+
} = relationReal;
|
|
3440
|
+
if (type === 'hasOne' || type === 'hasMany') {
|
|
3441
|
+
entityClass = $Class.omit(entityClass, key);
|
|
3442
|
+
}
|
|
3422
3443
|
}
|
|
3423
3444
|
}
|
|
3424
3445
|
// relations
|