vona-module-a-orm 5.0.52 → 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
@@ -1,14 +1,13 @@
1
1
  import BigNumber$1, { BigNumber } from 'bignumber.js';
2
- import { BeanInfo, BeanAopMethodBase, BeanBase, deepExtend, Virtual, appResource, beanFullNameFromOnionName, cast, appMetadata, useApp, BeanSimple, combineConfigDefault, BeanScopeBase, createBeanDecorator, PickClassInner, $Class } from 'vona';
2
+ import { BeanInfo, BeanAopMethodBase, BeanBase, deepExtend, Virtual, appResource, beanFullNameFromOnionName, cast, useApp, BeanSimple, combineConfigDefault, BeanScopeBase, createBeanDecorator, PickClassInner, $Class } from 'vona';
3
3
  import { AopMethod, Aspect } from 'vona-module-a-aspect';
4
4
  import { Service, Bean, Scope } from 'vona-module-a-bean';
5
5
  import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
6
6
  import knex from 'knex';
7
7
  import { isNil, safeBoolean, isClass, ensureArray, hashkey } from '@cabloy/utils';
8
8
  import { swapDeps } from '@cabloy/deps';
9
- import { prepareClassType, getTargetDecoratorRules, Api, v, OrderMaxBase, OrderCoreBase, getSchemaDynamic, SymbolSchemaDynamicRefId, addSchemaDynamic, mergeFieldsOpenapiMetadata } from 'vona-module-a-openapi';
9
+ import { prepareClassType, getTargetDecoratorRules, getTargetDecoratorRuleColumnsMap, Api, v, OrderMaxBase, OrderCoreBase, getSchemaDynamic, SymbolSchemaDynamicRefId, addSchemaDynamic, mergeFieldsOpenapiMetadata } from 'vona-module-a-openapi';
10
10
  import { ZodMetadata } from '@cabloy/zod-openapi';
11
- import { SymbolDecoratorRuleColumn } from 'vona-module-a-openapiutils';
12
11
  import z from 'zod';
13
12
  import { Broadcast, BeanBroadcastBase } from 'vona-module-a-broadcast';
14
13
  import { Event, BeanEventBase } from 'vona-module-a-event';
@@ -416,14 +415,12 @@ let ServiceTransaction = (_dec$o = Service(), _dec2$o = BeanInfo({
416
415
  get connection() {
417
416
  return this.transactionFiber?.connection;
418
417
  }
419
- commit(cb, options) {
420
- if (options?.ctxPrefer) {
421
- this.ctx?.commit(cb);
422
- return;
423
- }
418
+ async commit(cb, options) {
424
419
  const fiber = this.transactionFiber;
425
420
  if (!fiber) {
426
- this.ctx?.commit(cb);
421
+ if (!options?.ignoreIfNotInTransaction) {
422
+ await cb();
423
+ }
427
424
  } else {
428
425
  fiber.commit(cb);
429
426
  }
@@ -578,8 +575,8 @@ let ServiceDb = (_dec$n = Service(), _dec2$n = BeanInfo({
578
575
  get dialect() {
579
576
  return this.scope.service.database.getDialect(this.dialectName);
580
577
  }
581
- commit(cb, options) {
582
- return this.transaction.commit(cb, options);
578
+ async commit(cb, options) {
579
+ return await this.transaction.commit(cb, options);
583
580
  }
584
581
  compensate(cb) {
585
582
  return this.transaction.compensate(cb);
@@ -1115,7 +1112,7 @@ function $columns(key) {
1115
1112
  }
1116
1113
  function $columnsAll(classEntity, withTableName, withMeta) {
1117
1114
  const classEntity2 = _prepareClassEntity(classEntity);
1118
- let columns = appMetadata.getMetadata(SymbolDecoratorRuleColumn, classEntity2.prototype);
1115
+ let columns = getTargetDecoratorRuleColumnsMap(classEntity2.prototype);
1119
1116
  if (withTableName) {
1120
1117
  const tableName = $tableName(classEntity2);
1121
1118
  columns = {
@@ -2266,21 +2263,25 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2266
2263
  __init__(model) {
2267
2264
  this._model = model;
2268
2265
  }
2269
- async handleRelationsOne(entity, includeWrapper, methodOptions) {
2266
+ async handleRelationsOne(relations, entity, includeWrapper, methodOptions) {
2270
2267
  if (!entity) return entity;
2271
2268
  // relations
2272
- const relations = this.__handleRelationsCollection(includeWrapper);
2273
- if (!relations) return entity;
2269
+ if (!relations) {
2270
+ relations = this.handleRelationsCollection(includeWrapper);
2271
+ }
2272
+ if (relations.length === 0) return entity;
2274
2273
  for (const relation of relations) {
2275
2274
  await this.__handleRelationOne(entity, relation, methodOptions);
2276
2275
  }
2277
2276
  return entity;
2278
2277
  }
2279
- async handleRelationsMany(entities, includeWrapper, methodOptions) {
2278
+ async handleRelationsMany(relations, entities, includeWrapper, methodOptions) {
2280
2279
  if (entities.length === 0) return entities;
2281
2280
  // relations
2282
- const relations = this.__handleRelationsCollection(includeWrapper);
2283
- if (!relations) return entities;
2281
+ if (!relations) {
2282
+ relations = this.handleRelationsCollection(includeWrapper);
2283
+ }
2284
+ if (relations.length === 0) return entities;
2284
2285
  for (const relation of relations) {
2285
2286
  await this.__handleRelationMany(entities, relation, methodOptions);
2286
2287
  }
@@ -2289,7 +2290,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2289
2290
  async handleRelationsMutate(entitiesResult, entities, includeWrapper, methodOptions) {
2290
2291
  if (entitiesResult.length === 0) return entitiesResult;
2291
2292
  // relations
2292
- const relations = this.__handleRelationsCollection(includeWrapper);
2293
+ const relations = this.handleRelationsCollection(includeWrapper);
2293
2294
  if (!relations) return entitiesResult;
2294
2295
  for (const relation of relations) {
2295
2296
  entitiesResult = await this.__handleRelationMutate(entitiesResult, entities, relation, methodOptions);
@@ -2299,7 +2300,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2299
2300
  async handleRelationsDelete(ids, includeWrapper, methodOptions) {
2300
2301
  if (ids.length === 0) return;
2301
2302
  // relations
2302
- const relations = this.__handleRelationsCollection(includeWrapper);
2303
+ const relations = this.handleRelationsCollection(includeWrapper);
2303
2304
  if (!relations) return;
2304
2305
  for (const relation of relations) {
2305
2306
  await this.__handleRelationDelete(ids, relation, methodOptions);
@@ -2757,10 +2758,35 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2757
2758
  columns = columns.concat(key);
2758
2759
  return [columns, false];
2759
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
+ }
2760
2786
  __getModelTarget(modelClassTarget, meta) {
2761
2787
  return this._model.newInstanceTarget(modelClassTarget, meta?.client, meta?.table);
2762
2788
  }
2763
- __handleRelationsCollection(includeWrapper) {
2789
+ handleRelationsCollection(includeWrapper) {
2764
2790
  // collect
2765
2791
  const relations = [];
2766
2792
  // include
@@ -2924,8 +2950,19 @@ class BeanModelCache extends BeanModelCrud {
2924
2950
  }
2925
2951
  async mget(ids, options) {
2926
2952
  if (ids.length === 0) return [];
2927
- const items = await this.__mget_raw(undefined, ids, options);
2928
- 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;
2929
2966
  }
2930
2967
  async __mget_raw(table, ids, options) {
2931
2968
  // table
@@ -3009,8 +3046,19 @@ class BeanModelCache extends BeanModelCrud {
3009
3046
  };
3010
3047
  }
3011
3048
  async select(params, options, _modelJoins) {
3012
- const items = await this.__select_raw(undefined, params, options);
3013
- 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;
3014
3062
  }
3015
3063
  async __select_raw(table, params, options) {
3016
3064
  // table
@@ -3067,8 +3115,17 @@ class BeanModelCache extends BeanModelCrud {
3067
3115
  return items;
3068
3116
  }
3069
3117
  async get(where, options) {
3070
- const item = await this.__get_raw(undefined, where, options);
3071
- 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;
3072
3129
  }
3073
3130
  async __get_raw(table, where, options) {
3074
3131
  // table
@@ -3270,11 +3327,11 @@ class BeanModelCache extends BeanModelCrud {
3270
3327
  }
3271
3328
  async cacheEntityDel(id, table) {
3272
3329
  await this.cacheEntityDelInner(id, table);
3273
- if (this.db.inTransaction) {
3274
- this.db.commit(async () => {
3275
- await this.cacheEntityDelInner(id, table);
3276
- });
3277
- }
3330
+ this.db.commit(async () => {
3331
+ await this.cacheEntityDelInner(id, table);
3332
+ }, {
3333
+ ignoreIfNotInTransaction: true
3334
+ });
3278
3335
  this._shardingCacheDoubleDelete({
3279
3336
  beanFullName: this.$beanFullName,
3280
3337
  clientName: this.db.clientName,
@@ -3289,11 +3346,11 @@ class BeanModelCache extends BeanModelCrud {
3289
3346
  }
3290
3347
  async cacheEntityClear(table) {
3291
3348
  await this.cacheEntityClearInner(table);
3292
- if (this.db.inTransaction) {
3293
- this.db.commit(async () => {
3294
- await this.cacheEntityClearInner(table);
3295
- });
3296
- }
3349
+ this.db.commit(async () => {
3350
+ await this.cacheEntityClearInner(table);
3351
+ }, {
3352
+ ignoreIfNotInTransaction: true
3353
+ });
3297
3354
  this._shardingCacheDoubleDelete({
3298
3355
  beanFullName: this.$beanFullName,
3299
3356
  clientName: this.db.clientName,
@@ -3308,11 +3365,11 @@ class BeanModelCache extends BeanModelCrud {
3308
3365
  }
3309
3366
  async cacheQueryClear(table) {
3310
3367
  await this.cacheQueryClearInner(table);
3311
- if (this.db.inTransaction) {
3312
- this.db.commit(async () => {
3313
- await this.cacheQueryClearInner(table);
3314
- });
3315
- }
3368
+ this.db.commit(async () => {
3369
+ await this.cacheQueryClearInner(table);
3370
+ }, {
3371
+ ignoreIfNotInTransaction: true
3372
+ });
3316
3373
  this._shardingCacheDoubleDelete({
3317
3374
  beanFullName: this.$beanFullName,
3318
3375
  clientName: this.db.clientName,
@@ -3328,13 +3385,9 @@ class BeanModelCache extends BeanModelCrud {
3328
3385
  _shardingCacheDoubleDelete(jobData) {
3329
3386
  const doubleDelete = this.scopeOrm.config.sharding.cache.doubleDelete;
3330
3387
  if (!doubleDelete) return;
3331
- if (this.db.inTransaction) {
3332
- this.db.commit(() => {
3333
- this.scopeOrm.queue.doubleDelete.push(jobData);
3334
- });
3335
- } else {
3388
+ this.db.commit(() => {
3336
3389
  this.scopeOrm.queue.doubleDelete.push(jobData);
3337
- }
3390
+ });
3338
3391
  }
3339
3392
  async _cacheQueryClearModelsClear() {
3340
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.52",
4
+ "version": "5.0.54",
5
5
  "title": "a-orm",
6
6
  "vonaModule": {
7
7
  "capabilities": {