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.
- package/README.md +4580 -133
- package/dist/lib/config/index.d.ts +2 -2
- package/dist/lib/config/index.js +24 -16
- package/dist/lib/config/index.js.map +1 -1
- package/dist/lib/constants/index.d.ts +10 -1
- package/dist/lib/constants/index.js +11 -2
- package/dist/lib/constants/index.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +27 -23
- package/dist/lib/core/Abstracts/AbstractBuilder.js +11 -13
- package/dist/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -2
- package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractModel.d.ts +7 -2
- package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
- package/dist/lib/core/Blueprint.d.ts +37 -17
- package/dist/lib/core/Blueprint.js +38 -14
- package/dist/lib/core/Blueprint.js.map +1 -1
- package/dist/lib/core/Builder.d.ts +314 -42
- package/dist/lib/core/Builder.js +686 -391
- package/dist/lib/core/Builder.js.map +1 -1
- package/dist/lib/core/Cache/DBCache.js +19 -20
- package/dist/lib/core/Cache/DBCache.js.map +1 -1
- package/dist/lib/core/Cache/MemoryCache.js +3 -3
- package/dist/lib/core/Cache/MemoryCache.js.map +1 -1
- package/dist/lib/core/Cache/RedisCache.js +27 -17
- package/dist/lib/core/Cache/RedisCache.js.map +1 -1
- package/dist/lib/core/Cache/index.js +37 -17
- package/dist/lib/core/Cache/index.js.map +1 -1
- package/dist/lib/core/Contracts/AlterTable.d.ts +152 -0
- package/dist/lib/core/Contracts/AlterTable.js +243 -0
- package/dist/lib/core/Contracts/AlterTable.js.map +1 -0
- package/dist/lib/core/Contracts/Audit.d.ts +1 -1
- package/dist/lib/core/Contracts/Audit.js +2 -5
- package/dist/lib/core/Contracts/Audit.js.map +1 -1
- package/dist/lib/core/Contracts/Logger.d.ts +1 -1
- package/dist/lib/core/Contracts/Logger.js.map +1 -1
- package/dist/lib/core/DB.d.ts +103 -11
- package/dist/lib/core/DB.js +158 -32
- package/dist/lib/core/DB.js.map +1 -1
- package/dist/lib/core/Decorator.d.ts +144 -16
- package/dist/lib/core/Decorator.js +156 -14
- package/dist/lib/core/Decorator.js.map +1 -1
- package/dist/lib/core/Driver/index.d.ts +50 -14
- package/dist/lib/core/Driver/index.js +13 -9
- package/dist/lib/core/Driver/index.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js +84 -29
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +48 -12
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +189 -28
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +24 -0
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js +261 -0
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +141 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +571 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -0
- package/dist/lib/core/Driver/mysql/MysqlDriver.js +115 -97
- package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +48 -12
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +189 -28
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresDriver.js +106 -73
- package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +53 -12
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +318 -48
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +21 -0
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js +213 -0
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +146 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +705 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -0
- package/dist/lib/core/Join.d.ts +63 -5
- package/dist/lib/core/Join.js +92 -15
- package/dist/lib/core/Join.js.map +1 -1
- package/dist/lib/core/JoinModel.d.ts +65 -9
- package/dist/lib/core/JoinModel.js +81 -52
- package/dist/lib/core/JoinModel.js.map +1 -1
- package/dist/lib/core/Meta.d.ts +22 -16
- package/dist/lib/core/Meta.js +28 -21
- package/dist/lib/core/Meta.js.map +1 -1
- package/dist/lib/core/Model.d.ts +644 -134
- package/dist/lib/core/Model.js +1361 -631
- package/dist/lib/core/Model.js.map +1 -1
- package/dist/lib/{tools/index.d.ts → core/Package.d.ts} +11 -3
- package/dist/lib/{tools/index.js → core/Package.js} +20 -7
- package/dist/lib/core/Package.js.map +1 -0
- package/dist/lib/core/Pool.js +42 -30
- package/dist/lib/core/Pool.js.map +1 -1
- package/dist/lib/core/Queue.d.ts +249 -0
- package/dist/lib/core/Queue.js +726 -0
- package/dist/lib/core/Queue.js.map +1 -0
- package/dist/lib/core/RelationManager.d.ts +4 -4
- package/dist/lib/core/RelationManager.js +168 -41
- package/dist/lib/core/RelationManager.js.map +1 -1
- package/dist/lib/core/Repository.d.ts +73 -207
- package/dist/lib/core/Repository.js +122 -234
- package/dist/lib/core/Repository.js.map +1 -1
- package/dist/lib/core/Schema.d.ts +211 -1
- package/dist/lib/core/Schema.js +370 -65
- package/dist/lib/core/Schema.js.map +1 -1
- package/dist/lib/core/StateManager.d.ts +31 -12
- package/dist/lib/core/StateManager.js +11 -5
- package/dist/lib/core/StateManager.js.map +1 -1
- package/dist/lib/core/UtilityTypes.d.ts +103 -81
- package/dist/lib/core/UtilityTypes.js.map +1 -1
- package/dist/lib/core/index.d.ts +4 -0
- package/dist/lib/core/index.js +6 -2
- package/dist/lib/core/index.js.map +1 -1
- package/dist/lib/types/decorator/index.d.ts +1 -1
- package/dist/lib/types/index.d.ts +120 -30
- package/dist/lib/types/repository/index.d.ts +68 -37
- package/dist/lib/utils/index.d.ts +13 -2
- package/dist/lib/utils/index.js +81 -2
- package/dist/lib/utils/index.js.map +1 -1
- package/package.json +14 -6
- package/dist/lib/tools/index.js.map +0 -1
package/dist/lib/core/Model.js
CHANGED
|
@@ -48,6 +48,8 @@ const Cache_1 = require("./Cache");
|
|
|
48
48
|
const JoinModel_1 = require("./JoinModel");
|
|
49
49
|
const constants_1 = require("../constants");
|
|
50
50
|
const Decorator_1 = require("./Decorator");
|
|
51
|
+
const Join_1 = require("./Join");
|
|
52
|
+
const Repository_1 = require("./Repository");
|
|
51
53
|
let globalSettings = {
|
|
52
54
|
softDelete: false,
|
|
53
55
|
debug: false,
|
|
@@ -164,12 +166,475 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
164
166
|
static get instance() {
|
|
165
167
|
return new this();
|
|
166
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* The 'query' method is used to return instance
|
|
171
|
+
* @static
|
|
172
|
+
* @example
|
|
173
|
+
* const user = await User.query().where('id',1).findOne();
|
|
174
|
+
* console.log(user);
|
|
175
|
+
*/
|
|
176
|
+
static query() {
|
|
177
|
+
return new this();
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
*
|
|
181
|
+
* The 'find' method is used to retrieve a single record from a database table by its primary key.
|
|
182
|
+
*
|
|
183
|
+
* It allows you to retrieve a single record from a database table that meets the specified criteria.
|
|
184
|
+
* @type {?object} options
|
|
185
|
+
* @property {?object} options.select
|
|
186
|
+
* @property {?object} options.except
|
|
187
|
+
* @property {?object[]} options.orderBy
|
|
188
|
+
* @property {?string[]} options.groupBy
|
|
189
|
+
* @property {?string} options.having
|
|
190
|
+
* @property {?number} options.limit
|
|
191
|
+
* @property {?number} options.offset
|
|
192
|
+
* @property {?object} options.where
|
|
193
|
+
* @property {?string[]} options.whereRaw
|
|
194
|
+
* @property {?object} options.whereQuery
|
|
195
|
+
* @property {?{condition,callback}} options.when
|
|
196
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
197
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
198
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
199
|
+
* @property {?string[]} options.relations
|
|
200
|
+
* @property {string[]} options.relationExists
|
|
201
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
202
|
+
* @property {?boolean} options.debug
|
|
203
|
+
* @returns {promise<object>[]}
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* import { User } from '../Models/User'
|
|
207
|
+
*
|
|
208
|
+
* const users = await User.find({
|
|
209
|
+
* select : { id: true, name: true },
|
|
210
|
+
* where : {
|
|
211
|
+
* id: 1
|
|
212
|
+
* }
|
|
213
|
+
* })
|
|
214
|
+
*
|
|
215
|
+
*/
|
|
216
|
+
static async find(primaryKey, options = {}) {
|
|
217
|
+
return await (0, Repository_1.Repository)(this).find(primaryKey, options);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
*
|
|
221
|
+
* The 'findOne' method is used to retrieve the get record that matches the query conditions.
|
|
222
|
+
*
|
|
223
|
+
* It allows you to retrieve a single record from a database table that meets the specified criteria.
|
|
224
|
+
* @type {?object} options
|
|
225
|
+
* @property {?object} options.select
|
|
226
|
+
* @property {?object} options.except
|
|
227
|
+
* @property {?object[]} options.orderBy
|
|
228
|
+
* @property {?string[]} options.groupBy
|
|
229
|
+
* @property {?string} options.having
|
|
230
|
+
* @property {?number} options.limit
|
|
231
|
+
* @property {?number} options.offset
|
|
232
|
+
* @property {?object} options.where
|
|
233
|
+
* @property {?string[]} options.whereRaw
|
|
234
|
+
* @property {?object} options.whereQuery
|
|
235
|
+
* @property {?{condition,callback}} options.when
|
|
236
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
237
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
238
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
239
|
+
* @property {?string[]} options.relations
|
|
240
|
+
* @property {string[]} options.relationExists
|
|
241
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
242
|
+
* @property {?boolean} options.debug
|
|
243
|
+
* @returns {promise<object>[]}
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* import { User } from '../Models/User'
|
|
247
|
+
*
|
|
248
|
+
* const users = await User.findOne({
|
|
249
|
+
* select : { id: true, name: true },
|
|
250
|
+
* where : {
|
|
251
|
+
* id: 1
|
|
252
|
+
* }
|
|
253
|
+
* })
|
|
254
|
+
*
|
|
255
|
+
*/
|
|
256
|
+
static async findOne(options = {}) {
|
|
257
|
+
return await (0, Repository_1.Repository)(this).findOne(options);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
*
|
|
261
|
+
* The 'findMany' method is used to retrieve the get record that matches the query conditions.
|
|
262
|
+
*
|
|
263
|
+
* It allows you to retrieve a single record from a database table that meets the specified criteria.
|
|
264
|
+
* @type {?object} options
|
|
265
|
+
* @property {?object} options.select
|
|
266
|
+
* @property {?object} options.except
|
|
267
|
+
* @property {?object[]} options.orderBy
|
|
268
|
+
* @property {?string[]} options.groupBy
|
|
269
|
+
* @property {?string} options.having
|
|
270
|
+
* @property {?number} options.limit
|
|
271
|
+
* @property {?number} options.offset
|
|
272
|
+
* @property {?object} options.where
|
|
273
|
+
* @property {?string[]} options.whereRaw
|
|
274
|
+
* @property {?object} options.whereQuery
|
|
275
|
+
* @property {?{condition,callback}} options.when
|
|
276
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
277
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
278
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
279
|
+
* @property {?string[]} options.relations
|
|
280
|
+
* @property {string[]} options.relationExists
|
|
281
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
282
|
+
* @property {?boolean} options.debug
|
|
283
|
+
* @returns {promise<object>[]}
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* import { User } from '../Models/User'
|
|
287
|
+
*
|
|
288
|
+
* const users = await User.findMany({
|
|
289
|
+
* select : { id: true, name: true },
|
|
290
|
+
* where : {
|
|
291
|
+
* id: 1
|
|
292
|
+
* }
|
|
293
|
+
* })
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
296
|
+
static async findMany(options = {}) {
|
|
297
|
+
return await (0, Repository_1.Repository)(this).findMany(options);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
*
|
|
301
|
+
* The 'paginate' method is used to perform pagination on a set of database query results obtained through the Query Builder.
|
|
302
|
+
*
|
|
303
|
+
* It allows you to split a large set of query results into smaller, more manageable pages,
|
|
304
|
+
* making it easier to display data in a web application and improve user experience.
|
|
305
|
+
* @type {?object} options
|
|
306
|
+
* @property {?object} options.select
|
|
307
|
+
* @property {?object} options.except
|
|
308
|
+
* @property {?object[]} options.orderBy
|
|
309
|
+
* @property {?string[]} options.groupBy
|
|
310
|
+
* @property {?string} options.having
|
|
311
|
+
* @property {?number} options.limit
|
|
312
|
+
* @property {?number} options.offset
|
|
313
|
+
* @property {?object} options.where
|
|
314
|
+
* @property {?string[]} options.whereRaw
|
|
315
|
+
* @property {?object} options.whereQuery
|
|
316
|
+
* @property {?{condition,callback}} options.when
|
|
317
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
318
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
319
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
320
|
+
* @property {?string[]} options.relations
|
|
321
|
+
* @property {string[]} options.relationExists
|
|
322
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
323
|
+
* @property {?boolean} options.debug
|
|
324
|
+
* @property {?number} options.page
|
|
325
|
+
* @returns {promise<{ meta , data[]}>}
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* import { User } from '../Models/User'
|
|
329
|
+
*
|
|
330
|
+
* const users = await User.paginate({
|
|
331
|
+
* limit:15,
|
|
332
|
+
* page: 1,
|
|
333
|
+
* select : { id: true, name: true },
|
|
334
|
+
* where : {
|
|
335
|
+
* id: 1
|
|
336
|
+
* }
|
|
337
|
+
* })
|
|
338
|
+
*/
|
|
339
|
+
static async paginate(options = {}) {
|
|
340
|
+
return await (0, Repository_1.Repository)(this).paginate(options);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
|
|
344
|
+
*
|
|
345
|
+
* It returns a boolean value indicating whether there are any matching records.
|
|
346
|
+
* @type {?object} options
|
|
347
|
+
* @property {?object} options.select
|
|
348
|
+
* @property {?object} options.except
|
|
349
|
+
* @property {?object[]} options.orderBy
|
|
350
|
+
* @property {?string[]} options.groupBy
|
|
351
|
+
* @property {?string} options.having
|
|
352
|
+
* @property {?number} options.limit
|
|
353
|
+
* @property {?number} options.offset
|
|
354
|
+
* @property {?object} options.where
|
|
355
|
+
* @property {?string[]} options.whereRaw
|
|
356
|
+
* @property {?object} options.whereQuery
|
|
357
|
+
* @property {?{condition,callback}} options.when
|
|
358
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
359
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
360
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
361
|
+
* @property {?boolean} options.debug
|
|
362
|
+
* @property {?number} options.page
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* import { User } from '../Models/User'
|
|
366
|
+
*
|
|
367
|
+
* const users = await User.exists({
|
|
368
|
+
* where : {
|
|
369
|
+
* id: 1
|
|
370
|
+
* }
|
|
371
|
+
* })
|
|
372
|
+
*
|
|
373
|
+
*/
|
|
374
|
+
static async exists(options) {
|
|
375
|
+
return await (0, Repository_1.Repository)(this).exists(options);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* The 'toQuery' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
|
|
379
|
+
*
|
|
380
|
+
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
|
|
381
|
+
* @type {?object} options
|
|
382
|
+
* @property {?object} options.select
|
|
383
|
+
* @property {?object} options.except
|
|
384
|
+
* @property {?object[]} options.orderBy
|
|
385
|
+
* @property {?string[]} options.groupBy
|
|
386
|
+
* @property {?string} options.having
|
|
387
|
+
* @property {?number} options.limit
|
|
388
|
+
* @property {?number} options.offset
|
|
389
|
+
* @property {?object} options.where
|
|
390
|
+
* @property {?string[]} options.whereRaw
|
|
391
|
+
* @property {?object} options.whereQuery
|
|
392
|
+
* @property {?{condition,callback}} options.when
|
|
393
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
394
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
395
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
396
|
+
* @property {?boolean} options.debug
|
|
397
|
+
* @property {?number} options.page
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* import { User } from '../Models/User'
|
|
401
|
+
*
|
|
402
|
+
* const users = await User.exists({
|
|
403
|
+
* where : {
|
|
404
|
+
* id: 1
|
|
405
|
+
* }
|
|
406
|
+
* })
|
|
407
|
+
*
|
|
408
|
+
*/
|
|
409
|
+
static toQuery(options) {
|
|
410
|
+
return (0, Repository_1.Repository)(this).toString(options);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* The 'create' method is used to insert a new record into a database table associated.
|
|
414
|
+
*
|
|
415
|
+
* It simplifies the process of creating and inserting records.
|
|
416
|
+
* @type {object} options
|
|
417
|
+
* @property {object} options.data
|
|
418
|
+
* @property {?boolean} options.debug
|
|
419
|
+
* @property {?transaction} options.transaction
|
|
420
|
+
* @return {promise<T.Result<M>>}
|
|
421
|
+
*/
|
|
422
|
+
static async create(options) {
|
|
423
|
+
return (0, Repository_1.Repository)(this).create(options);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* The 'createMany' method is used to insert a new records into a database table associated.
|
|
427
|
+
*
|
|
428
|
+
* It simplifies the process of creating and inserting records with an array.
|
|
429
|
+
* @type {object} options
|
|
430
|
+
* @property {object[]} options.data
|
|
431
|
+
* @property {?boolean} options.debug
|
|
432
|
+
* @property {?transaction} options.transaction
|
|
433
|
+
* @return {promise<TS[]>}
|
|
434
|
+
*/
|
|
435
|
+
static async createMany(options) {
|
|
436
|
+
return (0, Repository_1.Repository)(this).createMany(options);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
*
|
|
440
|
+
* 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.
|
|
441
|
+
*
|
|
442
|
+
* This method is particularly useful when you want to update a record based on certain conditions and,
|
|
443
|
+
* if the record matching those conditions doesn't exist, create a new one with the provided data.
|
|
444
|
+
* @type {object} options
|
|
445
|
+
* @property {object} options.data
|
|
446
|
+
* @property {object} options.where
|
|
447
|
+
* @property {?boolean} options.debug
|
|
448
|
+
* @return {promise<NR extends true ? undefined : T.Result<M>[]>}
|
|
449
|
+
*/
|
|
450
|
+
static async createOrUpdate(options) {
|
|
451
|
+
//@ts-ignore
|
|
452
|
+
return (0, Repository_1.Repository)(this).createOrUpdate(options);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* The 'createNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
|
|
456
|
+
*
|
|
457
|
+
* This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
|
|
458
|
+
* but without raising an error or exception if duplicates are encountered.
|
|
459
|
+
*
|
|
460
|
+
* @type {object} options
|
|
461
|
+
* @property {object} options.data
|
|
462
|
+
* @property {object} options.where
|
|
463
|
+
* @property {?boolean} options.debug
|
|
464
|
+
* @property {?transaction} options.transaction
|
|
465
|
+
* @return {promise<T | null>}
|
|
466
|
+
*/
|
|
467
|
+
static async createNotExists(options) {
|
|
468
|
+
return (0, Repository_1.Repository)(this).createNotExists(options);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
*
|
|
472
|
+
* The 'createOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
|
|
473
|
+
*
|
|
474
|
+
* This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
|
|
475
|
+
* but if exists should be returns a result.
|
|
476
|
+
* @type {object} options
|
|
477
|
+
* @property {object} options.data
|
|
478
|
+
* @property {object} options.where
|
|
479
|
+
* @property {?boolean} options.debug
|
|
480
|
+
* @return {promise<T.Result<M>>}
|
|
481
|
+
*/
|
|
482
|
+
static async createOrSelect(options) {
|
|
483
|
+
return (0, Repository_1.Repository)(this).createOrSelect(options);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* The 'update' method is used to update existing records in a database table that are associated.
|
|
487
|
+
*
|
|
488
|
+
* It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
|
|
489
|
+
*
|
|
490
|
+
* It allows you to remove one record that match certain criteria.
|
|
491
|
+
* @type {object} options
|
|
492
|
+
* @property {object} options.data
|
|
493
|
+
* @property {object} options.where
|
|
494
|
+
* @property {?boolean} options.debug
|
|
495
|
+
* @property {?transaction} options.transaction
|
|
496
|
+
* @return {promise< NR extends true ? undefined : T.Result<M> | null>}
|
|
497
|
+
*/
|
|
498
|
+
static async update(options) {
|
|
499
|
+
return (0, Repository_1.Repository)(this).update(options);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* The 'updateMany' method is used to update existing records in a database table that are associated.
|
|
503
|
+
*
|
|
504
|
+
* It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
|
|
505
|
+
*
|
|
506
|
+
* It allows you to remove more records that match certain criteria.
|
|
507
|
+
* @type {object} options
|
|
508
|
+
* @property {object} options.data
|
|
509
|
+
* @property {object} options.where
|
|
510
|
+
* @property {?boolean} options.debug
|
|
511
|
+
* @property {?transaction} options.transaction
|
|
512
|
+
* @return {promise<T.Result<M>[]>}
|
|
513
|
+
*/
|
|
514
|
+
static async updateMany(options) {
|
|
515
|
+
return (0, Repository_1.Repository)(this).updateMany(options);
|
|
516
|
+
}
|
|
167
517
|
/**
|
|
168
518
|
* The 'cache' method is used get the functions from the Cache
|
|
169
|
-
* @returns {
|
|
519
|
+
* @returns {TCacheModel} cache
|
|
170
520
|
*/
|
|
171
521
|
static get cache() {
|
|
172
|
-
|
|
522
|
+
const getCacheKey = (key) => {
|
|
523
|
+
const db = new this().database();
|
|
524
|
+
const table = new this().getTableName();
|
|
525
|
+
return `${db}:${table}:${key}`;
|
|
526
|
+
};
|
|
527
|
+
return {
|
|
528
|
+
provider: () => Cache_1.Cache.provider(),
|
|
529
|
+
driver: (driver) => Cache_1.Cache.driver(driver),
|
|
530
|
+
all: async () => {
|
|
531
|
+
return await Cache_1.Cache.all();
|
|
532
|
+
},
|
|
533
|
+
clear: async () => {
|
|
534
|
+
return await Cache_1.Cache.clear();
|
|
535
|
+
},
|
|
536
|
+
get: async (key, options) => {
|
|
537
|
+
const cacheKey = options?.namespace ? getCacheKey(key) : key;
|
|
538
|
+
return await Cache_1.Cache.get(cacheKey);
|
|
539
|
+
},
|
|
540
|
+
exists: async (key, options) => {
|
|
541
|
+
const cacheKey = options?.namespace ? getCacheKey(key) : key;
|
|
542
|
+
return await Cache_1.Cache.exists(cacheKey);
|
|
543
|
+
},
|
|
544
|
+
set: async (key, value, ms, options) => {
|
|
545
|
+
const cacheKey = options?.namespace ? getCacheKey(key) : key;
|
|
546
|
+
return await Cache_1.Cache.set(cacheKey, value, ms);
|
|
547
|
+
},
|
|
548
|
+
delete: async (key, options) => {
|
|
549
|
+
const cacheKey = options?.namespace ? getCacheKey(key) : key;
|
|
550
|
+
return await Cache_1.Cache.delete(cacheKey);
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* The 'lockTable' method is used table lock and execute the callback within the lock scope.
|
|
556
|
+
*
|
|
557
|
+
* The lock is automatically released after the callback completes,
|
|
558
|
+
* regardless of whether it resolves or throws an error.
|
|
559
|
+
*
|
|
560
|
+
* READ:
|
|
561
|
+
* - Other sessions can read.
|
|
562
|
+
* - Other sessions must wait before writing.
|
|
563
|
+
* - SELECT: allowed
|
|
564
|
+
* - INSERT: waits
|
|
565
|
+
* - UPDATE: waits
|
|
566
|
+
* - DELETE: waits
|
|
567
|
+
*
|
|
568
|
+
* WRITE:
|
|
569
|
+
* - Other sessions must wait before reading or writing.
|
|
570
|
+
* - SELECT: waits
|
|
571
|
+
* - INSERT: waits
|
|
572
|
+
* - UPDATE: waits
|
|
573
|
+
* - DELETE: waits
|
|
574
|
+
*
|
|
575
|
+
* The lock is released automatically after the callback completes.
|
|
576
|
+
*
|
|
577
|
+
* @Note
|
|
578
|
+
* This approach is not recommended in clustered or load-balance environments,
|
|
579
|
+
* because locks are session-bound and requests may be routed to different database connections.
|
|
580
|
+
*
|
|
581
|
+
* @template T
|
|
582
|
+
* @param {'READ'|'WRITE'} mode Lock mode.
|
|
583
|
+
* @param {(query: this) => Promise<T>} handler Function executed while the table is locked.
|
|
584
|
+
* @returns {Promise<T>} The value returned by the callback.
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* await User.lockTable('READ', async (query) => {
|
|
588
|
+
* query.get();
|
|
589
|
+
* return
|
|
590
|
+
* });
|
|
591
|
+
*
|
|
592
|
+
* @example
|
|
593
|
+
* await User.lockTable('WRITE', async (query) => {
|
|
594
|
+
* // Avoid creating a new query instance inside the lock.
|
|
595
|
+
* // The new instance uses a different connection/session and
|
|
596
|
+
* // may wait for the current table lock to be released.
|
|
597
|
+
*
|
|
598
|
+
* new User().insert({
|
|
599
|
+
* first_name: 'John',
|
|
600
|
+
* last_name: 'Doe'
|
|
601
|
+
* }).save();
|
|
602
|
+
*
|
|
603
|
+
* // Use the locked query instance instead.
|
|
604
|
+
* await query.insert({
|
|
605
|
+
* first_name: 'John',
|
|
606
|
+
* last_name: 'Doe'
|
|
607
|
+
* }).save();
|
|
608
|
+
* });
|
|
609
|
+
*/
|
|
610
|
+
static async lockTable(mode, handler) {
|
|
611
|
+
const trx = await DB_1.DB.beginTransaction();
|
|
612
|
+
try {
|
|
613
|
+
await trx.startTransaction();
|
|
614
|
+
const model = new this()
|
|
615
|
+
.bind(trx);
|
|
616
|
+
const sqlLockTable = model
|
|
617
|
+
._queryBuilder()
|
|
618
|
+
.lockTable(mode);
|
|
619
|
+
await model.rawQuery(sqlLockTable);
|
|
620
|
+
const results = await handler(model);
|
|
621
|
+
const sqlUnlockTable = model
|
|
622
|
+
._queryBuilder()
|
|
623
|
+
.unlockTable();
|
|
624
|
+
if (sqlUnlockTable != null) {
|
|
625
|
+
await model
|
|
626
|
+
.rawQuery(sqlUnlockTable);
|
|
627
|
+
}
|
|
628
|
+
await trx.commit();
|
|
629
|
+
return results;
|
|
630
|
+
}
|
|
631
|
+
catch (err) {
|
|
632
|
+
await trx.rollback();
|
|
633
|
+
throw err;
|
|
634
|
+
}
|
|
635
|
+
finally {
|
|
636
|
+
await trx.end();
|
|
637
|
+
}
|
|
173
638
|
}
|
|
174
639
|
/**
|
|
175
640
|
* The 'boot' method is a special method that you can define within a model.
|
|
@@ -219,18 +684,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
219
684
|
this.$state.set("SELECT", [...this.$state.get("SELECT"), ...select]);
|
|
220
685
|
}
|
|
221
686
|
if (except.length) {
|
|
222
|
-
this.$state.set("EXCEPTS", [
|
|
223
|
-
...this.$state.get("EXCEPTS"),
|
|
224
|
-
...except
|
|
225
|
-
]);
|
|
687
|
+
this.$state.set("EXCEPTS", [...this.$state.get("EXCEPTS"), ...except]);
|
|
226
688
|
}
|
|
227
689
|
if (where.length) {
|
|
228
690
|
this.$state.set("WHERE", [
|
|
229
691
|
...this.$state.get("WHERE"),
|
|
230
|
-
|
|
231
|
-
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
232
|
-
...where,
|
|
233
|
-
].join(" "),
|
|
692
|
+
...where
|
|
234
693
|
]);
|
|
235
694
|
}
|
|
236
695
|
if (groupBy.length) {
|
|
@@ -370,31 +829,31 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
370
829
|
return this;
|
|
371
830
|
}
|
|
372
831
|
/**
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
832
|
+
* The "useTransform " method is used to define value transformers for model columns..
|
|
833
|
+
*
|
|
834
|
+
* Each transformer defines how a value is converted:
|
|
835
|
+
* - `to` : before persisting to the database
|
|
836
|
+
* - `from` : after retrieving from the database
|
|
837
|
+
*
|
|
838
|
+
* Transformers can be synchronous or asynchronous.
|
|
839
|
+
*
|
|
840
|
+
* @param {object} transforms
|
|
841
|
+
* @example
|
|
842
|
+
* import { Blueprint } from 'tspace-mysql';
|
|
843
|
+
* class User extends Model {
|
|
844
|
+
* boot() {
|
|
845
|
+
* this.useTransform({
|
|
846
|
+
* name : {
|
|
847
|
+
* to : async (v) => `${v}-> transform@before`,
|
|
848
|
+
* from : async (v) => `${v}-> transform@after`,
|
|
849
|
+
* },
|
|
850
|
+
* })
|
|
851
|
+
* }
|
|
852
|
+
* }
|
|
853
|
+
* @returns {this} this
|
|
854
|
+
*/
|
|
396
855
|
useTransform(transforms) {
|
|
397
|
-
this.$state.set(
|
|
856
|
+
this.$state.set("TRANSFORMS", transforms);
|
|
398
857
|
return this;
|
|
399
858
|
}
|
|
400
859
|
/**
|
|
@@ -453,11 +912,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
453
912
|
*/
|
|
454
913
|
usePattern(pattern) {
|
|
455
914
|
const allowPattern = [
|
|
456
|
-
this.$constants("PATTERN").
|
|
915
|
+
this.$constants("PATTERN").snake_case,
|
|
457
916
|
this.$constants("PATTERN").camelCase,
|
|
458
917
|
];
|
|
459
918
|
if (!allowPattern.includes(pattern)) {
|
|
460
|
-
throw this._assertError(`The 'tspace-mysql' support only pattern '${this.$constants("PATTERN").
|
|
919
|
+
throw this._assertError(`The 'tspace-mysql' support only pattern '${this.$constants("PATTERN").snake_case}',
|
|
461
920
|
'${this.$constants("PATTERN").camelCase}'`);
|
|
462
921
|
}
|
|
463
922
|
this.$state.set("PATTERN", pattern);
|
|
@@ -490,7 +949,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
490
949
|
* }
|
|
491
950
|
*/
|
|
492
951
|
useSnakeCase() {
|
|
493
|
-
this.$state.set("PATTERN", this.$constants("PATTERN").
|
|
952
|
+
this.$state.set("PATTERN", this.$constants("PATTERN").snake_case);
|
|
494
953
|
this._makeTableName();
|
|
495
954
|
return this;
|
|
496
955
|
}
|
|
@@ -746,7 +1205,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
746
1205
|
* this
|
|
747
1206
|
* .useLifecycle("beforeInsert", fnA)
|
|
748
1207
|
* .useLifecycle("afterInsert", fnB);
|
|
749
|
-
|
|
1208
|
+
*/
|
|
750
1209
|
useLifecycle(type, funcs) {
|
|
751
1210
|
const MAP = {
|
|
752
1211
|
beforeInsert: "LIFECYCLE_BEFORE_INSERTS",
|
|
@@ -886,7 +1345,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
886
1345
|
return except !== column;
|
|
887
1346
|
});
|
|
888
1347
|
});
|
|
889
|
-
removeExcepts.push(hasDot
|
|
1348
|
+
removeExcepts.push(hasDot
|
|
1349
|
+
? removeExcept.map((r) => `\`${tableName}\`.${r}`)
|
|
1350
|
+
: removeExcept);
|
|
890
1351
|
}
|
|
891
1352
|
return removeExcepts.flat();
|
|
892
1353
|
}
|
|
@@ -898,13 +1359,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
898
1359
|
*/
|
|
899
1360
|
buildMethodRelation(name, callback) {
|
|
900
1361
|
this.relations(name);
|
|
901
|
-
const relation = this.$state
|
|
902
|
-
.get("RELATIONS")
|
|
903
|
-
.find((v) => v.name === name);
|
|
1362
|
+
const relation = this.$state.get("RELATIONS").find((v) => v.name === name);
|
|
904
1363
|
if (relation == null) {
|
|
905
1364
|
throw this._assertError(`This Relation '${String(name)}' not be register in Model '${this.constructor?.name}'.`);
|
|
906
1365
|
}
|
|
907
|
-
const relationHasExists = Object.values(this.$constants("RELATIONSHIP"))?.includes(relation.relation);
|
|
1366
|
+
const relationHasExists = Object.values(this.$constants("RELATIONSHIP"))?.includes(relation.relation ?? "");
|
|
908
1367
|
if (!relationHasExists) {
|
|
909
1368
|
throw this._assertError(`Unknown relationship in '${this.$constants("RELATIONSHIP")}'.`);
|
|
910
1369
|
}
|
|
@@ -928,7 +1387,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
928
1387
|
this.$state.set("AUDIT", userId);
|
|
929
1388
|
return this;
|
|
930
1389
|
}
|
|
931
|
-
|
|
1390
|
+
metaTag(meta) {
|
|
932
1391
|
this.$state.set("META", meta);
|
|
933
1392
|
return this;
|
|
934
1393
|
}
|
|
@@ -951,12 +1410,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
951
1410
|
* @param {Object} object
|
|
952
1411
|
* @property {string} key key of cache
|
|
953
1412
|
* @property {number} expires ms
|
|
1413
|
+
* @property {boolean} namespace whether to use namespace for cache key, default is false, namespace is `${database}:${table}:${key}`
|
|
954
1414
|
* @returns {this} this
|
|
955
1415
|
*/
|
|
956
|
-
cache({ key, expires }) {
|
|
1416
|
+
cache({ key, expires, namespace }) {
|
|
1417
|
+
const getCacheKey = (key) => {
|
|
1418
|
+
const db = this.database();
|
|
1419
|
+
const table = this.getTableName();
|
|
1420
|
+
return `${db}:${table}:${key}`;
|
|
1421
|
+
};
|
|
1422
|
+
const cacheKey = namespace ? getCacheKey(key) : key;
|
|
957
1423
|
this.$state.set("CACHE", {
|
|
958
|
-
key,
|
|
959
|
-
expires
|
|
1424
|
+
key: cacheKey,
|
|
1425
|
+
expires
|
|
960
1426
|
});
|
|
961
1427
|
return this;
|
|
962
1428
|
}
|
|
@@ -997,6 +1463,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
997
1463
|
this.$state.set("SELECT", select);
|
|
998
1464
|
return this;
|
|
999
1465
|
}
|
|
1466
|
+
/**
|
|
1467
|
+
*
|
|
1468
|
+
* @override
|
|
1469
|
+
* @param {string[]} ...columns
|
|
1470
|
+
* @returns {this} this
|
|
1471
|
+
*/
|
|
1000
1472
|
addSelect(...columns) {
|
|
1001
1473
|
let select = columns.map((c) => {
|
|
1002
1474
|
const column = String(c);
|
|
@@ -1172,11 +1644,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1172
1644
|
* @param {string} column
|
|
1173
1645
|
* @returns {string} return table.column
|
|
1174
1646
|
*/
|
|
1175
|
-
bindColumn(column,
|
|
1647
|
+
bindColumn(column, opts = { pattern: true }) {
|
|
1176
1648
|
if (!/\./.test(column)) {
|
|
1177
1649
|
if (column === "*")
|
|
1178
1650
|
return "*";
|
|
1179
|
-
const c = pattern ? this._valuePattern(column) : column;
|
|
1651
|
+
const c = opts.pattern ? this._valuePattern(column) : column;
|
|
1180
1652
|
const alias = this.$state.get("ALIAS");
|
|
1181
1653
|
return [
|
|
1182
1654
|
alias == null || alias === ""
|
|
@@ -1187,7 +1659,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1187
1659
|
].join("");
|
|
1188
1660
|
}
|
|
1189
1661
|
let [table, c] = column.split(".");
|
|
1190
|
-
c = pattern ? this._valuePattern(c) : c;
|
|
1662
|
+
c = opts.pattern ? this._valuePattern(c) : c;
|
|
1191
1663
|
if (c === "*") {
|
|
1192
1664
|
return `\`${table.replace(/`/g, "")}\`.*`;
|
|
1193
1665
|
}
|
|
@@ -1346,8 +1818,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1346
1818
|
newInstance.$state.set("SAVE", null);
|
|
1347
1819
|
newInstance.$state.set("DEBUG", false);
|
|
1348
1820
|
newInstance.$state.set("LOGGER", false);
|
|
1349
|
-
newInstance.$state.set("AUDIT", null);
|
|
1350
|
-
newInstance.$state.set("AUDIT_METADATA", null);
|
|
1351
1821
|
newInstance.$state.set("LIFECYCLE_BEFORE_INSERTS", []);
|
|
1352
1822
|
newInstance.$state.set("LIFECYCLE_BEFORE_UPDATES", []);
|
|
1353
1823
|
newInstance.$state.set("LIFECYCLE_BEFORE_REMOVES", []);
|
|
@@ -1389,7 +1859,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1389
1859
|
*/
|
|
1390
1860
|
async _queryStatement(sql, { retry = false } = {}) {
|
|
1391
1861
|
try {
|
|
1392
|
-
sql = this._queryBuilder({ onFormat: true }).format([sql]);
|
|
1393
1862
|
const getResults = async (sql) => {
|
|
1394
1863
|
if (this.$state.get("DEBUG")) {
|
|
1395
1864
|
const startTime = +new Date();
|
|
@@ -1423,7 +1892,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1423
1892
|
throw error;
|
|
1424
1893
|
const retryCount = Number(this.$state.get("RETRY"));
|
|
1425
1894
|
await this._checkSchemaOrNextError(error, retryCount, error);
|
|
1426
|
-
this.$state.set("RETRY", retryCount + 1);
|
|
1427
1895
|
return await this._queryStatement(sql, { retry: true });
|
|
1428
1896
|
}
|
|
1429
1897
|
}
|
|
@@ -1436,7 +1904,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1436
1904
|
*/
|
|
1437
1905
|
async _actionStatement(sql, { retry = false } = {}) {
|
|
1438
1906
|
try {
|
|
1439
|
-
sql = this._queryBuilder({ onFormat: true }).format([sql]);
|
|
1440
1907
|
const getResults = async (sql) => {
|
|
1441
1908
|
if (this.$state.get("DEBUG")) {
|
|
1442
1909
|
const startTime = +new Date();
|
|
@@ -1470,7 +1937,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1470
1937
|
if (this.$state.get("JOIN")?.length)
|
|
1471
1938
|
throw error;
|
|
1472
1939
|
await this._checkSchemaOrNextError(error, retryCount, error);
|
|
1473
|
-
this.$state.set("RETRY", retryCount + 1);
|
|
1474
1940
|
return await this._actionStatement(sql, { retry: true });
|
|
1475
1941
|
}
|
|
1476
1942
|
}
|
|
@@ -1917,6 +2383,55 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1917
2383
|
this.$relation.callback(String(nameRelation), callback);
|
|
1918
2384
|
return this;
|
|
1919
2385
|
}
|
|
2386
|
+
/**
|
|
2387
|
+
*
|
|
2388
|
+
* The 'withQueryExists' method is particularly useful when you want to filter or add conditions records based on related data.
|
|
2389
|
+
*
|
|
2390
|
+
* Use relation '${name}' registry models then return callback queries
|
|
2391
|
+
* @param {string} nameRelation name relation in registry in your model
|
|
2392
|
+
* @param {function} callback query callback
|
|
2393
|
+
* @param {object} options pivot the query
|
|
2394
|
+
* @example
|
|
2395
|
+
* import { Model } from 'tspace-mysql'
|
|
2396
|
+
* class User extends Model {
|
|
2397
|
+
* constructor(){
|
|
2398
|
+
* super()
|
|
2399
|
+
* this.hasMany({ name : 'posts' , model : Post })
|
|
2400
|
+
* }
|
|
2401
|
+
* }
|
|
2402
|
+
*
|
|
2403
|
+
* class Post extends Model {
|
|
2404
|
+
* constructor(){
|
|
2405
|
+
* super()
|
|
2406
|
+
* this.hasMany({ name : 'comments' , model : Comment })
|
|
2407
|
+
* this.belongsTo({ name : 'user' , model : User })
|
|
2408
|
+
* }
|
|
2409
|
+
* }
|
|
2410
|
+
*
|
|
2411
|
+
* class Comment extends Model {
|
|
2412
|
+
* constructor(){
|
|
2413
|
+
* super()
|
|
2414
|
+
* this.hasMany({ name : 'users' , model : User })
|
|
2415
|
+
* this.belongsTo({ name : 'post' , model : Post })
|
|
2416
|
+
* }
|
|
2417
|
+
* }
|
|
2418
|
+
*
|
|
2419
|
+
* await new User().with('posts')
|
|
2420
|
+
* .withQueryExists('posts', (query : Post) => {
|
|
2421
|
+
* return query.with('comments','user')
|
|
2422
|
+
* .withQuery('comments', (query : Comment) => {
|
|
2423
|
+
* return query.with('user','post')
|
|
2424
|
+
* })
|
|
2425
|
+
* .withQueryExists('user', (query : User) => {
|
|
2426
|
+
* return query.with('posts').withQueryExists('posts',(query : Post)=> {
|
|
2427
|
+
* return query.with('comments','user')
|
|
2428
|
+
* // relation n, n, ...n
|
|
2429
|
+
* })
|
|
2430
|
+
* })
|
|
2431
|
+
* })
|
|
2432
|
+
* .findMany()
|
|
2433
|
+
* @returns {this} this
|
|
2434
|
+
*/
|
|
1920
2435
|
withQueryExists(nameRelation, callback, options = { pivot: false }) {
|
|
1921
2436
|
this.withExists(nameRelation);
|
|
1922
2437
|
if (options.pivot) {
|
|
@@ -2041,6 +2556,36 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2041
2556
|
const instance = this.$relation.returnCallback(String(name));
|
|
2042
2557
|
return instance == null ? null : instance;
|
|
2043
2558
|
}
|
|
2559
|
+
/**
|
|
2560
|
+
* The 'ofMany' method is used to transform a many relationship,
|
|
2561
|
+
* into a single related model by selecting one record using an aggregate
|
|
2562
|
+
* function such as MAX or MIN on the specified column.
|
|
2563
|
+
*
|
|
2564
|
+
* @note
|
|
2565
|
+
* This method is not supported on `belongsToMany` relationships.
|
|
2566
|
+
*
|
|
2567
|
+
* @param {string} column The column used for aggregation (e.g. `"id"`, `"created_at"`).
|
|
2568
|
+
* @param {"MAX" | "MIN"} aggregate The aggregate function used to select the record.
|
|
2569
|
+
* @param {((query: Model) => void) | undefined} callback A callback used to apply additional constraints to the aggregate query.
|
|
2570
|
+
*
|
|
2571
|
+
* Only filtering methods are supported. Methods that modify the result set,
|
|
2572
|
+
* such as `select` `orderBy`, `groupBy`, `limit`, `offset`, `union`, and `having`, are not supported.
|
|
2573
|
+
* @returns this
|
|
2574
|
+
*/
|
|
2575
|
+
ofMany(column, aggregate, callback) {
|
|
2576
|
+
const cb = callback == null
|
|
2577
|
+
? null
|
|
2578
|
+
: callback(new Model().copyModel(this));
|
|
2579
|
+
if (cb instanceof Promise) {
|
|
2580
|
+
throw new Error("'ofMany' does not support Promises");
|
|
2581
|
+
}
|
|
2582
|
+
this.$state.set('OF_MANY', {
|
|
2583
|
+
column: this.bindColumn(String(column)),
|
|
2584
|
+
aggregate,
|
|
2585
|
+
query: cb
|
|
2586
|
+
});
|
|
2587
|
+
return this;
|
|
2588
|
+
}
|
|
2044
2589
|
/**
|
|
2045
2590
|
* The 'hasOne' relationship defines a one-to-one relationship between two database tables.
|
|
2046
2591
|
*
|
|
@@ -2189,6 +2734,70 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2189
2734
|
});
|
|
2190
2735
|
return this;
|
|
2191
2736
|
}
|
|
2737
|
+
/**
|
|
2738
|
+
* The 'createRelation' method is useful for Creates a relation accessor for a model.
|
|
2739
|
+
*
|
|
2740
|
+
* This method defines how a model relates to another model
|
|
2741
|
+
* (hasOne, hasMany, belongsTo, belongsToMany) and returns
|
|
2742
|
+
* a function that can be used to modify the relation query.
|
|
2743
|
+
*
|
|
2744
|
+
* Example:
|
|
2745
|
+
* ```ts
|
|
2746
|
+
* public posts = this.createRelation(Post, {
|
|
2747
|
+
* type: 'hasMany',
|
|
2748
|
+
* foreignKey: 'user_id',
|
|
2749
|
+
* });
|
|
2750
|
+
*
|
|
2751
|
+
* user.posts(q => q.where('id', 1));
|
|
2752
|
+
* ```
|
|
2753
|
+
*
|
|
2754
|
+
* @template M - Target model type
|
|
2755
|
+
* @template K - Optional relation name type
|
|
2756
|
+
*
|
|
2757
|
+
* @param model - The related model constructor
|
|
2758
|
+
* @param options - Relation configuration options
|
|
2759
|
+
* @param options.type - Type of relation (hasOne, hasMany, belongsTo, belongsToMany)
|
|
2760
|
+
* @param options.name - Optional relation name (when applicable)
|
|
2761
|
+
* @param options.as - Alias for relation
|
|
2762
|
+
* @param options.localKey - Local key for the relation
|
|
2763
|
+
* @param options.foreignKey - Foreign key for the relation
|
|
2764
|
+
* @param options.freezeTable - Override pivot/table name
|
|
2765
|
+
* @param options.pivot - Pivot table name (for many-to-many)
|
|
2766
|
+
* @param options.oldVersion - Enable legacy relation behavior
|
|
2767
|
+
*
|
|
2768
|
+
* @returns A function that accepts a query modifier and returns the relation result
|
|
2769
|
+
*/
|
|
2770
|
+
createRelation(model, options) {
|
|
2771
|
+
return (modifier) => {
|
|
2772
|
+
const opts = {
|
|
2773
|
+
name: options.name,
|
|
2774
|
+
as: options.as,
|
|
2775
|
+
localKey: options.localKey,
|
|
2776
|
+
foreignKey: options.foreignKey,
|
|
2777
|
+
freezeTable: options.freezeTable
|
|
2778
|
+
};
|
|
2779
|
+
switch (options.type) {
|
|
2780
|
+
case 'hasOne': {
|
|
2781
|
+
return this.hasOneBuilder({ model, ...opts }, modifier);
|
|
2782
|
+
}
|
|
2783
|
+
case 'hasMany': {
|
|
2784
|
+
return this.hasManyBuilder({ model, ...opts }, modifier);
|
|
2785
|
+
}
|
|
2786
|
+
case 'belongsTo': {
|
|
2787
|
+
return this.belongsToBuilder({ model, ...opts }, modifier);
|
|
2788
|
+
}
|
|
2789
|
+
case 'belongsToMany': {
|
|
2790
|
+
return this.belongsToManyBuilder({
|
|
2791
|
+
model,
|
|
2792
|
+
...opts,
|
|
2793
|
+
pivot: options.pivot,
|
|
2794
|
+
oldVersion: options.oldVersion,
|
|
2795
|
+
modelPivot: options.modelPivot
|
|
2796
|
+
}, modifier);
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
2800
|
+
}
|
|
2192
2801
|
/**
|
|
2193
2802
|
* The 'hasOneBuilder' method is useful for creating 'hasOne' relationship to function
|
|
2194
2803
|
*
|
|
@@ -2325,11 +2934,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2325
2934
|
const query = this.$state.get("TIMESTAMP")
|
|
2326
2935
|
? `${deletedAt} = NULL , ${updatedAt} = '${this.$utils.timestamp()}'`
|
|
2327
2936
|
: `${deletedAt} = NULL`;
|
|
2328
|
-
this.$state.set("UPDATE", [
|
|
2329
|
-
`${query}`
|
|
2330
|
-
]);
|
|
2937
|
+
this.$state.set("UPDATE", [`${query}`]);
|
|
2331
2938
|
this.$state.set("SAVE", "UPDATE");
|
|
2332
|
-
return (await this.save());
|
|
2939
|
+
return (await this.bind(this.$pool.get()).save());
|
|
2333
2940
|
}
|
|
2334
2941
|
/**
|
|
2335
2942
|
*
|
|
@@ -2349,7 +2956,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2349
2956
|
/**
|
|
2350
2957
|
* @override
|
|
2351
2958
|
* @param {string | K} column if arguments is object
|
|
2352
|
-
* @param {string?} operator
|
|
2959
|
+
* @param {string?} operator "=" | "<" | ">" | "!=" | "<>" | "<=" | ">=" | "LIKE" | "like"
|
|
2353
2960
|
* @param {any?} value
|
|
2354
2961
|
* @returns {this} this
|
|
2355
2962
|
*/
|
|
@@ -2358,9 +2965,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2358
2965
|
return this.whereObject(column);
|
|
2359
2966
|
}
|
|
2360
2967
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
2361
|
-
value = this.$utils.transfromDateToDateString(value);
|
|
2362
|
-
value = this.$utils.escape(value);
|
|
2363
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
2364
2968
|
const blueprint = this._getBlueprintByColumn(String(column));
|
|
2365
2969
|
if (blueprint?.isVirtual) {
|
|
2366
2970
|
const sql = blueprint.sql?.where;
|
|
@@ -2371,7 +2975,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2371
2975
|
if (Array.isArray(value)) {
|
|
2372
2976
|
const values = value
|
|
2373
2977
|
? `${value
|
|
2374
|
-
.map((value) => this.$utils.
|
|
2978
|
+
.map((value) => this.$utils.formatQueryValue(value))
|
|
2375
2979
|
.join(",")}`
|
|
2376
2980
|
: this.$constants(this.$constants("NULL"));
|
|
2377
2981
|
return this.whereRaw(`${sql} ${this.$constants("IN")} (${values})`);
|
|
@@ -2385,32 +2989,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2385
2989
|
if (Array.isArray(value)) {
|
|
2386
2990
|
return this.whereIn(column, value);
|
|
2387
2991
|
}
|
|
2388
|
-
const
|
|
2992
|
+
const formatedValue = this.$utils.formatQueryValue(value);
|
|
2389
2993
|
this.$state.set("WHERE", [
|
|
2390
|
-
...
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
]
|
|
2397
|
-
.join(" ")
|
|
2398
|
-
.replace(/^\s+/, "")
|
|
2994
|
+
...this.$state.get("WHERE"),
|
|
2995
|
+
{
|
|
2996
|
+
column: this.bindColumn(String(column)),
|
|
2997
|
+
operator: operator,
|
|
2998
|
+
value: formatedValue
|
|
2999
|
+
}
|
|
2399
3000
|
]);
|
|
2400
3001
|
return this;
|
|
2401
3002
|
}
|
|
2402
|
-
/**
|
|
2403
|
-
* @override
|
|
2404
|
-
* @param {string} column
|
|
2405
|
-
* @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
|
|
2406
|
-
* @param {any?} value
|
|
2407
|
-
* @returns {this}
|
|
2408
|
-
*/
|
|
2409
3003
|
orWhere(column, operator, value) {
|
|
2410
3004
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
2411
|
-
value = this.$utils.escape(value);
|
|
2412
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
2413
|
-
value = this.$utils.transfromDateToDateString(value);
|
|
2414
3005
|
const blueprint = this._getBlueprintByColumn(String(column));
|
|
2415
3006
|
if (blueprint?.isVirtual) {
|
|
2416
3007
|
const sql = blueprint.sql?.where;
|
|
@@ -2421,7 +3012,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2421
3012
|
if (Array.isArray(value)) {
|
|
2422
3013
|
const values = value
|
|
2423
3014
|
? `${value
|
|
2424
|
-
.map((value) => this.$utils.
|
|
3015
|
+
.map((value) => this.$utils.formatQueryValue(value))
|
|
2425
3016
|
.join(",")}`
|
|
2426
3017
|
: this.$constants(this.$constants("NULL"));
|
|
2427
3018
|
return this.orWhereRaw(`${sql} ${this.$constants("IN")} (${values})`);
|
|
@@ -2435,17 +3026,15 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2435
3026
|
if (Array.isArray(value)) {
|
|
2436
3027
|
return this.orWhereIn(column, value);
|
|
2437
3028
|
}
|
|
2438
|
-
const
|
|
3029
|
+
const formatedValue = this.$utils.formatQueryValue(value);
|
|
2439
3030
|
this.$state.set("WHERE", [
|
|
2440
|
-
...
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
.join(" ")
|
|
2448
|
-
.replace(/^\s+/, ""),
|
|
3031
|
+
...this.$state.get("WHERE"),
|
|
3032
|
+
{
|
|
3033
|
+
column: this.bindColumn(String(column)),
|
|
3034
|
+
operator: operator,
|
|
3035
|
+
value: formatedValue,
|
|
3036
|
+
condition: 'OR'
|
|
3037
|
+
}
|
|
2449
3038
|
]);
|
|
2450
3039
|
return this;
|
|
2451
3040
|
}
|
|
@@ -2456,17 +3045,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2456
3045
|
* @returns {this}
|
|
2457
3046
|
*/
|
|
2458
3047
|
whereDay(column, day) {
|
|
2459
|
-
const wheres = this.$state.get("WHERE");
|
|
2460
3048
|
this.$state.set("WHERE", [
|
|
2461
|
-
...
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
]
|
|
2468
|
-
.join(" ")
|
|
2469
|
-
.replace(/^\s+/, ""),
|
|
3049
|
+
...this.$state.get("WHERE"),
|
|
3050
|
+
{
|
|
3051
|
+
column: `DAY(${this.bindColumn(String(column))})`,
|
|
3052
|
+
operator: '=',
|
|
3053
|
+
value: `'${`00${this.$utils.escape(day)}`.slice(-2)}'`,
|
|
3054
|
+
}
|
|
2470
3055
|
]);
|
|
2471
3056
|
return this;
|
|
2472
3057
|
}
|
|
@@ -2477,17 +3062,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2477
3062
|
* @returns {this}
|
|
2478
3063
|
*/
|
|
2479
3064
|
whereMonth(column, month) {
|
|
2480
|
-
const wheres = this.$state.get("WHERE");
|
|
2481
3065
|
this.$state.set("WHERE", [
|
|
2482
|
-
...
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
]
|
|
2489
|
-
.join(" ")
|
|
2490
|
-
.replace(/^\s+/, ""),
|
|
3066
|
+
...this.$state.get("WHERE"),
|
|
3067
|
+
{
|
|
3068
|
+
column: `MONTH(${this.bindColumn(String(column))})`,
|
|
3069
|
+
operator: '=',
|
|
3070
|
+
value: `'${`00${this.$utils.escape(month)}`.slice(-2)}'`,
|
|
3071
|
+
}
|
|
2491
3072
|
]);
|
|
2492
3073
|
return this;
|
|
2493
3074
|
}
|
|
@@ -2498,17 +3079,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2498
3079
|
* @returns {this}
|
|
2499
3080
|
*/
|
|
2500
3081
|
whereYear(column, year) {
|
|
2501
|
-
const wheres = this.$state.get("WHERE");
|
|
2502
3082
|
this.$state.set("WHERE", [
|
|
2503
|
-
...
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
]
|
|
2510
|
-
.join(" ")
|
|
2511
|
-
.replace(/^\s+/, ""),
|
|
3083
|
+
...this.$state.get("WHERE"),
|
|
3084
|
+
{
|
|
3085
|
+
column: `YEAR(${this.bindColumn(String(column))})`,
|
|
3086
|
+
operator: '=',
|
|
3087
|
+
value: `'${`0000${this.$utils.escape(year)}`.slice(-2)}'`,
|
|
3088
|
+
}
|
|
2512
3089
|
]);
|
|
2513
3090
|
return this;
|
|
2514
3091
|
}
|
|
@@ -2579,9 +3156,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2579
3156
|
}
|
|
2580
3157
|
default: {
|
|
2581
3158
|
if (useOp.op.includes("|")) {
|
|
3159
|
+
//@ts-ignore
|
|
2582
3160
|
this.orWhere(c, useOp.op.replace("|", ""), useOp.value);
|
|
2583
3161
|
break;
|
|
2584
3162
|
}
|
|
3163
|
+
//@ts-ignore
|
|
2585
3164
|
this.where(c, useOp.op, useOp.value);
|
|
2586
3165
|
}
|
|
2587
3166
|
}
|
|
@@ -2598,19 +3177,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2598
3177
|
* @returns {this}
|
|
2599
3178
|
*/
|
|
2600
3179
|
whereJSON(column, { key, value, operator }) {
|
|
2601
|
-
|
|
2602
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
2603
|
-
const wheres = this.$state.get("WHERE");
|
|
3180
|
+
const formatedValue = this.$utils.formatQueryValue(value);
|
|
2604
3181
|
this.$state.set("WHERE", [
|
|
2605
|
-
...
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
`${
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
]
|
|
2612
|
-
.join(" ")
|
|
2613
|
-
.replace(/^\s+/, ""),
|
|
3182
|
+
...this.$state.get("WHERE"),
|
|
3183
|
+
{
|
|
3184
|
+
column: `${this.bindColumn(String(column))}->>'$.${key}'`,
|
|
3185
|
+
operator: `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
|
|
3186
|
+
value: formatedValue
|
|
3187
|
+
}
|
|
2614
3188
|
]);
|
|
2615
3189
|
return this;
|
|
2616
3190
|
}
|
|
@@ -2633,15 +3207,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2633
3207
|
* @returns {this}
|
|
2634
3208
|
*/
|
|
2635
3209
|
whereUser(userId, column = "user_id") {
|
|
2636
|
-
const
|
|
3210
|
+
const formatedValue = this.$utils.formatQueryValue(userId);
|
|
2637
3211
|
this.$state.set("WHERE", [
|
|
2638
|
-
...
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
.replace(/^\s+/, ""),
|
|
3212
|
+
...this.$state.get("WHERE"),
|
|
3213
|
+
{
|
|
3214
|
+
column: this.bindColumn(String(column)),
|
|
3215
|
+
operator: '=',
|
|
3216
|
+
value: formatedValue
|
|
3217
|
+
}
|
|
2645
3218
|
]);
|
|
2646
3219
|
return this;
|
|
2647
3220
|
}
|
|
@@ -2654,14 +3227,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2654
3227
|
if (sql instanceof Model && !sql.$state.get("SELECT").length) {
|
|
2655
3228
|
sql.select1();
|
|
2656
3229
|
}
|
|
2657
|
-
const wheres = this.$state.get("WHERE");
|
|
2658
3230
|
this.$state.set("WHERE", [
|
|
2659
|
-
...
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
`(${sql})`,
|
|
2664
|
-
].join(" "),
|
|
3231
|
+
...this.$state.get("WHERE"),
|
|
3232
|
+
{
|
|
3233
|
+
value: `${this.$constants("EXISTS")} (${sql})`
|
|
3234
|
+
}
|
|
2665
3235
|
]);
|
|
2666
3236
|
return this;
|
|
2667
3237
|
}
|
|
@@ -2674,29 +3244,32 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2674
3244
|
whereIn(column, array) {
|
|
2675
3245
|
if (!Array.isArray(array))
|
|
2676
3246
|
array = [array];
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
.join(",")}`
|
|
2681
|
-
: this.$constants(this.$constants("NULL"));
|
|
3247
|
+
if (!array.length) {
|
|
3248
|
+
array = [DB_1.DB.raw(this.$constants("NULL"))];
|
|
3249
|
+
}
|
|
2682
3250
|
const blueprint = this._getBlueprintByColumn(String(column));
|
|
2683
3251
|
if (blueprint?.isVirtual) {
|
|
3252
|
+
const values = array.length ? `${array
|
|
3253
|
+
.map((value) => {
|
|
3254
|
+
return this.$utils.formatQueryValue(value);
|
|
3255
|
+
})
|
|
3256
|
+
.join(",")}`
|
|
3257
|
+
: this.$constants(this.$constants("NULL"));
|
|
2684
3258
|
const sql = blueprint.sql?.where;
|
|
2685
3259
|
if (sql) {
|
|
2686
3260
|
return this.whereRaw(`${sql} ${this.$constants("IN")} (${values})`);
|
|
2687
3261
|
}
|
|
2688
3262
|
}
|
|
2689
|
-
const wheres = this.$state.get("WHERE");
|
|
2690
3263
|
this.$state.set("WHERE", [
|
|
2691
|
-
...
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
`${this
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
3264
|
+
...this.$state.get("WHERE"),
|
|
3265
|
+
{
|
|
3266
|
+
column: this.bindColumn(String(column)),
|
|
3267
|
+
operator: `${this.$constants("IN")}`,
|
|
3268
|
+
value: array
|
|
3269
|
+
.map((value) => {
|
|
3270
|
+
return this.$utils.formatQueryValue(value);
|
|
3271
|
+
})
|
|
3272
|
+
}
|
|
2700
3273
|
]);
|
|
2701
3274
|
return this;
|
|
2702
3275
|
}
|
|
@@ -2709,22 +3282,33 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2709
3282
|
orWhereIn(column, array) {
|
|
2710
3283
|
if (!Array.isArray(array))
|
|
2711
3284
|
array = [array];
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
3285
|
+
if (!array.length) {
|
|
3286
|
+
array = [DB_1.DB.raw(this.$constants("NULL"))];
|
|
3287
|
+
}
|
|
3288
|
+
const blueprint = this._getBlueprintByColumn(String(column));
|
|
3289
|
+
if (blueprint?.isVirtual) {
|
|
3290
|
+
const values = array.length ? `${array
|
|
3291
|
+
.map((value) => {
|
|
3292
|
+
return this.$utils.formatQueryValue(value);
|
|
3293
|
+
})
|
|
2715
3294
|
.join(",")}`
|
|
2716
|
-
|
|
2717
|
-
|
|
3295
|
+
: this.$constants(this.$constants("NULL"));
|
|
3296
|
+
const sql = blueprint.sql?.where;
|
|
3297
|
+
if (sql) {
|
|
3298
|
+
return this.orWhereRaw(`${sql} ${this.$constants("IN")} (${values})`);
|
|
3299
|
+
}
|
|
3300
|
+
}
|
|
2718
3301
|
this.$state.set("WHERE", [
|
|
2719
|
-
...
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
`${this.$constants("IN")}`,
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
3302
|
+
...this.$state.get("WHERE"),
|
|
3303
|
+
{
|
|
3304
|
+
column: this.bindColumn(String(column)),
|
|
3305
|
+
condition: 'OR',
|
|
3306
|
+
operator: `${this.$constants("IN")}`,
|
|
3307
|
+
value: array
|
|
3308
|
+
.map((value) => {
|
|
3309
|
+
return this.$utils.formatQueryValue(value);
|
|
3310
|
+
})
|
|
3311
|
+
}
|
|
2728
3312
|
]);
|
|
2729
3313
|
return this;
|
|
2730
3314
|
}
|
|
@@ -2737,22 +3321,32 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2737
3321
|
whereNotIn(column, array) {
|
|
2738
3322
|
if (!Array.isArray(array))
|
|
2739
3323
|
array = [array];
|
|
2740
|
-
if (!array.length)
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
3324
|
+
if (!array.length) {
|
|
3325
|
+
array = [DB_1.DB.raw(this.$constants("NULL"))];
|
|
3326
|
+
}
|
|
3327
|
+
const blueprint = this._getBlueprintByColumn(String(column));
|
|
3328
|
+
if (blueprint?.isVirtual) {
|
|
3329
|
+
const values = array.length ? `${array
|
|
3330
|
+
.map((value) => {
|
|
3331
|
+
return this.$utils.formatQueryValue(value);
|
|
3332
|
+
})
|
|
3333
|
+
.join(",")}`
|
|
3334
|
+
: this.$constants(this.$constants("NULL"));
|
|
3335
|
+
const sql = blueprint.sql?.where;
|
|
3336
|
+
if (sql) {
|
|
3337
|
+
return this.whereRaw(`${sql} ${this.$constants("NOT_IN")} (${values})`);
|
|
3338
|
+
}
|
|
3339
|
+
}
|
|
2746
3340
|
this.$state.set("WHERE", [
|
|
2747
|
-
...
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
`${this
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
3341
|
+
...this.$state.get("WHERE"),
|
|
3342
|
+
{
|
|
3343
|
+
column: this.bindColumn(String(column)),
|
|
3344
|
+
operator: `${this.$constants("NOT_IN")}`,
|
|
3345
|
+
value: array
|
|
3346
|
+
.map((value) => {
|
|
3347
|
+
return this.$utils.formatQueryValue(value);
|
|
3348
|
+
})
|
|
3349
|
+
}
|
|
2756
3350
|
]);
|
|
2757
3351
|
return this;
|
|
2758
3352
|
}
|
|
@@ -2765,22 +3359,33 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2765
3359
|
orWhereNotIn(column, array) {
|
|
2766
3360
|
if (!Array.isArray(array))
|
|
2767
3361
|
array = [array];
|
|
2768
|
-
if (!array.length)
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
3362
|
+
if (!array.length) {
|
|
3363
|
+
array = [DB_1.DB.raw(this.$constants("NULL"))];
|
|
3364
|
+
}
|
|
3365
|
+
const blueprint = this._getBlueprintByColumn(String(column));
|
|
3366
|
+
if (blueprint?.isVirtual) {
|
|
3367
|
+
const values = array.length ? `${array
|
|
3368
|
+
.map((value) => {
|
|
3369
|
+
return this.$utils.formatQueryValue(value);
|
|
3370
|
+
})
|
|
3371
|
+
.join(",")}`
|
|
3372
|
+
: this.$constants(this.$constants("NULL"));
|
|
3373
|
+
const sql = blueprint.sql?.where;
|
|
3374
|
+
if (sql) {
|
|
3375
|
+
return this.orWhereRaw(`${sql} ${this.$constants("NOT_IN")} (${values})`);
|
|
3376
|
+
}
|
|
3377
|
+
}
|
|
2774
3378
|
this.$state.set("WHERE", [
|
|
2775
|
-
...
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
`${this
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
3379
|
+
...this.$state.get("WHERE"),
|
|
3380
|
+
{
|
|
3381
|
+
column: this.bindColumn(String(column)),
|
|
3382
|
+
operator: `${this.$constants("NOT_IN")}`,
|
|
3383
|
+
condition: 'OR',
|
|
3384
|
+
value: array
|
|
3385
|
+
.map((value) => {
|
|
3386
|
+
return this.$utils.formatQueryValue(value);
|
|
3387
|
+
})
|
|
3388
|
+
}
|
|
2784
3389
|
]);
|
|
2785
3390
|
return this;
|
|
2786
3391
|
}
|
|
@@ -2794,17 +3399,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2794
3399
|
if (subQuery instanceof Model && !subQuery.$state.get("SELECT").length) {
|
|
2795
3400
|
subQuery.select("id");
|
|
2796
3401
|
}
|
|
2797
|
-
const wheres = this.$state.get("WHERE");
|
|
2798
3402
|
this.$state.set("WHERE", [
|
|
2799
|
-
...
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
]
|
|
2806
|
-
.join(" ")
|
|
2807
|
-
.replace(/^\s+/, ""),
|
|
3403
|
+
...this.$state.get("WHERE"),
|
|
3404
|
+
{
|
|
3405
|
+
column: this.bindColumn(String(column)),
|
|
3406
|
+
operator: options.operator,
|
|
3407
|
+
value: `(${subQuery})`
|
|
3408
|
+
}
|
|
2808
3409
|
]);
|
|
2809
3410
|
return this;
|
|
2810
3411
|
}
|
|
@@ -2818,17 +3419,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2818
3419
|
if (subQuery instanceof Model && !subQuery.$state.get("SELECT").length) {
|
|
2819
3420
|
subQuery.select("id");
|
|
2820
3421
|
}
|
|
2821
|
-
const wheres = this.$state.get("WHERE");
|
|
2822
3422
|
this.$state.set("WHERE", [
|
|
2823
|
-
...
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
]
|
|
2830
|
-
.join(" ")
|
|
2831
|
-
.replace(/^\s+/, ""),
|
|
3423
|
+
...this.$state.get("WHERE"),
|
|
3424
|
+
{
|
|
3425
|
+
column: this.bindColumn(String(column)),
|
|
3426
|
+
operator: options.operator,
|
|
3427
|
+
value: `(${subQuery})`
|
|
3428
|
+
}
|
|
2832
3429
|
]);
|
|
2833
3430
|
return this;
|
|
2834
3431
|
}
|
|
@@ -2842,17 +3439,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2842
3439
|
if (subQuery instanceof Model && !subQuery.$state.get("SELECT").length) {
|
|
2843
3440
|
subQuery.select("id");
|
|
2844
3441
|
}
|
|
2845
|
-
const wheres = this.$state.get("WHERE");
|
|
2846
3442
|
this.$state.set("WHERE", [
|
|
2847
|
-
...
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
`(${subQuery})
|
|
2853
|
-
|
|
2854
|
-
.join(" ")
|
|
2855
|
-
.replace(/^\s+/, ""),
|
|
3443
|
+
...this.$state.get("WHERE"),
|
|
3444
|
+
{
|
|
3445
|
+
column: this.bindColumn(String(column)),
|
|
3446
|
+
operator: options.operator,
|
|
3447
|
+
condition: 'OR',
|
|
3448
|
+
value: `(${subQuery})`
|
|
3449
|
+
}
|
|
2856
3450
|
]);
|
|
2857
3451
|
return this;
|
|
2858
3452
|
}
|
|
@@ -2866,17 +3460,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2866
3460
|
if (subQuery instanceof Model && !subQuery.$state.get("SELECT").length) {
|
|
2867
3461
|
subQuery.select("id");
|
|
2868
3462
|
}
|
|
2869
|
-
const wheres = this.$state.get("WHERE");
|
|
2870
3463
|
this.$state.set("WHERE", [
|
|
2871
|
-
...
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
`(${subQuery})
|
|
2877
|
-
|
|
2878
|
-
.join(" ")
|
|
2879
|
-
.replace(/^\s+/, ""),
|
|
3464
|
+
...this.$state.get("WHERE"),
|
|
3465
|
+
{
|
|
3466
|
+
column: this.bindColumn(String(column)),
|
|
3467
|
+
operator: options.operator,
|
|
3468
|
+
condition: 'OR',
|
|
3469
|
+
value: `(${subQuery})`
|
|
3470
|
+
}
|
|
2880
3471
|
]);
|
|
2881
3472
|
return this;
|
|
2882
3473
|
}
|
|
@@ -2887,36 +3478,24 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2887
3478
|
* @returns {this}
|
|
2888
3479
|
*/
|
|
2889
3480
|
whereBetween(column, array) {
|
|
2890
|
-
const wheres = this.$state.get("WHERE");
|
|
2891
3481
|
if (!array.length) {
|
|
2892
|
-
this
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
wheres.length ? `${this.$constants("AND")}` : "",
|
|
2896
|
-
`${this.bindColumn(String(column))}`,
|
|
2897
|
-
`${this.$constants("BETWEEN")}`,
|
|
2898
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2899
|
-
`${this.$constants("AND")}`,
|
|
2900
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2901
|
-
]
|
|
2902
|
-
.join(" ")
|
|
2903
|
-
.replace(/^\s+/, ""),
|
|
3482
|
+
return this.whereBetween(column, [
|
|
3483
|
+
DB_1.DB.raw(this.$constants("NULL")),
|
|
3484
|
+
DB_1.DB.raw(this.$constants("NULL"))
|
|
2904
3485
|
]);
|
|
2905
|
-
return this;
|
|
2906
3486
|
}
|
|
2907
|
-
|
|
3487
|
+
let [value1, value2] = array;
|
|
2908
3488
|
this.$state.set("WHERE", [
|
|
2909
|
-
...
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
`${this
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
.replace(/^\s+/, ""),
|
|
3489
|
+
...this.$state.get("WHERE"),
|
|
3490
|
+
{
|
|
3491
|
+
column: this.bindColumn(String(column)),
|
|
3492
|
+
operator: `${this.$constants("BETWEEN")}`,
|
|
3493
|
+
value: [
|
|
3494
|
+
`${this.$utils.formatQueryValue(value1)}`,
|
|
3495
|
+
`${this.$constants("AND")}`,
|
|
3496
|
+
`${this.$utils.formatQueryValue(value2)}`,
|
|
3497
|
+
].join(" ")
|
|
3498
|
+
}
|
|
2920
3499
|
]);
|
|
2921
3500
|
return this;
|
|
2922
3501
|
}
|
|
@@ -2927,36 +3506,22 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2927
3506
|
* @returns {this}
|
|
2928
3507
|
*/
|
|
2929
3508
|
orWhereBetween(column, array) {
|
|
2930
|
-
const wheres = this.$state.get("WHERE");
|
|
2931
3509
|
if (!array.length) {
|
|
2932
|
-
this
|
|
2933
|
-
...wheres,
|
|
2934
|
-
[
|
|
2935
|
-
wheres.length ? `${this.$constants("OR")}` : "",
|
|
2936
|
-
`${this.bindColumn(String(column))}`,
|
|
2937
|
-
`${this.$constants("BETWEEN")}`,
|
|
2938
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2939
|
-
`${this.$constants("AND")}`,
|
|
2940
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2941
|
-
]
|
|
2942
|
-
.join(" ")
|
|
2943
|
-
.replace(/^\s+/, ""),
|
|
2944
|
-
]);
|
|
2945
|
-
return this;
|
|
3510
|
+
return this.orWhereBetween(column, [DB_1.DB.raw(this.$constants("NULL")), DB_1.DB.raw(this.$constants("NULL"))]);
|
|
2946
3511
|
}
|
|
2947
|
-
|
|
3512
|
+
let [value1, value2] = array;
|
|
2948
3513
|
this.$state.set("WHERE", [
|
|
2949
|
-
...
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
`${this
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
.join(" ")
|
|
2959
|
-
|
|
3514
|
+
...this.$state.get("WHERE"),
|
|
3515
|
+
{
|
|
3516
|
+
column: this.bindColumn(String(column)),
|
|
3517
|
+
operator: `${this.$constants("BETWEEN")}`,
|
|
3518
|
+
condition: 'OR',
|
|
3519
|
+
value: [
|
|
3520
|
+
`${this.$utils.formatQueryValue(value1)}`,
|
|
3521
|
+
`${this.$constants("AND")}`,
|
|
3522
|
+
`${this.$utils.formatQueryValue(value2)}`
|
|
3523
|
+
].join(" ")
|
|
3524
|
+
}
|
|
2960
3525
|
]);
|
|
2961
3526
|
return this;
|
|
2962
3527
|
}
|
|
@@ -2967,36 +3532,24 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2967
3532
|
* @returns {this}
|
|
2968
3533
|
*/
|
|
2969
3534
|
whereNotBetween(column, array) {
|
|
2970
|
-
const wheres = this.$state.get("WHERE");
|
|
2971
3535
|
if (!array.length) {
|
|
2972
|
-
this
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
wheres.length ? `${this.$constants("AND")}` : "",
|
|
2976
|
-
`${this.bindColumn(String(column))}`,
|
|
2977
|
-
`${this.$constants("NOT_BETWEEN")}`,
|
|
2978
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2979
|
-
`${this.$constants("AND")}`,
|
|
2980
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
2981
|
-
]
|
|
2982
|
-
.join(" ")
|
|
2983
|
-
.replace(/^\s+/, ""),
|
|
3536
|
+
return this.whereNotBetween(column, [
|
|
3537
|
+
DB_1.DB.raw(this.$constants("NULL")),
|
|
3538
|
+
DB_1.DB.raw(this.$constants("NULL"))
|
|
2984
3539
|
]);
|
|
2985
|
-
return this;
|
|
2986
3540
|
}
|
|
2987
3541
|
const [value1, value2] = array;
|
|
2988
3542
|
this.$state.set("WHERE", [
|
|
2989
|
-
...
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
`${this
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
.replace(/^\s+/, ""),
|
|
3543
|
+
...this.$state.get("WHERE"),
|
|
3544
|
+
{
|
|
3545
|
+
column: this.bindColumn(String(column)),
|
|
3546
|
+
operator: `${this.$constants("NOT_BETWEEN")}`,
|
|
3547
|
+
value: [
|
|
3548
|
+
`${this.$utils.formatQueryValue(value1)}`,
|
|
3549
|
+
`${this.$constants("AND")}`,
|
|
3550
|
+
`${this.$utils.formatQueryValue(value2)}`,
|
|
3551
|
+
].join(" ")
|
|
3552
|
+
}
|
|
3000
3553
|
]);
|
|
3001
3554
|
return this;
|
|
3002
3555
|
}
|
|
@@ -3007,36 +3560,25 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3007
3560
|
* @returns {this}
|
|
3008
3561
|
*/
|
|
3009
3562
|
orWhereNotBetween(column, array) {
|
|
3010
|
-
const wheres = this.$state.get("WHERE");
|
|
3011
3563
|
if (!array.length) {
|
|
3012
|
-
this
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
wheres.length ? `${this.$constants("OR")}` : "",
|
|
3016
|
-
`${this.bindColumn(String(column))}`,
|
|
3017
|
-
`${this.$constants("NOT_BETWEEN")}`,
|
|
3018
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
3019
|
-
`${this.$constants("AND")}`,
|
|
3020
|
-
`${this.$constants(this.$constants("NULL"))}`,
|
|
3021
|
-
]
|
|
3022
|
-
.join(" ")
|
|
3023
|
-
.replace(/^\s+/, ""),
|
|
3564
|
+
return this.orWhereNotBetween(column, [
|
|
3565
|
+
DB_1.DB.raw(this.$constants("NULL")),
|
|
3566
|
+
DB_1.DB.raw(this.$constants("NULL"))
|
|
3024
3567
|
]);
|
|
3025
|
-
return this;
|
|
3026
3568
|
}
|
|
3027
3569
|
const [value1, value2] = array;
|
|
3028
3570
|
this.$state.set("WHERE", [
|
|
3029
3571
|
...this.$state.get("WHERE"),
|
|
3030
|
-
|
|
3031
|
-
this
|
|
3032
|
-
`${this
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
.join(" ")
|
|
3039
|
-
|
|
3572
|
+
{
|
|
3573
|
+
column: this.bindColumn(String(column)),
|
|
3574
|
+
operator: `${this.$constants("NOT_BETWEEN")}`,
|
|
3575
|
+
condition: 'OR',
|
|
3576
|
+
value: [
|
|
3577
|
+
`${this.$utils.formatQueryValue(value1)}`,
|
|
3578
|
+
`${this.$constants("AND")}`,
|
|
3579
|
+
`${this.$utils.formatQueryValue(value2)}`,
|
|
3580
|
+
].join(" ")
|
|
3581
|
+
}
|
|
3040
3582
|
]);
|
|
3041
3583
|
return this;
|
|
3042
3584
|
}
|
|
@@ -3046,16 +3588,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3046
3588
|
* @returns {this}
|
|
3047
3589
|
*/
|
|
3048
3590
|
whereNull(column) {
|
|
3049
|
-
const wheres = this.$state.get("WHERE");
|
|
3050
3591
|
this.$state.set("WHERE", [
|
|
3051
|
-
...
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
`${this
|
|
3055
|
-
|
|
3056
|
-
]
|
|
3057
|
-
.join(" ")
|
|
3058
|
-
.replace(/^\s+/, ""),
|
|
3592
|
+
...this.$state.get("WHERE"),
|
|
3593
|
+
{
|
|
3594
|
+
column: this.bindColumn(String(column)),
|
|
3595
|
+
operator: `${this.$constants("IS_NULL")}`,
|
|
3596
|
+
}
|
|
3059
3597
|
]);
|
|
3060
3598
|
return this;
|
|
3061
3599
|
}
|
|
@@ -3065,16 +3603,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3065
3603
|
* @returns {this}
|
|
3066
3604
|
*/
|
|
3067
3605
|
orWhereNull(column) {
|
|
3068
|
-
const wheres = this.$state.get("WHERE");
|
|
3069
3606
|
this.$state.set("WHERE", [
|
|
3070
|
-
...
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
`${this
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
.join(" ")
|
|
3077
|
-
.replace(/^\s+/, ""),
|
|
3607
|
+
...this.$state.get("WHERE"),
|
|
3608
|
+
{
|
|
3609
|
+
column: this.bindColumn(String(column)),
|
|
3610
|
+
operator: `${this.$constants("IS_NULL")}`,
|
|
3611
|
+
condition: 'OR'
|
|
3612
|
+
}
|
|
3078
3613
|
]);
|
|
3079
3614
|
return this;
|
|
3080
3615
|
}
|
|
@@ -3084,16 +3619,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3084
3619
|
* @returns {this}
|
|
3085
3620
|
*/
|
|
3086
3621
|
whereNotNull(column) {
|
|
3087
|
-
const wheres = this.$state.get("WHERE");
|
|
3088
3622
|
this.$state.set("WHERE", [
|
|
3089
|
-
...
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
`${this
|
|
3093
|
-
|
|
3094
|
-
]
|
|
3095
|
-
.join(" ")
|
|
3096
|
-
.replace(/^\s+/, ""),
|
|
3623
|
+
...this.$state.get("WHERE"),
|
|
3624
|
+
{
|
|
3625
|
+
column: this.bindColumn(String(column)),
|
|
3626
|
+
operator: `${this.$constants("IS_NOT_NULL")}`,
|
|
3627
|
+
}
|
|
3097
3628
|
]);
|
|
3098
3629
|
return this;
|
|
3099
3630
|
}
|
|
@@ -3103,16 +3634,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3103
3634
|
* @returns {this}
|
|
3104
3635
|
*/
|
|
3105
3636
|
orWhereNotNull(column) {
|
|
3106
|
-
const wheres = this.$state.get("WHERE");
|
|
3107
3637
|
this.$state.set("WHERE", [
|
|
3108
|
-
...
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
`${this
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
.join(" ")
|
|
3115
|
-
.replace(/^\s+/, ""),
|
|
3638
|
+
...this.$state.get("WHERE"),
|
|
3639
|
+
{
|
|
3640
|
+
column: this.bindColumn(String(column)),
|
|
3641
|
+
operator: `${this.$constants("IS_NOT_NULL")}`,
|
|
3642
|
+
condition: 'OR'
|
|
3643
|
+
}
|
|
3116
3644
|
]);
|
|
3117
3645
|
return this;
|
|
3118
3646
|
}
|
|
@@ -3125,20 +3653,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3125
3653
|
*/
|
|
3126
3654
|
whereSensitive(column, operator, value) {
|
|
3127
3655
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
3128
|
-
value = this.$utils.escape(value);
|
|
3129
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
3130
|
-
const wheres = this.$state.get("WHERE");
|
|
3131
3656
|
this.$state.set("WHERE", [
|
|
3132
|
-
...
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
`${this.
|
|
3137
|
-
|
|
3138
|
-
`${this.$utils.transfromValueHasRaw(this.$utils.escape(value))}`,
|
|
3139
|
-
]
|
|
3140
|
-
.join(" ")
|
|
3141
|
-
.replace(/^\s+/, ""),
|
|
3657
|
+
...this.$state.get("WHERE"),
|
|
3658
|
+
{
|
|
3659
|
+
column: `${this.$constants("BINARY")} ${this.bindColumn(String(column))}`,
|
|
3660
|
+
operator,
|
|
3661
|
+
value: `${this.$utils.formatQueryValue(value)}`
|
|
3662
|
+
}
|
|
3142
3663
|
]);
|
|
3143
3664
|
return this;
|
|
3144
3665
|
}
|
|
@@ -3151,20 +3672,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3151
3672
|
*/
|
|
3152
3673
|
whereStrict(column, operator, value) {
|
|
3153
3674
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
3154
|
-
value = this.$utils.escape(value);
|
|
3155
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
3156
|
-
const wheres = this.$state.get("WHERE");
|
|
3157
3675
|
this.$state.set("WHERE", [
|
|
3158
|
-
...
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
`${this.
|
|
3163
|
-
|
|
3164
|
-
`${this.$utils.transfromValueHasRaw(this.$utils.escape(value))}`,
|
|
3165
|
-
]
|
|
3166
|
-
.join(" ")
|
|
3167
|
-
.replace(/^\s+/, ""),
|
|
3676
|
+
...this.$state.get("WHERE"),
|
|
3677
|
+
{
|
|
3678
|
+
column: `${this.$constants("BINARY")} ${this.bindColumn(String(column))}`,
|
|
3679
|
+
operator,
|
|
3680
|
+
value: `${this.$utils.formatQueryValue(value)}`,
|
|
3681
|
+
}
|
|
3168
3682
|
]);
|
|
3169
3683
|
return this;
|
|
3170
3684
|
}
|
|
@@ -3177,20 +3691,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3177
3691
|
*/
|
|
3178
3692
|
orWhereSensitive(column, operator, value) {
|
|
3179
3693
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
3180
|
-
value = this.$utils.escape(value);
|
|
3181
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
3182
|
-
const wheres = this.$state.get("WHERE");
|
|
3183
3694
|
this.$state.set("WHERE", [
|
|
3184
|
-
...
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
`${
|
|
3190
|
-
|
|
3191
|
-
]
|
|
3192
|
-
.join(" ")
|
|
3193
|
-
.replace(/^\s+/, ""),
|
|
3695
|
+
...this.$state.get("WHERE"),
|
|
3696
|
+
{
|
|
3697
|
+
column: `${this.$constants("BINARY")} ${this.bindColumn(String(column))}`,
|
|
3698
|
+
operator,
|
|
3699
|
+
condition: 'OR',
|
|
3700
|
+
value: `${this.$utils.formatQueryValue(value)}`,
|
|
3701
|
+
}
|
|
3194
3702
|
]);
|
|
3195
3703
|
return this;
|
|
3196
3704
|
}
|
|
@@ -3240,16 +3748,29 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3240
3748
|
const where = repository?.$state.get("WHERE") || [];
|
|
3241
3749
|
if (!where.length)
|
|
3242
3750
|
return this;
|
|
3243
|
-
const query = where.join(" ");
|
|
3244
|
-
const wheres = this.$state.get("WHERE");
|
|
3245
3751
|
this.$state.set("WHERE", [
|
|
3246
|
-
...
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3752
|
+
...this.$state.get("WHERE"),
|
|
3753
|
+
...this.$utils.nestConditions(repository?.$state.get("WHERE"))
|
|
3754
|
+
]);
|
|
3755
|
+
return this;
|
|
3756
|
+
}
|
|
3757
|
+
/**
|
|
3758
|
+
* @override
|
|
3759
|
+
* @param {Function} callback callback query
|
|
3760
|
+
* @returns {this}
|
|
3761
|
+
*/
|
|
3762
|
+
orWhereQuery(callback) {
|
|
3763
|
+
const copy = new Model().copyModel(this);
|
|
3764
|
+
const repository = callback(copy);
|
|
3765
|
+
if (repository instanceof Promise) {
|
|
3766
|
+
throw this._assertError('The "orWhereQuery" method is not supported a Promise');
|
|
3767
|
+
}
|
|
3768
|
+
if (!(repository instanceof Model)) {
|
|
3769
|
+
throw this._assertError(`Unknown callback query: '${repository}'`);
|
|
3770
|
+
}
|
|
3771
|
+
this.$state.set("WHERE", [
|
|
3772
|
+
...this.$state.get("WHERE"),
|
|
3773
|
+
...this.$utils.nestConditions(repository?.$state.get("WHERE"), 'OR')
|
|
3253
3774
|
]);
|
|
3254
3775
|
return this;
|
|
3255
3776
|
}
|
|
@@ -3262,8 +3783,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3262
3783
|
*/
|
|
3263
3784
|
whereAny(columns, operator, value) {
|
|
3264
3785
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
3265
|
-
value = this.$utils.escape(value);
|
|
3266
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
3267
3786
|
this.whereQuery((query) => {
|
|
3268
3787
|
for (const index in columns) {
|
|
3269
3788
|
const column = String(columns[index]);
|
|
@@ -3290,8 +3809,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3290
3809
|
*/
|
|
3291
3810
|
whereAll(columns, operator, value) {
|
|
3292
3811
|
[value, operator] = this.$utils.valueAndOperator(value, operator, arguments.length === 2);
|
|
3293
|
-
value = this.$utils.escape(value);
|
|
3294
|
-
value = this.$utils.transfromBooleanToNumber(value);
|
|
3295
3812
|
this.whereQuery((query) => {
|
|
3296
3813
|
for (const key in columns) {
|
|
3297
3814
|
const column = String(columns[key]);
|
|
@@ -3361,11 +3878,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3361
3878
|
* @returns {this}
|
|
3362
3879
|
*/
|
|
3363
3880
|
join(localKey, referenceKey) {
|
|
3364
|
-
if (
|
|
3365
|
-
|
|
3366
|
-
return this
|
|
3881
|
+
if (typeof localKey === 'string') {
|
|
3882
|
+
this._handleJoin("INNER_JOIN", localKey, referenceKey);
|
|
3883
|
+
return this;
|
|
3884
|
+
}
|
|
3885
|
+
if (typeof localKey === "function" && !this._isModel(localKey)) {
|
|
3886
|
+
const cb = localKey;
|
|
3887
|
+
const callback = cb(new Join_1.Join(this, "INNER_JOIN"));
|
|
3888
|
+
this.$state.set("JOIN", [
|
|
3889
|
+
...this.$state.get("JOIN"),
|
|
3890
|
+
callback["toString"](),
|
|
3891
|
+
]);
|
|
3892
|
+
return this;
|
|
3367
3893
|
}
|
|
3368
|
-
this.
|
|
3894
|
+
this.joinModel(localKey, referenceKey);
|
|
3369
3895
|
return this;
|
|
3370
3896
|
}
|
|
3371
3897
|
/**
|
|
@@ -3376,11 +3902,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3376
3902
|
* @returns {this}
|
|
3377
3903
|
*/
|
|
3378
3904
|
rightJoin(localKey, referenceKey) {
|
|
3379
|
-
if (
|
|
3380
|
-
|
|
3381
|
-
return this
|
|
3905
|
+
if (typeof localKey === 'string') {
|
|
3906
|
+
this._handleJoin("RIGHT_JOIN", localKey, referenceKey);
|
|
3907
|
+
return this;
|
|
3908
|
+
}
|
|
3909
|
+
if (typeof localKey === "function" && !this._isModel(localKey)) {
|
|
3910
|
+
const cb = localKey;
|
|
3911
|
+
const callback = cb(new Join_1.Join(this, "RIGHT_JOIN"));
|
|
3912
|
+
this.$state.set("JOIN", [
|
|
3913
|
+
...this.$state.get("JOIN"),
|
|
3914
|
+
callback["toString"](),
|
|
3915
|
+
]);
|
|
3916
|
+
return this;
|
|
3382
3917
|
}
|
|
3383
|
-
this.
|
|
3918
|
+
this.rightJoinModel(localKey, referenceKey);
|
|
3384
3919
|
return this;
|
|
3385
3920
|
}
|
|
3386
3921
|
/**
|
|
@@ -3391,11 +3926,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3391
3926
|
* @returns {this}
|
|
3392
3927
|
*/
|
|
3393
3928
|
leftJoin(localKey, referenceKey) {
|
|
3394
|
-
if (
|
|
3395
|
-
|
|
3396
|
-
return this
|
|
3929
|
+
if (typeof localKey === 'string') {
|
|
3930
|
+
this._handleJoin("LEFT_JOIN", localKey, referenceKey);
|
|
3931
|
+
return this;
|
|
3932
|
+
}
|
|
3933
|
+
if (typeof localKey === "function" && !this._isModel(localKey)) {
|
|
3934
|
+
const cb = localKey;
|
|
3935
|
+
const callback = cb(new Join_1.Join(this, "LEFT_JOIN"));
|
|
3936
|
+
this.$state.set("JOIN", [
|
|
3937
|
+
...this.$state.get("JOIN"),
|
|
3938
|
+
callback["toString"](),
|
|
3939
|
+
]);
|
|
3940
|
+
return this;
|
|
3397
3941
|
}
|
|
3398
|
-
this.
|
|
3942
|
+
this.leftJoinModel(localKey, referenceKey);
|
|
3399
3943
|
return this;
|
|
3400
3944
|
}
|
|
3401
3945
|
/**
|
|
@@ -3405,11 +3949,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3405
3949
|
* @returns {this}
|
|
3406
3950
|
*/
|
|
3407
3951
|
crossJoin(localKey, referenceKey) {
|
|
3408
|
-
if (
|
|
3409
|
-
|
|
3410
|
-
return this
|
|
3952
|
+
if (typeof localKey === 'string') {
|
|
3953
|
+
this._handleJoin("CROSS_JOIN", localKey, referenceKey);
|
|
3954
|
+
return this;
|
|
3955
|
+
}
|
|
3956
|
+
if (typeof localKey === "function" && !this._isModel(localKey)) {
|
|
3957
|
+
const cb = localKey;
|
|
3958
|
+
const callback = cb(new Join_1.Join(this, "CROSS_JOIN"));
|
|
3959
|
+
this.$state.set("JOIN", [
|
|
3960
|
+
...this.$state.get("JOIN"),
|
|
3961
|
+
callback["toString"](),
|
|
3962
|
+
]);
|
|
3963
|
+
return this;
|
|
3411
3964
|
}
|
|
3412
|
-
this.
|
|
3965
|
+
this.crossJoinModel(localKey, referenceKey);
|
|
3413
3966
|
return this;
|
|
3414
3967
|
}
|
|
3415
3968
|
/**
|
|
@@ -3455,8 +4008,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3455
4008
|
]);
|
|
3456
4009
|
return this;
|
|
3457
4010
|
}
|
|
3458
|
-
if (m2 == null)
|
|
3459
|
-
|
|
4011
|
+
if (this._isModel(m1) && m2 == null) {
|
|
4012
|
+
m2 = m1;
|
|
4013
|
+
m1 = this;
|
|
4014
|
+
}
|
|
3460
4015
|
const { alias1, alias2, table1, table2, model1, model2, localKey, foreignKey, } = this._handleJoinModel(m1, m2);
|
|
3461
4016
|
this.join(`${alias1 === "" ? table1 : `${table1}|${alias1}`}.${localKey}`, `${alias2 === "" ? table2 : `${table2}|${alias2}`}.${foreignKey}`);
|
|
3462
4017
|
if (model1["$state"].get("MODEL_NAME") !== this["$state"].get("MODEL_NAME")) {
|
|
@@ -3519,8 +4074,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3519
4074
|
]);
|
|
3520
4075
|
return this;
|
|
3521
4076
|
}
|
|
3522
|
-
if (m2 == null)
|
|
3523
|
-
|
|
4077
|
+
if (this._isModel(m1) && m2 == null) {
|
|
4078
|
+
m2 = m1;
|
|
4079
|
+
m1 = this;
|
|
4080
|
+
}
|
|
3524
4081
|
const { alias1, alias2, table1, table2, model1, model2, localKey, foreignKey, } = this._handleJoinModel(m1, m2);
|
|
3525
4082
|
this.leftJoin(`${alias1 === "" ? table1 : `${table1}|${alias1}`}.${localKey}`, `${alias2 === "" ? table2 : `${table2}|${alias2}`}.${foreignKey}`);
|
|
3526
4083
|
if (model1["$state"].get("MODEL_NAME") !== this["$state"].get("MODEL_NAME")) {
|
|
@@ -3562,6 +4119,104 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3562
4119
|
}
|
|
3563
4120
|
return this;
|
|
3564
4121
|
}
|
|
4122
|
+
/**
|
|
4123
|
+
*
|
|
4124
|
+
* @override
|
|
4125
|
+
* @param {string=} c [column=id]
|
|
4126
|
+
* @returns {promise<number>}
|
|
4127
|
+
*/
|
|
4128
|
+
async count(c) {
|
|
4129
|
+
await this._prepareQueryPipeline();
|
|
4130
|
+
const distinct = this.$state.get("DISTINCT");
|
|
4131
|
+
let column = c == null ? this.$state.get('PRIMARY_KEY') : String(c);
|
|
4132
|
+
column =
|
|
4133
|
+
column === "*"
|
|
4134
|
+
? "*"
|
|
4135
|
+
: distinct
|
|
4136
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4137
|
+
: `${this.bindColumn(column)}`;
|
|
4138
|
+
this.selectRaw([
|
|
4139
|
+
`${this.$constants("COUNT")}(${column})`,
|
|
4140
|
+
`${this.$constants("AS")}`,
|
|
4141
|
+
"\`aggregate\`",
|
|
4142
|
+
].join(" "));
|
|
4143
|
+
const sql = this._queryBuilder().select();
|
|
4144
|
+
const result = await this._queryStatement(sql);
|
|
4145
|
+
return Number(this._resultHandler(result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) || 0));
|
|
4146
|
+
}
|
|
4147
|
+
/**
|
|
4148
|
+
*
|
|
4149
|
+
* @override
|
|
4150
|
+
* @param {string=} c [column=id]
|
|
4151
|
+
* @returns {promise<number>}
|
|
4152
|
+
*/
|
|
4153
|
+
async avg(c) {
|
|
4154
|
+
await this._prepareQueryPipeline();
|
|
4155
|
+
const distinct = this.$state.get("DISTINCT");
|
|
4156
|
+
let column = c == null ? this.$state.get('PRIMARY_KEY') : String(c);
|
|
4157
|
+
column = distinct
|
|
4158
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4159
|
+
: `${this.bindColumn(column)}`;
|
|
4160
|
+
this.selectRaw(`${this.$constants("AVG")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4161
|
+
const sql = this._queryBuilder().select();
|
|
4162
|
+
const result = await this._queryStatement(sql);
|
|
4163
|
+
return Number(this._resultHandler((result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) ||
|
|
4164
|
+
0) / result.length));
|
|
4165
|
+
}
|
|
4166
|
+
/**
|
|
4167
|
+
*
|
|
4168
|
+
* @override
|
|
4169
|
+
* @param {string=} c [column=id]
|
|
4170
|
+
* @returns {promise<number>}
|
|
4171
|
+
*/
|
|
4172
|
+
async sum(c) {
|
|
4173
|
+
await this._prepareQueryPipeline();
|
|
4174
|
+
let column = c == null ? this.$state.get('PRIMARY_KEY') : String(c);
|
|
4175
|
+
const distinct = this.$state.get("DISTINCT");
|
|
4176
|
+
column = distinct
|
|
4177
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4178
|
+
: `${this.bindColumn(column)}`;
|
|
4179
|
+
this.selectRaw(`${this.$constants("SUM")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4180
|
+
const sql = this._queryBuilder().select();
|
|
4181
|
+
const result = await this._queryStatement(sql);
|
|
4182
|
+
return Number(this._resultHandler(result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) ||
|
|
4183
|
+
0));
|
|
4184
|
+
}
|
|
4185
|
+
/**
|
|
4186
|
+
*
|
|
4187
|
+
* @override
|
|
4188
|
+
* @param {string=} c [column=id]
|
|
4189
|
+
* @returns {promise<number>}
|
|
4190
|
+
*/
|
|
4191
|
+
async max(c) {
|
|
4192
|
+
await this._prepareQueryPipeline();
|
|
4193
|
+
const distinct = this.$state.get("DISTINCT");
|
|
4194
|
+
let column = c == null ? this.$state.get('PRIMARY_KEY') : String(c);
|
|
4195
|
+
column = distinct
|
|
4196
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4197
|
+
: `${this.bindColumn(column)}`;
|
|
4198
|
+
this.selectRaw(`${this.$constants("MAX")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4199
|
+
const sql = this._queryBuilder().select();
|
|
4200
|
+
const result = await this._queryStatement(sql);
|
|
4201
|
+
return Number(this._resultHandler(result.sort((a, b) => b?.aggregate - a?.aggregate)[0]?.aggregate || 0));
|
|
4202
|
+
}
|
|
4203
|
+
/**
|
|
4204
|
+
* @override
|
|
4205
|
+
* @param {string} c
|
|
4206
|
+
* @returns {promise<number>}
|
|
4207
|
+
*/
|
|
4208
|
+
async min(c) {
|
|
4209
|
+
await this._prepareQueryPipeline();
|
|
4210
|
+
const distinct = this.$state.get("DISTINCT");
|
|
4211
|
+
let column = c == null ? "id" : String(c);
|
|
4212
|
+
column = distinct
|
|
4213
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4214
|
+
: `${this.bindColumn(column)}`;
|
|
4215
|
+
this.selectRaw(`${this.$constants("MIN")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4216
|
+
const sql = this._queryBuilder().select();
|
|
4217
|
+
const result = await this._queryStatement(sql);
|
|
4218
|
+
return Number(this._resultHandler(result.sort((a, b) => a?.aggregate - b?.aggregate)[0]?.aggregate || 0));
|
|
4219
|
+
}
|
|
3565
4220
|
/**
|
|
3566
4221
|
* @override
|
|
3567
4222
|
* @returns {promise<boolean>} promise boolean
|
|
@@ -3572,36 +4227,27 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3572
4227
|
if (this.$state.get("SOFT_DELETE")) {
|
|
3573
4228
|
const deletedAt = this._valuePattern(this.$state.get("SOFT_DELETE_FORMAT"));
|
|
3574
4229
|
await this._runBefore("update");
|
|
3575
|
-
const
|
|
4230
|
+
const sql = new Model()
|
|
3576
4231
|
.copyModel(this, { where: true, limit: true, orderBy: true })
|
|
3577
4232
|
.update({
|
|
3578
4233
|
[deletedAt]: this.$utils.timestamp(),
|
|
3579
4234
|
})
|
|
3580
|
-
.disableSoftDelete()
|
|
3581
4235
|
.bind(this.$pool.get())
|
|
3582
4236
|
.debug(this.$state.get("DEBUG"))
|
|
3583
|
-
.
|
|
4237
|
+
.toString();
|
|
4238
|
+
const r = await this._actionStatement(sql);
|
|
4239
|
+
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
3584
4240
|
await this._observer(result, "updated");
|
|
3585
4241
|
await this._runAfter("update", result);
|
|
3586
4242
|
return Boolean(this._resultHandler(result));
|
|
3587
4243
|
}
|
|
3588
|
-
const PK = this.$state.get("PRIMARY_KEY");
|
|
3589
|
-
const TEMP = "TEMP";
|
|
3590
|
-
const from = new Model()
|
|
3591
|
-
.copyModel(this, { where: true, limit: true, orderBy: true })
|
|
3592
|
-
.selectRaw(PK);
|
|
3593
|
-
this
|
|
3594
|
-
.unset({ where: true, limit: true })
|
|
3595
|
-
.select(PK)
|
|
3596
|
-
.whereSubQuery(PK, new Model().from(DB_1.DB.raw(`
|
|
3597
|
-
(${from}) ${this.$constants("AS")} ${TEMP}`)).selectRaw(PK));
|
|
3598
4244
|
this.$state.set("DELETE", true);
|
|
3599
4245
|
await this._runBefore("remove");
|
|
3600
|
-
const
|
|
3601
|
-
const
|
|
3602
|
-
await this._observer(
|
|
3603
|
-
await this._runAfter("remove",
|
|
3604
|
-
return
|
|
4246
|
+
const r = await this._actionStatement(this._queryBuilder().remove());
|
|
4247
|
+
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
4248
|
+
await this._observer(result, "deleted");
|
|
4249
|
+
await this._runAfter("remove", result);
|
|
4250
|
+
return result;
|
|
3605
4251
|
}
|
|
3606
4252
|
/**
|
|
3607
4253
|
* @override
|
|
@@ -3612,36 +4258,27 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3612
4258
|
if (this.$state.get("SOFT_DELETE")) {
|
|
3613
4259
|
const deletedAt = this._valuePattern(this.$state.get("SOFT_DELETE_FORMAT"));
|
|
3614
4260
|
await this._runBefore("update");
|
|
3615
|
-
const
|
|
3616
|
-
.copyModel(this, { where: true,
|
|
4261
|
+
const sql = new Model()
|
|
4262
|
+
.copyModel(this, { where: true, orderBy: true })
|
|
3617
4263
|
.updateMany({
|
|
3618
4264
|
[deletedAt]: this.$utils.timestamp(),
|
|
3619
4265
|
})
|
|
3620
|
-
.disableSoftDelete()
|
|
3621
|
-
.debug(this.$state.get("DEBUG"))
|
|
3622
4266
|
.bind(this.$pool.get())
|
|
3623
|
-
.
|
|
4267
|
+
.debug(this.$state.get("DEBUG"))
|
|
4268
|
+
.toString();
|
|
4269
|
+
const r = await this._actionStatement(sql);
|
|
4270
|
+
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
3624
4271
|
await this._observer(result, "updated");
|
|
3625
4272
|
await this._runAfter("update", result);
|
|
3626
4273
|
return Boolean(this._resultHandler(result));
|
|
3627
4274
|
}
|
|
3628
|
-
const PK = this.$state.get("PRIMARY_KEY");
|
|
3629
|
-
const TEMP = "TEMP";
|
|
3630
|
-
const from = new Model()
|
|
3631
|
-
.copyModel(this, { where: true, limit: true, orderBy: true })
|
|
3632
|
-
.selectRaw(PK);
|
|
3633
|
-
this
|
|
3634
|
-
.unset({ where: true, limit: true })
|
|
3635
|
-
.select(PK)
|
|
3636
|
-
.whereSubQuery(PK, new Model().from(DB_1.DB.raw(`
|
|
3637
|
-
(${from}) ${this.$constants("AS")} ${TEMP}`)).selectRaw(PK));
|
|
3638
4275
|
this.$state.set("DELETE", true);
|
|
3639
4276
|
await this._runBefore("remove");
|
|
3640
|
-
const
|
|
3641
|
-
const
|
|
3642
|
-
await this._observer(
|
|
3643
|
-
await this._runAfter("remove",
|
|
3644
|
-
return
|
|
4277
|
+
const r = await this._actionStatement(this._queryBuilder().remove());
|
|
4278
|
+
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
4279
|
+
await this._observer(result, "deleted");
|
|
4280
|
+
await this._runAfter("remove", result);
|
|
4281
|
+
return result;
|
|
3645
4282
|
}
|
|
3646
4283
|
/**
|
|
3647
4284
|
*
|
|
@@ -3654,16 +4291,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3654
4291
|
*/
|
|
3655
4292
|
async forceDelete() {
|
|
3656
4293
|
this.disableSoftDelete();
|
|
3657
|
-
const PK = this.$state.get(
|
|
3658
|
-
const TEMP =
|
|
4294
|
+
const PK = this.$state.get("PRIMARY_KEY");
|
|
4295
|
+
const TEMP = "TEMP";
|
|
3659
4296
|
const from = new Model()
|
|
3660
4297
|
.copyModel(this, { where: true, limit: true, orderBy: true })
|
|
3661
4298
|
.selectRaw(PK);
|
|
3662
|
-
this
|
|
3663
|
-
.unset({ where: true, limit: true })
|
|
4299
|
+
this.unset({ where: true, limit: true })
|
|
3664
4300
|
.select(PK)
|
|
3665
|
-
.whereSubQuery(PK, new Model()
|
|
3666
|
-
|
|
4301
|
+
.whereSubQuery(PK, new Model()
|
|
4302
|
+
.from(DB_1.DB.raw(`(${from}) AS ${TEMP}`))
|
|
4303
|
+
.selectRaw(PK));
|
|
3667
4304
|
this.$state.set("DELETE", true);
|
|
3668
4305
|
const result = await this._actionStatement(this._queryBuilder().remove());
|
|
3669
4306
|
const r = Boolean(this._resultHandler(result?.$meta?.affected || false));
|
|
@@ -3709,7 +4346,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3709
4346
|
async toArray(column) {
|
|
3710
4347
|
if (column == null)
|
|
3711
4348
|
column = "id";
|
|
3712
|
-
this.selectRaw(`${this.bindColumn(column)}`);
|
|
3713
4349
|
const sql = this._queryBuilder().select();
|
|
3714
4350
|
const result = await this._queryStatement(sql);
|
|
3715
4351
|
const toArray = result.map((data) => data[column]);
|
|
@@ -3745,6 +4381,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3745
4381
|
return this._resultHandler(undefined);
|
|
3746
4382
|
await this._prepareQueryPipeline();
|
|
3747
4383
|
this.limit(1);
|
|
4384
|
+
this._ensureRelationOwnerKeysSelected();
|
|
3748
4385
|
let sql = this._queryBuilder().select();
|
|
3749
4386
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
3750
4387
|
sql = String(this.$relation.loadExists());
|
|
@@ -3768,6 +4405,15 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3768
4405
|
async findOne(cb) {
|
|
3769
4406
|
return await this.first(cb);
|
|
3770
4407
|
}
|
|
4408
|
+
/**
|
|
4409
|
+
* @override
|
|
4410
|
+
* @param {number} id callback function return query sql
|
|
4411
|
+
* @returns {promise<Record<string,any> | null>} Record | null
|
|
4412
|
+
*/
|
|
4413
|
+
async find(primaryKey) {
|
|
4414
|
+
this.where(this.$state.get("PRIMARY_KEY"), primaryKey);
|
|
4415
|
+
return await this.first();
|
|
4416
|
+
}
|
|
3771
4417
|
/**
|
|
3772
4418
|
* @override
|
|
3773
4419
|
* @returns {promise<object | Error>} Record | throw error
|
|
@@ -3777,6 +4423,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3777
4423
|
return this._resultHandler(undefined);
|
|
3778
4424
|
await this._prepareQueryPipeline();
|
|
3779
4425
|
this.limit(1);
|
|
4426
|
+
this._ensureRelationOwnerKeysSelected();
|
|
3780
4427
|
let sql = this._queryBuilder().select();
|
|
3781
4428
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
3782
4429
|
sql = String(this.$relation.loadExists());
|
|
@@ -3805,6 +4452,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3805
4452
|
if (this.$state.get("VOID"))
|
|
3806
4453
|
return [];
|
|
3807
4454
|
await this._prepareQueryPipeline();
|
|
4455
|
+
this._ensureRelationOwnerKeysSelected();
|
|
3808
4456
|
let sql = this._queryBuilder().select();
|
|
3809
4457
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
3810
4458
|
sql = String(this.$relation.loadExists());
|
|
@@ -3831,23 +4479,28 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3831
4479
|
}
|
|
3832
4480
|
/**
|
|
3833
4481
|
* @override
|
|
3834
|
-
* @param {object?}
|
|
3835
|
-
* @property {number}
|
|
3836
|
-
* @property {number}
|
|
4482
|
+
* @param {object?} opts by default page = 1 , limit = 15
|
|
4483
|
+
* @property {number?} opts.limit
|
|
4484
|
+
* @property {number?} opts.page
|
|
4485
|
+
* @property {boolean?} opts.distinct
|
|
3837
4486
|
* @returns {promise<Pagination>} Pagination
|
|
3838
4487
|
*/
|
|
3839
|
-
async pagination(
|
|
4488
|
+
async pagination(opts) {
|
|
3840
4489
|
let limit = 15;
|
|
3841
4490
|
let page = 1;
|
|
3842
|
-
if (
|
|
3843
|
-
|
|
3844
|
-
|
|
4491
|
+
if (opts?.distinct) {
|
|
4492
|
+
this.distinct();
|
|
4493
|
+
}
|
|
4494
|
+
if (opts != null) {
|
|
4495
|
+
limit = this.$utils.softNumber(opts?.limit || limit);
|
|
4496
|
+
page = this.$utils.softNumber(opts?.page || page);
|
|
3845
4497
|
}
|
|
3846
4498
|
await this._prepareQueryPipeline();
|
|
3847
4499
|
const offset = (page - 1) * limit;
|
|
3848
4500
|
this.$state.set("PAGE", page);
|
|
3849
4501
|
this.limit(limit);
|
|
3850
4502
|
this.offset(offset);
|
|
4503
|
+
this._ensureRelationOwnerKeysSelected();
|
|
3851
4504
|
let sql = this._queryBuilder().select();
|
|
3852
4505
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
3853
4506
|
sql = String(this.$relation.loadExists());
|
|
@@ -3859,13 +4512,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3859
4512
|
/**
|
|
3860
4513
|
*
|
|
3861
4514
|
* @override
|
|
3862
|
-
* @param {?object}
|
|
3863
|
-
* @property {number}
|
|
3864
|
-
* @property {number}
|
|
4515
|
+
* @param {?object} opts by default page = 1 , limit = 15
|
|
4516
|
+
* @property {number?} opts.limit
|
|
4517
|
+
* @property {number?} opts.page
|
|
4518
|
+
* @property {boolean?} opts.distinct
|
|
3865
4519
|
* @returns {promise<Pagination>} Pagination
|
|
3866
4520
|
*/
|
|
3867
|
-
async paginate(
|
|
3868
|
-
return await this.pagination(
|
|
4521
|
+
async paginate(opts) {
|
|
4522
|
+
return await this.pagination(opts);
|
|
3869
4523
|
}
|
|
3870
4524
|
/**
|
|
3871
4525
|
* @override
|
|
@@ -3940,11 +4594,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3940
4594
|
* @returns {this} this
|
|
3941
4595
|
*/
|
|
3942
4596
|
insert(data) {
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
}
|
|
3946
|
-
this.$state.set("DATA", data);
|
|
3947
|
-
if (this.$state.get('TRANSFORMS') == null) {
|
|
4597
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4598
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
3948
4599
|
this._queryInsertModel();
|
|
3949
4600
|
}
|
|
3950
4601
|
this.$state.set("SAVE", "INSERT");
|
|
@@ -3956,7 +4607,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3956
4607
|
* @returns {this} this
|
|
3957
4608
|
*/
|
|
3958
4609
|
create(data) {
|
|
3959
|
-
//@ts-ignore
|
|
3960
4610
|
return this.insert(data);
|
|
3961
4611
|
}
|
|
3962
4612
|
/**
|
|
@@ -3984,9 +4634,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3984
4634
|
}
|
|
3985
4635
|
}
|
|
3986
4636
|
}
|
|
3987
|
-
this.$state.set("DATA", data);
|
|
4637
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
3988
4638
|
this.limit(1);
|
|
3989
|
-
if (this.$state.get(
|
|
4639
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
3990
4640
|
this._queryUpdateModel();
|
|
3991
4641
|
}
|
|
3992
4642
|
this.$state.set("SAVE", "UPDATE");
|
|
@@ -4017,25 +4667,25 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4017
4667
|
}
|
|
4018
4668
|
}
|
|
4019
4669
|
}
|
|
4020
|
-
this.$state.set("DATA", data);
|
|
4021
|
-
if (this.$state.get(
|
|
4670
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4671
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4022
4672
|
this._queryUpdateModel();
|
|
4023
4673
|
}
|
|
4024
4674
|
this.$state.set("SAVE", "UPDATE");
|
|
4025
4675
|
return this;
|
|
4026
4676
|
}
|
|
4027
4677
|
/**
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4678
|
+
*
|
|
4679
|
+
* @override
|
|
4680
|
+
* @param {Array<{when: Record<string, string | number | boolean | null | undefined>, columns: Record<string, string | number | boolean | null | undefined>}>>} cases
|
|
4681
|
+
* An array of update cases.
|
|
4682
|
+
* - `when` is an object specifying the conditions to match records.
|
|
4683
|
+
* - `columns` is an object specifying the new values to set for the matched records.
|
|
4684
|
+
*
|
|
4685
|
+
* @property {Record<string,string | number | boolean | null | undefined>} cases.when
|
|
4686
|
+
* @property {Record<string,string | number | boolean | null | undefined>} cases.columns
|
|
4687
|
+
* @returns {this} this
|
|
4688
|
+
*/
|
|
4039
4689
|
updateCases(cases) {
|
|
4040
4690
|
if (!cases.length) {
|
|
4041
4691
|
throw this._assertError("This method must require a non-empty array.");
|
|
@@ -4122,7 +4772,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4122
4772
|
? this.$constants("NULL")
|
|
4123
4773
|
: this.$utils.transfromValueHasRaw(value)}`;
|
|
4124
4774
|
});
|
|
4125
|
-
this.$state.set("DATA", columns);
|
|
4775
|
+
this.$state.set("DATA", this._formatedInputData(columns));
|
|
4126
4776
|
this.$state.set("UPDATE", keyValue);
|
|
4127
4777
|
this.whereRaw("1");
|
|
4128
4778
|
this.void();
|
|
@@ -4149,8 +4799,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4149
4799
|
[column]: this._updateHandler(column, value),
|
|
4150
4800
|
};
|
|
4151
4801
|
}
|
|
4152
|
-
this.$state.set("DATA", data);
|
|
4153
|
-
if (this.$state.get(
|
|
4802
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4803
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4154
4804
|
this._queryUpdateModel();
|
|
4155
4805
|
}
|
|
4156
4806
|
this.$state.set("SAVE", "UPDATE");
|
|
@@ -4166,8 +4816,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4166
4816
|
if (!Object.keys(data).length) {
|
|
4167
4817
|
throw this._assertError("This method must require at least 1 argument.");
|
|
4168
4818
|
}
|
|
4169
|
-
this.$state.set("DATA", data);
|
|
4170
|
-
if (this.$state.get(
|
|
4819
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4820
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4171
4821
|
this._queryUpdateModel();
|
|
4172
4822
|
this._queryInsertModel();
|
|
4173
4823
|
}
|
|
@@ -4207,8 +4857,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4207
4857
|
if (!Object.keys(data).length) {
|
|
4208
4858
|
throw this._assertError("This method must require at least 1 argument.");
|
|
4209
4859
|
}
|
|
4210
|
-
this.$state.set("DATA", data);
|
|
4211
|
-
if (this.$state.get(
|
|
4860
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4861
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4212
4862
|
this._queryInsertModel();
|
|
4213
4863
|
}
|
|
4214
4864
|
this.$state.set("SAVE", "INSERT_OR_SELECT");
|
|
@@ -4232,8 +4882,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4232
4882
|
if (!Object.keys(data).length) {
|
|
4233
4883
|
throw this._assertError("This method must require at least 1 argument.");
|
|
4234
4884
|
}
|
|
4235
|
-
this.$state.set("DATA", data);
|
|
4236
|
-
if (this.$state.get(
|
|
4885
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4886
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4237
4887
|
this._queryInsertModel();
|
|
4238
4888
|
}
|
|
4239
4889
|
this.$state.set("SAVE", "INSERT_NOT_EXISTS");
|
|
@@ -4257,8 +4907,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4257
4907
|
if (!Array.isArray(data) || !data.length) {
|
|
4258
4908
|
throw this._assertError("This method must require a non-empty array.");
|
|
4259
4909
|
}
|
|
4260
|
-
this.$state.set("DATA", data);
|
|
4261
|
-
if (this.$state.get(
|
|
4910
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4911
|
+
if (this.$state.get("TRANSFORMS") == null) {
|
|
4262
4912
|
this._queryInsertMultipleModel();
|
|
4263
4913
|
}
|
|
4264
4914
|
this.$state.set("SAVE", "INSERT_MULTIPLE");
|
|
@@ -4271,7 +4921,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4271
4921
|
* @returns {this} this
|
|
4272
4922
|
*/
|
|
4273
4923
|
createMany(data) {
|
|
4274
|
-
//@ts-ignore
|
|
4275
4924
|
return this.createMultiple(data);
|
|
4276
4925
|
}
|
|
4277
4926
|
/**
|
|
@@ -4281,7 +4930,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4281
4930
|
* @returns {this} this
|
|
4282
4931
|
*/
|
|
4283
4932
|
insertMultiple(data) {
|
|
4284
|
-
//@ts-ignore
|
|
4285
4933
|
return this.createMultiple(data);
|
|
4286
4934
|
}
|
|
4287
4935
|
/**
|
|
@@ -4291,7 +4939,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4291
4939
|
* @returns {this} this
|
|
4292
4940
|
*/
|
|
4293
4941
|
insertMany(data) {
|
|
4294
|
-
//@ts-ignore
|
|
4295
4942
|
return this.createMultiple(data);
|
|
4296
4943
|
}
|
|
4297
4944
|
/**
|
|
@@ -4385,7 +5032,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4385
5032
|
for (const { Field: field, Type: type } of fields) {
|
|
4386
5033
|
if (passed(field))
|
|
4387
5034
|
continue;
|
|
4388
|
-
const blueprint = this._getBlueprintByColumn(field, {
|
|
5035
|
+
const blueprint = this._getBlueprintByColumn(field, {
|
|
5036
|
+
schema: schemaModel,
|
|
5037
|
+
});
|
|
4389
5038
|
if (blueprint?.isVirtual)
|
|
4390
5039
|
continue;
|
|
4391
5040
|
const value = field === uuid
|
|
@@ -4409,9 +5058,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4409
5058
|
const debug = this.$state.get("DEBUG");
|
|
4410
5059
|
const queue = [...chunkedData];
|
|
4411
5060
|
const runNext = async () => {
|
|
4412
|
-
const batch = Array
|
|
4413
|
-
.from({ length: 3 }, () => queue.shift())
|
|
4414
|
-
.filter(Boolean);
|
|
5061
|
+
const batch = Array.from({ length: 3 }, () => queue.shift()).filter(Boolean);
|
|
4415
5062
|
if (!batch.length)
|
|
4416
5063
|
return;
|
|
4417
5064
|
await Promise.all(batch.map((data) => {
|
|
@@ -4419,13 +5066,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4419
5066
|
.from(table)
|
|
4420
5067
|
.debug(debug)
|
|
4421
5068
|
.createMultiple(data)
|
|
5069
|
+
.bind(this.$pool.get())
|
|
4422
5070
|
.void()
|
|
4423
5071
|
.save();
|
|
4424
5072
|
}));
|
|
4425
5073
|
await runNext();
|
|
4426
5074
|
};
|
|
4427
5075
|
const promises = Array.from({ length: 5 }, () => () => runNext());
|
|
4428
|
-
await Promise.all(promises.map(v => v()));
|
|
5076
|
+
await Promise.all(promises.map((v) => v()));
|
|
4429
5077
|
return;
|
|
4430
5078
|
}
|
|
4431
5079
|
/**
|
|
@@ -4826,7 +5474,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4826
5474
|
case this.$constants("PATTERN").default: {
|
|
4827
5475
|
return column;
|
|
4828
5476
|
}
|
|
4829
|
-
case this.$constants("PATTERN").
|
|
5477
|
+
case this.$constants("PATTERN").snake_case: {
|
|
4830
5478
|
return column.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
|
|
4831
5479
|
}
|
|
4832
5480
|
case this.$constants("PATTERN").camelCase: {
|
|
@@ -4861,7 +5509,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4861
5509
|
this.bindColumn(`${this.getTableName()}.${deletedAt}`),
|
|
4862
5510
|
this.$constants("IS_NULL"),
|
|
4863
5511
|
].join(" ");
|
|
4864
|
-
if (!wheres.some((
|
|
5512
|
+
if (!wheres.some((v) => v.column?.includes(softDeleteIsNull))) {
|
|
4865
5513
|
this.whereNull(deletedAt);
|
|
4866
5514
|
return this;
|
|
4867
5515
|
}
|
|
@@ -4873,9 +5521,23 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4873
5521
|
if (this.$state.get("EXCEPTS")?.length) {
|
|
4874
5522
|
pipeline.push(async () => this.select(...(await this.exceptColumns())));
|
|
4875
5523
|
}
|
|
4876
|
-
await Promise.all(pipeline.map(fn => fn())).catch((err) => console.log(err));
|
|
5524
|
+
await Promise.all(pipeline.map((fn) => fn())).catch((err) => console.log(err));
|
|
4877
5525
|
return;
|
|
4878
5526
|
}
|
|
5527
|
+
_ensureRelationOwnerKeysSelected() {
|
|
5528
|
+
const relations = this.$state.get("RELATIONS");
|
|
5529
|
+
const relationship = this.$constants("RELATIONSHIP");
|
|
5530
|
+
const ownerKeyRelations = relations.filter((r) => {
|
|
5531
|
+
return (r.relation === relationship.hasMany ||
|
|
5532
|
+
r.relation === relationship.hasOne);
|
|
5533
|
+
});
|
|
5534
|
+
if (!ownerKeyRelations.length)
|
|
5535
|
+
return;
|
|
5536
|
+
const ownerKeys = new Set(ownerKeyRelations.map((r) => {
|
|
5537
|
+
return r.localKey ?? this.$state.get("PRIMARY_KEY");
|
|
5538
|
+
}));
|
|
5539
|
+
this.addSelect(...ownerKeys);
|
|
5540
|
+
}
|
|
4879
5541
|
_handleGlobalScope() {
|
|
4880
5542
|
if (!this.$state.get("GLOBAL_SCOPE"))
|
|
4881
5543
|
return this;
|
|
@@ -4918,7 +5580,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4918
5580
|
const sql = schemaColumn.sql.select;
|
|
4919
5581
|
if (sql == null)
|
|
4920
5582
|
continue;
|
|
4921
|
-
if (sql.toLowerCase().includes(
|
|
5583
|
+
if (sql.toLowerCase().includes(` ${this.$constants("AS")} `)) {
|
|
4922
5584
|
columns.push(sql);
|
|
4923
5585
|
continue;
|
|
4924
5586
|
}
|
|
@@ -4929,7 +5591,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4929
5591
|
columns.push(this.bindColumn(key));
|
|
4930
5592
|
continue;
|
|
4931
5593
|
}
|
|
4932
|
-
columns.push(this.bindColumn(schemaColumn.column, false));
|
|
5594
|
+
columns.push(this.bindColumn(schemaColumn.column, { pattern: false }));
|
|
4933
5595
|
}
|
|
4934
5596
|
if (!columns.length)
|
|
4935
5597
|
return this;
|
|
@@ -4945,9 +5607,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4945
5607
|
* generate sql statements
|
|
4946
5608
|
* @override
|
|
4947
5609
|
*/
|
|
4948
|
-
_queryBuilder(
|
|
4949
|
-
if (onFormat)
|
|
4950
|
-
return this._buildQueryStatement();
|
|
5610
|
+
_queryBuilder() {
|
|
4951
5611
|
this._handleSelect();
|
|
4952
5612
|
this._handleSoftDelete();
|
|
4953
5613
|
return this._buildQueryStatement();
|
|
@@ -4968,7 +5628,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4968
5628
|
data = callback;
|
|
4969
5629
|
}
|
|
4970
5630
|
}
|
|
4971
|
-
this.$state.set("DATA", data);
|
|
5631
|
+
this.$state.set("DATA", this._formatedInputData(data));
|
|
4972
5632
|
return;
|
|
4973
5633
|
}
|
|
4974
5634
|
async _runAfter(action, data) {
|
|
@@ -5090,7 +5750,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5090
5750
|
if (findCache == null)
|
|
5091
5751
|
return;
|
|
5092
5752
|
if (this.$state.get("DEBUG")) {
|
|
5093
|
-
this.$utils.consoleCache(
|
|
5753
|
+
this.$utils.consoleCache({
|
|
5094
5754
|
driver: this.$cache.provider(),
|
|
5095
5755
|
key: cache.key,
|
|
5096
5756
|
expires: cache.expires,
|
|
@@ -5099,7 +5759,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5099
5759
|
this.$utils.typeOf(findCache) === "string"
|
|
5100
5760
|
? findCache.length
|
|
5101
5761
|
: undefined,
|
|
5102
|
-
})
|
|
5762
|
+
});
|
|
5103
5763
|
const endTime = +new Date();
|
|
5104
5764
|
this.$utils.consoleExec(startTime, endTime);
|
|
5105
5765
|
}
|
|
@@ -5136,8 +5796,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5136
5796
|
this._returnEmpty(type, result, message, options));
|
|
5137
5797
|
}
|
|
5138
5798
|
async _pagination(data) {
|
|
5139
|
-
const currentPage =
|
|
5140
|
-
const limit = Number(this.$state.get("LIMIT"));
|
|
5799
|
+
const currentPage = this.$state.get("PAGE") ?? 0;
|
|
5800
|
+
const limit = Number(this.$state.get("LIMIT") ?? 0);
|
|
5141
5801
|
if (limit < 1) {
|
|
5142
5802
|
throw this._assertError("This pagination needed limit minimun less 1 for limit");
|
|
5143
5803
|
}
|
|
@@ -5145,7 +5805,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5145
5805
|
.copyModel(this, { where: true, join: true })
|
|
5146
5806
|
.bind(this.$pool.get())
|
|
5147
5807
|
.debug(this.$state.get("DEBUG"))
|
|
5148
|
-
.unset({ alias: true })
|
|
5149
5808
|
.count(this.$state.get("PRIMARY_KEY"));
|
|
5150
5809
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
5151
5810
|
lastPage = lastPage > 1 ? lastPage : 1;
|
|
@@ -5160,15 +5819,21 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5160
5819
|
last_page: lastPage,
|
|
5161
5820
|
next_page: nextPage,
|
|
5162
5821
|
prev_page: prevPage,
|
|
5822
|
+
page: {
|
|
5823
|
+
prev: prevPage,
|
|
5824
|
+
next: nextPage,
|
|
5825
|
+
current: currentPage,
|
|
5826
|
+
last: lastPage,
|
|
5827
|
+
},
|
|
5163
5828
|
};
|
|
5164
5829
|
const pattern = this.$state.get("PATTERN");
|
|
5165
|
-
if (pattern ===
|
|
5830
|
+
if (pattern === this.$constants("PATTERN").snake_case) {
|
|
5166
5831
|
return this.$utils.snakeCase(this._resultHandler({
|
|
5167
5832
|
meta,
|
|
5168
5833
|
data,
|
|
5169
5834
|
}));
|
|
5170
5835
|
}
|
|
5171
|
-
if (pattern ===
|
|
5836
|
+
if (pattern === this.$constants("PATTERN").camelCase) {
|
|
5172
5837
|
return this.$utils.camelCase(this._resultHandler({
|
|
5173
5838
|
meta,
|
|
5174
5839
|
data,
|
|
@@ -5215,6 +5880,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5215
5880
|
last_page: 0,
|
|
5216
5881
|
next_page: 0,
|
|
5217
5882
|
prev_page: 0,
|
|
5883
|
+
page: {
|
|
5884
|
+
prev: 0,
|
|
5885
|
+
next: 0,
|
|
5886
|
+
current: Number(this.$state.get("PAGE")),
|
|
5887
|
+
last: 0,
|
|
5888
|
+
},
|
|
5218
5889
|
},
|
|
5219
5890
|
data: [],
|
|
5220
5891
|
};
|
|
@@ -5223,14 +5894,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5223
5894
|
default:
|
|
5224
5895
|
throw this._assertError("Missing method first get or pagination");
|
|
5225
5896
|
}
|
|
5226
|
-
if (this.$state.get(
|
|
5897
|
+
if (this.$state.get("PATTERN") === this.$constants("PATTERN").snake_case) {
|
|
5227
5898
|
const empty = this.$utils.snakeCase(this._resultHandler(emptyData));
|
|
5228
5899
|
await this.$utils.hookHandle(this.$state.get("HOOKS"), empty);
|
|
5229
5900
|
await this._observer(empty, "selected");
|
|
5230
5901
|
return empty;
|
|
5231
5902
|
}
|
|
5232
|
-
if (this.$state.get(
|
|
5233
|
-
const empty = this.$utils.
|
|
5903
|
+
if (this.$state.get("PATTERN") === this.$constants("PATTERN").camelCase) {
|
|
5904
|
+
const empty = this.$utils.camelCase(this._resultHandler(emptyData));
|
|
5234
5905
|
await this.$utils.hookHandle(this.$state.get("HOOKS"), empty);
|
|
5235
5906
|
await this._observer(empty, "selected");
|
|
5236
5907
|
return empty;
|
|
@@ -5266,11 +5937,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5266
5937
|
throw this._assertError("Missing method first get or pagination");
|
|
5267
5938
|
}
|
|
5268
5939
|
}
|
|
5269
|
-
if (this.$state.get(
|
|
5940
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5270
5941
|
await this.$utils.applyTransforms({
|
|
5271
5942
|
result,
|
|
5272
|
-
transforms: this.$state.get(
|
|
5273
|
-
action:
|
|
5943
|
+
transforms: this.$state.get("TRANSFORMS"),
|
|
5944
|
+
action: "from",
|
|
5274
5945
|
});
|
|
5275
5946
|
}
|
|
5276
5947
|
await this.$utils.hookHandle(this.$state.get("HOOKS"), result);
|
|
@@ -5290,11 +5961,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5290
5961
|
: this.$utils.transfromDateToDateString(objects[updatedAt]),
|
|
5291
5962
|
};
|
|
5292
5963
|
}
|
|
5293
|
-
if (this.$state.get(
|
|
5964
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5294
5965
|
await this.$utils.applyTransforms({
|
|
5295
5966
|
result: objects,
|
|
5296
|
-
transforms: this.$state.get(
|
|
5297
|
-
action:
|
|
5967
|
+
transforms: this.$state.get("TRANSFORMS"),
|
|
5968
|
+
action: "to",
|
|
5298
5969
|
});
|
|
5299
5970
|
}
|
|
5300
5971
|
const keyValue = Object.entries(objects).map(([column, value]) => {
|
|
@@ -5327,11 +5998,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5327
5998
|
: this.$utils.transfromDateToDateString(data[updatedAt]),
|
|
5328
5999
|
};
|
|
5329
6000
|
}
|
|
5330
|
-
if (this.$state.get(
|
|
6001
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5331
6002
|
await this.$utils.applyTransforms({
|
|
5332
6003
|
result: data,
|
|
5333
|
-
transforms: this.$state.get(
|
|
5334
|
-
action:
|
|
6004
|
+
transforms: this.$state.get("TRANSFORMS"),
|
|
6005
|
+
action: "to",
|
|
5335
6006
|
});
|
|
5336
6007
|
}
|
|
5337
6008
|
const hasUUID = data.hasOwnProperty(this.$state.get("UUID_FORMAT"));
|
|
@@ -5358,7 +6029,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5358
6029
|
});
|
|
5359
6030
|
this.$state.set("INSERT", {
|
|
5360
6031
|
columns,
|
|
5361
|
-
values: [values.join(
|
|
6032
|
+
values: [values.join(", ")],
|
|
5362
6033
|
});
|
|
5363
6034
|
return;
|
|
5364
6035
|
}
|
|
@@ -5385,11 +6056,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5385
6056
|
};
|
|
5386
6057
|
columns = [...columns, `\`${createdAt}\``, `\`${updatedAt}\``];
|
|
5387
6058
|
}
|
|
5388
|
-
if (this.$state.get(
|
|
6059
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5389
6060
|
await this.$utils.applyTransforms({
|
|
5390
6061
|
result: objects,
|
|
5391
|
-
transforms: this.$state.get(
|
|
5392
|
-
action:
|
|
6062
|
+
transforms: this.$state.get("TRANSFORMS"),
|
|
6063
|
+
action: "to",
|
|
5393
6064
|
});
|
|
5394
6065
|
}
|
|
5395
6066
|
const hasUUID = objects.hasOwnProperty(this.$state.get("UUID_FORMAT"));
|
|
@@ -5417,13 +6088,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5417
6088
|
values = [...values, `${v.join(",")}`];
|
|
5418
6089
|
newData.push(objects);
|
|
5419
6090
|
}
|
|
5420
|
-
this.$state.set("DATA", newData);
|
|
6091
|
+
this.$state.set("DATA", this._formatedInputData(newData));
|
|
5421
6092
|
columns = [
|
|
5422
6093
|
...new Set(columns.map((c) => `\`${this._valuePattern(c).replace(/\`/g, "")}\``)),
|
|
5423
6094
|
];
|
|
5424
6095
|
this.$state.set("INSERT", {
|
|
5425
6096
|
columns,
|
|
5426
|
-
values
|
|
6097
|
+
values,
|
|
5427
6098
|
});
|
|
5428
6099
|
return;
|
|
5429
6100
|
}
|
|
@@ -5442,7 +6113,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5442
6113
|
if (check)
|
|
5443
6114
|
return this._resultHandler(null);
|
|
5444
6115
|
await this._runBefore("insert");
|
|
5445
|
-
if (this.$state.get(
|
|
6116
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5446
6117
|
await this._queryInsertModel();
|
|
5447
6118
|
}
|
|
5448
6119
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
@@ -5452,7 +6123,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5452
6123
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
5453
6124
|
let resultData = await new Model()
|
|
5454
6125
|
.copyModel(this, { select: true, relations: true })
|
|
5455
|
-
.whereIn(
|
|
6126
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5456
6127
|
.bind(this.$pool.get())
|
|
5457
6128
|
.debug(this.$state.get("DEBUG"))
|
|
5458
6129
|
.first();
|
|
@@ -5460,7 +6131,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5460
6131
|
await this.$utils.wait(500);
|
|
5461
6132
|
resultData = await new Model()
|
|
5462
6133
|
.copyModel(this, { select: true, relations: true })
|
|
5463
|
-
.whereIn(
|
|
6134
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5464
6135
|
.bind(this.$pool.get())
|
|
5465
6136
|
.debug(this.$state.get("DEBUG"))
|
|
5466
6137
|
.first();
|
|
@@ -5469,7 +6140,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5469
6140
|
}
|
|
5470
6141
|
async _insertModel() {
|
|
5471
6142
|
await this._runBefore("insert");
|
|
5472
|
-
if (this.$state.get(
|
|
6143
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5473
6144
|
await this._queryInsertModel();
|
|
5474
6145
|
}
|
|
5475
6146
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
@@ -5481,7 +6152,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5481
6152
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
5482
6153
|
let resultData = await new Model()
|
|
5483
6154
|
.copyModel(this, { select: true, relations: true })
|
|
5484
|
-
.whereIn(
|
|
6155
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5485
6156
|
.bind(this.$pool.get())
|
|
5486
6157
|
.debug(this.$state.get("DEBUG"))
|
|
5487
6158
|
.first();
|
|
@@ -5489,7 +6160,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5489
6160
|
await this.$utils.wait(1000);
|
|
5490
6161
|
resultData = await new Model()
|
|
5491
6162
|
.copyModel(this, { select: true, relations: true })
|
|
5492
|
-
.whereIn(
|
|
6163
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5493
6164
|
.bind(this.$pool.get())
|
|
5494
6165
|
.debug(this.$state.get("DEBUG"))
|
|
5495
6166
|
.first();
|
|
@@ -5501,7 +6172,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5501
6172
|
async _insertMultipleModel() {
|
|
5502
6173
|
const data = this.$state.get("DATA") ?? [];
|
|
5503
6174
|
await this._runBefore("insert");
|
|
5504
|
-
if (this.$state.get(
|
|
6175
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5505
6176
|
await this._queryInsertMultipleModel();
|
|
5506
6177
|
}
|
|
5507
6178
|
for (const v of data) {
|
|
@@ -5542,7 +6213,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5542
6213
|
switch (check) {
|
|
5543
6214
|
case false: {
|
|
5544
6215
|
await this._runBefore("insert");
|
|
5545
|
-
if (this.$state.get(
|
|
6216
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5546
6217
|
await this._queryInsertModel();
|
|
5547
6218
|
}
|
|
5548
6219
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
@@ -5553,7 +6224,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5553
6224
|
let data = await new Model()
|
|
5554
6225
|
.copyModel(this, { select: true })
|
|
5555
6226
|
.bind(this.$pool.get())
|
|
5556
|
-
.whereIn(
|
|
6227
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5557
6228
|
.debug(this.$state.get("DEBUG"))
|
|
5558
6229
|
.first();
|
|
5559
6230
|
if (data == null) {
|
|
@@ -5561,7 +6232,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5561
6232
|
data = await new Model()
|
|
5562
6233
|
.copyModel(this, { select: true })
|
|
5563
6234
|
.bind(this.$pool.get())
|
|
5564
|
-
.whereIn(
|
|
6235
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5565
6236
|
.debug(this.$state.get("DEBUG"))
|
|
5566
6237
|
.first();
|
|
5567
6238
|
}
|
|
@@ -5573,7 +6244,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5573
6244
|
}
|
|
5574
6245
|
case true: {
|
|
5575
6246
|
await this._runBefore("update");
|
|
5576
|
-
if (this.$state.get(
|
|
6247
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5577
6248
|
await this._queryUpdateModel();
|
|
5578
6249
|
}
|
|
5579
6250
|
await this._validateSchema(this.$state.get("DATA"), "update");
|
|
@@ -5612,7 +6283,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5612
6283
|
switch (check) {
|
|
5613
6284
|
case false: {
|
|
5614
6285
|
await this._runBefore("insert");
|
|
5615
|
-
if (this.$state.get(
|
|
6286
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5616
6287
|
await this._queryInsertModel();
|
|
5617
6288
|
}
|
|
5618
6289
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
@@ -5623,7 +6294,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5623
6294
|
let data = await new Model()
|
|
5624
6295
|
.copyModel(this, { select: true })
|
|
5625
6296
|
.bind(this.$pool.get())
|
|
5626
|
-
.whereIn(
|
|
6297
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5627
6298
|
.debug(this.$state.get("DEBUG"))
|
|
5628
6299
|
.first();
|
|
5629
6300
|
if (data == null) {
|
|
@@ -5631,7 +6302,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5631
6302
|
data = await new Model()
|
|
5632
6303
|
.copyModel(this, { select: true })
|
|
5633
6304
|
.bind(this.$pool.get())
|
|
5634
|
-
.whereIn(
|
|
6305
|
+
.whereIn(this.$state.get('PRIMARY_KEY'), result.$meta.insertIds)
|
|
5635
6306
|
.debug(this.$state.get("DEBUG"))
|
|
5636
6307
|
.first();
|
|
5637
6308
|
}
|
|
@@ -5670,7 +6341,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5670
6341
|
async _updateModel() {
|
|
5671
6342
|
this._guardWhereCondition();
|
|
5672
6343
|
await this._runBefore("update");
|
|
5673
|
-
if (this.$state.get(
|
|
6344
|
+
if (this.$state.get("TRANSFORMS") != null) {
|
|
5674
6345
|
await this._queryUpdateModel();
|
|
5675
6346
|
}
|
|
5676
6347
|
await this._validateSchema(this.$state.get("DATA"), "update");
|
|
@@ -5709,6 +6380,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5709
6380
|
throw new Error(message);
|
|
5710
6381
|
return;
|
|
5711
6382
|
}
|
|
6383
|
+
disabledRetry() {
|
|
6384
|
+
this.$state.set("RETRY", 3);
|
|
6385
|
+
return this;
|
|
6386
|
+
}
|
|
5712
6387
|
async _checkSchemaOrNextError(e, retry = 1, originError) {
|
|
5713
6388
|
const throwError = originError == null ? e : originError;
|
|
5714
6389
|
try {
|
|
@@ -5719,12 +6394,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5719
6394
|
return this._stoppedRetry(throwError);
|
|
5720
6395
|
if (!(e instanceof Error))
|
|
5721
6396
|
return this._stoppedRetry(throwError);
|
|
5722
|
-
await
|
|
6397
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
6398
|
+
await new Model()
|
|
6399
|
+
.copyModel(this)
|
|
6400
|
+
.disabledRetry()
|
|
6401
|
+
.debug(this.$state.get('DEBUG'))
|
|
6402
|
+
.sync({ force: true }).catch(() => null);
|
|
6403
|
+
this.$state.set("RETRY", retry + 1);
|
|
5723
6404
|
}
|
|
5724
6405
|
catch (e) {
|
|
5725
6406
|
if (retry >= 3) {
|
|
5726
6407
|
throw throwError;
|
|
5727
6408
|
}
|
|
6409
|
+
this.$state.set("RETRY", retry + 1);
|
|
5728
6410
|
await this._checkSchemaOrNextError(e, retry + 1, originError);
|
|
5729
6411
|
}
|
|
5730
6412
|
}
|
|
@@ -5749,20 +6431,26 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5749
6431
|
const uuidIsEnabled = Reflect.getMetadata(Decorator_1.REFLECT_META.UUID.enabled, this.constructor) || null;
|
|
5750
6432
|
if (uuidIsEnabled) {
|
|
5751
6433
|
this.$uuid = true;
|
|
5752
|
-
this.$uuidColumn =
|
|
6434
|
+
this.$uuidColumn =
|
|
6435
|
+
Reflect.getMetadata(Decorator_1.REFLECT_META.UUID.column, this.constructor) || null;
|
|
5753
6436
|
}
|
|
5754
|
-
const timestamp = Reflect.getMetadata(Decorator_1.REFLECT_META.TIMESTAMP.enabled, this.constructor) ||
|
|
6437
|
+
const timestamp = Reflect.getMetadata(Decorator_1.REFLECT_META.TIMESTAMP.enabled, this.constructor) ||
|
|
6438
|
+
null;
|
|
5755
6439
|
if (timestamp) {
|
|
5756
6440
|
this.$timestamp = true;
|
|
5757
|
-
this.$timestampColumns =
|
|
6441
|
+
this.$timestampColumns =
|
|
6442
|
+
Reflect.getMetadata(Decorator_1.REFLECT_META.TIMESTAMP.columns, this.constructor) ||
|
|
6443
|
+
null;
|
|
5758
6444
|
}
|
|
5759
6445
|
const pattern = Reflect.getMetadata(Decorator_1.REFLECT_META.PATTERN, this.constructor) || null;
|
|
5760
6446
|
if (pattern)
|
|
5761
6447
|
this.$pattern = pattern;
|
|
5762
|
-
const softDelete = Reflect.getMetadata(Decorator_1.REFLECT_META.SOFT_DELETE.enabled, this.constructor) ||
|
|
6448
|
+
const softDelete = Reflect.getMetadata(Decorator_1.REFLECT_META.SOFT_DELETE.enabled, this.constructor) ||
|
|
6449
|
+
null;
|
|
5763
6450
|
if (softDelete) {
|
|
5764
6451
|
this.$softDelete = true;
|
|
5765
|
-
this.$softDeleteColumn =
|
|
6452
|
+
this.$softDeleteColumn =
|
|
6453
|
+
Reflect.getMetadata(Decorator_1.REFLECT_META.SOFT_DELETE.columns, this.constructor) || null;
|
|
5766
6454
|
}
|
|
5767
6455
|
const schema = Reflect.getMetadata(Decorator_1.REFLECT_META.SCHEMA, this) || null;
|
|
5768
6456
|
if (schema)
|
|
@@ -5834,27 +6522,27 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5834
6522
|
this.useHooks(hooks);
|
|
5835
6523
|
const beforeInserts = Reflect.getMetadata(Decorator_1.REFLECT_META.BEFORE.INSERT, this) || [];
|
|
5836
6524
|
if (beforeInserts.length) {
|
|
5837
|
-
this.useLifecycle(
|
|
6525
|
+
this.useLifecycle("beforeInsert", beforeInserts);
|
|
5838
6526
|
}
|
|
5839
6527
|
const afterInserts = Reflect.getMetadata(Decorator_1.REFLECT_META.AFTER.INSERT, this) || [];
|
|
5840
6528
|
if (afterInserts.length) {
|
|
5841
|
-
this.useLifecycle(
|
|
6529
|
+
this.useLifecycle("afterInsert", afterInserts);
|
|
5842
6530
|
}
|
|
5843
6531
|
const beforeUpdates = Reflect.getMetadata(Decorator_1.REFLECT_META.BEFORE.UPDATE, this) || [];
|
|
5844
6532
|
if (beforeUpdates.length) {
|
|
5845
|
-
this.useLifecycle(
|
|
6533
|
+
this.useLifecycle("beforeUpdate", beforeUpdates);
|
|
5846
6534
|
}
|
|
5847
6535
|
const afterUpdates = Reflect.getMetadata(Decorator_1.REFLECT_META.AFTER.UPDATE, this) || [];
|
|
5848
6536
|
if (afterUpdates.length) {
|
|
5849
|
-
this.useLifecycle(
|
|
6537
|
+
this.useLifecycle("afterUpdate", afterUpdates);
|
|
5850
6538
|
}
|
|
5851
6539
|
const beforeRemoves = Reflect.getMetadata(Decorator_1.REFLECT_META.BEFORE.REMOVE, this) || [];
|
|
5852
6540
|
if (beforeRemoves.length) {
|
|
5853
|
-
this.useLifecycle(
|
|
6541
|
+
this.useLifecycle("beforeRemove", beforeRemoves);
|
|
5854
6542
|
}
|
|
5855
6543
|
const afterRemoves = Reflect.getMetadata(Decorator_1.REFLECT_META.AFTER.REMOVE, this) || [];
|
|
5856
6544
|
if (afterRemoves.length) {
|
|
5857
|
-
this.useLifecycle(
|
|
6545
|
+
this.useLifecycle("afterRemove", afterRemoves);
|
|
5858
6546
|
}
|
|
5859
6547
|
return this;
|
|
5860
6548
|
}
|
|
@@ -5869,7 +6557,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5869
6557
|
this.bindColumn(`${this.getTableName()}.${deletedAt}`),
|
|
5870
6558
|
this.$constants("IS_NULL"),
|
|
5871
6559
|
].join(" ");
|
|
5872
|
-
if (wheres.some((
|
|
6560
|
+
if (wheres.some((v) => v.column?.includes(softDeleteIsNull))) {
|
|
5873
6561
|
throw this._assertError(`The statement is not allowed to use the '${deletedAt}' column as a condition for any action`);
|
|
5874
6562
|
}
|
|
5875
6563
|
}
|
|
@@ -5883,7 +6571,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5883
6571
|
return false;
|
|
5884
6572
|
}
|
|
5885
6573
|
}
|
|
5886
|
-
_getBlueprintByColumn(column, { schema } = {}) {
|
|
6574
|
+
_getBlueprintByColumn(column, { schema, } = {}) {
|
|
5887
6575
|
schema = schema ?? this.getSchemaModel();
|
|
5888
6576
|
if (!schema)
|
|
5889
6577
|
return null;
|
|
@@ -5893,8 +6581,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5893
6581
|
return blueprint;
|
|
5894
6582
|
}
|
|
5895
6583
|
_handleJoinModel(m1, m2) {
|
|
5896
|
-
let model1 = typeof m1 === "object"
|
|
5897
|
-
|
|
6584
|
+
let model1 = typeof m1 === "object"
|
|
6585
|
+
? m1 instanceof Model
|
|
6586
|
+
? m1
|
|
6587
|
+
: new m1.model()
|
|
6588
|
+
: new m1();
|
|
6589
|
+
let model2 = typeof m2 === "object"
|
|
6590
|
+
? m2 instanceof Model
|
|
6591
|
+
? m2
|
|
6592
|
+
: new m2.model()
|
|
6593
|
+
: new m2();
|
|
5898
6594
|
let localKey = typeof m1 === "object"
|
|
5899
6595
|
? m1.key != null && m1.key !== ""
|
|
5900
6596
|
? String(m1.key)
|
|
@@ -5906,18 +6602,30 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5906
6602
|
: ""
|
|
5907
6603
|
: "";
|
|
5908
6604
|
let alias1 = typeof m1 === "object"
|
|
5909
|
-
? m1
|
|
5910
|
-
?
|
|
5911
|
-
: ""
|
|
6605
|
+
? m1 instanceof Model
|
|
6606
|
+
? model1.$state.get("ALIAS") ?? ""
|
|
6607
|
+
: m1.alias != null && m1.alias !== ""
|
|
6608
|
+
? m1.alias
|
|
6609
|
+
: ""
|
|
5912
6610
|
: "";
|
|
5913
6611
|
let alias2 = typeof m2 === "object"
|
|
5914
|
-
? m2
|
|
5915
|
-
?
|
|
5916
|
-
: ""
|
|
6612
|
+
? m2 instanceof Model
|
|
6613
|
+
? model2.$state.get("ALIAS") ?? ""
|
|
6614
|
+
: m2.alias != null && m2.alias !== ""
|
|
6615
|
+
? m2.alias
|
|
6616
|
+
: ""
|
|
5917
6617
|
: "";
|
|
5918
6618
|
if (alias1 !== "") {
|
|
5919
6619
|
if (model1["$state"].get("MODEL_NAME") === this["$state"].get("MODEL_NAME")) {
|
|
5920
6620
|
this.alias(alias1);
|
|
6621
|
+
const selecteds = this.$state.get("SELECT");
|
|
6622
|
+
const formated = [];
|
|
6623
|
+
for (const selected of selecteds) {
|
|
6624
|
+
const table = this.getTableName();
|
|
6625
|
+
formated.push(selected.replace(table, alias1));
|
|
6626
|
+
}
|
|
6627
|
+
if (formated.length)
|
|
6628
|
+
this.$state.set("SELECT", formated);
|
|
5921
6629
|
}
|
|
5922
6630
|
model1.alias(alias1);
|
|
5923
6631
|
}
|
|
@@ -5942,10 +6650,32 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5942
6650
|
foreignKey,
|
|
5943
6651
|
};
|
|
5944
6652
|
}
|
|
6653
|
+
_formatedInputData(data) {
|
|
6654
|
+
const schema = this.getSchemaModel();
|
|
6655
|
+
if (!schema)
|
|
6656
|
+
return data;
|
|
6657
|
+
if (data == null)
|
|
6658
|
+
return data;
|
|
6659
|
+
const formatObject = (obj) => {
|
|
6660
|
+
if (obj === null || typeof obj !== 'object')
|
|
6661
|
+
return obj;
|
|
6662
|
+
const result = {};
|
|
6663
|
+
for (const key of Object.keys(schema)) {
|
|
6664
|
+
if (key in obj) {
|
|
6665
|
+
result[key] = obj[key];
|
|
6666
|
+
}
|
|
6667
|
+
}
|
|
6668
|
+
return result;
|
|
6669
|
+
};
|
|
6670
|
+
if (Array.isArray(data)) {
|
|
6671
|
+
return data.map(item => formatObject(item));
|
|
6672
|
+
}
|
|
6673
|
+
return formatObject(data);
|
|
6674
|
+
}
|
|
5945
6675
|
_initialModel() {
|
|
5946
6676
|
this.$cache = Cache_1.Cache;
|
|
5947
6677
|
this.$state = new StateManager_1.StateManager("model");
|
|
5948
|
-
this.
|
|
6678
|
+
this.metaTag("MAIN");
|
|
5949
6679
|
this._makeTableName();
|
|
5950
6680
|
this.$relation = new RelationManager_1.RelationManager(this);
|
|
5951
6681
|
if (globalSettings.debug)
|