tspace-mysql 1.9.4 → 1.9.5
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 +376 -72
- package/dist/cli/dump/db.js +3 -15
- package/dist/cli/dump/db.js.map +1 -1
- package/dist/cli/index.js +0 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrations/make-db.js +1 -1
- package/dist/cli/migrations/make-db.js.map +1 -1
- package/dist/cli/migrations/make-model.js +1 -6
- package/dist/cli/migrations/make-model.js.map +1 -1
- package/dist/lib/config/index.d.ts +6 -5
- package/dist/lib/config/index.js +18 -8
- package/dist/lib/config/index.js.map +1 -1
- package/dist/lib/constants/index.d.ts +1 -1
- package/dist/lib/constants/index.js +1 -1
- package/dist/lib/constants/index.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +2 -1
- package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -3
- package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractModel.d.ts +1 -2
- package/dist/lib/core/Abstracts/AbstractModel.js +0 -1
- package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractView.d.ts +2 -1
- package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
- package/dist/lib/core/Blueprint.d.ts +13 -3
- package/dist/lib/core/Blueprint.js +16 -1
- package/dist/lib/core/Blueprint.js.map +1 -1
- package/dist/lib/core/Builder.d.ts +172 -19
- package/dist/lib/core/Builder.js +564 -197
- package/dist/lib/core/Builder.js.map +1 -1
- package/dist/lib/core/Cache/DBCache.js +6 -5
- package/dist/lib/core/Cache/DBCache.js.map +1 -1
- package/dist/lib/core/Cache/index.js +1 -1
- package/dist/lib/core/Cache/index.js.map +1 -1
- package/dist/lib/core/Contracts/Audit.d.ts +5 -3
- package/dist/lib/core/Contracts/Audit.js +18 -2
- package/dist/lib/core/Contracts/Audit.js.map +1 -1
- package/dist/lib/core/DB.d.ts +148 -190
- package/dist/lib/core/DB.js +459 -525
- package/dist/lib/core/DB.js.map +1 -1
- package/dist/lib/core/Driver/index.d.ts +40 -15
- package/dist/lib/core/Driver/index.js +3 -0
- package/dist/lib/core/Driver/index.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbDriver.d.ts +3 -2
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js +7 -5
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +42 -17
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +281 -154
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +4 -3
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js +7 -5
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +40 -15
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +205 -157
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlDriver.d.ts +3 -2
- package/dist/lib/core/Driver/mysql/MysqlDriver.js +6 -14
- package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +42 -17
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +251 -144
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresDriver.d.ts +3 -2
- package/dist/lib/core/Driver/postgres/PostgresDriver.js +7 -5
- package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +45 -19
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +471 -287
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +3 -2
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js +8 -6
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +47 -19
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +331 -203
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -1
- package/dist/lib/core/Model.d.ts +255 -52
- package/dist/lib/core/Model.js +459 -117
- package/dist/lib/core/Model.js.map +1 -1
- package/dist/lib/core/Package.d.ts +5 -0
- package/dist/lib/core/Package.js +7 -2
- package/dist/lib/core/Package.js.map +1 -1
- package/dist/lib/core/Pool.d.ts +1 -1
- package/dist/lib/core/Pool.js +53 -75
- package/dist/lib/core/Pool.js.map +1 -1
- package/dist/lib/core/Queue/index.d.ts +2 -11
- package/dist/lib/core/Queue/index.js +3 -3
- package/dist/lib/core/Queue/index.js.map +1 -1
- package/dist/lib/core/Queue/types.d.ts +32 -3
- package/dist/lib/core/Queue/worker-lock.d.ts +17 -0
- package/dist/lib/core/Queue/worker-lock.js +28 -0
- package/dist/lib/core/Queue/worker-lock.js.map +1 -0
- package/dist/lib/core/Queue/worker.d.ts +37 -23
- package/dist/lib/core/Queue/worker.js +385 -171
- package/dist/lib/core/Queue/worker.js.map +1 -1
- package/dist/lib/core/RelationManager.js +1 -1
- package/dist/lib/core/RelationManager.js.map +1 -1
- package/dist/lib/core/Repository.d.ts +164 -29
- package/dist/lib/core/Repository.js +181 -40
- package/dist/lib/core/Repository.js.map +1 -1
- package/dist/lib/core/Schema.d.ts +11 -7
- package/dist/lib/core/Schema.js +249 -80
- package/dist/lib/core/Schema.js.map +1 -1
- package/dist/lib/core/SqlLike.js +4 -4
- package/dist/lib/core/SqlLike.js.map +1 -1
- package/dist/lib/core/StateManager.d.ts +19 -0
- package/dist/lib/core/StateManager.js +9 -4
- package/dist/lib/core/StateManager.js.map +1 -1
- package/dist/lib/core/UtilityTypes.d.ts +17 -3
- package/dist/lib/core/UtilityTypes.js.map +1 -1
- package/dist/lib/core/View.d.ts +3 -1
- package/dist/lib/core/View.js +18 -25
- package/dist/lib/core/View.js.map +1 -1
- package/dist/lib/core/index.d.ts +2 -0
- package/dist/lib/core/index.js +3 -1
- package/dist/lib/core/index.js.map +1 -1
- package/dist/lib/types/index.d.ts +77 -90
- package/dist/lib/types/repository/index.d.ts +18 -26
- package/dist/lib/utils/index.d.ts +7 -3
- package/dist/lib/utils/index.js +99 -37
- package/dist/lib/utils/index.js.map +1 -1
- package/package.json +1 -2
- package/dist/cli/dump/table.d.ts +0 -4
- package/dist/cli/dump/table.js +0 -41
- package/dist/cli/dump/table.js.map +0 -1
package/dist/lib/core/Model.js
CHANGED
|
@@ -301,6 +301,49 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
301
301
|
static async findMany(options = {}) {
|
|
302
302
|
return await (0, Repository_1.Repository)(this).findMany(options);
|
|
303
303
|
}
|
|
304
|
+
/**
|
|
305
|
+
*
|
|
306
|
+
* The 'stream' method is used to executes a SQL statement and asynchronously yields result rows.
|
|
307
|
+
*
|
|
308
|
+
* It allows you to retrieve a single record from a database table that meets the specified criteria.
|
|
309
|
+
* @static
|
|
310
|
+
* @type {?object} options
|
|
311
|
+
* @property {?object} options.select
|
|
312
|
+
* @property {?object} options.except
|
|
313
|
+
* @property {?object[]} options.orderBy
|
|
314
|
+
* @property {?string[]} options.groupBy
|
|
315
|
+
* @property {?string} options.having
|
|
316
|
+
* @property {?number} options.limit
|
|
317
|
+
* @property {?number} options.offset
|
|
318
|
+
* @property {?object} options.where
|
|
319
|
+
* @property {?string[]} options.whereRaw
|
|
320
|
+
* @property {?object} options.whereQuery
|
|
321
|
+
* @property {?{condition,callback}} options.when
|
|
322
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
323
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
324
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
325
|
+
* @property {?string[]} options.relations
|
|
326
|
+
* @property {string[]} options.relationExists
|
|
327
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
328
|
+
* @property {?boolean} options.debug
|
|
329
|
+
* @returns {AsyncGenerator<T>}
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* import { User } from '../Models/User'
|
|
333
|
+
*
|
|
334
|
+
* const stream = User.stream({
|
|
335
|
+
* select : { id: true, name: true },
|
|
336
|
+
* where : {
|
|
337
|
+
* id: 1
|
|
338
|
+
* }
|
|
339
|
+
* })
|
|
340
|
+
*
|
|
341
|
+
* for await (const user of stream) console.log(user);
|
|
342
|
+
*
|
|
343
|
+
*/
|
|
344
|
+
static async *stream(options = {}) {
|
|
345
|
+
yield* (0, Repository_1.Repository)(this).stream(options);
|
|
346
|
+
}
|
|
304
347
|
/**
|
|
305
348
|
*
|
|
306
349
|
* The 'paginate' method is used to perform pagination on a set of database query results obtained through the Query Builder.
|
|
@@ -346,9 +389,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
346
389
|
return await (0, Repository_1.Repository)(this).paginate(options);
|
|
347
390
|
}
|
|
348
391
|
/**
|
|
349
|
-
* The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
|
|
350
392
|
*
|
|
351
|
-
*
|
|
393
|
+
* The 'pagination' method is used to perform pagination on a set of database query results obtained through the Query Builder.
|
|
394
|
+
*
|
|
395
|
+
* It allows you to split a large set of query results into smaller, more manageable pages,
|
|
396
|
+
* making it easier to display data in a web application and improve user experience.
|
|
352
397
|
* @static
|
|
353
398
|
* @type {?object} options
|
|
354
399
|
* @property {?object} options.select
|
|
@@ -365,26 +410,114 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
365
410
|
* @property {?{localKey , referenceKey}[]} options.join
|
|
366
411
|
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
367
412
|
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
413
|
+
* @property {?string[]} options.relations
|
|
414
|
+
* @property {string[]} options.relationExists
|
|
415
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
368
416
|
* @property {?boolean} options.debug
|
|
369
417
|
* @property {?number} options.page
|
|
418
|
+
* @returns {promise<{ meta , data[]}>}
|
|
370
419
|
*
|
|
371
420
|
* @example
|
|
372
|
-
*
|
|
421
|
+
* import { User } from '../Models/User'
|
|
373
422
|
*
|
|
374
|
-
* const users = await User.
|
|
423
|
+
* const users = await User.paginate({
|
|
424
|
+
* limit:15,
|
|
425
|
+
* page: 1,
|
|
426
|
+
* select : { id: true, name: true },
|
|
375
427
|
* where : {
|
|
376
428
|
* id: 1
|
|
377
429
|
* }
|
|
378
430
|
* })
|
|
431
|
+
*/
|
|
432
|
+
static async pagination(options = {}) {
|
|
433
|
+
return await (0, Repository_1.Repository)(this).pagination(options);
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
*
|
|
437
|
+
* The 'cursorPagination' method is used to perform cursor-based pagination
|
|
438
|
+
* on a set of database query results obtained through the Query Builder.
|
|
439
|
+
*
|
|
440
|
+
* It uses cursor-based pagination instead of offset pagination, making it
|
|
441
|
+
* more efficient when navigating through large datasets.
|
|
442
|
+
* @static
|
|
443
|
+
* @type {?object} options
|
|
444
|
+
* @property {?object} options.select
|
|
445
|
+
* @property {?object} options.except
|
|
446
|
+
* @property {?object[]} options.orderBy
|
|
447
|
+
* @property {?string[]} options.groupBy
|
|
448
|
+
* @property {?string} options.having
|
|
449
|
+
* @property {?number} options.limit
|
|
450
|
+
* @property {?number} options.offset
|
|
451
|
+
* @property {?object} options.where
|
|
452
|
+
* @property {?string[]} options.whereRaw
|
|
453
|
+
* @property {?object} options.whereQuery
|
|
454
|
+
* @property {?{condition,callback}} options.when
|
|
455
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
456
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
457
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
458
|
+
* @property {?string[]} options.relations
|
|
459
|
+
* @property {string[]} options.relationExists
|
|
460
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
461
|
+
* @property {?boolean} options.debug
|
|
462
|
+
* @returns {promise<{ meta , data[]}>}
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* import { User } from '../Models/User'
|
|
466
|
+
*
|
|
467
|
+
* const users = await User.cursorPagination({
|
|
468
|
+
* limit :15,
|
|
469
|
+
* cursor : "ey....",
|
|
470
|
+
* select : { id: true, name: true }
|
|
471
|
+
* })
|
|
472
|
+
*/
|
|
473
|
+
static async cursorPagination(options = {}) {
|
|
474
|
+
return await (0, Repository_1.Repository)(this).cursorPagination(options);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
379
477
|
*
|
|
478
|
+
* The 'cursorPaginate' method is used to perform cursor-based pagination
|
|
479
|
+
* on a set of database query results obtained through the Query Builder.
|
|
480
|
+
*
|
|
481
|
+
* It uses cursor-based pagination instead of offset pagination, making it
|
|
482
|
+
* more efficient when navigating through large datasets.
|
|
483
|
+
* @static
|
|
484
|
+
* @type {?object} options
|
|
485
|
+
* @property {?object} options.select
|
|
486
|
+
* @property {?object} options.except
|
|
487
|
+
* @property {?object[]} options.orderBy
|
|
488
|
+
* @property {?string[]} options.groupBy
|
|
489
|
+
* @property {?string} options.having
|
|
490
|
+
* @property {?number} options.limit
|
|
491
|
+
* @property {?number} options.offset
|
|
492
|
+
* @property {?object} options.where
|
|
493
|
+
* @property {?string[]} options.whereRaw
|
|
494
|
+
* @property {?object} options.whereQuery
|
|
495
|
+
* @property {?{condition,callback}} options.when
|
|
496
|
+
* @property {?{localKey , referenceKey}[]} options.join
|
|
497
|
+
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
498
|
+
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
499
|
+
* @property {?string[]} options.relations
|
|
500
|
+
* @property {string[]} options.relationExists
|
|
501
|
+
* @property {?{condition,callback}} options.relationQuery
|
|
502
|
+
* @property {?boolean} options.debug
|
|
503
|
+
* @returns {promise<{ meta , data[]}>}
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* import { User } from '../Models/User'
|
|
507
|
+
*
|
|
508
|
+
* const users = await User.cursorPaginate({
|
|
509
|
+
* limit : 15,
|
|
510
|
+
* cursor : "ey....",
|
|
511
|
+
* select : { id: true, name: true }
|
|
512
|
+
* })
|
|
380
513
|
*/
|
|
381
|
-
static async
|
|
382
|
-
return await (0, Repository_1.Repository)(this).
|
|
514
|
+
static async cursorPaginate(options = {}) {
|
|
515
|
+
return await (0, Repository_1.Repository)(this).cursorPaginate(options);
|
|
383
516
|
}
|
|
384
517
|
/**
|
|
385
|
-
* The '
|
|
518
|
+
* The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
|
|
386
519
|
*
|
|
387
|
-
*
|
|
520
|
+
* It returns a boolean value indicating whether there are any matching records.
|
|
388
521
|
* @static
|
|
389
522
|
* @type {?object} options
|
|
390
523
|
* @property {?object} options.select
|
|
@@ -414,11 +547,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
414
547
|
* })
|
|
415
548
|
*
|
|
416
549
|
*/
|
|
417
|
-
static
|
|
418
|
-
return (0, Repository_1.Repository)(this).
|
|
550
|
+
static async exists(options) {
|
|
551
|
+
return await (0, Repository_1.Repository)(this).exists(options);
|
|
419
552
|
}
|
|
420
553
|
/**
|
|
421
|
-
* The '
|
|
554
|
+
* The 'toQuery' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
|
|
422
555
|
*
|
|
423
556
|
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
|
|
424
557
|
* @static
|
|
@@ -443,24 +576,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
443
576
|
* @example
|
|
444
577
|
* import { User } from '../Models/User'
|
|
445
578
|
*
|
|
446
|
-
* const users = await User.
|
|
579
|
+
* const users = await User.exists({
|
|
447
580
|
* where : {
|
|
448
581
|
* id: 1
|
|
449
582
|
* }
|
|
450
583
|
* })
|
|
451
584
|
*
|
|
452
|
-
* // [1]
|
|
453
|
-
*
|
|
454
585
|
*/
|
|
455
|
-
static
|
|
456
|
-
return (0, Repository_1.Repository)(this).
|
|
586
|
+
static toQuery(options) {
|
|
587
|
+
return (0, Repository_1.Repository)(this).toString(options);
|
|
457
588
|
}
|
|
458
589
|
/**
|
|
590
|
+
* The 'toArray' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
|
|
459
591
|
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
* It retrieves multiple records from a database table based on the criteria specified in the query.
|
|
463
|
-
* The optional callback function can be used to modify the generated SQL query before execution.
|
|
592
|
+
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
|
|
464
593
|
* @static
|
|
465
594
|
* @type {?object} options
|
|
466
595
|
* @property {?object} options.select
|
|
@@ -477,25 +606,23 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
477
606
|
* @property {?{localKey , referenceKey}[]} options.join
|
|
478
607
|
* @property {?{localKey , referenceKey}[]} options.rightJoin
|
|
479
608
|
* @property {?{localKey , referenceKey}[]} options.leftJoin
|
|
480
|
-
* @property {?string[]} options.relations
|
|
481
|
-
* @property {string[]} options.relationExists
|
|
482
|
-
* @property {?{condition,callback}} options.relationQuery
|
|
483
609
|
* @property {?boolean} options.debug
|
|
484
|
-
* @
|
|
610
|
+
* @property {?number} options.page
|
|
485
611
|
*
|
|
486
612
|
* @example
|
|
487
|
-
*
|
|
613
|
+
* import { User } from '../Models/User'
|
|
488
614
|
*
|
|
489
|
-
*
|
|
490
|
-
* select : { id: true, name: true },
|
|
615
|
+
* const users = await User.toArray('id',{
|
|
491
616
|
* where : {
|
|
492
617
|
* id: 1
|
|
493
618
|
* }
|
|
494
619
|
* })
|
|
495
620
|
*
|
|
621
|
+
* // [1]
|
|
622
|
+
*
|
|
496
623
|
*/
|
|
497
|
-
static
|
|
498
|
-
return
|
|
624
|
+
static toArray(column, options = {}) {
|
|
625
|
+
return (0, Repository_1.Repository)(this).toArray(column, options);
|
|
499
626
|
}
|
|
500
627
|
/**
|
|
501
628
|
* The 'count' method is used to retrieve the total number of records that match the specified query conditions.
|
|
@@ -785,7 +912,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
785
912
|
* selecting a cache driver, managing cache entries, and optionally generating
|
|
786
913
|
* model-scoped cache keys.
|
|
787
914
|
*
|
|
788
|
-
* When the `namespace`
|
|
915
|
+
* When the `namespace` is default true, cache keys are automatically prefixed
|
|
789
916
|
* with the current database and table name in the following format:
|
|
790
917
|
*
|
|
791
918
|
* `database:table:key`
|
|
@@ -800,61 +927,95 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
800
927
|
*
|
|
801
928
|
* @example
|
|
802
929
|
* // Store a value using a model-scoped cache key.
|
|
803
|
-
* await User.cache.set("users", users, 60_000
|
|
804
|
-
* namespace: true
|
|
805
|
-
* });
|
|
930
|
+
* await User.cache.set("users", users, 60_000);
|
|
806
931
|
*
|
|
807
932
|
* @example
|
|
808
933
|
* // Retrieve a model-scoped cache value.
|
|
809
|
-
* const users = await User.cache.get("users"
|
|
810
|
-
* namespace: true
|
|
811
|
-
* });
|
|
934
|
+
* const users = await User.cache.get("users");
|
|
812
935
|
*
|
|
813
936
|
* @example
|
|
814
937
|
* // Check whether a cache entry exists.
|
|
815
|
-
* const exists = await User.cache.exists("users"
|
|
816
|
-
* namespace: true
|
|
817
|
-
* });
|
|
938
|
+
* const exists = await User.cache.exists("users");
|
|
818
939
|
*
|
|
819
940
|
* @example
|
|
820
941
|
* // Delete a cache entry.
|
|
821
|
-
* await User.cache.delete("users"
|
|
822
|
-
* namespace: true
|
|
823
|
-
* });
|
|
942
|
+
* await User.cache.delete("users");
|
|
824
943
|
*
|
|
825
944
|
* @example
|
|
826
945
|
* // Clear all cache entries.
|
|
827
946
|
* await User.cache.clear();
|
|
828
947
|
*/
|
|
829
948
|
static get cache() {
|
|
830
|
-
const
|
|
949
|
+
const keyWithNamespace = (key) => {
|
|
831
950
|
const db = new this().database();
|
|
832
951
|
const table = new this().getTableName();
|
|
833
952
|
return `${db}:${table}:${key}`;
|
|
834
953
|
};
|
|
835
954
|
return {
|
|
955
|
+
/** Returns the configured cache provider. */
|
|
836
956
|
provider: () => Cache_1.Cache.provider(),
|
|
957
|
+
/**
|
|
958
|
+
* Sets the cache driver.
|
|
959
|
+
*
|
|
960
|
+
* @param driver - The cache driver to use: `db`, `memory`, or `redis`.
|
|
961
|
+
*/
|
|
837
962
|
driver: (driver) => Cache_1.Cache.driver(driver),
|
|
963
|
+
/** Retrieves all cached entries. */
|
|
838
964
|
all: async () => {
|
|
839
965
|
return await Cache_1.Cache.all();
|
|
840
966
|
},
|
|
967
|
+
/** Removes all cached entries. */
|
|
841
968
|
clear: async () => {
|
|
842
969
|
return await Cache_1.Cache.clear();
|
|
843
970
|
},
|
|
844
|
-
|
|
845
|
-
|
|
971
|
+
/**
|
|
972
|
+
* Retrieves a cached value by key.
|
|
973
|
+
*
|
|
974
|
+
* @param key - The cache key.
|
|
975
|
+
* @param options - Cache options.
|
|
976
|
+
* @param options.namespace - Whether to prefix the key with the model's database and table. Defaults to `true`.
|
|
977
|
+
* @returns The cached value, or the result returned by the cache provider when the key is missing.
|
|
978
|
+
*/
|
|
979
|
+
get: async (key, { namespace = true } = {}) => {
|
|
980
|
+
const cacheKey = namespace ? keyWithNamespace(key) : key;
|
|
846
981
|
return await Cache_1.Cache.get(cacheKey);
|
|
847
982
|
},
|
|
848
|
-
|
|
849
|
-
|
|
983
|
+
/**
|
|
984
|
+
* Checks whether a cache key exists.
|
|
985
|
+
*
|
|
986
|
+
* @param key - The cache key.
|
|
987
|
+
* @param options - Cache options.
|
|
988
|
+
* @param options.namespace - Whether to prefix the key with the model's database and table. Defaults to `true`.
|
|
989
|
+
* @returns Whether the cache key exists.
|
|
990
|
+
*/
|
|
991
|
+
exists: async (key, { namespace = true } = {}) => {
|
|
992
|
+
const cacheKey = namespace ? keyWithNamespace(key) : key;
|
|
850
993
|
return await Cache_1.Cache.exists(cacheKey);
|
|
851
994
|
},
|
|
852
|
-
|
|
853
|
-
|
|
995
|
+
/**
|
|
996
|
+
* Stores a value in the cache.
|
|
997
|
+
*
|
|
998
|
+
* @param key - The cache key.
|
|
999
|
+
* @param value - The value to cache.
|
|
1000
|
+
* @param ms - The cache expiration time in milliseconds.
|
|
1001
|
+
* @param options - Cache options.
|
|
1002
|
+
* @param options.namespace - Whether to prefix the key with the model's database and table. Defaults to `true`.
|
|
1003
|
+
* @returns The result returned by the cache provider.
|
|
1004
|
+
*/
|
|
1005
|
+
set: async (key, value, ms, { namespace = true } = {}) => {
|
|
1006
|
+
const cacheKey = namespace ? keyWithNamespace(key) : key;
|
|
854
1007
|
return await Cache_1.Cache.set(cacheKey, value, ms);
|
|
855
1008
|
},
|
|
856
|
-
|
|
857
|
-
|
|
1009
|
+
/**
|
|
1010
|
+
* Deletes a cached value by key.
|
|
1011
|
+
*
|
|
1012
|
+
* @param key - The cache key.
|
|
1013
|
+
* @param options - Cache options.
|
|
1014
|
+
* @param options.namespace - Whether to prefix the key with the model's database and table. Defaults to `true`.
|
|
1015
|
+
* @returns The result returned by the cache provider.
|
|
1016
|
+
*/
|
|
1017
|
+
delete: async (key, { namespace = true } = {}) => {
|
|
1018
|
+
const cacheKey = namespace ? keyWithNamespace(key) : key;
|
|
858
1019
|
return await Cache_1.Cache.delete(cacheKey);
|
|
859
1020
|
},
|
|
860
1021
|
};
|
|
@@ -923,12 +1084,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
923
1084
|
const model = new this()
|
|
924
1085
|
.bind(trx);
|
|
925
1086
|
const sqlLockTable = model
|
|
926
|
-
.
|
|
1087
|
+
.getQueryBuilder()
|
|
927
1088
|
.lockTable(mode);
|
|
928
1089
|
await model.rawQuery(sqlLockTable);
|
|
929
1090
|
const results = await handler(model);
|
|
930
1091
|
const sqlUnlockTable = model
|
|
931
|
-
.
|
|
1092
|
+
.getQueryBuilder()
|
|
932
1093
|
.unlockTable();
|
|
933
1094
|
if (sqlUnlockTable != null) {
|
|
934
1095
|
await model
|
|
@@ -1485,7 +1646,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1485
1646
|
* The lifecycle event to register. Determines which hook
|
|
1486
1647
|
* collection the function(s) will be stored in.
|
|
1487
1648
|
*
|
|
1488
|
-
* @param {Function | Function[]}
|
|
1649
|
+
* @param {Function | Function[]} callback
|
|
1489
1650
|
* A function or array of functions to be executed when the
|
|
1490
1651
|
* lifecycle event is triggered. All functions must be valid
|
|
1491
1652
|
* JavaScript functions, otherwise an assertion error is thrown.
|
|
@@ -1515,7 +1676,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1515
1676
|
* .useLifecycle("beforeInsert", fnA)
|
|
1516
1677
|
* .useLifecycle("afterInsert", fnB);
|
|
1517
1678
|
*/
|
|
1518
|
-
useLifecycle(type,
|
|
1679
|
+
useLifecycle(type, callback) {
|
|
1519
1680
|
const MAP = {
|
|
1520
1681
|
beforeInsert: "LIFECYCLE_BEFORE_INSERTS",
|
|
1521
1682
|
afterInsert: "LIFECYCLE_AFTER_INSERTS",
|
|
@@ -1524,7 +1685,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1524
1685
|
beforeRemove: "LIFECYCLE_BEFORE_REMOVES",
|
|
1525
1686
|
afterRemove: "LIFECYCLE_AFTER_REMOVES",
|
|
1526
1687
|
};
|
|
1527
|
-
const lists = Array.isArray(
|
|
1688
|
+
const lists = Array.isArray(callback) ? callback : [callback];
|
|
1528
1689
|
for (const func of lists) {
|
|
1529
1690
|
if (typeof func !== "function") {
|
|
1530
1691
|
throw this._assertError(`this 'function' is not a function`);
|
|
@@ -1719,16 +1880,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1719
1880
|
* @param {Object} object
|
|
1720
1881
|
* @property {string} key key of cache
|
|
1721
1882
|
* @property {number} expires ms
|
|
1722
|
-
* @property {boolean} namespace whether to use namespace for cache key, default is
|
|
1883
|
+
* @property {boolean} namespace whether to use namespace for cache key, default is true, namespace is `${database}:${table}:${key}`
|
|
1723
1884
|
* @returns {this} this
|
|
1724
1885
|
*/
|
|
1725
|
-
cache({ key, expires, namespace }) {
|
|
1726
|
-
const
|
|
1886
|
+
cache({ key, expires, namespace = true, }) {
|
|
1887
|
+
const keyWithNamespace = (key) => {
|
|
1727
1888
|
const db = this.database();
|
|
1728
1889
|
const table = this.getTableName();
|
|
1729
1890
|
return `${db}:${table}:${key}`;
|
|
1730
1891
|
};
|
|
1731
|
-
const cacheKey = namespace ?
|
|
1892
|
+
const cacheKey = namespace ? keyWithNamespace(key) : key;
|
|
1732
1893
|
this.$state.set("CACHE", {
|
|
1733
1894
|
key: cacheKey,
|
|
1734
1895
|
expires
|
|
@@ -2173,7 +2334,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2173
2334
|
const startTime = +new Date();
|
|
2174
2335
|
const results = await this.$pool.query(sql);
|
|
2175
2336
|
const endTime = +new Date();
|
|
2176
|
-
this.$utils.consoleDebug(sql, retry);
|
|
2337
|
+
this.$utils.consoleDebug(sql, retry, this.$state.get("NODE"));
|
|
2177
2338
|
this.$state.set("QUERIES", [...this.$state.get("QUERIES"), sql]);
|
|
2178
2339
|
this.$utils.consoleExec(startTime, endTime);
|
|
2179
2340
|
return results;
|
|
@@ -2195,10 +2356,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2195
2356
|
return result == null ? await getResults(sql) : result;
|
|
2196
2357
|
}
|
|
2197
2358
|
catch (error) {
|
|
2198
|
-
if (this.$state.get("
|
|
2199
|
-
this.$
|
|
2359
|
+
if (this.$state.get("STOP_RETRY")) {
|
|
2360
|
+
if (this.$state.get("DEBUG")) {
|
|
2361
|
+
this.$utils.consoleDebug(sql, false, this.$state.get("NODE"));
|
|
2362
|
+
}
|
|
2363
|
+
throw error;
|
|
2364
|
+
}
|
|
2200
2365
|
if (this.$state.get("JOIN")?.length)
|
|
2201
2366
|
throw error;
|
|
2367
|
+
if (this.$state.get("DEBUG")) {
|
|
2368
|
+
this.$utils.consoleDebug(sql, retry, this.$state.get("NODE"));
|
|
2369
|
+
}
|
|
2202
2370
|
const retryCount = Number(this.$state.get("RETRY"));
|
|
2203
2371
|
await this._checkSchemaOrNextError(error, retryCount, error);
|
|
2204
2372
|
return await this._queryStatement(sql, { retry: true });
|
|
@@ -2218,7 +2386,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2218
2386
|
const startTime = +new Date();
|
|
2219
2387
|
const results = await this.$pool.query(sql);
|
|
2220
2388
|
const endTime = +new Date();
|
|
2221
|
-
this.$utils.consoleDebug(sql, retry);
|
|
2389
|
+
this.$utils.consoleDebug(sql, retry, this.$state.get("NODE"));
|
|
2222
2390
|
this.$utils.consoleExec(startTime, endTime);
|
|
2223
2391
|
this.$state.set("QUERIES", [...this.$state.get("QUERIES"), sql]);
|
|
2224
2392
|
return results;
|
|
@@ -2240,9 +2408,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2240
2408
|
return result == null ? await getResults(sql) : result;
|
|
2241
2409
|
}
|
|
2242
2410
|
catch (error) {
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2411
|
+
if (this.$state.get("STOP_RETRY")) {
|
|
2412
|
+
if (this.$state.get("DEBUG")) {
|
|
2413
|
+
this.$utils.consoleDebug(sql, false, this.$state.get("NODE"));
|
|
2414
|
+
}
|
|
2415
|
+
throw error;
|
|
2416
|
+
}
|
|
2417
|
+
const retryCount = this.$state.get("RETRY");
|
|
2418
|
+
if (this.$state.get("DEBUG")) {
|
|
2419
|
+
this.$utils.consoleDebug(sql, retry, this.$state.get("NODE"));
|
|
2420
|
+
}
|
|
2246
2421
|
if (this.$state.get("JOIN")?.length)
|
|
2247
2422
|
throw error;
|
|
2248
2423
|
await this._checkSchemaOrNextError(error, retryCount, error);
|
|
@@ -2263,6 +2438,15 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2263
2438
|
this.$state.set("CTE", [...this.$state.get("CTE"), `${as} AS (${query})`]);
|
|
2264
2439
|
return this;
|
|
2265
2440
|
}
|
|
2441
|
+
/**
|
|
2442
|
+
* The 'stopRetry' method is used to stop retry in query statement
|
|
2443
|
+
*
|
|
2444
|
+
* @returns {this} this
|
|
2445
|
+
*/
|
|
2446
|
+
stopRetry() {
|
|
2447
|
+
this.$state.set('STOP_RETRY', true);
|
|
2448
|
+
return this;
|
|
2449
|
+
}
|
|
2266
2450
|
/**
|
|
2267
2451
|
* The 'disableSoftDelete' method is used to disable the soft delete.
|
|
2268
2452
|
*
|
|
@@ -3247,6 +3431,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3247
3431
|
this.$state.set("SAVE", "UPDATE");
|
|
3248
3432
|
return (await this.bind(this.$pool.get()).save());
|
|
3249
3433
|
}
|
|
3434
|
+
/**
|
|
3435
|
+
* @override
|
|
3436
|
+
* @returns {this} this
|
|
3437
|
+
*/
|
|
3438
|
+
void() {
|
|
3439
|
+
this.$state.set("VOID", true);
|
|
3440
|
+
return this;
|
|
3441
|
+
}
|
|
3250
3442
|
/**
|
|
3251
3443
|
*
|
|
3252
3444
|
* @returns {string} string
|
|
@@ -3487,10 +3679,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3487
3679
|
*/
|
|
3488
3680
|
whereJSON(column, { key, value, operator }) {
|
|
3489
3681
|
const formatedValue = this.$utils.formatQueryValue(value);
|
|
3682
|
+
const qb = this.getQueryBuilder();
|
|
3683
|
+
const json = qb.whereJson(this.bindColumn(String(column)), key);
|
|
3490
3684
|
this.$state.set("WHERE", [
|
|
3491
3685
|
...this.$state.get("WHERE"),
|
|
3492
3686
|
{
|
|
3493
|
-
column:
|
|
3687
|
+
column: json,
|
|
3494
3688
|
operator: `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
|
|
3495
3689
|
value: formatedValue
|
|
3496
3690
|
}
|
|
@@ -4449,7 +4643,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4449
4643
|
`${this.$constants("AS")}`,
|
|
4450
4644
|
"\`aggregate\`",
|
|
4451
4645
|
].join(" "));
|
|
4452
|
-
const sql = this.
|
|
4646
|
+
const sql = this.getQueryBuilder().select();
|
|
4453
4647
|
const result = await this._queryStatement(sql);
|
|
4454
4648
|
return Number(this._resultHandler(result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) || 0));
|
|
4455
4649
|
}
|
|
@@ -4467,7 +4661,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4467
4661
|
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4468
4662
|
: `${this.bindColumn(column)}`;
|
|
4469
4663
|
this.selectRaw(`${this.$constants("AVG")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4470
|
-
const sql = this.
|
|
4664
|
+
const sql = this.getQueryBuilder().select();
|
|
4471
4665
|
const result = await this._queryStatement(sql);
|
|
4472
4666
|
return Number(this._resultHandler((result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) ||
|
|
4473
4667
|
0) / result.length));
|
|
@@ -4486,7 +4680,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4486
4680
|
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4487
4681
|
: `${this.bindColumn(column)}`;
|
|
4488
4682
|
this.selectRaw(`${this.$constants("SUM")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4489
|
-
const sql = this.
|
|
4683
|
+
const sql = this.getQueryBuilder().select();
|
|
4490
4684
|
const result = await this._queryStatement(sql);
|
|
4491
4685
|
return Number(this._resultHandler(result.reduce((prev, cur) => prev + Number(cur?.aggregate ?? 0), 0) ||
|
|
4492
4686
|
0));
|
|
@@ -4505,7 +4699,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4505
4699
|
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4506
4700
|
: `${this.bindColumn(column)}`;
|
|
4507
4701
|
this.selectRaw(`${this.$constants("MAX")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4508
|
-
const sql = this.
|
|
4702
|
+
const sql = this.getQueryBuilder().select();
|
|
4509
4703
|
const result = await this._queryStatement(sql);
|
|
4510
4704
|
return Number(this._resultHandler(result.sort((a, b) => b?.aggregate - a?.aggregate)[0]?.aggregate || 0));
|
|
4511
4705
|
}
|
|
@@ -4522,7 +4716,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4522
4716
|
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
4523
4717
|
: `${this.bindColumn(column)}`;
|
|
4524
4718
|
this.selectRaw(`${this.$constants("MIN")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
4525
|
-
const sql = this.
|
|
4719
|
+
const sql = this.getQueryBuilder().select();
|
|
4526
4720
|
const result = await this._queryStatement(sql);
|
|
4527
4721
|
return Number(this._resultHandler(result.sort((a, b) => a?.aggregate - b?.aggregate)[0]?.aggregate || 0));
|
|
4528
4722
|
}
|
|
@@ -4552,7 +4746,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4552
4746
|
}
|
|
4553
4747
|
this.$state.set("DELETE", true);
|
|
4554
4748
|
await this._runBefore("remove");
|
|
4555
|
-
const r = await this._actionStatement(this.
|
|
4749
|
+
const r = await this._actionStatement(this.getQueryBuilder().remove());
|
|
4556
4750
|
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
4557
4751
|
await this._observer(result, "deleted");
|
|
4558
4752
|
await this._runAfter("remove", result);
|
|
@@ -4583,7 +4777,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4583
4777
|
}
|
|
4584
4778
|
this.$state.set("DELETE", true);
|
|
4585
4779
|
await this._runBefore("remove");
|
|
4586
|
-
const r = await this._actionStatement(this.
|
|
4780
|
+
const r = await this._actionStatement(this.getQueryBuilder().remove());
|
|
4587
4781
|
const result = Boolean(this._resultHandler(r?.$meta?.affected || false));
|
|
4588
4782
|
await this._observer(result, "deleted");
|
|
4589
4783
|
await this._runAfter("remove", result);
|
|
@@ -4611,7 +4805,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4611
4805
|
.from(DB_1.DB.raw(`(${from}) AS ${TEMP}`))
|
|
4612
4806
|
.selectRaw(PK));
|
|
4613
4807
|
this.$state.set("DELETE", true);
|
|
4614
|
-
const result = await this._actionStatement(this.
|
|
4808
|
+
const result = await this._actionStatement(this.getQueryBuilder().remove());
|
|
4615
4809
|
const r = Boolean(this._resultHandler(result?.$meta?.affected || false));
|
|
4616
4810
|
await this._observer(r, "deleted");
|
|
4617
4811
|
return r;
|
|
@@ -4633,7 +4827,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4633
4827
|
const queries = this.getQueries();
|
|
4634
4828
|
return queries[queries.length - 1] ?? this.toString();
|
|
4635
4829
|
}
|
|
4636
|
-
const sql = this.
|
|
4830
|
+
const sql = this.getQueryBuilder().any();
|
|
4637
4831
|
return this._resultHandler(sql);
|
|
4638
4832
|
}
|
|
4639
4833
|
/**
|
|
@@ -4647,18 +4841,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4647
4841
|
toSQL({ latest = false, oldest = false, } = {}) {
|
|
4648
4842
|
return this.toString({ latest, oldest });
|
|
4649
4843
|
}
|
|
4650
|
-
/**
|
|
4651
|
-
*
|
|
4652
|
-
* @override
|
|
4653
|
-
* @param {Function?} cb callback function return query sql
|
|
4654
|
-
* @returns {AsyncIterable<T.Result<this, K>>}
|
|
4655
|
-
*/
|
|
4656
|
-
async *toAsyncIterable(cb) {
|
|
4657
|
-
if (this.$state.get("VOID"))
|
|
4658
|
-
return yield* [];
|
|
4659
|
-
const results = await this.get(cb);
|
|
4660
|
-
return yield* results;
|
|
4661
|
-
}
|
|
4662
4844
|
/**
|
|
4663
4845
|
* @override
|
|
4664
4846
|
* @param {string=} column [column=id]
|
|
@@ -4667,7 +4849,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4667
4849
|
async toArray(column) {
|
|
4668
4850
|
if (column == null)
|
|
4669
4851
|
column = "id";
|
|
4670
|
-
const sql = this.
|
|
4852
|
+
const sql = this.getQueryBuilder().select();
|
|
4671
4853
|
const result = await this._queryStatement(sql);
|
|
4672
4854
|
const toArray = result.map((data) => data[column]);
|
|
4673
4855
|
return this._resultHandler(toArray);
|
|
@@ -4703,7 +4885,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4703
4885
|
await this._prepareQueryPipeline();
|
|
4704
4886
|
this.limit(1);
|
|
4705
4887
|
this._ensureRelationOwnerKeysSelected();
|
|
4706
|
-
let sql = this.
|
|
4888
|
+
let sql = this.getQueryBuilder().select();
|
|
4707
4889
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
4708
4890
|
sql = String(this.$relation.loadExists());
|
|
4709
4891
|
if (cb) {
|
|
@@ -4745,7 +4927,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4745
4927
|
await this._prepareQueryPipeline();
|
|
4746
4928
|
this.limit(1);
|
|
4747
4929
|
this._ensureRelationOwnerKeysSelected();
|
|
4748
|
-
let sql = this.
|
|
4930
|
+
let sql = this.getQueryBuilder().select();
|
|
4749
4931
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
4750
4932
|
sql = String(this.$relation.loadExists());
|
|
4751
4933
|
return await this._execute({
|
|
@@ -4774,7 +4956,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4774
4956
|
return [];
|
|
4775
4957
|
await this._prepareQueryPipeline();
|
|
4776
4958
|
this._ensureRelationOwnerKeysSelected();
|
|
4777
|
-
let sql = this.
|
|
4959
|
+
let sql = this.getQueryBuilder().select();
|
|
4778
4960
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
4779
4961
|
sql = String(this.$relation.loadExists());
|
|
4780
4962
|
if (cb) {
|
|
@@ -4798,6 +4980,97 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4798
4980
|
async findMany(cb) {
|
|
4799
4981
|
return await this.get(cb);
|
|
4800
4982
|
}
|
|
4983
|
+
/**
|
|
4984
|
+
* @override
|
|
4985
|
+
* @param {Function} callback callback function that receives each retrieved record
|
|
4986
|
+
* @param {Object?} opts query opts
|
|
4987
|
+
* @param {number?} opts.limit number of records to retrieve per query
|
|
4988
|
+
* @param {number?} opts.offset number of records to skip before processing
|
|
4989
|
+
* @param {number?} opts.delayMs delay in milliseconds between each batch
|
|
4990
|
+
* @returns {Promise<void>}
|
|
4991
|
+
*/
|
|
4992
|
+
async lazy(callback, opts = {}) {
|
|
4993
|
+
const limit = opts.limit ?? 100;
|
|
4994
|
+
const delayMs = opts.delayMs ?? 0;
|
|
4995
|
+
let offset = opts.offset ?? 0;
|
|
4996
|
+
while (true) {
|
|
4997
|
+
const results = await this
|
|
4998
|
+
.limit(limit)
|
|
4999
|
+
.offset(offset)
|
|
5000
|
+
.get();
|
|
5001
|
+
if (results.length === 0) {
|
|
5002
|
+
break;
|
|
5003
|
+
}
|
|
5004
|
+
for (const result of results) {
|
|
5005
|
+
await callback(result);
|
|
5006
|
+
}
|
|
5007
|
+
if (results.length < limit) {
|
|
5008
|
+
break;
|
|
5009
|
+
}
|
|
5010
|
+
offset += results.length;
|
|
5011
|
+
if (delayMs > 0) {
|
|
5012
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
}
|
|
5016
|
+
/**
|
|
5017
|
+
* @override
|
|
5018
|
+
* @param {Function} callback callback function that receives each retrieved record
|
|
5019
|
+
* @param {Object?} opts query opts
|
|
5020
|
+
* @param {number?} opts.limit number of records to retrieve per query
|
|
5021
|
+
* @param {number?} opts.offset number of records to skip before processing
|
|
5022
|
+
* @param {number?} opts.delayMs delay in milliseconds between each batch
|
|
5023
|
+
* @returns {Promise<void>}
|
|
5024
|
+
*/
|
|
5025
|
+
async chunk(callback, opts = {}) {
|
|
5026
|
+
const limit = opts.limit ?? 100;
|
|
5027
|
+
const delayMs = opts.delayMs ?? 0;
|
|
5028
|
+
let offset = opts.offset ?? 0;
|
|
5029
|
+
while (true) {
|
|
5030
|
+
const results = await this
|
|
5031
|
+
.limit(limit)
|
|
5032
|
+
.offset(offset)
|
|
5033
|
+
.get();
|
|
5034
|
+
if (results.length === 0) {
|
|
5035
|
+
break;
|
|
5036
|
+
}
|
|
5037
|
+
await callback(results);
|
|
5038
|
+
if (results.length < limit) {
|
|
5039
|
+
break;
|
|
5040
|
+
}
|
|
5041
|
+
offset += results.length;
|
|
5042
|
+
if (delayMs > 0) {
|
|
5043
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
5044
|
+
}
|
|
5045
|
+
}
|
|
5046
|
+
}
|
|
5047
|
+
/**
|
|
5048
|
+
* @override
|
|
5049
|
+
* @returns {AsyncGenerator<T>} An async generator that yields result rows.
|
|
5050
|
+
*
|
|
5051
|
+
* @example
|
|
5052
|
+
* ```ts
|
|
5053
|
+
* for await (const row of new User().stream()) {
|
|
5054
|
+
* console.log(row);
|
|
5055
|
+
* }
|
|
5056
|
+
* ```
|
|
5057
|
+
*/
|
|
5058
|
+
async *stream() {
|
|
5059
|
+
if (this.$state.get('VOID')) {
|
|
5060
|
+
return yield* [];
|
|
5061
|
+
}
|
|
5062
|
+
const sql = this.toString();
|
|
5063
|
+
if (this.$state.get("DEBUG")) {
|
|
5064
|
+
const startTime = +new Date();
|
|
5065
|
+
const results = await this.$pool.stream(sql);
|
|
5066
|
+
const endTime = +new Date();
|
|
5067
|
+
this.$utils.consoleDebug(sql, false, this.$state.get("NODE"));
|
|
5068
|
+
this.$state.set("QUERIES", [...this.$state.get("QUERIES"), sql]);
|
|
5069
|
+
this.$utils.consoleExec(startTime, endTime);
|
|
5070
|
+
return yield* results;
|
|
5071
|
+
}
|
|
5072
|
+
return yield* await this.$pool.stream(sql);
|
|
5073
|
+
}
|
|
4801
5074
|
/**
|
|
4802
5075
|
* @override
|
|
4803
5076
|
* @param {object?} opts by default page = 1 , limit = 15
|
|
@@ -4806,23 +5079,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4806
5079
|
* @property {boolean?} opts.distinct
|
|
4807
5080
|
* @returns {promise<Pagination>} Pagination
|
|
4808
5081
|
*/
|
|
4809
|
-
async pagination(opts) {
|
|
4810
|
-
|
|
4811
|
-
|
|
5082
|
+
async pagination(opts = {}) {
|
|
5083
|
+
const limit = this.$utils.softNumber(opts.limit ?? 15);
|
|
5084
|
+
const page = this.$utils.softNumber(opts.page ?? 1);
|
|
4812
5085
|
if (opts?.distinct) {
|
|
4813
5086
|
this.distinct();
|
|
4814
5087
|
}
|
|
4815
|
-
if (opts != null) {
|
|
4816
|
-
limit = this.$utils.softNumber(opts?.limit || limit);
|
|
4817
|
-
page = this.$utils.softNumber(opts?.page || page);
|
|
4818
|
-
}
|
|
4819
5088
|
await this._prepareQueryPipeline();
|
|
4820
5089
|
const offset = (page - 1) * limit;
|
|
4821
5090
|
this.$state.set("PAGE", page);
|
|
4822
5091
|
this.limit(limit);
|
|
4823
5092
|
this.offset(offset);
|
|
4824
5093
|
this._ensureRelationOwnerKeysSelected();
|
|
4825
|
-
let sql = this.
|
|
5094
|
+
let sql = this.getQueryBuilder().select();
|
|
4826
5095
|
if (this.$state.get("RELATIONS_EXISTS"))
|
|
4827
5096
|
sql = String(this.$relation.loadExists());
|
|
4828
5097
|
return await this._execute({
|
|
@@ -4839,9 +5108,82 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4839
5108
|
* @property {boolean?} opts.distinct
|
|
4840
5109
|
* @returns {promise<Pagination>} Pagination
|
|
4841
5110
|
*/
|
|
4842
|
-
async paginate(opts) {
|
|
5111
|
+
async paginate(opts = {}) {
|
|
4843
5112
|
return await this.pagination(opts);
|
|
4844
5113
|
}
|
|
5114
|
+
/**
|
|
5115
|
+
* The 'cursorPagination' method is used to perform cursor-based pagination
|
|
5116
|
+
* on a set of database query results obtained through the Query Builder.
|
|
5117
|
+
*
|
|
5118
|
+
* It uses cursor-based pagination instead of offset pagination, making it
|
|
5119
|
+
* more efficient when navigating through large datasets.
|
|
5120
|
+
*
|
|
5121
|
+
* @param {Object?} opts query opts
|
|
5122
|
+
* @param {number?} opts.limit number of records per page, default is 15
|
|
5123
|
+
* @param {string?} opts.cursor cursor used to retrieve the next page
|
|
5124
|
+
* @param {boolean?} opts.distinct whether to retrieve distinct records
|
|
5125
|
+
* @returns {Promise<T.CursorPaginateResult>}
|
|
5126
|
+
*/
|
|
5127
|
+
async cursorPagination(opts = {}) {
|
|
5128
|
+
const limit = this.$utils.softNumber(opts.limit ?? 15);
|
|
5129
|
+
if (opts.distinct) {
|
|
5130
|
+
this.distinct();
|
|
5131
|
+
}
|
|
5132
|
+
let cursor = null;
|
|
5133
|
+
if (opts.cursor) {
|
|
5134
|
+
try {
|
|
5135
|
+
cursor = JSON.parse(Buffer.from(opts.cursor, 'base64').toString('utf8'));
|
|
5136
|
+
}
|
|
5137
|
+
catch {
|
|
5138
|
+
throw new Error('Invalid cursor');
|
|
5139
|
+
}
|
|
5140
|
+
}
|
|
5141
|
+
;
|
|
5142
|
+
const primaryKey = this.$state.get('PRIMARY_KEY');
|
|
5143
|
+
const results = await this
|
|
5144
|
+
.when(cursor != null, q => q.where(primaryKey, '>', cursor[primaryKey]))
|
|
5145
|
+
.orderBy(primaryKey, 'ASC')
|
|
5146
|
+
.limit(limit + 1)
|
|
5147
|
+
.get();
|
|
5148
|
+
const hasNextPage = results.length > limit;
|
|
5149
|
+
if (hasNextPage) {
|
|
5150
|
+
results.pop();
|
|
5151
|
+
}
|
|
5152
|
+
const lastResult = results[results.length - 1];
|
|
5153
|
+
const nextCursor = hasNextPage && lastResult
|
|
5154
|
+
? Buffer.from(JSON.stringify({
|
|
5155
|
+
[primaryKey]: lastResult[primaryKey],
|
|
5156
|
+
timestamp: +new Date()
|
|
5157
|
+
})).toString('base64')
|
|
5158
|
+
: null;
|
|
5159
|
+
return {
|
|
5160
|
+
meta: {
|
|
5161
|
+
limit: limit,
|
|
5162
|
+
count: results.length,
|
|
5163
|
+
next: {
|
|
5164
|
+
cursor: nextCursor,
|
|
5165
|
+
has: hasNextPage,
|
|
5166
|
+
}
|
|
5167
|
+
},
|
|
5168
|
+
data: results,
|
|
5169
|
+
};
|
|
5170
|
+
}
|
|
5171
|
+
/**
|
|
5172
|
+
* The 'cursorPaginate' method is used to perform cursor-based pagination
|
|
5173
|
+
* on a set of database query results obtained through the Query Builder.
|
|
5174
|
+
*
|
|
5175
|
+
* It uses cursor-based pagination instead of offset pagination, making it
|
|
5176
|
+
* more efficient when navigating through large datasets.
|
|
5177
|
+
*
|
|
5178
|
+
* @param {Object?} opts query opts
|
|
5179
|
+
* @param {number?} opts.limit number of records per page, default is 15
|
|
5180
|
+
* @param {string?} opts.cursor cursor used to retrieve the next page
|
|
5181
|
+
* @param {boolean?} opts.distinct whether to retrieve distinct records
|
|
5182
|
+
* @returns {Promise<T.CursorPaginateResult>}
|
|
5183
|
+
*/
|
|
5184
|
+
async cursorPaginate(opts = {}) {
|
|
5185
|
+
return await this.cursorPagination(opts);
|
|
5186
|
+
}
|
|
4845
5187
|
/**
|
|
4846
5188
|
* @override
|
|
4847
5189
|
* @param {string} column
|
|
@@ -5405,13 +5747,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5405
5747
|
* @property {boolean} options.index - add index to column
|
|
5406
5748
|
* @returns {Promise<void>}
|
|
5407
5749
|
*/
|
|
5408
|
-
async sync({ force = false, foreign = false, changed = false, index = false, } = {}) {
|
|
5750
|
+
async sync({ force = false, foreign = false, changed = false, index = false, unique = false } = {}) {
|
|
5409
5751
|
return await new Schema_1.Schema()["syncExecute"]({
|
|
5410
5752
|
models: [this],
|
|
5411
5753
|
force,
|
|
5412
5754
|
foreign,
|
|
5413
5755
|
changed,
|
|
5414
5756
|
index,
|
|
5757
|
+
unique,
|
|
5415
5758
|
log: this.$state.get("DEBUG"),
|
|
5416
5759
|
});
|
|
5417
5760
|
}
|
|
@@ -5926,7 +6269,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
5926
6269
|
* generate sql statements
|
|
5927
6270
|
* @override
|
|
5928
6271
|
*/
|
|
5929
|
-
|
|
6272
|
+
getQueryBuilder() {
|
|
5930
6273
|
this._handleSelect();
|
|
5931
6274
|
this._handleSoftDelete();
|
|
5932
6275
|
return this._buildQueryStatement();
|
|
@@ -6065,12 +6408,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6065
6408
|
if (cache == null)
|
|
6066
6409
|
return;
|
|
6067
6410
|
const startTime = +new Date();
|
|
6068
|
-
const findCache = await
|
|
6411
|
+
const findCache = await Cache_1.Cache.get(cache.key);
|
|
6069
6412
|
if (findCache == null)
|
|
6070
6413
|
return;
|
|
6071
6414
|
if (this.$state.get("DEBUG")) {
|
|
6072
6415
|
this.$utils.consoleCache({
|
|
6073
|
-
driver:
|
|
6416
|
+
driver: Cache_1.Cache.provider(),
|
|
6074
6417
|
key: cache.key,
|
|
6075
6418
|
expires: cache.expires,
|
|
6076
6419
|
type: this.$utils.typeOf(findCache),
|
|
@@ -6088,10 +6431,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6088
6431
|
const cache = this.$state.get("CACHE");
|
|
6089
6432
|
if (cache == null)
|
|
6090
6433
|
return;
|
|
6091
|
-
const exists = await
|
|
6434
|
+
const exists = await Cache_1.Cache.exists(cache.key);
|
|
6092
6435
|
if (exists)
|
|
6093
6436
|
return;
|
|
6094
|
-
await
|
|
6437
|
+
await Cache_1.Cache.set(cache.key, data, cache.expires);
|
|
6095
6438
|
}
|
|
6096
6439
|
async _execute({ sql, type, message, options }) {
|
|
6097
6440
|
const middlewares = this.$state.get("MIDDLEWARES");
|
|
@@ -6427,7 +6770,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6427
6770
|
await this._queryInsertModel();
|
|
6428
6771
|
}
|
|
6429
6772
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
6430
|
-
const result = await this._actionStatement(this.
|
|
6773
|
+
const result = await this._actionStatement(this.getQueryBuilder().insert());
|
|
6431
6774
|
if (!result)
|
|
6432
6775
|
return this._resultHandler(null);
|
|
6433
6776
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
@@ -6454,7 +6797,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6454
6797
|
await this._queryInsertModel();
|
|
6455
6798
|
}
|
|
6456
6799
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
6457
|
-
const result = await this._actionStatement(this.
|
|
6800
|
+
const result = await this._actionStatement(this.getQueryBuilder().insert());
|
|
6458
6801
|
if (this.$state.get("VOID"))
|
|
6459
6802
|
return this._resultHandler(undefined);
|
|
6460
6803
|
if (!result)
|
|
@@ -6488,7 +6831,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6488
6831
|
for (const v of data) {
|
|
6489
6832
|
await this._validateSchema(v, "insert");
|
|
6490
6833
|
}
|
|
6491
|
-
const result = await this._actionStatement(this.
|
|
6834
|
+
const result = await this._actionStatement(this.getQueryBuilder().insert());
|
|
6492
6835
|
if (this.$state.get("VOID"))
|
|
6493
6836
|
return this._resultHandler(undefined);
|
|
6494
6837
|
if (!result)
|
|
@@ -6527,7 +6870,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6527
6870
|
await this._queryInsertModel();
|
|
6528
6871
|
}
|
|
6529
6872
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
6530
|
-
const result = await this._actionStatement(this.
|
|
6873
|
+
const result = await this._actionStatement(this.getQueryBuilder().insert());
|
|
6531
6874
|
if (this.$state.get("VOID") || !result)
|
|
6532
6875
|
return this._resultHandler(undefined);
|
|
6533
6876
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
@@ -6558,7 +6901,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6558
6901
|
await this._queryUpdateModel();
|
|
6559
6902
|
}
|
|
6560
6903
|
await this._validateSchema(this.$state.get("DATA"), "update");
|
|
6561
|
-
const result = await this._actionStatement(this.
|
|
6904
|
+
const result = await this._actionStatement(this.getQueryBuilder().update());
|
|
6562
6905
|
if (this.$state.get("VOID") || !result)
|
|
6563
6906
|
return this._resultHandler(undefined);
|
|
6564
6907
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
@@ -6597,7 +6940,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6597
6940
|
await this._queryInsertModel();
|
|
6598
6941
|
}
|
|
6599
6942
|
await this._validateSchema(this.$state.get("DATA"), "insert");
|
|
6600
|
-
const result = await this._actionStatement(this.
|
|
6943
|
+
const result = await this._actionStatement(this.getQueryBuilder().insert());
|
|
6601
6944
|
if (this.$state.get("VOID") || !result)
|
|
6602
6945
|
return this._resultHandler(undefined);
|
|
6603
6946
|
await this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
@@ -6655,7 +6998,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6655
6998
|
await this._queryUpdateModel();
|
|
6656
6999
|
}
|
|
6657
7000
|
await this._validateSchema(this.$state.get("DATA"), "update");
|
|
6658
|
-
const result = await this._actionStatement(this.
|
|
7001
|
+
const result = await this._actionStatement(this.getQueryBuilder().update());
|
|
6659
7002
|
if (this.$state.get("VOID") || !result || result == null) {
|
|
6660
7003
|
return this._resultHandler(undefined);
|
|
6661
7004
|
}
|
|
@@ -6983,7 +7326,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6983
7326
|
return formatObject(data);
|
|
6984
7327
|
}
|
|
6985
7328
|
_initialModel() {
|
|
6986
|
-
this.$cache = Cache_1.Cache;
|
|
6987
7329
|
this.$state = new StateManager_1.StateManager("model");
|
|
6988
7330
|
this.metaTag("MAIN");
|
|
6989
7331
|
this._makeTableName();
|
|
@@ -6996,7 +7338,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
6996
7338
|
this.useUUID();
|
|
6997
7339
|
if (globalSettings.timestamp)
|
|
6998
7340
|
this.useTimestamp();
|
|
6999
|
-
if (globalSettings.logger
|
|
7341
|
+
if (globalSettings.logger) {
|
|
7000
7342
|
this.useLogger({
|
|
7001
7343
|
selected: globalSettings.logger?.selected,
|
|
7002
7344
|
inserted: globalSettings.logger?.inserted,
|