vona-module-a-orm 5.0.61 → 5.0.62

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.
@@ -6,9 +6,10 @@ import path from 'node:path';
6
6
  import { ensureArray } from '@cabloy/utils';
7
7
  import { toUpperCaseFirstChar } from '@cabloy/word-utils';
8
8
 
9
- type TypeMagicFieldMethod = 'getBy' | 'selectBy';
9
+ type TypeMagicFieldMethod = 'getBy' | 'selectBy' | 'updateBy' | 'deleteBy';
10
10
  type TypeMagicFieldOp = '' | 'eqI';
11
11
  interface IMagicField {
12
+ optional: boolean;
12
13
  type: 'auto' | 'number' | 'TableIdentity' | 'string' | 'boolean';
13
14
  methods: TypeMagicFieldMethod | Array<TypeMagicFieldMethod>;
14
15
  ops?: TypeMagicFieldOp | Array<TypeMagicFieldOp>;
@@ -17,31 +18,38 @@ interface IMagicField {
17
18
  // id/name/enabled/disabled/closed/active/current/
18
19
  const __MagicFields: Record<string, IMagicField> = {
19
20
  id: {
21
+ optional: false,
20
22
  type: 'auto',
21
- methods: 'getBy',
23
+ methods: ['getBy', 'updateBy', 'deleteBy'],
22
24
  },
23
25
  name: {
26
+ optional: true,
24
27
  type: 'string',
25
28
  methods: ['getBy', 'selectBy'],
26
29
  ops: ['', 'eqI'],
27
30
  },
28
31
  enabled: {
32
+ optional: true,
29
33
  type: 'boolean',
30
34
  methods: ['getBy', 'selectBy'],
31
35
  },
32
36
  disabled: {
37
+ optional: true,
33
38
  type: 'boolean',
34
39
  methods: ['getBy', 'selectBy'],
35
40
  },
36
41
  closed: {
42
+ optional: true,
37
43
  type: 'boolean',
38
44
  methods: ['getBy', 'selectBy'],
39
45
  },
40
46
  active: {
47
+ optional: true,
41
48
  type: 'boolean',
42
49
  methods: ['getBy', 'selectBy'],
43
50
  },
44
51
  current: {
52
+ optional: true,
45
53
  type: 'boolean',
46
54
  methods: ['getBy', 'selectBy'],
47
55
  },
@@ -56,22 +64,22 @@ export function __parseMagics(cli: BeanCliBase, ast: GoGoCode.GoGoAST, globFile:
56
64
  const contentRecords: string[] = [];
57
65
  for (const fieldName in __MagicFields) {
58
66
  const magicField = __MagicFields[fieldName];
59
- if (fieldName === 'id') {
60
- if (!modelInfo.fieldNames.includes('getById') && entityInfo.idType) {
61
- contentRecords.push(`getById<T extends IModelGetOptions<${entityName},${className}>>(id: ${entityInfo.idType}, options?: T): Promise<TypeModelRelationResult<${entityName}, ${className}, T> | undefined>;`);
62
- }
63
- continue;
64
- }
65
67
  if (!entityInfo.fieldNames.includes(fieldName)) continue;
66
68
  for (const method of ensureArray(magicField.methods)!) {
67
69
  const ops = ensureArray(magicField.ops || [''])!;
68
70
  for (const op of ops) {
69
71
  const actionName = `${method}${toUpperCaseFirstChar(fieldName)}${toUpperCaseFirstChar(op)}`;
70
72
  if (modelInfo.fieldNames.includes(actionName)) continue;
73
+ const optional = magicField.optional ? '?' : '';
74
+ const type = fieldName === 'id' ? entityInfo.idType : magicField.type;
71
75
  if (method === 'getBy') {
72
- contentRecords.push(`${actionName}<T extends IModelGetOptions<${entityName},${className}>>(${fieldName}?: ${magicField.type}, options?: T): Promise<TypeModelRelationResult<${entityName}, ${className}, T> | undefined>;`);
76
+ contentRecords.push(`${actionName}<T extends IModelGetOptions<${entityName},${className}>>(${fieldName}${optional}: ${type}, options?: T): Promise<TypeModelRelationResult<${entityName}, ${className}, T> | undefined>;`);
73
77
  } else if (method === 'selectBy') {
74
- contentRecords.push(`${actionName}<T extends IModelSelectParams<${entityName},${className},ModelJoins>, ModelJoins extends TypeModelsClassLikeGeneral | undefined = undefined>(${fieldName}?: ${magicField.type}, params?: T, options?: IModelMethodOptions, modelJoins?: ModelJoins): Promise<TypeModelRelationResult<${entityName}, ${className}, T>[]>;`);
78
+ contentRecords.push(`${actionName}<T extends IModelSelectParams<${entityName},${className},ModelJoins>, ModelJoins extends TypeModelsClassLikeGeneral | undefined = undefined>(${fieldName}${optional}: ${type}, params?: T, options?: IModelMethodOptions, modelJoins?: ModelJoins): Promise<TypeModelRelationResult<${entityName}, ${className}, T>[]>;`);
79
+ } else if (method === 'updateBy') {
80
+ contentRecords.push(`${actionName}<T extends IModelUpdateOptions<${entityName},${className}>>(${fieldName}: ${type}${optional ? ' | undefined' : ''}, data: TypeModelMutateRelationData<${entityName},${className}, T>, options?: T): Promise<TypeModelMutateRelationData<${entityName},${className}, T>>;`);
81
+ } else if (method === 'deleteBy') {
82
+ contentRecords.push(`${actionName}<T extends IModelDeleteOptions<${entityName},${className}>>(${fieldName}${optional}: ${type}, options?: T): Promise<void>;`);
75
83
  }
76
84
  }
77
85
  }
@@ -95,6 +103,7 @@ function __parseEntityInfo(cli: BeanCliBase, fileEntity: string, entityName: str
95
103
  for (const astNode of astNodes) {
96
104
  fieldNames.push((astNode as any).key.name);
97
105
  }
106
+ if (idType) fieldNames.push('id');
98
107
  return { idType, fieldNames };
99
108
  }
100
109
 
@@ -58,6 +58,6 @@ export declare class BeanModelCache<TRecord extends {} = {}> extends BeanModelCr
58
58
  protected _checkDisableCacheEntityByOptions(options?: IModelMethodOptionsGeneral): boolean;
59
59
  private __checkIfOnlyKey;
60
60
  private __checkCacheKeyValid;
61
- protected __get__(prop: string): ((fieldValue?: any, options?: any) => Promise<Partial<TRecord> | undefined>) | ((fieldValue?: any, params?: any, options?: any, modelJoins?: any) => Promise<any[]>) | undefined;
61
+ protected __get__(prop: string): ((fieldValue?: any, options?: any) => Promise<Partial<TRecord> | undefined>) | ((fieldValue?: any, params?: any, options?: any, modelJoins?: any) => Promise<any[]>) | ((fieldValue: any, data: any, options?: any) => Promise<Partial<TRecord>>) | ((fieldValue: any, options?: any) => Promise<void>) | undefined;
62
62
  }
63
63
  export {};
package/dist/index.js CHANGED
@@ -3541,12 +3541,33 @@ class BeanModelCache extends BeanModelCrud {
3541
3541
  };
3542
3542
  return this.select(params2, options, modelJoins);
3543
3543
  };
3544
+ } else if (prop.startsWith('updateBy')) {
3545
+ const [fieldName, op] = __parseMagicField(prop.substring('updateBy'.length));
3546
+ if (!fieldName) throw new Error(`invalid magic method: ${prop}`);
3547
+ return (fieldValue, data, options) => {
3548
+ const where = __combineMagicWhere(fieldName, op, fieldValue);
3549
+ if (fieldName === 'id') {
3550
+ data = Object.assign({}, data, where);
3551
+ } else {
3552
+ options = deepExtend({}, options, {
3553
+ where
3554
+ });
3555
+ }
3556
+ return this.update(data, options);
3557
+ };
3558
+ } else if (prop.startsWith('deleteBy')) {
3559
+ const [fieldName, op] = __parseMagicField(prop.substring('deleteBy'.length));
3560
+ if (!fieldName) throw new Error(`invalid magic method: ${prop}`);
3561
+ return (fieldValue, options) => {
3562
+ const where = __combineMagicWhere(fieldName, op, fieldValue);
3563
+ return this.delete(where, options);
3564
+ };
3544
3565
  }
3545
3566
  }
3546
3567
  }
3547
3568
  function __combineMagicWhere(fieldName, op, fieldValue) {
3548
3569
  return {
3549
- [fieldName]: fieldValue === undefined ? undefined : {
3570
+ [fieldName]: fieldValue === undefined ? undefined : op === 'eq' ? fieldValue : {
3550
3571
  [`_${op}_`]: fieldValue
3551
3572
  }
3552
3573
  };
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.61",
4
+ "version": "5.0.62",
5
5
  "title": "a-orm",
6
6
  "vonaModule": {
7
7
  "capabilities": {