imodel 0.5.2 → 0.7.0

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/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.5.2
2
+ * imodel v0.7.0
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -686,6 +686,16 @@ interface IndexInfo extends IndexOptions {
686
686
  interface Index extends IndexInfo {
687
687
  name: string;
688
688
  }
689
+ interface Hooks<T extends Record<string, any> = Record<string, any>> {
690
+ beforeCreateMany(conn: Connection, records: Record<string, any>[]): PromiseLike<void | Record<string, any>[]> | void | Record<string, any>[];
691
+ afterCreateMany(conn: Connection, records: Record<string, any>[]): PromiseLike<void> | void;
692
+ beforeCreate(conn: Connection, record: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
693
+ afterCreate(conn: Connection, record: Record<string, any>): PromiseLike<void> | void;
694
+ beforeUpdate(conn: Connection, record: Record<string, any>, set: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
695
+ afterUpdate(conn: Connection, record: Record<string, any>, oldRecord: Record<string, any>): PromiseLike<void> | void;
696
+ beforeDelete(conn: Connection, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
697
+ afterDelete(conn: Connection, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
698
+ }
689
699
  interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | undefined = string | VirtualTable | undefined> {
690
700
  /** 字段信息 */
691
701
  readonly fields: T;
@@ -695,6 +705,8 @@ interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTabl
695
705
  readonly pseudo?: string;
696
706
  /** 索引信息 */
697
707
  readonly indexes?: readonly IndexInfo[];
708
+ /** 钩子 */
709
+ readonly hooks?: Partial<Hooks>;
698
710
  }
699
711
 
700
712
  type deleted = typeof deleted;
@@ -863,7 +875,7 @@ interface Save {
863
875
  }
864
876
  declare const Create: unique symbol;
865
877
  interface Create<T = unknown> {
866
- [Create](connection: Connection, data: object, run: (data: any) => Promise<object>, table: TableDefine): PromiseLike<T>;
878
+ [Create](connection: Connection, data: object, run: (data: any, that: any) => Promise<object>, table: TableDefine): PromiseLike<T>;
867
879
  }
868
880
  declare const Build: unique symbol;
869
881
  interface Build<T = unknown> {
@@ -1032,21 +1044,21 @@ declare class Connection<E extends {} = {}> {
1032
1044
  /**
1033
1045
  * @param {TableDefine} tableDefine
1034
1046
  * @param {Record<string, any>} update
1035
- * @param {WhereValue[]?} [where]
1047
+ * @param {Wheres?} [where]
1036
1048
  * @param {boolean | Skip} [skip]
1037
1049
  * @returns {Promise<number>}
1038
1050
  */
1039
- update({ table, fields }: TableDefine, update: Record<string, any>, where?: WhereValue[] | null, skip?: boolean | Skip): Promise<number>;
1051
+ update({ table, fields }: TableDefine, update: Record<string, any>, where?: Wheres | null, skip?: boolean | Skip): Promise<number>;
1040
1052
  /**
1041
1053
  * @template {Fields<MainFieldType>} T
1042
1054
  * @param {TableDefine<T>} tableDefine
1043
1055
  * @param {Record<string, any>} update
1044
1056
  * @param {string[]?} [returning]
1045
- * @param {WhereValue[]?} [where]
1057
+ * @param {Wheres?} [where]
1046
1058
  * @param {boolean | Skip} [skip]
1047
1059
  * @returns {Promise<any[]>}
1048
1060
  */
1049
- updateReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: WhereValue[] | null, skip?: boolean | Skip): Promise<any[]>;
1061
+ updateReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: Wheres | null, skip?: boolean | Skip): Promise<any[]>;
1050
1062
  /**
1051
1063
  * @param {TableDefine} tableDefine
1052
1064
  * @param {string[]} pKeys
@@ -1069,18 +1081,18 @@ declare class Connection<E extends {} = {}> {
1069
1081
  updateManyReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, pKeys: string[], setKeys: string[], list: Record<string, any>[], returning?: string[] | null, skip?: boolean | Skip): Promise<any[]>;
1070
1082
  /**
1071
1083
  * @param {TableDefine} tableDefine
1072
- * @param {WhereValue[]} [where]
1084
+ * @param {Wheres?} [where]
1073
1085
  * @returns {Promise<number>}
1074
1086
  */
1075
- delete({ table, fields }: TableDefine, where?: WhereValue[]): Promise<number>;
1087
+ delete({ table, fields }: TableDefine, where?: Wheres | null): Promise<number>;
1076
1088
  /**
1077
1089
  * @template {Fields<MainFieldType>} T
1078
1090
  * @param {TableDefine<T>} tableDefine
1079
1091
  * @param {string[]?} [returning]
1080
- * @param {WhereValue[]} [where]
1092
+ * @param {Wheres?} [where]
1081
1093
  * @returns {Promise<any[]>}
1082
1094
  */
1083
- deleteReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, returning?: string[] | null, where?: WhereValue[]): Promise<any[]>;
1095
+ deleteReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, returning?: string[] | null, where?: Wheres | null): Promise<any[]>;
1084
1096
  /**
1085
1097
  *
1086
1098
  * @param {Queryable} queryable
@@ -1549,7 +1561,7 @@ declare function defaultValue<T>(value: T): FieldDecorator<T>;
1549
1561
  * @param {string} table
1550
1562
  * @returns {ClassDecorator}
1551
1563
  */
1552
- declare function submodel(table: string): ClassDecorator;
1564
+ declare function model(table: string): ClassDecorator;
1553
1565
  /**
1554
1566
  * @template {Record<string, any>} T
1555
1567
  * @overload
@@ -1559,7 +1571,7 @@ declare function submodel(table: string): ClassDecorator;
1559
1571
  * @param {FieldDefineOption} [options]
1560
1572
  * @returns {FieldDecorator<T[]>}
1561
1573
  */
1562
- declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1574
+ declare function model<T extends Record<string, any>>(type: TableDefine & {
1563
1575
  new (...a: any): T;
1564
1576
  }, constraints: Record<string, Constraint>, array: true, options?: FieldDefineOption | undefined): FieldDecorator<T[]>;
1565
1577
  /**
@@ -1571,7 +1583,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1571
1583
  * @param {FieldDefineOption} [options]
1572
1584
  * @returns {FieldDecorator<T>}
1573
1585
  */
1574
- declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1586
+ declare function model<T extends Record<string, any>>(type: TableDefine & {
1575
1587
  new (...a: any): T;
1576
1588
  }, constraints: Record<string, Constraint>, array?: false | undefined, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1577
1589
  /**
@@ -1582,7 +1594,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1582
1594
  * @param {FieldDefineOption} [options]
1583
1595
  * @returns {FieldDecorator<T>}
1584
1596
  */
1585
- declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1597
+ declare function model<T extends Record<string, any>>(type: TableDefine & {
1586
1598
  new (...a: any): T;
1587
1599
  }, constraints: Record<string, Constraint>, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1588
1600
  /**
@@ -1595,7 +1607,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine & {
1595
1607
  * @param {FieldDefineOption} [options]
1596
1608
  * @returns {FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>}
1597
1609
  */
1598
- declare function submodel<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine & {
1610
+ declare function model<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine & {
1599
1611
  new (...a: any): T;
1600
1612
  }, constraints: Record<string, Constraint>, array: A, options?: FieldDefineOption | undefined): FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>;
1601
1613
  /**
@@ -1607,7 +1619,7 @@ declare function submodel<T extends Record<string, any>, A extends boolean = boo
1607
1619
  * @param {FieldDefineOption} [options]
1608
1620
  * @returns {FieldDecorator<T[]>}
1609
1621
  */
1610
- declare function submodel<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, array: true, options?: FieldDefineOption | undefined): FieldDecorator<T[]>;
1622
+ declare function model<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, array: true, options?: FieldDefineOption | undefined): FieldDecorator<T[]>;
1611
1623
  /**
1612
1624
  * @template {object} T
1613
1625
  * @overload
@@ -1617,7 +1629,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine, cons
1617
1629
  * @param {FieldDefineOption} [options]
1618
1630
  * @returns {FieldDecorator<T>}
1619
1631
  */
1620
- declare function submodel<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, array?: false | undefined, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1632
+ declare function model<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, array?: false | undefined, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1621
1633
  /**
1622
1634
  * @template {object} T
1623
1635
  * @overload
@@ -1626,7 +1638,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine, cons
1626
1638
  * @param {FieldDefineOption} [options]
1627
1639
  * @returns {FieldDecorator<T>}
1628
1640
  */
1629
- declare function submodel<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1641
+ declare function model<T extends Record<string, any>>(type: TableDefine, constraints: Record<string, Constraint>, options?: FieldDefineOption | undefined): FieldDecorator<T>;
1630
1642
  /**
1631
1643
  * @template {object} T
1632
1644
  * @template {boolean} [A=boolean]
@@ -1637,7 +1649,7 @@ declare function submodel<T extends Record<string, any>>(type: TableDefine, cons
1637
1649
  * @param {FieldDefineOption} [options]
1638
1650
  * @returns {FieldDecorator<A extends false ? T :A extends true ? T[] : T | T[]>}
1639
1651
  */
1640
- declare function submodel<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine, constraints: Record<string, Constraint>, array: A, options?: FieldDefineOption | undefined): FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>;
1652
+ declare function model<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine, constraints: Record<string, Constraint>, array: A, options?: FieldDefineOption | undefined): FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>;
1641
1653
 
1642
1654
  /**
1643
1655
  * @param {number} [size]
@@ -1655,11 +1667,29 @@ declare function updating(size?: number): FieldDecorator<Date | null>;
1655
1667
  */
1656
1668
  declare function deleting(size?: number): FieldDecorator<Date | null>;
1657
1669
 
1670
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['beforeCreateMany']>} */
1671
+ declare const beforeCreateMany: (insert?: boolean) => MethodDecorator<Hooks["beforeCreateMany"]>;
1672
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['afterCreateMany']>} */
1673
+ declare const afterCreateMany: (insert?: boolean) => MethodDecorator<Hooks["afterCreateMany"]>;
1674
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['beforeCreate']>} */
1675
+ declare const beforeCreate: (insert?: boolean) => MethodDecorator<Hooks["beforeCreate"]>;
1676
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['afterCreate']>} */
1677
+ declare const afterCreate: (insert?: boolean) => MethodDecorator<Hooks["afterCreate"]>;
1678
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['beforeUpdate']>} */
1679
+ declare const beforeUpdate: (insert?: boolean) => MethodDecorator<Hooks["beforeUpdate"]>;
1680
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['afterUpdate']>} */
1681
+ declare const afterUpdate: (insert?: boolean) => MethodDecorator<Hooks["afterUpdate"]>;
1682
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['beforeDelete']>} */
1683
+ declare const beforeDelete: (insert?: boolean) => MethodDecorator<Hooks["beforeDelete"]>;
1684
+ /** @type {(insert?: boolean) => MethodDecorator<Hooks['afterDelete']>} */
1685
+ declare const afterDelete: (insert?: boolean) => MethodDecorator<Hooks["afterDelete"]>;
1686
+
1658
1687
  type ClassDecorator = (val: Function, ctx: ClassDecoratorContext) => any;
1659
1688
  type FieldDecorator<T> = (val: {
1660
1689
  get(): T;
1661
1690
  set(v: T): void;
1662
1691
  }, ctx: ClassAccessorDecoratorContext) => any;
1692
+ type MethodDecorator<T extends Function> = (val: T, ctx: ClassMethodDecoratorContext<any, T>) => any;
1663
1693
 
1664
1694
  type QueryOptions<T extends Fields, TB extends unknown = any> = {
1665
1695
  table?: string | VirtualTable<object> | undefined;
@@ -1921,6 +1951,7 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1921
1951
  static readonly fields: Fields;
1922
1952
  /** @readonly @type {readonly IndexInfo[]} */
1923
1953
  static readonly indexes: readonly IndexInfo[];
1954
+ static get hooks(): Readonly<Partial<Hooks<Record<string, any>>>>;
1924
1955
  /**
1925
1956
  * @template {typeof Model} T
1926
1957
  * @this {T}
@@ -1941,10 +1972,10 @@ declare class Model implements Save, Destroy, PseudoDestroy {
1941
1972
  * @this {T}
1942
1973
  * @param {Connection} t
1943
1974
  * @param {object} p
1944
- * @param {(data: object) => Promise<object>} run
1975
+ * @param {(data: object, that: any) => Promise<object>} run
1945
1976
  * @returns {Promise<InstanceType<T>>}
1946
1977
  */
1947
- static [Create]<T extends typeof Model>(this: T, t: Connection, p: object, run: (data: object) => Promise<object>): Promise<InstanceType<T>>;
1978
+ static [Create]<T extends typeof Model>(this: T, t: Connection, p: object, run: (data: object, that: any) => Promise<object>): Promise<InstanceType<T>>;
1948
1979
  /**
1949
1980
  * @template {typeof Model} T
1950
1981
  * @this {T}
@@ -2272,4 +2303,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
2272
2303
  declare function setDevelopment(d?: boolean): void;
2273
2304
  declare let isDevelopment: boolean;
2274
2305
 
2275
- export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, withDeleted };
2306
+ export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, withDeleted };