vona-module-a-orm 5.0.55 → 5.0.57

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.
@@ -1,5 +1,5 @@
1
1
  import type { ServiceDb } from '../../service/db_.ts';
2
- import type { IDatabaseClientRecord, IDecoratorModelOptions, IModelClassRecord, IModelMethodOptionsGeneral, IModelUpdateOptionsGeneral, ITableRecord, TypeEntityMeta, TypeModelClassLike, TypeModelRelationOptionsMetaClient } from '../../types/index.ts';
2
+ import type { IDatabaseClientRecord, IDecoratorModelOptions, IModelClassRecord, IModelMethodOptionsGeneral, IModelUpdateOptionsGeneral, ITableRecord, TypeEntityMeta, TypeModelClassLike, TypeModelOptionsTable, TypeModelRelationOptionsMetaClient } from '../../types/index.ts';
3
3
  import type { BeanModel } from '../bean.model.ts';
4
4
  import { BeanBase } from 'vona';
5
5
  import { SymbolKeyEntity, SymbolKeyEntityMeta, SymbolKeyModelOptions } from '../../types/index.ts';
@@ -11,7 +11,7 @@ export declare class BeanModelMeta<TRecord extends {} = {}> extends BeanBase {
11
11
  [SymbolKeyModelOptions]: IDecoratorModelOptions;
12
12
  private [SymbolModelDb]?;
13
13
  private [SymbolModelTable]?;
14
- protected __init__(clientName?: keyof IDatabaseClientRecord | ServiceDb, table?: keyof ITableRecord): void;
14
+ protected __init__(clientName?: keyof IDatabaseClientRecord | ServiceDb, table?: TypeModelOptionsTable): void;
15
15
  protected get self(): BeanModel<any>;
16
16
  protected get connection(): import("knex").Knex<any, any[]>;
17
17
  protected get dialect(): import("vona-module-a-orm").BeanDatabaseDialectBase;
@@ -19,7 +19,7 @@ export declare class BeanModelMeta<TRecord extends {} = {}> extends BeanBase {
19
19
  get modelViewRecord(): import("vona-module-a-version").ModelViewRecord;
20
20
  get db(): ServiceDb;
21
21
  private _getDb;
22
- getTable(): keyof ITableRecord;
22
+ getTable(where: object | undefined): keyof ITableRecord;
23
23
  private _getTable;
24
24
  get options(): IDecoratorModelOptions;
25
25
  get disableInstance(): boolean;
@@ -34,7 +34,7 @@ export declare class BeanModelMeta<TRecord extends {} = {}> extends BeanBase {
34
34
  protected _checkDisableDeletedByOptions(options?: IModelMethodOptionsGeneral): boolean;
35
35
  protected _checkDisableCreateTimeByOptions(options?: IModelUpdateOptionsGeneral<TRecord>): boolean;
36
36
  protected _checkDisableUpdateTimeByOptions(options?: IModelUpdateOptionsGeneral<TRecord>): boolean;
37
- newInstance(client?: keyof IDatabaseClientRecord | ServiceDb, table?: keyof ITableRecord): this;
38
- newInstanceTarget<MODEL extends BeanModelMeta | (keyof IModelClassRecord)>(modelClassTarget: TypeModelClassLike<MODEL>, client?: TypeModelRelationOptionsMetaClient, table?: keyof ITableRecord): BeanModelMeta;
37
+ newInstance(client?: keyof IDatabaseClientRecord | ServiceDb, table?: TypeModelOptionsTable): this;
38
+ newInstanceTarget<MODEL extends BeanModelMeta | (keyof IModelClassRecord)>(modelClassTarget: TypeModelClassLike<MODEL>, client?: TypeModelRelationOptionsMetaClient, table?: TypeModelOptionsTable): BeanModelMeta;
39
39
  }
40
40
  export {};
@@ -5,7 +5,7 @@ import { BeanQueueBase } from 'vona-module-a-queue';
5
5
  export interface TypeQueueDoubleDeleteJobData {
6
6
  beanFullName: string;
7
7
  clientName: keyof IDatabaseClientRecord;
8
- table: keyof ITableRecord;
8
+ table?: keyof ITableRecord;
9
9
  method: 'cacheEntityDelInner' | 'cacheEntityClearInner' | 'cacheQueryClearInner';
10
10
  args: any[];
11
11
  }
@@ -59,6 +59,7 @@ export declare function config(_app: VonaApplication): {
59
59
  client?: keyof import("vona-module-a-redis").IRedisClientRecord;
60
60
  disableInstance?: boolean;
61
61
  };
62
+ emptyArrayAsNull?: boolean;
62
63
  enable?: boolean;
63
64
  meta?: Omit<IOnionOptionsMeta, never> | undefined;
64
65
  };
@@ -81,6 +82,7 @@ export declare function config(_app: VonaApplication): {
81
82
  client?: keyof import("vona-module-a-redis").IRedisClientRecord;
82
83
  disableInstance?: boolean;
83
84
  };
85
+ emptyArrayAsNull?: boolean;
84
86
  enable?: boolean;
85
87
  meta?: Omit<IOnionOptionsMeta, never> | undefined;
86
88
  };
package/dist/index.js CHANGED
@@ -230,9 +230,16 @@ let ServiceDatabase = (_dec$t = Service(), _dec2$t = BeanInfo({
230
230
  return !clientName || clientName === 'default' ? this.getDefaultClientName() : clientName;
231
231
  }
232
232
  getDefaultClientName() {
233
- let defaultClient = this.app.config.database.defaultClient;
233
+ const defaultClient = this.app.config.database.defaultClient;
234
234
  if (typeof defaultClient === 'function') {
235
- defaultClient = defaultClient(this.ctx);
235
+ return defaultClient(this.ctx);
236
+ }
237
+ // check instance
238
+ if (!isNil(this.ctx.instanceName)) {
239
+ const configInstanceBase = this.$scope.instance.service.instance.getConfigInstanceBase(this.ctx.instanceName);
240
+ if (configInstanceBase?.isolate) {
241
+ return configInstanceBase.isolateClient;
242
+ }
236
243
  }
237
244
  return defaultClient;
238
245
  }
@@ -687,10 +694,10 @@ let ServiceDatabaseClient = (_dec$m = Service(), _dec2$m = BeanInfo({
687
694
  // * should not use this.clientConfig.connection, because password is hidden
688
695
  const config = this.getClientConfig(this.clientName, true);
689
696
  config.connection = Object.assign({}, config.connection, connDatabaseName);
690
- // only used by startup, so no consider that workders broadcast
697
+ // only used by startup, so no consider that workers broadcast
691
698
  this.configDatabase.clients[this.clientName] = config;
692
699
  // reload
693
- await this.reload(config);
700
+ await this.scope.service.database.reloadClients(this.clientName, config);
694
701
  }
695
702
  }) || _class$m) || _class$m);
696
703
 
@@ -1399,15 +1406,15 @@ class BeanModelMeta extends BeanBase {
1399
1406
  // db
1400
1407
  return this.bean.database.getDb(clientName);
1401
1408
  }
1402
- getTable() {
1403
- return this[SymbolModelTable] ?? this._getTable();
1409
+ getTable(where) {
1410
+ return this._getTable(where);
1404
1411
  }
1405
- _getTable() {
1406
- const table = this.options.table;
1412
+ _getTable(where) {
1413
+ const table = this[SymbolModelTable] ?? this.options.table;
1407
1414
  if (table && typeof table === 'string') return table;
1408
1415
  const defaultTable = this.options.entity && $tableName(this.options.entity);
1409
1416
  if (table && typeof table === 'function') {
1410
- return table(this.ctx, defaultTable, this);
1417
+ return table(this.ctx, where, defaultTable, this);
1411
1418
  }
1412
1419
  if (defaultTable) return defaultTable;
1413
1420
  throw new Error(`not found table of ${this.$beanFullName}`);
@@ -1487,6 +1494,7 @@ class BeanModelMeta extends BeanBase {
1487
1494
  const beanOptions = appResource.getBean(modelClass2);
1488
1495
  const beanFullName = beanOptions.beanFullName;
1489
1496
  const options = beanOptions?.options;
1497
+ // client
1490
1498
  if (isNil(client) || client === '_auto_') {
1491
1499
  client = options?.client ? '_initial_' : '_inherit_';
1492
1500
  }
@@ -1506,7 +1514,7 @@ class BeanModelUtils extends BeanModelMeta {
1506
1514
  table = undefined;
1507
1515
  }
1508
1516
  // table
1509
- table = table || this.getTable();
1517
+ table = table || this.getTable(item);
1510
1518
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1511
1519
  // item
1512
1520
  if (!item) return [{}, {}];
@@ -1532,11 +1540,11 @@ class BeanModelUtils extends BeanModelMeta {
1532
1540
  if (this.options.entity) {
1533
1541
  return $tableDefaults(this.options.entity);
1534
1542
  }
1535
- table = table || this.getTable();
1543
+ table = table || this.getTable(undefined);
1536
1544
  return await this.db.columns.defaultData(table);
1537
1545
  }
1538
1546
  async columns(table) {
1539
- table = table || this.getTable();
1547
+ table = table || this.getTable(undefined);
1540
1548
  return await this.db.columns.columns(table);
1541
1549
  }
1542
1550
  isRaw(raw) {
@@ -1651,7 +1659,7 @@ class BeanModelUtils extends BeanModelMeta {
1651
1659
  }
1652
1660
  prepareWhere(builder, table, where, options) {
1653
1661
  // table
1654
- table = table || this.getTable();
1662
+ table = table || this.getTable(where);
1655
1663
  if (!table) throw new Error('should specify the table name');
1656
1664
  // disableInstance/disableDeleted
1657
1665
  where = Object.assign({}, where);
@@ -1731,7 +1739,7 @@ class BeanModelKnex extends BeanModelUtils {
1731
1739
  }
1732
1740
  builder(table) {
1733
1741
  // table
1734
- table = table || this.getTable();
1742
+ table = table || this.getTable(undefined);
1735
1743
  if (table) {
1736
1744
  return this.connection(table);
1737
1745
  }
@@ -1743,7 +1751,7 @@ class BeanModelKnex extends BeanModelUtils {
1743
1751
  table = undefined;
1744
1752
  }
1745
1753
  // table
1746
- table = table || this.getTable();
1754
+ table = table || this.getTable(undefined);
1747
1755
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1748
1756
  // builder
1749
1757
  const builder = this.builder(table);
@@ -1904,7 +1912,7 @@ class BeanModelCrudInner extends BeanModelView {
1904
1912
  // with undefined
1905
1913
  async _mget_original(table, ids, options) {
1906
1914
  // table
1907
- table = table || this.getTable();
1915
+ table = table || this.getTable(undefined);
1908
1916
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1909
1917
  // ids maybe empty
1910
1918
  if (!ids || ids.length === 0) return [];
@@ -1933,7 +1941,7 @@ class BeanModelCrudInner extends BeanModelView {
1933
1941
  }
1934
1942
  async _select(table, params, options, builder) {
1935
1943
  // table
1936
- table = table || this.getTable();
1944
+ table = table || this.getTable(params?.where);
1937
1945
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1938
1946
  // builder
1939
1947
  builder = builder ?? this._select_buildParams(table, params, options);
@@ -1969,7 +1977,7 @@ class BeanModelCrudInner extends BeanModelView {
1969
1977
  }
1970
1978
  async _get(table, where, options) {
1971
1979
  // table
1972
- table = table || this.getTable();
1980
+ table = table || this.getTable(where);
1973
1981
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1974
1982
  // params
1975
1983
  const params = {
@@ -1987,7 +1995,7 @@ class BeanModelCrudInner extends BeanModelView {
1987
1995
  }
1988
1996
  async _count(table, params, options) {
1989
1997
  // table
1990
- table = table || this.getTable();
1998
+ table = table || this.getTable(params?.where);
1991
1999
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
1992
2000
  // params
1993
2001
  params = params || {};
@@ -2005,12 +2013,12 @@ class BeanModelCrudInner extends BeanModelView {
2005
2013
  return this.extractFirstNumber(res);
2006
2014
  }
2007
2015
  async _insertBulk(table, data, options) {
2008
- // table
2009
- table = table || this.getTable();
2010
- if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2011
2016
  // data
2012
2017
  data = data || {};
2013
2018
  const datasTemp = Array.isArray(data) ? data : [data];
2019
+ // table
2020
+ table = table || this.getTable(datasTemp[0]);
2021
+ if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2014
2022
  // options
2015
2023
  const datas = [];
2016
2024
  const datasOriginal = [];
@@ -2045,7 +2053,7 @@ class BeanModelCrudInner extends BeanModelView {
2045
2053
  }
2046
2054
  async _update(table, data, options) {
2047
2055
  // table
2048
- table = table || this.getTable();
2056
+ table = table || this.getTable(data);
2049
2057
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2050
2058
  // data
2051
2059
  [data] = await this.prepareData(table, data);
@@ -2078,7 +2086,7 @@ class BeanModelCrudInner extends BeanModelView {
2078
2086
  }
2079
2087
  async _delete(table, where, options) {
2080
2088
  // table
2081
- table = table || this.getTable();
2089
+ table = table || this.getTable(where);
2082
2090
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2083
2091
  // disableDeleted
2084
2092
  if (!this._checkDisableDeletedByOptions(options)) {
@@ -2218,13 +2226,13 @@ let ServiceCacheEntity = (_dec$d = Service(), _dec2$d = BeanInfo({
2218
2226
  }
2219
2227
  async clear(table) {
2220
2228
  if (!this.enabled) return;
2221
- table = table || this._model.getTable();
2229
+ table = table || this._model.getTable(undefined);
2222
2230
  const cache = this.getInstance(table);
2223
2231
  await cache.clear();
2224
2232
  }
2225
2233
  async del(id, table) {
2226
2234
  if (!this.enabled) return;
2227
- table = table || this._model.getTable();
2235
+ table = table || this._model.getTable(undefined);
2228
2236
  const cache = this.getInstance(table);
2229
2237
  if (Array.isArray(id)) {
2230
2238
  await cache.mdel(id);
@@ -2246,7 +2254,7 @@ let ServiceCacheQuery = (_dec$c = Service(), _dec2$c = BeanInfo({
2246
2254
  }
2247
2255
  async clear(table) {
2248
2256
  if (!this.enabled) return;
2249
- table = table || this._model.getTable();
2257
+ table = table || this._model.getTable(undefined);
2250
2258
  const cache = this.getInstance(table);
2251
2259
  await cache.clear();
2252
2260
  }
@@ -2361,7 +2369,6 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2361
2369
  options
2362
2370
  } = relationReal;
2363
2371
  const modelTarget = this.__getModelTarget(model, options?.meta);
2364
- const tableNameTarget = modelTarget.getTable();
2365
2372
  const optionsReal = Object.assign({}, options, {
2366
2373
  include: includeReal,
2367
2374
  with: withReal
@@ -2399,21 +2406,21 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2399
2406
  if (optionsReal.groups) {
2400
2407
  const options2 = deepExtend({}, optionsReal, {
2401
2408
  where: {
2402
- [`${tableNameTarget}.${key}`]: idFrom
2409
+ [key]: idFrom
2403
2410
  }
2404
2411
  });
2405
2412
  entity[relationName] = await modelTarget.group(options2, methodOptionsReal);
2406
2413
  } else if (optionsReal.aggrs) {
2407
2414
  const options2 = deepExtend({}, optionsReal, {
2408
2415
  where: {
2409
- [`${tableNameTarget}.${key}`]: idFrom
2416
+ [key]: idFrom
2410
2417
  }
2411
2418
  });
2412
2419
  entity[relationName] = await modelTarget.aggregate(options2, methodOptionsReal);
2413
2420
  } else {
2414
2421
  const options2 = deepExtend({}, optionsReal, {
2415
2422
  where: {
2416
- [`${tableNameTarget}.${key}`]: idFrom
2423
+ [key]: idFrom
2417
2424
  }
2418
2425
  });
2419
2426
  entity[relationName] = await modelTarget.select(options2, methodOptionsReal);
@@ -2434,14 +2441,14 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2434
2441
  if (optionsReal.groups) {
2435
2442
  const options2 = deepExtend({}, optionsReal, {
2436
2443
  where: {
2437
- [`${tableNameTarget}.id`]: idsTo
2444
+ id: idsTo
2438
2445
  }
2439
2446
  });
2440
2447
  entity[relationName] = await modelTarget.group(options2, methodOptionsReal);
2441
2448
  } else if (optionsReal.aggrs) {
2442
2449
  const options2 = deepExtend({}, optionsReal, {
2443
2450
  where: {
2444
- [`${tableNameTarget}.id`]: idsTo
2451
+ id: idsTo
2445
2452
  }
2446
2453
  });
2447
2454
  entity[relationName] = await modelTarget.aggregate(options2, methodOptionsReal);
@@ -2464,7 +2471,6 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2464
2471
  options
2465
2472
  } = relationReal;
2466
2473
  const modelTarget = this.__getModelTarget(model, options?.meta);
2467
- const tableNameTarget = modelTarget.getTable();
2468
2474
  const optionsReal = Object.assign({}, options, {
2469
2475
  include: includeReal,
2470
2476
  with: withReal
@@ -2506,11 +2512,11 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2506
2512
  } else if (type === 'hasMany') {
2507
2513
  const idsFrom = entities.map(item => cast(item).id).filter(id => !isNil(id));
2508
2514
  if (optionsReal.groups) {
2509
- const groups = [`${tableNameTarget}.${key}`].concat(optionsReal.groups);
2515
+ const groups = [key].concat(optionsReal.groups);
2510
2516
  const options2 = deepExtend({}, optionsReal, {
2511
2517
  groups,
2512
2518
  where: {
2513
- [`${tableNameTarget}.${key}`]: idsFrom
2519
+ [key]: idsFrom
2514
2520
  }
2515
2521
  });
2516
2522
  const items = await modelTarget.group(options2, methodOptionsReal);
@@ -2525,9 +2531,9 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2525
2531
  }
2526
2532
  } else if (optionsReal.aggrs) {
2527
2533
  const options2 = deepExtend({}, optionsReal, {
2528
- groups: `${tableNameTarget}.${key}`,
2534
+ groups: key,
2529
2535
  where: {
2530
- [`${tableNameTarget}.${key}`]: idsFrom
2536
+ [key]: idsFrom
2531
2537
  }
2532
2538
  });
2533
2539
  const items = await modelTarget.group(options2, methodOptionsReal);
@@ -2543,7 +2549,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2543
2549
  const options2 = deepExtend({}, optionsReal, {
2544
2550
  columns,
2545
2551
  where: {
2546
- [`${tableNameTarget}.${key}`]: idsFrom
2552
+ [key]: idsFrom
2547
2553
  }
2548
2554
  });
2549
2555
  const items = await modelTarget.select(options2, methodOptionsReal);
@@ -2576,7 +2582,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2576
2582
  const options2 = deepExtend({}, optionsReal, {
2577
2583
  groups: optionsReal.groups,
2578
2584
  where: {
2579
- [`${tableNameTarget}.id`]: idsTo
2585
+ id: idsTo
2580
2586
  }
2581
2587
  });
2582
2588
  entity[relationName] = await modelTarget.group(options2, methodOptionsReal);
@@ -2586,7 +2592,7 @@ let ServiceRelations = (_dec$b = Service(), _dec2$b = BeanInfo({
2586
2592
  const idsTo = itemsMiddle.filter(item => item[keyFrom] === cast(entity).id).map(item => item[keyTo]);
2587
2593
  const options2 = deepExtend({}, optionsReal, {
2588
2594
  where: {
2589
- [`${tableNameTarget}.id`]: idsTo
2595
+ id: idsTo
2590
2596
  }
2591
2597
  });
2592
2598
  entity[relationName] = await modelTarget.aggregate(options2, methodOptionsReal);
@@ -2893,7 +2899,7 @@ class BeanModelCache extends BeanModelCrud {
2893
2899
  async __insertBulk_raw(table, items, options) {
2894
2900
  if (items.length === 0) return [];
2895
2901
  // table
2896
- table = table || this.getTable();
2902
+ table = table || this.getTable(items[0]);
2897
2903
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2898
2904
  // insert
2899
2905
  const res = await this._insertBulk(table, items, options);
@@ -2911,7 +2917,7 @@ class BeanModelCache extends BeanModelCrud {
2911
2917
  }
2912
2918
  async __mutateBulk_raw(table, items, options) {
2913
2919
  // table
2914
- table = table || this.getTable();
2920
+ table = table || this.getTable(items[0]);
2915
2921
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2916
2922
  // check
2917
2923
  const indexesInsert = [];
@@ -2973,7 +2979,7 @@ class BeanModelCache extends BeanModelCrud {
2973
2979
  }
2974
2980
  async __mget_raw(table, ids, options) {
2975
2981
  // table
2976
- table = table || this.getTable();
2982
+ table = table || this.getTable(undefined);
2977
2983
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
2978
2984
  // check if cache
2979
2985
  if (this._checkDisableCacheEntityByOptions(options)) {
@@ -3014,7 +3020,7 @@ class BeanModelCache extends BeanModelCrud {
3014
3020
  }
3015
3021
  async __aggregate_raw(table, params, options) {
3016
3022
  // table
3017
- table = table || this.getTable();
3023
+ table = table || this.getTable(params?.where);
3018
3024
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3019
3025
  const items = await this.__select_cache(table, params, options);
3020
3026
  return this.convertItemsToBigNumber(items);
@@ -3024,7 +3030,7 @@ class BeanModelCache extends BeanModelCrud {
3024
3030
  }
3025
3031
  async __group_raw(table, params, options) {
3026
3032
  // table
3027
- table = table || this.getTable();
3033
+ table = table || this.getTable(params?.where);
3028
3034
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3029
3035
  const items = await this.__select_cache(table, params, options);
3030
3036
  return this.convertItemsToBigNumber(items);
@@ -3069,7 +3075,7 @@ class BeanModelCache extends BeanModelCrud {
3069
3075
  }
3070
3076
  async __select_raw(table, params, options) {
3071
3077
  // table
3072
- table = table || this.getTable();
3078
+ table = table || this.getTable(params?.where);
3073
3079
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3074
3080
  // check if cache
3075
3081
  if (this._checkDisableCacheEntityByOptions(options)) {
@@ -3095,10 +3101,10 @@ class BeanModelCache extends BeanModelCrud {
3095
3101
  }
3096
3102
  // 2: mget
3097
3103
  const ids = items.map(item => cast(item).id);
3098
- const options2 = params?.columns ? Object.assign({}, options, {
3104
+ const options3 = params?.columns ? Object.assign({}, options, {
3099
3105
  columns: params?.columns
3100
3106
  }) : options;
3101
- return await this.__mget_raw(table, ids, options2);
3107
+ return await this.__mget_raw(table, ids, options3);
3102
3108
  }
3103
3109
  async __select_cache(table, params, options) {
3104
3110
  // check if cache
@@ -3114,6 +3120,7 @@ class BeanModelCache extends BeanModelCrud {
3114
3120
  // cache
3115
3121
  const cache = this.cacheQuery.getInstance(table);
3116
3122
  const items = await cache.get(key, {
3123
+ ...options?.cache,
3117
3124
  get: async () => {
3118
3125
  return await super._select(table, params, options, builder);
3119
3126
  },
@@ -3136,7 +3143,7 @@ class BeanModelCache extends BeanModelCrud {
3136
3143
  }
3137
3144
  async __get_raw(table, where, options) {
3138
3145
  // table
3139
- table = table || this.getTable();
3146
+ table = table || this.getTable(where);
3140
3147
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3141
3148
  // check if cache
3142
3149
  if (this._checkDisableCacheEntityByOptions(options)) {
@@ -3157,9 +3164,12 @@ class BeanModelCache extends BeanModelCrud {
3157
3164
  params.columns = options?.columns;
3158
3165
  }
3159
3166
  // select
3160
- const options2 = options?.columns ? Object.assign({}, options, {
3161
- columns: undefined
3162
- }) : options;
3167
+ const options2 = deepExtend({}, options, {
3168
+ columns: undefined,
3169
+ cache: {
3170
+ emptyArrayAsNull: true
3171
+ }
3172
+ });
3163
3173
  const items = await this.__select_raw(table, params, options2);
3164
3174
  return items[0];
3165
3175
  }
@@ -3188,7 +3198,7 @@ class BeanModelCache extends BeanModelCrud {
3188
3198
  }
3189
3199
  async __update_raw(table, data, options) {
3190
3200
  // table
3191
- table = table || this.getTable();
3201
+ table = table || this.getTable(data);
3192
3202
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3193
3203
  // check if cache
3194
3204
  if (this._checkDisableCacheEntityByOptions(options)) {
@@ -3215,13 +3225,16 @@ class BeanModelCache extends BeanModelCrud {
3215
3225
  const where = !isNil(id) ? Object.assign({}, options?.where, {
3216
3226
  id
3217
3227
  }) : options?.where;
3218
- options = Object.assign({}, options, {
3219
- where: undefined
3228
+ const options2 = deepExtend({}, options, {
3229
+ where: undefined,
3230
+ cache: {
3231
+ emptyArrayAsNull: true
3232
+ }
3220
3233
  });
3221
3234
  const items = await this.__select_raw(table, {
3222
3235
  where,
3223
3236
  columns: ['id']
3224
- }, options);
3237
+ }, options2);
3225
3238
  if (items.length === 0) {
3226
3239
  // donothing
3227
3240
  return;
@@ -3263,7 +3276,7 @@ class BeanModelCache extends BeanModelCrud {
3263
3276
  }
3264
3277
  async __delete_raw(table, where, options) {
3265
3278
  // table
3266
- table = table || this.getTable();
3279
+ table = table || this.getTable(where);
3267
3280
  if (!table) return this.scopeOrm.error.ShouldSpecifyTable.throw();
3268
3281
  // check if cache
3269
3282
  if (this._checkDisableCacheEntityByOptions(options)) {
@@ -3273,10 +3286,15 @@ class BeanModelCache extends BeanModelCrud {
3273
3286
  let id = this.__checkCacheKeyValid(where, table);
3274
3287
  if (isNil(id)) {
3275
3288
  // check where and get id
3289
+ const options2 = deepExtend({}, options, {
3290
+ cache: {
3291
+ emptyArrayAsNull: true
3292
+ }
3293
+ });
3276
3294
  const items = await this.__select_raw(table, {
3277
3295
  where,
3278
3296
  columns: ['id']
3279
- }, options);
3297
+ }, options2);
3280
3298
  if (items.length === 0) {
3281
3299
  // donothing
3282
3300
  return;
@@ -3342,7 +3360,7 @@ class BeanModelCache extends BeanModelCrud {
3342
3360
  this._shardingCacheDoubleDelete({
3343
3361
  beanFullName: this.$beanFullName,
3344
3362
  clientName: this.db.clientName,
3345
- table: this.getTable(),
3363
+ table,
3346
3364
  method: 'cacheEntityDelInner',
3347
3365
  args: [id, table]
3348
3366
  });
@@ -3361,7 +3379,7 @@ class BeanModelCache extends BeanModelCrud {
3361
3379
  this._shardingCacheDoubleDelete({
3362
3380
  beanFullName: this.$beanFullName,
3363
3381
  clientName: this.db.clientName,
3364
- table: this.getTable(),
3382
+ table,
3365
3383
  method: 'cacheEntityClearInner',
3366
3384
  args: [table]
3367
3385
  });
@@ -3380,7 +3398,7 @@ class BeanModelCache extends BeanModelCrud {
3380
3398
  this._shardingCacheDoubleDelete({
3381
3399
  beanFullName: this.$beanFullName,
3382
3400
  clientName: this.db.clientName,
3383
- table: this.getTable(),
3401
+ table,
3384
3402
  method: 'cacheQueryClearInner',
3385
3403
  args: [table]
3386
3404
  });
@@ -3823,71 +3841,17 @@ class Main extends BeanSimple {
3823
3841
  }
3824
3842
  async configLoaded(_config) {}
3825
3843
  }
3826
- async function configDefault(app) {
3827
- return {
3828
- testDatabase: false,
3829
- defaultClient: app.meta.env.DATABASE_DEFAULT_CLIENT,
3830
- clients: {
3831
- pg: {
3832
- client: 'pg',
3833
- connection: {
3834
- host: app.meta.env.DATABASE_CLIENT_PG_HOST,
3835
- port: Number.parseInt(app.meta.env.DATABASE_CLIENT_PG_PORT),
3836
- user: app.meta.env.DATABASE_CLIENT_PG_USER,
3837
- password: app.meta.env.DATABASE_CLIENT_PG_PASSWORD,
3838
- database: app.meta.env.DATABASE_CLIENT_PG_DATABASE
3839
- }
3840
- },
3841
- mysql: {
3842
- client: 'mysql2',
3843
- connection: {
3844
- host: app.meta.env.DATABASE_CLIENT_MYSQL_HOST,
3845
- port: Number.parseInt(app.meta.env.DATABASE_CLIENT_MYSQL_PORT),
3846
- user: app.meta.env.DATABASE_CLIENT_MYSQL_USER,
3847
- password: app.meta.env.DATABASE_CLIENT_MYSQL_PASSWORD,
3848
- database: app.meta.env.DATABASE_CLIENT_MYSQL_DATABASE
3849
- }
3850
- }
3851
- },
3852
- base: {
3853
- pool: {
3854
- min: 0,
3855
- max: 5
3856
- },
3857
- acquireConnectionTimeout: 60000 * 10,
3858
- asyncStackTraces: true
3859
- }
3860
- };
3844
+ async function configDefault(_app) {
3845
+ return {};
3861
3846
  }
3862
3847
  async function configDev(_app) {
3863
- return {
3864
- testDatabase: true,
3865
- base: {
3866
- pool: {
3867
- min: 0,
3868
- max: 1
3869
- }
3870
- }
3871
- };
3848
+ return {};
3872
3849
  }
3873
3850
  async function configProd(_app) {
3874
- return {
3875
- testDatabase: false,
3876
- base: {
3877
- asyncStackTraces: false
3878
- }
3879
- };
3851
+ return {};
3880
3852
  }
3881
3853
  async function configTest(_app) {
3882
- return {
3883
- testDatabase: true,
3884
- base: {
3885
- pool: {
3886
- min: 0,
3887
- max: 1
3888
- }
3889
- }
3890
- };
3854
+ return {};
3891
3855
  }
3892
3856
 
3893
3857
  var _dec, _dec2, _class;
package/dist/main.d.ts CHANGED
@@ -6,4 +6,4 @@ export declare class Main extends BeanSimple implements IModuleMain {
6
6
  moduleLoaded(): Promise<void>;
7
7
  configLoaded(_config: any): Promise<void>;
8
8
  }
9
- export declare function configDefault(app: VonaApplication): Promise<PowerPartial<ConfigDatabase>>;
9
+ export declare function configDefault(_app: VonaApplication): Promise<PowerPartial<ConfigDatabase>>;
@@ -1,4 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
+ import type { TSummerCacheActionOptions } from 'vona-module-a-summer';
2
3
  import type { BeanModelMeta } from '../bean/bean.model/bean.model_meta.ts';
3
4
  import type { TypeModelColumn, TypeModelColumns, TypeModelColumnsPatch, TypeModelWhere } from './modelWhere.ts';
4
5
  import type { TypeModelOfModelLike, TypeModelParamsInclude, TypeModelsClassLikeGeneral, TypeSymbolKeyEntity } from './relations.ts';
@@ -51,6 +52,7 @@ export interface IModelMethodOptionsGeneral {
51
52
  disableCacheQuery?: boolean;
52
53
  disableCacheEntity?: boolean;
53
54
  deleted?: boolean;
55
+ cache?: TSummerCacheActionOptions<unknown, unknown>;
54
56
  }
55
57
  export interface IModelInsertOptionsGeneral<_TRecord, Model extends BeanModelMeta | undefined = undefined> extends IModelMethodOptionsGeneral, IModelMutateRelationIncludeWrapper<Model> {
56
58
  }
@@ -10,7 +10,7 @@ export interface IModelRecord {
10
10
  }
11
11
  export interface IModelClassRecord {
12
12
  }
13
- export type TypeDynamicTableName = (ctx: VonaContext, defaultTable: keyof ITableRecord, modelInstance: any) => string;
13
+ export type TypeDynamicTableName = (ctx: VonaContext, where: any | undefined, defaultTable: keyof ITableRecord, modelInstance: any) => string;
14
14
  export type TypeDynamicClientName = (ctx: VonaContext, modelInstance: any) => keyof IDatabaseClientRecord;
15
15
  export type TypeModelsClearedByFn = (ctx: VonaContext, modelTarget: any, modelSource: any) => Promise<void>;
16
16
  export type TypeSoftDeletionPruneHandler = (ctx: VonaContext, modelInstance: any, options: ISoftDeletionPruneHandlerOptions) => Promise<void>;
@@ -21,9 +21,10 @@ export interface ISoftDeletionPrune {
21
21
  handler?: TypeSoftDeletionPruneHandler;
22
22
  expired?: number;
23
23
  }
24
+ export type TypeModelOptionsTable = TypeDynamicTableName | keyof ITableRecord;
24
25
  export interface IDecoratorModelOptions<TRecord extends EntityBaseEmpty = any> {
25
26
  entity?: Constructable<TRecord>;
26
- table?: TypeDynamicTableName | keyof ITableRecord;
27
+ table?: TypeModelOptionsTable;
27
28
  disableDeleted?: boolean;
28
29
  disableInstance?: boolean;
29
30
  disableCreateTime?: boolean;
@@ -4,8 +4,7 @@ import type { IDatabaseClientRecord } from './database.ts';
4
4
  import type { TypeModelSelectAggrParamsAggrs } from './modelAggr.ts';
5
5
  import type { IBuildModelSelectGeneralParamsBasic } from './modelGeneral.ts';
6
6
  import type { TypeModelColumn, TypeModelColumnsPatch, TypeModelColumnsStrict } from './modelWhere.ts';
7
- import type { IModelClassRecord } from './onion/model.ts';
8
- import type { ITableRecord } from './onion/table.ts';
7
+ import type { IModelClassRecord, TypeModelOptionsTable } from './onion/model.ts';
9
8
  import type { TypeModelClassLike, TypeModelOfModelLike, TypeModelsClassLikeGeneral, TypeSymbolKeyEntity } from './relations.ts';
10
9
  import type { TypeEntityTableColumnNamesOfGeneral, TypeEntityTableColumnsOfGeneral } from './relationsColumns.ts';
11
10
  import type { TypeEntityTableNamesOfGeneral } from './relationsTables.ts';
@@ -48,7 +47,7 @@ export interface IBuildModelRelationOptionsMany<TRecord, AUTOLOAD extends boolea
48
47
  }
49
48
  export interface IModelRelationOptionsMetaBasic {
50
49
  client?: TypeModelRelationOptionsMetaClient;
51
- table?: keyof ITableRecord;
50
+ table?: TypeModelOptionsTable;
52
51
  }
53
52
  export interface IModelRelationOptionsMeta extends IModelRelationOptionsMetaBasic {
54
53
  middle?: IModelRelationOptionsMetaBasic;
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.55",
4
+ "version": "5.0.57",
5
5
  "title": "a-orm",
6
6
  "vonaModule": {
7
7
  "capabilities": {