tspace-mysql 1.9.0 → 1.9.1-beta.2

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.
Files changed (118) hide show
  1. package/README.md +4580 -133
  2. package/dist/lib/config/index.d.ts +2 -2
  3. package/dist/lib/config/index.js +24 -16
  4. package/dist/lib/config/index.js.map +1 -1
  5. package/dist/lib/constants/index.d.ts +10 -1
  6. package/dist/lib/constants/index.js +11 -2
  7. package/dist/lib/constants/index.js.map +1 -1
  8. package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +27 -23
  9. package/dist/lib/core/Abstracts/AbstractBuilder.js +11 -13
  10. package/dist/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
  11. package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -2
  12. package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
  13. package/dist/lib/core/Abstracts/AbstractModel.d.ts +7 -2
  14. package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
  15. package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
  16. package/dist/lib/core/Blueprint.d.ts +37 -17
  17. package/dist/lib/core/Blueprint.js +38 -14
  18. package/dist/lib/core/Blueprint.js.map +1 -1
  19. package/dist/lib/core/Builder.d.ts +314 -42
  20. package/dist/lib/core/Builder.js +686 -391
  21. package/dist/lib/core/Builder.js.map +1 -1
  22. package/dist/lib/core/Cache/DBCache.js +19 -20
  23. package/dist/lib/core/Cache/DBCache.js.map +1 -1
  24. package/dist/lib/core/Cache/MemoryCache.js +3 -3
  25. package/dist/lib/core/Cache/MemoryCache.js.map +1 -1
  26. package/dist/lib/core/Cache/RedisCache.js +27 -17
  27. package/dist/lib/core/Cache/RedisCache.js.map +1 -1
  28. package/dist/lib/core/Cache/index.js +37 -17
  29. package/dist/lib/core/Cache/index.js.map +1 -1
  30. package/dist/lib/core/Contracts/AlterTable.d.ts +152 -0
  31. package/dist/lib/core/Contracts/AlterTable.js +243 -0
  32. package/dist/lib/core/Contracts/AlterTable.js.map +1 -0
  33. package/dist/lib/core/Contracts/Audit.d.ts +1 -1
  34. package/dist/lib/core/Contracts/Audit.js +2 -5
  35. package/dist/lib/core/Contracts/Audit.js.map +1 -1
  36. package/dist/lib/core/Contracts/Logger.d.ts +1 -1
  37. package/dist/lib/core/Contracts/Logger.js.map +1 -1
  38. package/dist/lib/core/DB.d.ts +103 -11
  39. package/dist/lib/core/DB.js +158 -32
  40. package/dist/lib/core/DB.js.map +1 -1
  41. package/dist/lib/core/Decorator.d.ts +144 -16
  42. package/dist/lib/core/Decorator.js +156 -14
  43. package/dist/lib/core/Decorator.js.map +1 -1
  44. package/dist/lib/core/Driver/index.d.ts +50 -14
  45. package/dist/lib/core/Driver/index.js +13 -9
  46. package/dist/lib/core/Driver/index.js.map +1 -1
  47. package/dist/lib/core/Driver/mariadb/MariadbDriver.js +84 -29
  48. package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
  49. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +48 -12
  50. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +189 -28
  51. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
  52. package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +24 -0
  53. package/dist/lib/core/Driver/mongodb/MongodbDriver.js +261 -0
  54. package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -0
  55. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +141 -0
  56. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +571 -0
  57. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -0
  58. package/dist/lib/core/Driver/mysql/MysqlDriver.js +115 -97
  59. package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
  60. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +48 -12
  61. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +189 -28
  62. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
  63. package/dist/lib/core/Driver/postgres/PostgresDriver.js +106 -73
  64. package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
  65. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +53 -12
  66. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +318 -48
  67. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
  68. package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +21 -0
  69. package/dist/lib/core/Driver/sqlite/SqliteDriver.js +213 -0
  70. package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -0
  71. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +146 -0
  72. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +705 -0
  73. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -0
  74. package/dist/lib/core/Join.d.ts +63 -5
  75. package/dist/lib/core/Join.js +92 -15
  76. package/dist/lib/core/Join.js.map +1 -1
  77. package/dist/lib/core/JoinModel.d.ts +65 -9
  78. package/dist/lib/core/JoinModel.js +81 -52
  79. package/dist/lib/core/JoinModel.js.map +1 -1
  80. package/dist/lib/core/Meta.d.ts +22 -16
  81. package/dist/lib/core/Meta.js +28 -21
  82. package/dist/lib/core/Meta.js.map +1 -1
  83. package/dist/lib/core/Model.d.ts +644 -134
  84. package/dist/lib/core/Model.js +1361 -631
  85. package/dist/lib/core/Model.js.map +1 -1
  86. package/dist/lib/{tools/index.d.ts → core/Package.d.ts} +11 -3
  87. package/dist/lib/{tools/index.js → core/Package.js} +20 -7
  88. package/dist/lib/core/Package.js.map +1 -0
  89. package/dist/lib/core/Pool.js +42 -30
  90. package/dist/lib/core/Pool.js.map +1 -1
  91. package/dist/lib/core/Queue.d.ts +249 -0
  92. package/dist/lib/core/Queue.js +726 -0
  93. package/dist/lib/core/Queue.js.map +1 -0
  94. package/dist/lib/core/RelationManager.d.ts +4 -4
  95. package/dist/lib/core/RelationManager.js +168 -41
  96. package/dist/lib/core/RelationManager.js.map +1 -1
  97. package/dist/lib/core/Repository.d.ts +73 -207
  98. package/dist/lib/core/Repository.js +122 -234
  99. package/dist/lib/core/Repository.js.map +1 -1
  100. package/dist/lib/core/Schema.d.ts +211 -1
  101. package/dist/lib/core/Schema.js +370 -65
  102. package/dist/lib/core/Schema.js.map +1 -1
  103. package/dist/lib/core/StateManager.d.ts +31 -12
  104. package/dist/lib/core/StateManager.js +11 -5
  105. package/dist/lib/core/StateManager.js.map +1 -1
  106. package/dist/lib/core/UtilityTypes.d.ts +103 -81
  107. package/dist/lib/core/UtilityTypes.js.map +1 -1
  108. package/dist/lib/core/index.d.ts +4 -0
  109. package/dist/lib/core/index.js +6 -2
  110. package/dist/lib/core/index.js.map +1 -1
  111. package/dist/lib/types/decorator/index.d.ts +1 -1
  112. package/dist/lib/types/index.d.ts +120 -30
  113. package/dist/lib/types/repository/index.d.ts +68 -37
  114. package/dist/lib/utils/index.d.ts +13 -2
  115. package/dist/lib/utils/index.js +81 -2
  116. package/dist/lib/utils/index.js.map +1 -1
  117. package/package.json +14 -6
  118. package/dist/lib/tools/index.js.map +0 -1
@@ -4,9 +4,10 @@ import { AbstractModel } from "./Abstracts/AbstractModel";
4
4
  import { Blueprint } from "./Blueprint";
5
5
  import { JoinModel } from "./JoinModel";
6
6
  import { CONSTANTS } from "../constants";
7
- import type { T } from "./UtilityTypes";
8
- import type { TCache, TRelationOptions, TValidateSchema, TGlobalSetting, TPattern, TRelationQueryOptions, TModelOrObject, TRelationKeys, TLifecycle } from "../types";
9
7
  import { Join } from "./Join";
8
+ import { QueryBuilder } from "./Driver";
9
+ import type { T } from "./UtilityTypes";
10
+ import type { TRelationOptions, TValidateSchema, TGlobalSetting, TPattern, TRelationQueryOptions, TModelOrObject, TRelationKeys, TLifecycle, TCacheModel, TRawStringQuery, TAction, TSaveModelResult } from "../types";
10
11
  /**
11
12
  *
12
13
  * The 'Model' class is a representation of a database table
@@ -33,7 +34,7 @@ import { Join } from "./Join";
33
34
  * const users = await new User().findMany()
34
35
  * console.log(users)
35
36
  */
36
- declare class Model<TS extends Record<string, any> = any, TR = unknown> extends AbstractModel {
37
+ declare class Model<TS extends Record<string, any> = any, TR = unknown, TA extends TAction = any> extends AbstractModel {
37
38
  constructor();
38
39
  /**
39
40
  * The 'global' method is used setting global variables in models.
@@ -79,11 +80,389 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
79
80
  * @returns {Model} instance of the Model
80
81
  */
81
82
  static get instance(): Model;
83
+ /**
84
+ * The 'query' method is used to return instance
85
+ * @static
86
+ * @example
87
+ * const user = await User.query().where('id',1).findOne();
88
+ * console.log(user);
89
+ */
90
+ static query<T>(this: new () => T): T;
91
+ /**
92
+ *
93
+ * The 'find' method is used to retrieve a single record from a database table by its primary key.
94
+ *
95
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
96
+ * @type {?object} options
97
+ * @property {?object} options.select
98
+ * @property {?object} options.except
99
+ * @property {?object[]} options.orderBy
100
+ * @property {?string[]} options.groupBy
101
+ * @property {?string} options.having
102
+ * @property {?number} options.limit
103
+ * @property {?number} options.offset
104
+ * @property {?object} options.where
105
+ * @property {?string[]} options.whereRaw
106
+ * @property {?object} options.whereQuery
107
+ * @property {?{condition,callback}} options.when
108
+ * @property {?{localKey , referenceKey}[]} options.join
109
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
110
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
111
+ * @property {?string[]} options.relations
112
+ * @property {string[]} options.relationExists
113
+ * @property {?{condition,callback}} options.relationQuery
114
+ * @property {?boolean} options.debug
115
+ * @returns {promise<object>[]}
116
+ *
117
+ * @example
118
+ * import { User } from '../Models/User'
119
+ *
120
+ * const users = await User.find({
121
+ * select : { id: true, name: true },
122
+ * where : {
123
+ * id: 1
124
+ * }
125
+ * })
126
+ *
127
+ */
128
+ static find<Self extends Model, M extends Model = Self, S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(this: new () => Self, primaryKey: number | string, options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G> | null>;
129
+ /**
130
+ *
131
+ * The 'findOne' method is used to retrieve the get record that matches the query conditions.
132
+ *
133
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
134
+ * @type {?object} options
135
+ * @property {?object} options.select
136
+ * @property {?object} options.except
137
+ * @property {?object[]} options.orderBy
138
+ * @property {?string[]} options.groupBy
139
+ * @property {?string} options.having
140
+ * @property {?number} options.limit
141
+ * @property {?number} options.offset
142
+ * @property {?object} options.where
143
+ * @property {?string[]} options.whereRaw
144
+ * @property {?object} options.whereQuery
145
+ * @property {?{condition,callback}} options.when
146
+ * @property {?{localKey , referenceKey}[]} options.join
147
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
148
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
149
+ * @property {?string[]} options.relations
150
+ * @property {string[]} options.relationExists
151
+ * @property {?{condition,callback}} options.relationQuery
152
+ * @property {?boolean} options.debug
153
+ * @returns {promise<object>[]}
154
+ *
155
+ * @example
156
+ * import { User } from '../Models/User'
157
+ *
158
+ * const users = await User.findOne({
159
+ * select : { id: true, name: true },
160
+ * where : {
161
+ * id: 1
162
+ * }
163
+ * })
164
+ *
165
+ */
166
+ static findOne<Self extends Model, M extends Model = Self, S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(this: new () => Self, options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G> | null>;
167
+ /**
168
+ *
169
+ * The 'findMany' method is used to retrieve the get record that matches the query conditions.
170
+ *
171
+ * It allows you to retrieve a single record from a database table that meets the specified criteria.
172
+ * @type {?object} options
173
+ * @property {?object} options.select
174
+ * @property {?object} options.except
175
+ * @property {?object[]} options.orderBy
176
+ * @property {?string[]} options.groupBy
177
+ * @property {?string} options.having
178
+ * @property {?number} options.limit
179
+ * @property {?number} options.offset
180
+ * @property {?object} options.where
181
+ * @property {?string[]} options.whereRaw
182
+ * @property {?object} options.whereQuery
183
+ * @property {?{condition,callback}} options.when
184
+ * @property {?{localKey , referenceKey}[]} options.join
185
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
186
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
187
+ * @property {?string[]} options.relations
188
+ * @property {string[]} options.relationExists
189
+ * @property {?{condition,callback}} options.relationQuery
190
+ * @property {?boolean} options.debug
191
+ * @returns {promise<object>[]}
192
+ *
193
+ * @example
194
+ * import { User } from '../Models/User'
195
+ *
196
+ * const users = await User.findMany({
197
+ * select : { id: true, name: true },
198
+ * where : {
199
+ * id: 1
200
+ * }
201
+ * })
202
+ *
203
+ */
204
+ static findMany<Self extends Model, M extends Model = Self, S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(this: new () => Self, options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G>[]>;
205
+ /**
206
+ *
207
+ * The 'paginate' method is used to perform pagination on a set of database query results obtained through the Query Builder.
208
+ *
209
+ * It allows you to split a large set of query results into smaller, more manageable pages,
210
+ * making it easier to display data in a web application and improve user experience.
211
+ * @type {?object} options
212
+ * @property {?object} options.select
213
+ * @property {?object} options.except
214
+ * @property {?object[]} options.orderBy
215
+ * @property {?string[]} options.groupBy
216
+ * @property {?string} options.having
217
+ * @property {?number} options.limit
218
+ * @property {?number} options.offset
219
+ * @property {?object} options.where
220
+ * @property {?string[]} options.whereRaw
221
+ * @property {?object} options.whereQuery
222
+ * @property {?{condition,callback}} options.when
223
+ * @property {?{localKey , referenceKey}[]} options.join
224
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
225
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
226
+ * @property {?string[]} options.relations
227
+ * @property {string[]} options.relationExists
228
+ * @property {?{condition,callback}} options.relationQuery
229
+ * @property {?boolean} options.debug
230
+ * @property {?number} options.page
231
+ * @returns {promise<{ meta , data[]}>}
232
+ *
233
+ * @example
234
+ * import { User } from '../Models/User'
235
+ *
236
+ * const users = await User.paginate({
237
+ * limit:15,
238
+ * page: 1,
239
+ * select : { id: true, name: true },
240
+ * where : {
241
+ * id: 1
242
+ * }
243
+ * })
244
+ */
245
+ static paginate<Self extends Model, M extends Model = Self, S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(this: new () => Self, options?: Omit<Partial<T.RepositoryOptions<M, S, SR, E, SRS, G>> & {
246
+ page?: number;
247
+ }, 'offset'>): Promise<T.PaginateResultFiltered<M, S, SR, E, SRS, G>>;
248
+ /**
249
+ * The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
250
+ *
251
+ * It returns a boolean value indicating whether there are any matching records.
252
+ * @type {?object} options
253
+ * @property {?object} options.select
254
+ * @property {?object} options.except
255
+ * @property {?object[]} options.orderBy
256
+ * @property {?string[]} options.groupBy
257
+ * @property {?string} options.having
258
+ * @property {?number} options.limit
259
+ * @property {?number} options.offset
260
+ * @property {?object} options.where
261
+ * @property {?string[]} options.whereRaw
262
+ * @property {?object} options.whereQuery
263
+ * @property {?{condition,callback}} options.when
264
+ * @property {?{localKey , referenceKey}[]} options.join
265
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
266
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
267
+ * @property {?boolean} options.debug
268
+ * @property {?number} options.page
269
+ *
270
+ * @example
271
+ * import { User } from '../Models/User'
272
+ *
273
+ * const users = await User.exists({
274
+ * where : {
275
+ * id: 1
276
+ * }
277
+ * })
278
+ *
279
+ */
280
+ static exists<Self extends Model, M extends Model = Self>(this: new () => Self, options: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<boolean>;
281
+ /**
282
+ * The 'toQuery' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
283
+ *
284
+ * This method is particularly useful for debugging and understanding the SQL queries generated by your application.
285
+ * @type {?object} options
286
+ * @property {?object} options.select
287
+ * @property {?object} options.except
288
+ * @property {?object[]} options.orderBy
289
+ * @property {?string[]} options.groupBy
290
+ * @property {?string} options.having
291
+ * @property {?number} options.limit
292
+ * @property {?number} options.offset
293
+ * @property {?object} options.where
294
+ * @property {?string[]} options.whereRaw
295
+ * @property {?object} options.whereQuery
296
+ * @property {?{condition,callback}} options.when
297
+ * @property {?{localKey , referenceKey}[]} options.join
298
+ * @property {?{localKey , referenceKey}[]} options.rightJoin
299
+ * @property {?{localKey , referenceKey}[]} options.leftJoin
300
+ * @property {?boolean} options.debug
301
+ * @property {?number} options.page
302
+ *
303
+ * @example
304
+ * import { User } from '../Models/User'
305
+ *
306
+ * const users = await User.exists({
307
+ * where : {
308
+ * id: 1
309
+ * }
310
+ * })
311
+ *
312
+ */
313
+ static toQuery<Self extends Model, M extends Model = Self>(this: new () => Self, options: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): string;
314
+ /**
315
+ * The 'create' method is used to insert a new record into a database table associated.
316
+ *
317
+ * It simplifies the process of creating and inserting records.
318
+ * @type {object} options
319
+ * @property {object} options.data
320
+ * @property {?boolean} options.debug
321
+ * @property {?transaction} options.transaction
322
+ * @return {promise<T.Result<M>>}
323
+ */
324
+ static create<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryCreate<M, NR>): Promise<NR extends true ? undefined : T.Result<M>>;
325
+ /**
326
+ * The 'createMany' method is used to insert a new records into a database table associated.
327
+ *
328
+ * It simplifies the process of creating and inserting records with an array.
329
+ * @type {object} options
330
+ * @property {object[]} options.data
331
+ * @property {?boolean} options.debug
332
+ * @property {?transaction} options.transaction
333
+ * @return {promise<TS[]>}
334
+ */
335
+ static createMany<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryCreateMultiple<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
336
+ /**
337
+ *
338
+ * The 'createOrUpdate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
339
+ *
340
+ * This method is particularly useful when you want to update a record based on certain conditions and,
341
+ * if the record matching those conditions doesn't exist, create a new one with the provided data.
342
+ * @type {object} options
343
+ * @property {object} options.data
344
+ * @property {object} options.where
345
+ * @property {?boolean} options.debug
346
+ * @return {promise<NR extends true ? undefined : T.Result<M>[]>}
347
+ */
348
+ static createOrUpdate<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
349
+ /**
350
+ * The 'createNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
351
+ *
352
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
353
+ * but without raising an error or exception if duplicates are encountered.
354
+ *
355
+ * @type {object} options
356
+ * @property {object} options.data
357
+ * @property {object} options.where
358
+ * @property {?boolean} options.debug
359
+ * @property {?transaction} options.transaction
360
+ * @return {promise<T | null>}
361
+ */
362
+ static createNotExists<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
363
+ /**
364
+ *
365
+ * The 'createOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
366
+ *
367
+ * This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
368
+ * but if exists should be returns a result.
369
+ * @type {object} options
370
+ * @property {object} options.data
371
+ * @property {object} options.where
372
+ * @property {?boolean} options.debug
373
+ * @return {promise<T.Result<M>>}
374
+ */
375
+ static createOrSelect<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
376
+ /**
377
+ * The 'update' method is used to update existing records in a database table that are associated.
378
+ *
379
+ * It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
380
+ *
381
+ * It allows you to remove one record that match certain criteria.
382
+ * @type {object} options
383
+ * @property {object} options.data
384
+ * @property {object} options.where
385
+ * @property {?boolean} options.debug
386
+ * @property {?transaction} options.transaction
387
+ * @return {promise< NR extends true ? undefined : T.Result<M> | null>}
388
+ */
389
+ static update<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryUpdate<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
390
+ /**
391
+ * The 'updateMany' method is used to update existing records in a database table that are associated.
392
+ *
393
+ * It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
394
+ *
395
+ * It allows you to remove more records that match certain criteria.
396
+ * @type {object} options
397
+ * @property {object} options.data
398
+ * @property {object} options.where
399
+ * @property {?boolean} options.debug
400
+ * @property {?transaction} options.transaction
401
+ * @return {promise<T.Result<M>[]>}
402
+ */
403
+ static updateMany<Self extends Model, M extends Model = Self, NR extends boolean | undefined = false>(this: new () => Self, options: T.RepositoryUpdate<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
82
404
  /**
83
405
  * The 'cache' method is used get the functions from the Cache
84
- * @returns {TCache} cache
406
+ * @returns {TCacheModel} cache
407
+ */
408
+ static get cache(): TCacheModel;
409
+ /**
410
+ * The 'lockTable' method is used table lock and execute the callback within the lock scope.
411
+ *
412
+ * The lock is automatically released after the callback completes,
413
+ * regardless of whether it resolves or throws an error.
414
+ *
415
+ * READ:
416
+ * - Other sessions can read.
417
+ * - Other sessions must wait before writing.
418
+ * - SELECT: allowed
419
+ * - INSERT: waits
420
+ * - UPDATE: waits
421
+ * - DELETE: waits
422
+ *
423
+ * WRITE:
424
+ * - Other sessions must wait before reading or writing.
425
+ * - SELECT: waits
426
+ * - INSERT: waits
427
+ * - UPDATE: waits
428
+ * - DELETE: waits
429
+ *
430
+ * The lock is released automatically after the callback completes.
431
+ *
432
+ * @Note
433
+ * This approach is not recommended in clustered or load-balance environments,
434
+ * because locks are session-bound and requests may be routed to different database connections.
435
+ *
436
+ * @template T
437
+ * @param {'READ'|'WRITE'} mode Lock mode.
438
+ * @param {(query: this) => Promise<T>} handler Function executed while the table is locked.
439
+ * @returns {Promise<T>} The value returned by the callback.
440
+ *
441
+ * @example
442
+ * await User.lockTable('READ', async (query) => {
443
+ * query.get();
444
+ * return
445
+ * });
446
+ *
447
+ * @example
448
+ * await User.lockTable('WRITE', async (query) => {
449
+ * // Avoid creating a new query instance inside the lock.
450
+ * // The new instance uses a different connection/session and
451
+ * // may wait for the current table lock to be released.
452
+ *
453
+ * new User().insert({
454
+ * first_name: 'John',
455
+ * last_name: 'Doe'
456
+ * }).save();
457
+ *
458
+ * // Use the locked query instance instead.
459
+ * await query.insert({
460
+ * first_name: 'John',
461
+ * last_name: 'Doe'
462
+ * }).save();
463
+ * });
85
464
  */
86
- static get cache(): TCache;
465
+ static lockTable<T, Self extends Model, M extends Model = Self>(this: new () => Self, mode: 'WRITE' | 'READ', handler: (model: M) => Promise<T>): Promise<T>;
87
466
  /**
88
467
  * The 'boot' method is a special method that you can define within a model.
89
468
  * @example
@@ -223,29 +602,29 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
223
602
  */
224
603
  protected useSchema(schema: Record<string, Blueprint>): this;
225
604
  /**
226
- * The "useTransform " method is used to define value transformers for model columns..
227
- *
228
- * Each transformer defines how a value is converted:
229
- * - `to` : before persisting to the database
230
- * - `from` : after retrieving from the database
231
- *
232
- * Transformers can be synchronous or asynchronous.
233
- *
234
- * @param {object} transforms
235
- * @example
236
- * import { Blueprint } from 'tspace-mysql';
237
- * class User extends Model {
238
- * boot() {
239
- * this.useTransform({
240
- * name : {
241
- * to : async (v) => `${v}-> transform@before`,
242
- * from : async (v) => `${v}-> transform@after`,
243
- * },
244
- * })
245
- * }
246
- * }
247
- * @returns {this} this
248
- */
605
+ * The "useTransform " method is used to define value transformers for model columns..
606
+ *
607
+ * Each transformer defines how a value is converted:
608
+ * - `to` : before persisting to the database
609
+ * - `from` : after retrieving from the database
610
+ *
611
+ * Transformers can be synchronous or asynchronous.
612
+ *
613
+ * @param {object} transforms
614
+ * @example
615
+ * import { Blueprint } from 'tspace-mysql';
616
+ * class User extends Model {
617
+ * boot() {
618
+ * this.useTransform({
619
+ * name : {
620
+ * to : async (v) => `${v}-> transform@before`,
621
+ * from : async (v) => `${v}-> transform@after`,
622
+ * },
623
+ * })
624
+ * }
625
+ * }
626
+ * @returns {this} this
627
+ */
249
628
  protected useTransform(transforms: Record<string, {
250
629
  to: (value: unknown) => any | Promise<any>;
251
630
  from: (value: unknown) => any | Promise<any>;
@@ -524,7 +903,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
524
903
  * this
525
904
  * .useLifecycle("beforeInsert", fnA)
526
905
  * .useLifecycle("afterInsert", fnB);
527
- */
906
+ */
528
907
  protected useLifecycle(type: TLifecycle, funcs: Function | Function[]): this;
529
908
  /**
530
909
  * The "whenCreatingTable" method is used exection function when creating the table.
@@ -597,7 +976,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
597
976
  * @returns {this} this
598
977
  */
599
978
  audit(userId: number, metadata?: Record<string, any>): this;
600
- meta(meta: "MAIN" | "SUBORDINATE"): this;
979
+ metaTag(meta: "MAIN" | "SUBORDINATE"): this;
601
980
  /**
602
981
  * The 'typeOfSchema' method is used get type of schema.
603
982
  * @returns {TS} type of schema
@@ -613,11 +992,13 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
613
992
  * @param {Object} object
614
993
  * @property {string} key key of cache
615
994
  * @property {number} expires ms
995
+ * @property {boolean} namespace whether to use namespace for cache key, default is false, namespace is `${database}:${table}:${key}`
616
996
  * @returns {this} this
617
997
  */
618
- cache({ key, expires }: {
998
+ cache({ key, expires, namespace }: {
619
999
  key: string;
620
1000
  expires: number;
1001
+ namespace?: boolean;
621
1002
  }): this;
622
1003
  /**
623
1004
  *
@@ -626,6 +1007,12 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
626
1007
  * @returns {this} this
627
1008
  */
628
1009
  select<K extends T.ColumnKeys<this> | "*">(...columns: K[]): this;
1010
+ /**
1011
+ *
1012
+ * @override
1013
+ * @param {string[]} ...columns
1014
+ * @returns {this} this
1015
+ */
629
1016
  addSelect<K extends T.ColumnKeys<this>>(...columns: K[]): this;
630
1017
  /**
631
1018
  *
@@ -674,7 +1061,9 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
674
1061
  * @param {string} column
675
1062
  * @returns {string} return table.column
676
1063
  */
677
- bindColumn(column: string, pattern?: boolean): string;
1064
+ bindColumn(column: string, opts?: {
1065
+ pattern: boolean;
1066
+ }): string;
678
1067
  /**
679
1068
  *
680
1069
  * @override
@@ -724,7 +1113,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
724
1113
  * @param {Object} options keep data
725
1114
  * @returns {Model} Model
726
1115
  */
727
- copyModel(instance: Model, options?: {
1116
+ copyModel(instance: Model<any, any, any>, options?: {
728
1117
  update?: boolean;
729
1118
  insert?: boolean;
730
1119
  delete?: boolean;
@@ -764,7 +1153,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
764
1153
  * @override
765
1154
  * @returns {string} return sql query
766
1155
  */
767
- CTEs<M extends Model>(as: string, callback: (query: M) => M, bindModel?: new () => M): this;
1156
+ CTEs<M extends Model<any, any, any>>(as: string, callback: (query: M) => M, bindModel?: new () => M): this;
768
1157
  /**
769
1158
  * The 'disableSoftDelete' method is used to disable the soft delete.
770
1159
  *
@@ -1106,6 +1495,55 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1106
1495
  withQuery<K extends T.RelationKeys<this>, R extends T.Relations<this>>(nameRelation: K, callback: (query: `$${K & string}` extends keyof R ? R[`$${K & string}`] extends (infer X)[] ? X : R[`$${K & string}`] extends Model ? R[`$${K & string}`] : Model : K extends keyof R ? R[K] extends (infer X)[] ? X : R[K] extends Model ? R[K] : Model : Model) => any, options?: {
1107
1496
  pivot: boolean;
1108
1497
  }): this;
1498
+ /**
1499
+ *
1500
+ * The 'withQueryExists' method is particularly useful when you want to filter or add conditions records based on related data.
1501
+ *
1502
+ * Use relation '${name}' registry models then return callback queries
1503
+ * @param {string} nameRelation name relation in registry in your model
1504
+ * @param {function} callback query callback
1505
+ * @param {object} options pivot the query
1506
+ * @example
1507
+ * import { Model } from 'tspace-mysql'
1508
+ * class User extends Model {
1509
+ * constructor(){
1510
+ * super()
1511
+ * this.hasMany({ name : 'posts' , model : Post })
1512
+ * }
1513
+ * }
1514
+ *
1515
+ * class Post extends Model {
1516
+ * constructor(){
1517
+ * super()
1518
+ * this.hasMany({ name : 'comments' , model : Comment })
1519
+ * this.belongsTo({ name : 'user' , model : User })
1520
+ * }
1521
+ * }
1522
+ *
1523
+ * class Comment extends Model {
1524
+ * constructor(){
1525
+ * super()
1526
+ * this.hasMany({ name : 'users' , model : User })
1527
+ * this.belongsTo({ name : 'post' , model : Post })
1528
+ * }
1529
+ * }
1530
+ *
1531
+ * await new User().with('posts')
1532
+ * .withQueryExists('posts', (query : Post) => {
1533
+ * return query.with('comments','user')
1534
+ * .withQuery('comments', (query : Comment) => {
1535
+ * return query.with('user','post')
1536
+ * })
1537
+ * .withQueryExists('user', (query : User) => {
1538
+ * return query.with('posts').withQueryExists('posts',(query : Post)=> {
1539
+ * return query.with('comments','user')
1540
+ * // relation n, n, ...n
1541
+ * })
1542
+ * })
1543
+ * })
1544
+ * .findMany()
1545
+ * @returns {this} this
1546
+ */
1109
1547
  withQueryExists<K extends T.RelationKeys<this>, R extends T.Relations<this>>(nameRelation: K, callback: (query: `$${K & string}` extends keyof R ? R[`$${K & string}`] extends (infer X)[] ? X : R[`$${K & string}`] extends Model ? R[`$${K & string}`] : Model : K extends keyof R ? R[K] extends (infer X)[] ? X : R[K] extends Model ? R[K] : Model : Model) => any, options?: {
1110
1548
  pivot: boolean;
1111
1549
  }): this;
@@ -1221,6 +1659,23 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1221
1659
  * @returns {Model} model instance
1222
1660
  */
1223
1661
  findWithQuery<K extends T.RelationKeys<this>>(name: K): Model | null;
1662
+ /**
1663
+ * The 'ofMany' method is used to transform a many relationship,
1664
+ * into a single related model by selecting one record using an aggregate
1665
+ * function such as MAX or MIN on the specified column.
1666
+ *
1667
+ * @note
1668
+ * This method is not supported on `belongsToMany` relationships.
1669
+ *
1670
+ * @param {string} column The column used for aggregation (e.g. `"id"`, `"created_at"`).
1671
+ * @param {"MAX" | "MIN"} aggregate The aggregate function used to select the record.
1672
+ * @param {((query: Model) => void) | undefined} callback A callback used to apply additional constraints to the aggregate query.
1673
+ *
1674
+ * Only filtering methods are supported. Methods that modify the result set,
1675
+ * such as `select` `orderBy`, `groupBy`, `limit`, `offset`, `union`, and `having`, are not supported.
1676
+ * @returns this
1677
+ */
1678
+ ofMany<K extends T.ColumnKeys<this>, S extends Model | unknown, M = S extends this ? this : S extends Model ? S : this>(column: K, aggregate: "MAX" | "MIN", callback?: (query: M) => M): this;
1224
1679
  /**
1225
1680
  * The 'hasOne' relationship defines a one-to-one relationship between two database tables.
1226
1681
  *
@@ -1313,6 +1768,42 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1313
1768
  * @returns {this} this
1314
1769
  */
1315
1770
  protected belongsToManySingle<K extends TR extends object ? TRelationKeys<TR> : string>({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot, }: TRelationOptions<K>): this;
1771
+ /**
1772
+ * The 'createRelation' method is useful for Creates a relation accessor for a model.
1773
+ *
1774
+ * This method defines how a model relates to another model
1775
+ * (hasOne, hasMany, belongsTo, belongsToMany) and returns
1776
+ * a function that can be used to modify the relation query.
1777
+ *
1778
+ * Example:
1779
+ * ```ts
1780
+ * public posts = this.createRelation(Post, {
1781
+ * type: 'hasMany',
1782
+ * foreignKey: 'user_id',
1783
+ * });
1784
+ *
1785
+ * user.posts(q => q.where('id', 1));
1786
+ * ```
1787
+ *
1788
+ * @template M - Target model type
1789
+ * @template K - Optional relation name type
1790
+ *
1791
+ * @param model - The related model constructor
1792
+ * @param options - Relation configuration options
1793
+ * @param options.type - Type of relation (hasOne, hasMany, belongsTo, belongsToMany)
1794
+ * @param options.name - Optional relation name (when applicable)
1795
+ * @param options.as - Alias for relation
1796
+ * @param options.localKey - Local key for the relation
1797
+ * @param options.foreignKey - Foreign key for the relation
1798
+ * @param options.freezeTable - Override pivot/table name
1799
+ * @param options.pivot - Pivot table name (for many-to-many)
1800
+ * @param options.oldVersion - Enable legacy relation behavior
1801
+ *
1802
+ * @returns A function that accepts a query modifier and returns the relation result
1803
+ */
1804
+ protected createRelation<M extends Model, K = void>(model: new () => M, options: (K extends void ? Omit<TRelationQueryOptions, 'name' | 'model'> & Partial<Pick<TRelationQueryOptions, 'name'>> : Omit<TRelationQueryOptions<K>, 'model'>) & {
1805
+ type: 'hasOne' | 'hasMany' | 'belongsTo' | 'belongsToMany';
1806
+ }): (modifier?: T.QueryModifier<M>) => this;
1316
1807
  /**
1317
1808
  * The 'hasOneBuilder' method is useful for creating 'hasOne' relationship to function
1318
1809
  *
@@ -1327,7 +1818,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1327
1818
  * @param {Function?} callback callback of query
1328
1819
  * @returns {this} this
1329
1820
  */
1330
- protected hasOneBuilder({ name, as, model, localKey, foreignKey, freezeTable, }: TRelationQueryOptions, callback?: Function): this;
1821
+ protected hasOneBuilder<K = void>({ name, as, model, localKey, foreignKey, freezeTable, }: K extends void ? Omit<TRelationQueryOptions, 'name'> & Partial<Pick<TRelationQueryOptions, 'name'>> : TRelationQueryOptions<K>, callback?: Function): this;
1331
1822
  /**
1332
1823
  * The 'hasManyBuilder' method is useful for creating 'hasMany' relationship to function
1333
1824
  *
@@ -1342,7 +1833,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1342
1833
  * @param {function?} callback callback of query
1343
1834
  * @returns {this} this
1344
1835
  */
1345
- protected hasManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, }: TRelationQueryOptions, callback?: Function): this;
1836
+ protected hasManyBuilder<K = void>({ name, as, model, localKey, foreignKey, freezeTable, }: K extends void ? Omit<TRelationQueryOptions, 'name'> & Partial<Pick<TRelationQueryOptions, 'name'>> : TRelationQueryOptions<K>, callback?: Function): this;
1346
1837
  /**
1347
1838
  * The 'belongsToBuilder' method is useful for creating 'belongsTo' relationship to function
1348
1839
  * @param {object} relation registry relation in your model
@@ -1356,7 +1847,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1356
1847
  * @param {function?} callback callback of query
1357
1848
  * @returns {this} this
1358
1849
  */
1359
- protected belongsToBuilder({ name, as, model, localKey, foreignKey, freezeTable, }: TRelationQueryOptions, callback?: Function): this;
1850
+ protected belongsToBuilder<K = void>({ name, as, model, localKey, foreignKey, freezeTable, }: K extends void ? Omit<TRelationQueryOptions, 'name'> & Partial<Pick<TRelationQueryOptions, 'name'>> : TRelationQueryOptions<K>, callback?: Function): this;
1360
1851
  /**
1361
1852
  * The 'belongsToManyBuilder' method is useful for creating 'belongsToMany' relationship to function
1362
1853
  *
@@ -1371,7 +1862,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1371
1862
  * @param {function?} callback callback of query
1372
1863
  * @returns {this} this
1373
1864
  */
1374
- protected belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot, }: TRelationQueryOptions, callback?: Function): this;
1865
+ protected belongsToManyBuilder<K = void>({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot, }: K extends void ? Omit<TRelationQueryOptions, 'name'> & Partial<Pick<TRelationQueryOptions, 'name'>> : TRelationQueryOptions<K>, callback?: Function): this;
1375
1866
  /**
1376
1867
  * The 'trashed' method is used to specify that you want to retrieve only the soft-deleted records from a database table.
1377
1868
  *
@@ -1404,22 +1895,26 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1404
1895
  * @returns {string} string
1405
1896
  */
1406
1897
  toTableNameAndColumn(column: string): string;
1898
+ where<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, value: V): this;
1899
+ where<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, operator: "=" | "<" | ">" | "!=" | "<>" | "<=" | ">=" | "LIKE" | "like", value: V): this;
1900
+ where(column: Partial<{
1901
+ [K in keyof T.ColumnOptions<this>]: T.ColumnOptions<this>[K];
1902
+ }>): this;
1903
+ /**
1904
+ * @override
1905
+ * @param {string | K} column if arguments is object
1906
+ * @param {any?} value
1907
+ * @returns {this} this
1908
+ */
1909
+ orWhere<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, value: V): this;
1407
1910
  /**
1408
1911
  * @override
1409
1912
  * @param {string | K} column if arguments is object
1410
- * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
1913
+ * @param {string?} operator "=" | "<" | ">" | "!=" | "<>" | "<=" | ">=" | "LIKE" | "like"
1411
1914
  * @param {any?} value
1412
1915
  * @returns {this} this
1413
1916
  */
1414
- where<K extends T.ColumnKeys<this>>(column: K | Record<string, any>, operator?: any, value?: any): this;
1415
- /**
1416
- * @override
1417
- * @param {string} column
1418
- * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
1419
- * @param {any?} value
1420
- * @returns {this}
1421
- */
1422
- orWhere<K extends T.ColumnKeys<this>>(column: K, operator?: any, value?: any): this;
1917
+ orWhere<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, operator: "=" | "<" | ">" | "!=" | "<>" | "<=" | ">=" | "LIKE" | "like", value: V): this;
1423
1918
  /**
1424
1919
  * @override
1425
1920
  * @param {string} column
@@ -1560,28 +2055,28 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1560
2055
  * @param {array} array
1561
2056
  * @returns {this}
1562
2057
  */
1563
- whereBetween<K extends T.ColumnKeys<this>>(column: K, array: [any, any]): this;
2058
+ whereBetween<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, array: [V, V]): this;
1564
2059
  /**
1565
2060
  * @override
1566
2061
  * @param {string} column
1567
2062
  * @param {array} array
1568
2063
  * @returns {this}
1569
2064
  */
1570
- orWhereBetween<K extends T.ColumnKeys<this>>(column: K, array: [any, any]): this;
2065
+ orWhereBetween<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, array: [V, V]): this;
1571
2066
  /**
1572
2067
  * @override
1573
2068
  * @param {string} column
1574
2069
  * @param {array} array
1575
2070
  * @returns {this}
1576
2071
  */
1577
- whereNotBetween<K extends T.ColumnKeys<this>>(column: K, array: [any, any]): this;
2072
+ whereNotBetween<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, array: [V, V]): this;
1578
2073
  /**
1579
2074
  * @override
1580
2075
  * @param {string} column
1581
2076
  * @param {array} array
1582
2077
  * @returns {this}
1583
2078
  */
1584
- orWhereNotBetween<K extends T.ColumnKeys<this>>(column: K, array: [any, any]): this;
2079
+ orWhereNotBetween<K extends T.ColumnKeys<this>, V extends T.ColumnValue<this, K>>(column: K, array: [V, V]): this;
1585
2080
  /**
1586
2081
  * @override
1587
2082
  * @param {string} column
@@ -1652,7 +2147,13 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1652
2147
  * @param {Function} callback callback query
1653
2148
  * @returns {this}
1654
2149
  */
1655
- whereQuery<T extends Model | unknown, M = T extends this ? this : T extends Model ? T : this>(callback: (query: M) => M): this;
2150
+ whereQuery(callback: T.QueryModifier<this>): this;
2151
+ /**
2152
+ * @override
2153
+ * @param {Function} callback callback query
2154
+ * @returns {this}
2155
+ */
2156
+ orWhereQuery<T extends Model | unknown, M = T extends this ? this : T extends Model ? T : this>(callback: (query: M) => M): this;
1656
2157
  /**
1657
2158
  * @override
1658
2159
  * @param {string[]} columns
@@ -1680,7 +2181,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1680
2181
  * @param {string | number | undefined | null | Boolean} condition when condition true will return query callback
1681
2182
  * @returns {this} this
1682
2183
  */
1683
- when<T extends Model | unknown, M = T extends this ? this : T extends Model ? T : this>(condition: string | number | undefined | null | boolean, callback: (query: M) => M): this;
2184
+ when(condition: string | number | undefined | null | boolean, callback: T.QueryModifier<this>): this;
1684
2185
  /**
1685
2186
  * This 'union' method is used to union statement sql
1686
2187
  *
@@ -1802,6 +2303,40 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1802
2303
  * @returns {this}
1803
2304
  */
1804
2305
  crossJoinModel(m1: TModelOrObject | ((join: JoinModel) => JoinModel), m2?: TModelOrObject): this;
2306
+ /**
2307
+ *
2308
+ * @override
2309
+ * @param {string=} c [column=id]
2310
+ * @returns {promise<number>}
2311
+ */
2312
+ count<K extends T.ColumnKeys<this> | "id" | "_id">(c?: K): Promise<number>;
2313
+ /**
2314
+ *
2315
+ * @override
2316
+ * @param {string=} c [column=id]
2317
+ * @returns {promise<number>}
2318
+ */
2319
+ avg<K extends T.ColumnKeys<this> | "id" | "_id">(c?: K): Promise<number>;
2320
+ /**
2321
+ *
2322
+ * @override
2323
+ * @param {string=} c [column=id]
2324
+ * @returns {promise<number>}
2325
+ */
2326
+ sum<K extends T.ColumnKeys<this> | "id" | "_id">(c?: K): Promise<number>;
2327
+ /**
2328
+ *
2329
+ * @override
2330
+ * @param {string=} c [column=id]
2331
+ * @returns {promise<number>}
2332
+ */
2333
+ max<K extends T.ColumnKeys<this> | "id" | "_id">(c?: K): Promise<number>;
2334
+ /**
2335
+ * @override
2336
+ * @param {string} c
2337
+ * @returns {promise<number>}
2338
+ */
2339
+ min<K extends T.ColumnKeys<this> | "id" | "_id">(c: K): Promise<number>;
1805
2340
  /**
1806
2341
  * @override
1807
2342
  * @returns {promise<boolean>} promise boolean
@@ -1871,6 +2406,12 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1871
2406
  * @returns {promise<Record<string,any> | null>} Record | null
1872
2407
  */
1873
2408
  findOne<K>(cb?: Function): Promise<T.Result<this, K> | null>;
2409
+ /**
2410
+ * @override
2411
+ * @param {number} id callback function return query sql
2412
+ * @returns {promise<Record<string,any> | null>} Record | null
2413
+ */
2414
+ find<K>(primaryKey: number | string): Promise<T.Result<this, K> | null>;
1874
2415
  /**
1875
2416
  * @override
1876
2417
  * @returns {promise<object | Error>} Record | throw error
@@ -1898,28 +2439,30 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1898
2439
  findMany<K>(cb?: Function): Promise<T.Result<this, K>[]>;
1899
2440
  /**
1900
2441
  * @override
1901
- * @param {object?} paginationOptions by default page = 1 , limit = 15
1902
- * @property {number} paginationOptions.limit
1903
- * @property {number} paginationOptions.page
2442
+ * @param {object?} opts by default page = 1 , limit = 15
2443
+ * @property {number?} opts.limit
2444
+ * @property {number?} opts.page
2445
+ * @property {boolean?} opts.distinct
1904
2446
  * @returns {promise<Pagination>} Pagination
1905
2447
  */
1906
- pagination<K>(paginationOptions?: {
2448
+ pagination<K>(opts?: {
1907
2449
  limit?: number;
1908
2450
  page?: number;
1909
- alias?: boolean;
2451
+ distinct?: boolean;
1910
2452
  }): Promise<T.PaginateResult<this, K>>;
1911
2453
  /**
1912
2454
  *
1913
2455
  * @override
1914
- * @param {?object} paginationOptions by default page = 1 , limit = 15
1915
- * @property {number} paginationOptions.limit
1916
- * @property {number} paginationOptions.page
2456
+ * @param {?object} opts by default page = 1 , limit = 15
2457
+ * @property {number?} opts.limit
2458
+ * @property {number?} opts.page
2459
+ * @property {boolean?} opts.distinct
1917
2460
  * @returns {promise<Pagination>} Pagination
1918
2461
  */
1919
- paginate<K>(paginationOptions?: {
2462
+ paginate<K>(opts?: {
1920
2463
  limit?: number;
1921
2464
  page?: number;
1922
- alias?: boolean;
2465
+ distinct?: boolean;
1923
2466
  }): Promise<T.PaginateResult<this, K>>;
1924
2467
  /**
1925
2468
  * @override
@@ -1950,164 +2493,130 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
1950
2493
  * @param {object} data for insert
1951
2494
  * @returns {this} this
1952
2495
  */
1953
- insert<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
1954
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
1955
- }): this;
2496
+ insert<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "create">;
1956
2497
  /**
1957
2498
  * @override
1958
2499
  * @param {object} data for insert
1959
2500
  * @returns {this} this
1960
2501
  */
1961
- create<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
1962
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
1963
- }): this;
2502
+ create<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "create">;
1964
2503
  /**
1965
2504
  * @override
1966
2505
  * @param {object} data
1967
2506
  * @param {array?} updateNotExists options for except update some records in your ${data}
1968
2507
  * @returns {this} this
1969
2508
  */
1970
- update<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
1971
- [P in K & keyof T]: T[P];
1972
- }, updateNotExists?: T.ColumnKeys<this>[]): this;
2509
+ update<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.UpdateInput<K, C>, updateNotExists?: T.ColumnKeys<this>[]): Model<TS, TR, "update">;
1973
2510
  /**
1974
2511
  * @override
1975
2512
  * @param {object} data
1976
2513
  * @param {array?} updateNotExists options for except update some records in your ${data}
1977
2514
  * @returns {this} this
1978
2515
  */
1979
- updateMany<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
1980
- [P in K & keyof T]: T[P];
1981
- }, updateNotExists?: string[]): this;
1982
- /**
1983
- *
1984
- * @override
1985
- * @param {Array<{when: Record<string, string | number | boolean | null | undefined>, columns: Record<string, string | number | boolean | null | undefined>}>>} cases
1986
- * An array of update cases.
1987
- * - `when` is an object specifying the conditions to match records.
1988
- * - `columns` is an object specifying the new values to set for the matched records.
1989
- *
1990
- * @property {Record<string,string | number | boolean | null | undefined>} cases.when
1991
- * @property {Record<string,string | number | boolean | null | undefined>} cases.columns
1992
- * @returns {this} this
1993
- */
2516
+ updateMany<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.UpdateInput<K, C>, updateNotExists?: string[]): Model<TS, TR, "updateMany">;
2517
+ /**
2518
+ *
2519
+ * @override
2520
+ * @param {Array<{when: Record<string, string | number | boolean | null | undefined>, columns: Record<string, string | number | boolean | null | undefined>}>>} cases
2521
+ * An array of update cases.
2522
+ * - `when` is an object specifying the conditions to match records.
2523
+ * - `columns` is an object specifying the new values to set for the matched records.
2524
+ *
2525
+ * @property {Record<string,string | number | boolean | null | undefined>} cases.when
2526
+ * @property {Record<string,string | number | boolean | null | undefined>} cases.columns
2527
+ * @returns {this} this
2528
+ */
1994
2529
  updateCases<T extends T.ColumnOptions<this>, K extends keyof T, U extends Model | unknown, M = U extends this ? this : U extends Model ? U : this>(cases: {
1995
2530
  condition: ((query: M) => M) | Partial<{
1996
- [P in K & keyof T]: T[P];
2531
+ [P in K & keyof T]: Extract<T[P], Record<string, unknown>> extends never ? T[P] : string;
1997
2532
  }>;
1998
2533
  columns: Partial<{
1999
- [P in K & keyof T]: T[P];
2534
+ [P in K & keyof T]: Extract<T[P], Record<string, unknown>> extends never ? T[P] : string;
2000
2535
  }>;
2001
- }[]): this;
2536
+ }[]): Model<TS, TR, "updateMany">;
2002
2537
  /**
2003
2538
  * @override
2004
2539
  * @param {object} data
2005
2540
  * @returns {this} this
2006
2541
  */
2007
- updateNotExists<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2008
- [P in K & keyof T]: T[P];
2009
- }): this;
2542
+ updateNotExists<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.UpdateInput<K, C>): Model<TS, TR, "update">;
2010
2543
  /**
2011
2544
  * @override
2012
2545
  * @param {object} data for update or create
2013
2546
  * @returns {this} this
2014
2547
  */
2015
- updateOrCreate<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2016
- [P in K & keyof T]: T[P];
2017
- }): this;
2548
+ updateOrCreate<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertOrUpdateInput<K, C>): Model<TS, TR, "createOrUpdate">;
2018
2549
  /**
2019
2550
  * @override
2020
2551
  * @param {object} data for update or create
2021
2552
  * @returns {this} this
2022
2553
  */
2023
- updateOrInsert<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2024
- [P in K & keyof T]: T[P];
2025
- }): this;
2554
+ updateOrInsert<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertOrUpdateInput<K, C>): Model<TS, TR, "createOrUpdate">;
2026
2555
  /**
2027
2556
  * @override
2028
2557
  * @param {object} data for update or create
2029
2558
  * @returns {this} this
2030
2559
  */
2031
- insertOrUpdate<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2032
- [P in K & keyof T]: T[P];
2033
- }): this;
2560
+ insertOrUpdate<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertOrUpdateInput<K, C>): Model<TS, TR, "createOrUpdate">;
2034
2561
  /**
2035
2562
  * @override
2036
2563
  * @param {object} data for update or create
2037
2564
  * @returns {this} this
2038
2565
  */
2039
- createOrUpdate<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2040
- [P in K & keyof T]: T[P];
2041
- }): this;
2566
+ createOrUpdate<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertOrUpdateInput<K, C>): Model<TS, TR, "createOrUpdate">;
2042
2567
  /**
2043
2568
  * @override
2044
2569
  * @param {object} data for create
2045
2570
  * @returns {this} this
2046
2571
  */
2047
- createOrSelect<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2048
- [P in K & keyof T]: T[P];
2049
- }): this;
2572
+ createOrSelect<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "createOrSelect">;
2050
2573
  /**
2051
2574
  * @override
2052
2575
  * @param {object} data for update or create
2053
2576
  * @returns {this} this
2054
2577
  */
2055
- insertOrSelect<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2056
- [P in K & keyof T]: T[P];
2057
- }): this;
2578
+ insertOrSelect<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "createOrSelect">;
2058
2579
  /**
2059
2580
  *
2060
2581
  * @override
2061
2582
  * @param {object} data create not exists data
2062
2583
  * @returns {this} this
2063
2584
  */
2064
- createNotExists<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2065
- [P in K & keyof T]: T[P];
2066
- }): this;
2585
+ createNotExists<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "createNotExists">;
2067
2586
  /**
2068
2587
  *
2069
2588
  * @override
2070
2589
  * @param {object} data create not exists data
2071
2590
  * @returns {this} this this
2072
2591
  */
2073
- insertNotExists<K extends T.ColumnKeys<this>, T extends T.ColumnOptions<this>>(data: {
2074
- [P in K & keyof T]: T[P];
2075
- }): this;
2592
+ insertNotExists<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>): Model<TS, TR, "createNotExists">;
2076
2593
  /**
2077
2594
  * @override
2078
2595
  * @param {Record<string,any>[]} data create multiple data
2079
2596
  * @returns {this} this this
2080
2597
  */
2081
- createMultiple<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
2082
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
2083
- }[]): this;
2598
+ createMultiple<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>[]): Model<TS, TR, "createMany">;
2084
2599
  /**
2085
2600
  *
2086
2601
  * @override
2087
2602
  * @param {Record<string,any>[]} data create multiple data
2088
2603
  * @returns {this} this
2089
2604
  */
2090
- createMany<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
2091
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
2092
- }[]): this;
2605
+ createMany<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>[]): Model<TS, TR, "createMany">;
2093
2606
  /**
2094
2607
  *
2095
2608
  * @override
2096
2609
  * @param {Record<string,any>[]} data create multiple data
2097
2610
  * @returns {this} this
2098
2611
  */
2099
- insertMultiple<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
2100
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
2101
- }[]): this;
2612
+ insertMultiple<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>[]): Model<TS, TR, "createMany">;
2102
2613
  /**
2103
2614
  *
2104
2615
  * @override
2105
2616
  * @param {Record<string,any>[]} data create multiple data
2106
2617
  * @returns {this} this
2107
2618
  */
2108
- insertMany<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: {
2109
- [P in Exclude<K & keyof C, "id"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? C[P] : any;
2110
- }[]): this;
2619
+ insertMany<K extends T.ColumnKeys<this>, C extends T.ColumnOptions<this>>(data: T.InsertInput<K, C>[]): Model<TS, TR, "createMany">;
2111
2620
  /**
2112
2621
  * The 'getSchemaModel' method is used get a schema model
2113
2622
  * @returns {Record<string, Blueprint> | null} Record<string, Blueprint> | null
@@ -2131,7 +2640,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
2131
2640
  */
2132
2641
  save({ waitMs }?: {
2133
2642
  waitMs?: number | undefined;
2134
- }): Promise<T.InsertResult<this> | T.InsertManyResult<this> | T.InsertNotExistsResult<this> | T.UpdateResult<this> | T.UpdateManyResult<this> | null | undefined>;
2643
+ }): Promise<TSaveModelResult<this, TA>>;
2135
2644
  /**
2136
2645
  *
2137
2646
  * @override
@@ -2180,6 +2689,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
2180
2689
  private _makeTableName;
2181
2690
  private _handleSoftDelete;
2182
2691
  private _prepareQueryPipeline;
2692
+ private _ensureRelationOwnerKeysSelected;
2183
2693
  private _handleGlobalScope;
2184
2694
  /**
2185
2695
  * @override
@@ -2191,9 +2701,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
2191
2701
  * generate sql statements
2192
2702
  * @override
2193
2703
  */
2194
- protected _queryBuilder({ onFormat }?: {
2195
- onFormat?: boolean | undefined;
2196
- }): any;
2704
+ protected _queryBuilder(): QueryBuilder;
2197
2705
  private _runBefore;
2198
2706
  private _runAfter;
2199
2707
  private _validateSchema;
@@ -2213,6 +2721,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
2213
2721
  private _insertOrSelectModel;
2214
2722
  private _updateModel;
2215
2723
  private _assertError;
2724
+ disabledRetry(): this;
2216
2725
  private _checkSchemaOrNextError;
2217
2726
  private _stoppedRetry;
2218
2727
  private _observer;
@@ -2221,6 +2730,7 @@ declare class Model<TS extends Record<string, any> = any, TR = unknown> extends
2221
2730
  private _isModel;
2222
2731
  private _getBlueprintByColumn;
2223
2732
  private _handleJoinModel;
2733
+ private _formatedInputData;
2224
2734
  private _initialModel;
2225
2735
  }
2226
2736
  export { Model };