vona-module-a-orm 5.0.53 → 5.0.54

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 CHANGED
@@ -415,14 +415,12 @@ let ServiceTransaction = (_dec$o = Service(), _dec2$o = BeanInfo({
415
415
  get connection() {
416
416
  return this.transactionFiber?.connection;
417
417
  }
418
- commit(cb, options) {
419
- if (options?.ctxPrefer) {
420
- this.ctx?.commit(cb);
421
- return;
422
- }
418
+ async commit(cb, options) {
423
419
  const fiber = this.transactionFiber;
424
420
  if (!fiber) {
425
- this.ctx?.commit(cb);
421
+ if (!options?.ignoreIfNotInTransaction) {
422
+ await cb();
423
+ }
426
424
  } else {
427
425
  fiber.commit(cb);
428
426
  }
@@ -577,8 +575,8 @@ let ServiceDb = (_dec$n = Service(), _dec2$n = BeanInfo({
577
575
  get dialect() {
578
576
  return this.scope.service.database.getDialect(this.dialectName);
579
577
  }
580
- commit(cb, options) {
581
- return this.transaction.commit(cb, options);
578
+ async commit(cb, options) {
579
+ return await this.transaction.commit(cb, options);
582
580
  }
583
581
  compensate(cb) {
584
582
  return this.transaction.compensate(cb);
@@ -2265,21 +2263,25 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2265
2263
  __init__(model) {
2266
2264
  this._model = model;
2267
2265
  }
2268
- async handleRelationsOne(entity, includeWrapper, methodOptions) {
2266
+ async handleRelationsOne(relations, entity, includeWrapper, methodOptions) {
2269
2267
  if (!entity) return entity;
2270
2268
  // relations
2271
- const relations = this.__handleRelationsCollection(includeWrapper);
2272
- if (!relations) return entity;
2269
+ if (!relations) {
2270
+ relations = this.handleRelationsCollection(includeWrapper);
2271
+ }
2272
+ if (relations.length === 0) return entity;
2273
2273
  for (const relation of relations) {
2274
2274
  await this.__handleRelationOne(entity, relation, methodOptions);
2275
2275
  }
2276
2276
  return entity;
2277
2277
  }
2278
- async handleRelationsMany(entities, includeWrapper, methodOptions) {
2278
+ async handleRelationsMany(relations, entities, includeWrapper, methodOptions) {
2279
2279
  if (entities.length === 0) return entities;
2280
2280
  // relations
2281
- const relations = this.__handleRelationsCollection(includeWrapper);
2282
- if (!relations) return entities;
2281
+ if (!relations) {
2282
+ relations = this.handleRelationsCollection(includeWrapper);
2283
+ }
2284
+ if (relations.length === 0) return entities;
2283
2285
  for (const relation of relations) {
2284
2286
  await this.__handleRelationMany(entities, relation, methodOptions);
2285
2287
  }
@@ -2288,7 +2290,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2288
2290
  async handleRelationsMutate(entitiesResult, entities, includeWrapper, methodOptions) {
2289
2291
  if (entitiesResult.length === 0) return entitiesResult;
2290
2292
  // relations
2291
- const relations = this.__handleRelationsCollection(includeWrapper);
2293
+ const relations = this.handleRelationsCollection(includeWrapper);
2292
2294
  if (!relations) return entitiesResult;
2293
2295
  for (const relation of relations) {
2294
2296
  entitiesResult = await this.__handleRelationMutate(entitiesResult, entities, relation, methodOptions);
@@ -2298,7 +2300,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2298
2300
  async handleRelationsDelete(ids, includeWrapper, methodOptions) {
2299
2301
  if (ids.length === 0) return;
2300
2302
  // relations
2301
- const relations = this.__handleRelationsCollection(includeWrapper);
2303
+ const relations = this.handleRelationsCollection(includeWrapper);
2302
2304
  if (!relations) return;
2303
2305
  for (const relation of relations) {
2304
2306
  await this.__handleRelationDelete(ids, relation, methodOptions);
@@ -2756,10 +2758,35 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2756
2758
  columns = columns.concat(key);
2757
2759
  return [columns, false];
2758
2760
  }
2761
+ prepareColumnsByRelations(relations, options) {
2762
+ if (!options || !options.columns) return [options, undefined];
2763
+ const columns = Array.isArray(options.columns) ? options.columns : [options.columns];
2764
+ if (columns.includes('*')) return [options, undefined];
2765
+ // refKeys
2766
+ const refKeys = [];
2767
+ for (const relation of relations) {
2768
+ const [_relationName, relationReal] = relation;
2769
+ const {
2770
+ type,
2771
+ key
2772
+ } = relationReal;
2773
+ if (type === 'belongsTo') {
2774
+ if (!columns.includes(key)) {
2775
+ columns.push(key);
2776
+ refKeys.push(key);
2777
+ }
2778
+ }
2779
+ }
2780
+ options = refKeys.length === 0 ? options : {
2781
+ ...options,
2782
+ columns
2783
+ };
2784
+ return [options, refKeys];
2785
+ }
2759
2786
  __getModelTarget(modelClassTarget, meta) {
2760
2787
  return this._model.newInstanceTarget(modelClassTarget, meta?.client, meta?.table);
2761
2788
  }
2762
- __handleRelationsCollection(includeWrapper) {
2789
+ handleRelationsCollection(includeWrapper) {
2763
2790
  // collect
2764
2791
  const relations = [];
2765
2792
  // include
@@ -2923,8 +2950,19 @@ class BeanModelCache extends BeanModelCrud {
2923
2950
  }
2924
2951
  async mget(ids, options) {
2925
2952
  if (ids.length === 0) return [];
2926
- const items = await this.__mget_raw(undefined, ids, options);
2927
- return await this.relations.handleRelationsMany(items, options, options);
2953
+ const relations = this.relations.handleRelationsCollection(options);
2954
+ const [options2, refKeys] = this.relations.prepareColumnsByRelations(relations, options);
2955
+ let items = await this.__mget_raw(undefined, ids, options2);
2956
+ if (items.length === 0) return items;
2957
+ items = await this.relations.handleRelationsMany(relations, items, options, options);
2958
+ if (refKeys) {
2959
+ for (const item of items) {
2960
+ for (const refKey of refKeys) {
2961
+ delete item[refKey];
2962
+ }
2963
+ }
2964
+ }
2965
+ return items;
2928
2966
  }
2929
2967
  async __mget_raw(table, ids, options) {
2930
2968
  // table
@@ -3008,8 +3046,19 @@ class BeanModelCache extends BeanModelCrud {
3008
3046
  };
3009
3047
  }
3010
3048
  async select(params, options, _modelJoins) {
3011
- const items = await this.__select_raw(undefined, params, options);
3012
- return await this.relations.handleRelationsMany(items, params, options);
3049
+ const relations = this.relations.handleRelationsCollection(params);
3050
+ const [params2, refKeys] = this.relations.prepareColumnsByRelations(relations, params);
3051
+ let items = await this.__select_raw(undefined, params2, options);
3052
+ if (items.length === 0) return items;
3053
+ items = await this.relations.handleRelationsMany(relations, items, params, options);
3054
+ if (refKeys) {
3055
+ for (const item of items) {
3056
+ for (const refKey of refKeys) {
3057
+ delete item[refKey];
3058
+ }
3059
+ }
3060
+ }
3061
+ return items;
3013
3062
  }
3014
3063
  async __select_raw(table, params, options) {
3015
3064
  // table
@@ -3066,8 +3115,17 @@ class BeanModelCache extends BeanModelCrud {
3066
3115
  return items;
3067
3116
  }
3068
3117
  async get(where, options) {
3069
- const item = await this.__get_raw(undefined, where, options);
3070
- return await this.relations.handleRelationsOne(item, options, options);
3118
+ const relations = this.relations.handleRelationsCollection(options);
3119
+ const [options2, refKeys] = this.relations.prepareColumnsByRelations(relations, options);
3120
+ let item = await this.__get_raw(undefined, where, options2);
3121
+ if (!item) return item;
3122
+ item = await this.relations.handleRelationsOne(relations, item, options, options);
3123
+ if (refKeys) {
3124
+ for (const refKey of refKeys) {
3125
+ delete item[refKey];
3126
+ }
3127
+ }
3128
+ return item;
3071
3129
  }
3072
3130
  async __get_raw(table, where, options) {
3073
3131
  // table
@@ -3269,11 +3327,11 @@ class BeanModelCache extends BeanModelCrud {
3269
3327
  }
3270
3328
  async cacheEntityDel(id, table) {
3271
3329
  await this.cacheEntityDelInner(id, table);
3272
- if (this.db.inTransaction) {
3273
- this.db.commit(async () => {
3274
- await this.cacheEntityDelInner(id, table);
3275
- });
3276
- }
3330
+ this.db.commit(async () => {
3331
+ await this.cacheEntityDelInner(id, table);
3332
+ }, {
3333
+ ignoreIfNotInTransaction: true
3334
+ });
3277
3335
  this._shardingCacheDoubleDelete({
3278
3336
  beanFullName: this.$beanFullName,
3279
3337
  clientName: this.db.clientName,
@@ -3288,11 +3346,11 @@ class BeanModelCache extends BeanModelCrud {
3288
3346
  }
3289
3347
  async cacheEntityClear(table) {
3290
3348
  await this.cacheEntityClearInner(table);
3291
- if (this.db.inTransaction) {
3292
- this.db.commit(async () => {
3293
- await this.cacheEntityClearInner(table);
3294
- });
3295
- }
3349
+ this.db.commit(async () => {
3350
+ await this.cacheEntityClearInner(table);
3351
+ }, {
3352
+ ignoreIfNotInTransaction: true
3353
+ });
3296
3354
  this._shardingCacheDoubleDelete({
3297
3355
  beanFullName: this.$beanFullName,
3298
3356
  clientName: this.db.clientName,
@@ -3307,11 +3365,11 @@ class BeanModelCache extends BeanModelCrud {
3307
3365
  }
3308
3366
  async cacheQueryClear(table) {
3309
3367
  await this.cacheQueryClearInner(table);
3310
- if (this.db.inTransaction) {
3311
- this.db.commit(async () => {
3312
- await this.cacheQueryClearInner(table);
3313
- });
3314
- }
3368
+ this.db.commit(async () => {
3369
+ await this.cacheQueryClearInner(table);
3370
+ }, {
3371
+ ignoreIfNotInTransaction: true
3372
+ });
3315
3373
  this._shardingCacheDoubleDelete({
3316
3374
  beanFullName: this.$beanFullName,
3317
3375
  clientName: this.db.clientName,
@@ -3327,13 +3385,9 @@ class BeanModelCache extends BeanModelCrud {
3327
3385
  _shardingCacheDoubleDelete(jobData) {
3328
3386
  const doubleDelete = this.scopeOrm.config.sharding.cache.doubleDelete;
3329
3387
  if (!doubleDelete) return;
3330
- if (this.db.inTransaction) {
3331
- this.db.commit(() => {
3332
- this.scopeOrm.queue.doubleDelete.push(jobData);
3333
- });
3334
- } else {
3388
+ this.db.commit(() => {
3335
3389
  this.scopeOrm.queue.doubleDelete.push(jobData);
3336
- }
3390
+ });
3337
3391
  }
3338
3392
  async _cacheQueryClearModelsClear() {
3339
3393
  const modelsClear = this._getModelsClear();
@@ -21,6 +21,6 @@ export declare class ServiceDb extends BeanBase {
21
21
  get connection(): import("knex").Knex<any, any[]>;
22
22
  get dialectName(): keyof IDatabaseClientDialectRecord;
23
23
  get dialect(): BeanDatabaseDialectBase;
24
- commit(cb: FunctionAny, options?: ITransactionConsistencyCommitOptions): void;
24
+ commit(cb: FunctionAny, options?: ITransactionConsistencyCommitOptions): Promise<void>;
25
25
  compensate(cb: FunctionAny): void;
26
26
  }
@@ -1,12 +1,13 @@
1
1
  import type { TableIdentity } from 'table-identity';
2
2
  import type { BeanModelCache } from '../bean/bean.model/bean.model_cache.ts';
3
3
  import type { IModelMethodOptions, IModelRelationIncludeWrapper } from '../types/model.ts';
4
+ import type { IRelationItem } from '../types/relationsDef.ts';
4
5
  import { BeanBase } from 'vona';
5
6
  export declare class ServiceRelations extends BeanBase {
6
7
  protected _model: BeanModelCache;
7
8
  protected __init__(model: BeanModelCache): void;
8
- handleRelationsOne<TRecord extends {}>(entity: TRecord | undefined, includeWrapper?: IModelRelationIncludeWrapper, methodOptions?: IModelMethodOptions): Promise<TRecord | undefined>;
9
- handleRelationsMany<TRecord extends {}>(entities: TRecord[], includeWrapper?: IModelRelationIncludeWrapper, methodOptions?: IModelMethodOptions): Promise<TRecord[]>;
9
+ handleRelationsOne<TRecord extends {}>(relations: IRelationItem[] | undefined, entity: TRecord | undefined, includeWrapper?: IModelRelationIncludeWrapper, methodOptions?: IModelMethodOptions): Promise<TRecord | undefined>;
10
+ handleRelationsMany<TRecord extends {}>(relations: IRelationItem[] | undefined, entities: TRecord[], includeWrapper?: IModelRelationIncludeWrapper, methodOptions?: IModelMethodOptions): Promise<TRecord[]>;
10
11
  handleRelationsMutate<TRecord extends {}>(entitiesResult: TRecord[], entities: TRecord[], includeWrapper: IModelRelationIncludeWrapper | undefined, methodOptions: IModelMethodOptions | undefined): Promise<TRecord[]>;
11
12
  handleRelationsDelete(ids: TableIdentity[], includeWrapper?: IModelRelationIncludeWrapper, methodOptions?: IModelMethodOptions): Promise<void>;
12
13
  private __handleRelationOne;
@@ -14,6 +15,9 @@ export declare class ServiceRelations extends BeanBase {
14
15
  private __handleRelationMutate;
15
16
  private __handleRelationDelete;
16
17
  private __prepareColumnsAndKey;
18
+ prepareColumnsByRelations<T extends {
19
+ columns?: any;
20
+ }>(relations: IRelationItem[], options?: T): [T | undefined, string[] | undefined];
17
21
  private __getModelTarget;
18
- private __handleRelationsCollection;
22
+ handleRelationsCollection(includeWrapper?: IModelRelationIncludeWrapper): IRelationItem[];
19
23
  }
@@ -11,7 +11,7 @@ export declare class ServiceTransaction extends BeanBase {
11
11
  get transactionFiber(): ServiceTransactionFiber | undefined;
12
12
  get inTransaction(): boolean;
13
13
  get connection(): knex.Knex.Transaction | undefined;
14
- commit(cb: FunctionAny, options?: ITransactionConsistencyCommitOptions): void;
14
+ commit(cb: FunctionAny, options?: ITransactionConsistencyCommitOptions): Promise<void>;
15
15
  compensate(cb: FunctionAny): void;
16
16
  begin<RESULT>(fn: FunctionAsync<RESULT>, options?: ITransactionOptions): Promise<RESULT>;
17
17
  private _isolationLevelRequired;
@@ -57,3 +57,4 @@ export interface IModelRelationOptionsMetaWrapper {
57
57
  meta?: IModelRelationOptionsMeta;
58
58
  }
59
59
  export type TypeModelRelationOptionsMetaClient = '_auto_' | '_initial_' | '_inherit_' | keyof IDatabaseClientRecord | ServiceDb;
60
+ export type IRelationItem = [string, any, any, any];
@@ -14,5 +14,5 @@ export interface ITransactionOptions {
14
14
  propagation?: TypeTransactionPropagation;
15
15
  }
16
16
  export interface ITransactionConsistencyCommitOptions {
17
- ctxPrefer?: boolean;
17
+ ignoreIfNotInTransaction?: boolean;
18
18
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-orm",
3
3
  "type": "module",
4
- "version": "5.0.53",
4
+ "version": "5.0.54",
5
5
  "title": "a-orm",
6
6
  "vonaModule": {
7
7
  "capabilities": {