tspace-mysql 1.9.0-beta.6 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +75 -5
  2. package/dist/lib/core/Builder.d.ts +2 -2
  3. package/dist/lib/core/Builder.js +3 -3
  4. package/dist/lib/core/Builder.js.map +1 -1
  5. package/dist/lib/core/Driver/index.d.ts +1 -0
  6. package/dist/lib/core/Driver/index.js.map +1 -1
  7. package/dist/lib/core/Driver/mariadb/MariadbDriver.js +22 -3
  8. package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
  9. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +1 -0
  10. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +4 -0
  11. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
  12. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +1 -0
  13. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +4 -0
  14. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -1
  15. package/dist/lib/core/Driver/mysql/MysqlDriver.js +21 -22
  16. package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
  17. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +1 -0
  18. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +4 -0
  19. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
  20. package/dist/lib/core/Driver/postgres/PostgresDriver.js +17 -2
  21. package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
  22. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +1 -0
  23. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +4 -0
  24. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
  25. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +1 -0
  26. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +4 -0
  27. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -1
  28. package/dist/lib/core/Model.d.ts +322 -1
  29. package/dist/lib/core/Model.js +381 -11
  30. package/dist/lib/core/Model.js.map +1 -1
  31. package/dist/lib/core/Queue.d.ts +2 -2
  32. package/dist/lib/core/Queue.js +10 -10
  33. package/dist/lib/core/Queue.js.map +1 -1
  34. package/dist/lib/core/Repository.d.ts +57 -189
  35. package/dist/lib/core/Repository.js +59 -224
  36. package/dist/lib/core/Repository.js.map +1 -1
  37. package/dist/lib/core/UtilityTypes.d.ts +5 -5
  38. package/dist/lib/types/repository/index.d.ts +11 -10
  39. package/package.json +2 -2
package/README.md CHANGED
@@ -1360,6 +1360,10 @@ makeCreateTableStatement()
1360
1360
  Within a database transaction, you can utilize the following:
1361
1361
 
1362
1362
  ```js
1363
+ import { DB } from 'tspace-mysql';
1364
+ import { User } from '../Models/User';
1365
+ import { Post } from '../Models/Post';
1366
+
1363
1367
  const trx = await new DB().beginTransaction();
1364
1368
 
1365
1369
  try {
@@ -1441,6 +1445,52 @@ try {
1441
1445
  */
1442
1446
  await trx.rollback();
1443
1447
  }
1448
+ ```
1449
+ Or you can use transaction method for auto commit and rollback, you can utilize the following:
1450
+
1451
+ ```js
1452
+ import { DB } from 'tspace-mysql';
1453
+ import { User } from '../Models/User';
1454
+ import { Post } from '../Models/Post';
1455
+
1456
+ try {
1457
+
1458
+ const { user, posts } = await DB.transaction(async (trx) => {
1459
+
1460
+ const user = await new User()
1461
+ .create({
1462
+ name: `tspace`,
1463
+ email: "tspace@example.com",
1464
+ })
1465
+ .bind(trx) // don't forget this
1466
+ .save();
1467
+
1468
+ const posts = await new Post()
1469
+ .createMultiple([
1470
+ {
1471
+ user_id: user.id,
1472
+ title: `tspace-post1`,
1473
+ },
1474
+ {
1475
+ user_id: user.id,
1476
+ title: `tspace-post2`,
1477
+ },
1478
+ {
1479
+ user_id: user.id,
1480
+ title: `tspace-post3`,
1481
+ },
1482
+ ])
1483
+ .bind(trx) // don't forget this
1484
+ .save();
1485
+
1486
+ return { user, posts };
1487
+
1488
+ });
1489
+ } catch (err) {
1490
+ // error here is from transaction method, you don't need to worry about rollback, just catch error and handle it.
1491
+ console.error(err);
1492
+ }
1493
+
1444
1494
  ```
1445
1495
 
1446
1496
  ## Race Condition
@@ -2662,12 +2712,16 @@ DB_CACHE = redis://username:password@server:6379
2662
2712
 
2663
2713
  const users = await new User()
2664
2714
  .cache({
2665
- key : 'users', // key of the cache
2666
- expires : 1000 * 60 // cache expires in 60 seconds
2715
+ key : 'users1', // key of the cache
2716
+ expires : 1000 * 60, // cache expires in 60 seconds
2717
+ namespace : true // add namespace to the key of the cache like that 'db:users:users1' to avoid conflicts with other keys in cache
2667
2718
  })
2668
2719
  .sleep(5) // assume the query takes longer than 5 seconds...
2669
2720
  .findMany()
2670
2721
 
2722
+ // delete cache by key
2723
+ User.cache.delete('users1', { namespace : true })
2724
+
2671
2725
  ```
2672
2726
 
2673
2727
  ### Decorator
@@ -3525,6 +3579,10 @@ import { User } from '../Models/User'
3525
3579
 
3526
3580
  // Create repository instance for User entity
3527
3581
  const userRepository = Repository(User)
3582
+
3583
+ // Or Use static methods for flexible querying
3584
+ // const user = await User.findOne({ ... }) // traditional way without repository
3585
+
3528
3586
  // Fetch a single user with flexible query options
3529
3587
  const user = await userRepository.findOne({
3530
3588
  /**
@@ -3633,6 +3691,8 @@ const user = await userRepository.findOne({
3633
3691
  })
3634
3692
 
3635
3693
 
3694
+ // Use static methods for flexible querying
3695
+ // const users = await User.findMany({ limit: 3 }) // traditional way without repository
3636
3696
  const users = await userRepository.findMany({
3637
3697
  select : {
3638
3698
  id : true,
@@ -3652,6 +3712,8 @@ const users = await userRepository.findMany({
3652
3712
  }
3653
3713
  })
3654
3714
 
3715
+ // Use static methods for flexible querying
3716
+ // const userPaginate = await User.pagination() // traditional way without repository
3655
3717
  const userPaginate = await userRepository.pagination({
3656
3718
  select : {
3657
3719
  id : true,
@@ -3677,6 +3739,8 @@ const findFullName = await userRepository.findOne({
3677
3739
  ```
3678
3740
  ### Repository Insert Statements
3679
3741
  ```js
3742
+ // Use static methods for flexible querying
3743
+ // const created = await User.create({...}) // traditional way without repository
3680
3744
 
3681
3745
  const userRepository = Repository(User)
3682
3746
 
@@ -3730,8 +3794,10 @@ const createdOrSelected = await userRepository.createOrSelect({
3730
3794
  ### Repository Update Statements
3731
3795
  ```js
3732
3796
 
3733
- const userRepository = Repository(User)
3797
+ // Use static methods for flexible querying
3798
+ // const updated = await User.update({...}) // traditional way without repository
3734
3799
 
3800
+ const userRepository = Repository(User)
3735
3801
  const updated = await userRepository.update({
3736
3802
  data : {
3737
3803
  name : "repository-name",
@@ -3746,8 +3812,10 @@ const updated = await userRepository.update({
3746
3812
  ### Repository Delete Statements
3747
3813
  ```js
3748
3814
 
3749
- const userRepository = Repository(User)
3815
+ // Use static methods for flexible querying
3816
+ // const deleted = await User.delete({...}) // traditional way without repository
3750
3817
 
3818
+ const userRepository = Repository(User)
3751
3819
  const deleted = await userRepository.delete({
3752
3820
  where : {
3753
3821
  id : 1
@@ -3806,8 +3874,10 @@ import { Repository , OP } from 'tspace-mysql'
3806
3874
  import { User } from '../Models/User'
3807
3875
  import { Phone } from '../Models/Phone'
3808
3876
 
3809
- const userRepository = Repository(User)
3877
+ // Use static methods for flexible querying
3878
+ // const userHasPhones = await User.findOne({...}) // traditional way without repository
3810
3879
 
3880
+ const userRepository = Repository(User)
3811
3881
  const userHasPhones = await userRepository.findOne({
3812
3882
  select : {
3813
3883
  id : true,
@@ -1575,10 +1575,10 @@ declare class Builder extends AbstractBuilder {
1575
1575
  * The 'find' method is used to retrieve a single record from a database table by its primary key.
1576
1576
  *
1577
1577
  * This method allows you to quickly fetch a specific record by specifying the primary key value, which is typically an integer id.
1578
- * @param {number} id
1578
+ * @param {number | string} primaryKey
1579
1579
  * @returns {promise<any>}
1580
1580
  */
1581
- find(id: number): Promise<Record<string, any> | null>;
1581
+ find(primaryKey: number | string): Promise<Record<string, any> | null>;
1582
1582
  /**
1583
1583
  * The 'pagination' method is used to perform pagination on a set of database query results obtained through the Query Builder.
1584
1584
  *
@@ -2915,11 +2915,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2915
2915
  * The 'find' method is used to retrieve a single record from a database table by its primary key.
2916
2916
  *
2917
2917
  * This method allows you to quickly fetch a specific record by specifying the primary key value, which is typically an integer id.
2918
- * @param {number} id
2918
+ * @param {number | string} primaryKey
2919
2919
  * @returns {promise<any>}
2920
2920
  */
2921
- async find(id) {
2922
- this.where("id", id);
2921
+ async find(primaryKey) {
2922
+ this.where("id", primaryKey);
2923
2923
  return await this.first();
2924
2924
  }
2925
2925
  /**