tspace-mysql 1.5.6 → 1.5.8

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 (101) hide show
  1. package/README.md +483 -87
  2. package/build/cli/dump/db.js +1 -0
  3. package/build/cli/dump/db.js.map +1 -0
  4. package/build/cli/dump/table.js +1 -0
  5. package/build/cli/dump/table.js.map +1 -0
  6. package/build/cli/generate/make.js +1 -0
  7. package/build/cli/generate/make.js.map +1 -0
  8. package/build/cli/generate/model.js +1 -0
  9. package/build/cli/generate/model.js.map +1 -0
  10. package/build/cli/generate/modelDecorator.js +1 -0
  11. package/build/cli/generate/modelDecorator.js.map +1 -0
  12. package/build/cli/index.js +25 -22
  13. package/build/cli/index.js.map +1 -0
  14. package/build/cli/migrate/make.js +1 -0
  15. package/build/cli/migrate/make.js.map +1 -0
  16. package/build/cli/migrations/make-db.js +75 -24
  17. package/build/cli/migrations/make-db.js.map +1 -0
  18. package/build/cli/migrations/{make.js → make-model.js} +65 -20
  19. package/build/cli/migrations/make-model.js.map +1 -0
  20. package/build/cli/models/make.js +1 -0
  21. package/build/cli/models/make.js.map +1 -0
  22. package/build/cli/models/model.js +1 -0
  23. package/build/cli/models/model.js.map +1 -0
  24. package/build/cli/query/index.js +1 -0
  25. package/build/cli/query/index.js.map +1 -0
  26. package/build/cli/tables/make.js +1 -0
  27. package/build/cli/tables/make.js.map +1 -0
  28. package/build/cli/tables/table.js +1 -0
  29. package/build/cli/tables/table.js.map +1 -0
  30. package/build/lib/connection/index.d.ts +4 -4
  31. package/build/lib/connection/index.js +1 -0
  32. package/build/lib/connection/index.js.map +1 -0
  33. package/build/lib/connection/options.js +2 -1
  34. package/build/lib/connection/options.js.map +1 -0
  35. package/build/lib/constants/index.js +1 -0
  36. package/build/lib/constants/index.js.map +1 -0
  37. package/build/lib/core/Abstracts/AbstractBuilder.d.ts +5 -5
  38. package/build/lib/core/Abstracts/AbstractBuilder.js +1 -0
  39. package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -0
  40. package/build/lib/core/Abstracts/AbstractDB.d.ts +2 -2
  41. package/build/lib/core/Abstracts/AbstractDB.js +1 -0
  42. package/build/lib/core/Abstracts/AbstractDB.js.map +1 -0
  43. package/build/lib/core/Abstracts/AbstractModel.d.ts +30 -29
  44. package/build/lib/core/Abstracts/AbstractModel.js +1 -0
  45. package/build/lib/core/Abstracts/AbstractModel.js.map +1 -0
  46. package/build/lib/core/Blueprint.d.ts +250 -74
  47. package/build/lib/core/Blueprint.js +460 -98
  48. package/build/lib/core/Blueprint.js.map +1 -0
  49. package/build/lib/core/Builder.d.ts +173 -168
  50. package/build/lib/core/Builder.js +229 -213
  51. package/build/lib/core/Builder.js.map +1 -0
  52. package/build/lib/core/DB.d.ts +60 -76
  53. package/build/lib/core/DB.js +80 -106
  54. package/build/lib/core/DB.js.map +1 -0
  55. package/build/lib/core/Decorator.d.ts +6 -6
  56. package/build/lib/core/Decorator.js +1 -0
  57. package/build/lib/core/Decorator.js.map +1 -0
  58. package/build/lib/core/Handlers/Logger.js +1 -0
  59. package/build/lib/core/Handlers/Logger.js.map +1 -0
  60. package/build/lib/core/Handlers/Proxy.js +1 -0
  61. package/build/lib/core/Handlers/Proxy.js.map +1 -0
  62. package/build/lib/core/Handlers/Relation.d.ts +16 -15
  63. package/build/lib/core/Handlers/Relation.js +71 -62
  64. package/build/lib/core/Handlers/Relation.js.map +1 -0
  65. package/build/lib/core/Handlers/State.js +1 -0
  66. package/build/lib/core/Handlers/State.js.map +1 -0
  67. package/build/lib/core/Model.d.ts +381 -295
  68. package/build/lib/core/Model.js +420 -316
  69. package/build/lib/core/Model.js.map +1 -0
  70. package/build/lib/core/Operator.d.ts +42 -0
  71. package/build/lib/core/Operator.js +139 -0
  72. package/build/lib/core/Operator.js.map +1 -0
  73. package/build/lib/core/Repository.d.ts +695 -0
  74. package/build/lib/core/Repository.js +1101 -0
  75. package/build/lib/core/Repository.js.map +1 -0
  76. package/build/lib/core/Schema.js +1 -0
  77. package/build/lib/core/Schema.js.map +1 -0
  78. package/build/lib/core/UtilityTypes.d.ts +182 -0
  79. package/build/lib/{Interface.js → core/UtilityTypes.js} +1 -0
  80. package/build/lib/core/UtilityTypes.js.map +1 -0
  81. package/build/lib/core/index.d.ts +4 -2
  82. package/build/lib/core/index.js +4 -1
  83. package/build/lib/core/index.js.map +1 -0
  84. package/build/lib/index.js +1 -0
  85. package/build/lib/index.js.map +1 -0
  86. package/build/lib/types.d.ts +285 -0
  87. package/build/lib/{core/Type.js → types.js} +1 -0
  88. package/build/lib/types.js.map +1 -0
  89. package/build/lib/utils/index.d.ts +1 -1
  90. package/build/lib/utils/index.js +3 -2
  91. package/build/lib/utils/index.js.map +1 -0
  92. package/build/tests/01-Pool.test.js +1 -0
  93. package/build/tests/01-Pool.test.js.map +1 -0
  94. package/build/tests/02-DB.test.js +1 -0
  95. package/build/tests/02-DB.test.js.map +1 -0
  96. package/build/tests/03-Model.test.js +1 -0
  97. package/build/tests/03-Model.test.js.map +1 -0
  98. package/package.json +3 -2
  99. package/build/lib/Interface.d.ts +0 -195
  100. package/build/lib/core/Type.d.ts +0 -60
  101. /package/build/cli/migrations/{make.d.ts → make-model.d.ts} +0 -0
package/README.md CHANGED
@@ -63,7 +63,7 @@ npm install tspace-mysql -g
63
63
  - [Observer](#observer)
64
64
  - [Logger](#logger)
65
65
  - [Hooks](#hooks)
66
- - [SoftDelete](#soft-delete)
66
+ - [SoftDelete](#softdelete)
67
67
  - [Schema](#schema)
68
68
  - [Schema Model](#schema-model)
69
69
  - [Validation](#validation)
@@ -75,6 +75,7 @@ npm install tspace-mysql -g
75
75
  - [Many To Many](#many-to-many)
76
76
  - [Deeply Nested Relations](#deeply-nested-relations)
77
77
  - [Relation Exists](#relation-exists)
78
+ - [Relation Trashed](#relation-trashed)
78
79
  - [Built in Relation Functions](#built-in-relation-functions)
79
80
  - [Decorator](#decorator)
80
81
  - [Type Safety](#type-safety)
@@ -86,6 +87,12 @@ npm install tspace-mysql -g
86
87
  - [Safety Update](#safety-update)
87
88
  - [Safety Delete](#safety-delete)
88
89
  - [Safety Relationships](#safety-relationships)
90
+ - [Repository](#repository)
91
+ - [Repository Select Statements](#repository-select-statements)
92
+ - [Repository Insert Statements](#repository-insert-statements)
93
+ - [Repository Update Statements](#repository-update-statements)
94
+ - [Repository Delete Statements](#repository-delete-statements)
95
+ - [Repository Transactions](#repository-transactions)
89
96
  - [Blueprint](#blueprint)
90
97
  - [Cli](#cli)
91
98
  - [Make Model](#make-model)
@@ -95,6 +102,7 @@ npm install tspace-mysql -g
95
102
  - [Dump](#dump)
96
103
  - [Generate Models](#generate-models)
97
104
  - [Migration Models](#migrate-models)
105
+ - [Migration DB](#migration-db)
98
106
 
99
107
  ## Configuration
100
108
 
@@ -232,7 +240,7 @@ const select = await new DB("users").select("id", "username").findOne();
232
240
 
233
241
  const selectRaw = await new DB("users").selectRaw("COUNT(id)").findMany();
234
242
  // SELECT COUNT(id) FROM `users`;
235
- // also you can use the DB.raw() function
243
+ // You can also use the DB.raw() function
236
244
  // const selectRaw = await new DB("users").selec(DB.raw("COUNT(id)")).findMany();
237
245
 
238
246
  const selectObject = await new DB("posts")
@@ -271,6 +279,12 @@ const users = await new DB("users")
271
279
  )
272
280
  .findMany();
273
281
  // SELECT * FROM `users` WHERE `users`.`id` = (SELECT `users`.`id` FROM `users` WHERE `users`.`id` = '1' LIMIT 1);
282
+
283
+ const findFullName = await new User()
284
+ .select('name',`${DB.raw('CONCAT(firstName," ",lastName) as fullName')}`)
285
+ .whereRaw(`CONCAT(firstName," ",lastName) LIKE '%${search}%'`)
286
+ .findOne()
287
+ // SELECT `users`.`name`, CONCAT(firstName," ",lastName) as fullName FROM `users` WHERE CONCAT(firstName," ",lastName) LIKE '%search%' LIMIT 1;
274
288
  ```
275
289
 
276
290
  ## Ordering, Grouping, Limit and Offset
@@ -404,24 +418,26 @@ const users = await new DB("users")
404
418
  ### Where Object Clauses
405
419
 
406
420
  ```js
421
+ import { Operator } from 'tspace-mysql'
422
+
407
423
  const whereObject = await new DB("users")
408
424
  .whereObject({
409
- id : DB.op('eq',1),
410
- username : DB.op('in',['user1','user2']),
411
- name : DB.op('like','%value%')
425
+ id : Operator.eq(1),
426
+ username : Operator.in(['user1','user2']),
427
+ name : Operator.like('%value%')
412
428
  })
413
429
  .findMany();
414
430
 
415
431
  // You can also use .where as well.
416
432
  const where = await new DB("users")
417
433
  .where({
418
- id : DB.op('eq',1),
419
- username : DB.op('in',['user1','user2']),
420
- name : DB.op('like','%value%')
434
+ id : 1,
435
+ username : 'username1',
436
+ name : Operator.like('%value%')
421
437
  })
422
438
  .findMany();
423
439
 
424
- // SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` IN ('user1','user2') AND `users`.`name` LIKE '%value%';
440
+ // SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` = 'user1' AND `users`.`name` LIKE '%value%';
425
441
 
426
442
  ```
427
443
 
@@ -587,7 +603,7 @@ const pageTwoUsers = await new DB("users").paginate({ page: 2, limit: 5 });
587
603
  */
588
604
  ```
589
605
 
590
- ## Insert Statement
606
+ ## Insert Statements
591
607
 
592
608
  ```js
593
609
  const user = await new DB("users")
@@ -1399,6 +1415,10 @@ class User extends Model {
1399
1415
  const user = await new User().where('user_id',1).findOne()
1400
1416
  // SELECT * FROM `users` WHERE `users`.`userId` = '1' and `users`.`deletedAtCustom` IS NULL LIMIT 1;
1401
1417
 
1418
+ // find in trashed
1419
+ const user = await new User().trashed().findMany()
1420
+ // SELECT * FROM `users` WHERE `users`.`userId` = '1' and `users`.`deletedAtCustom` IS NOT NULL;
1421
+
1402
1422
  ```
1403
1423
 
1404
1424
  ### Relationships
@@ -1685,6 +1705,9 @@ class Post extends Model {
1685
1705
  }
1686
1706
  // normal relations
1687
1707
  await new User().relations('posts').findMany()
1708
+ // SELECT * FROM `users` WHERE `users`.`deleted_at`;
1709
+ // SELECT * FROM `posts` WHERE `posts`.`userId` IN (...) AND `posts`.`deleted_at` IS NULL;
1710
+
1688
1711
  /*
1689
1712
  * @returns [
1690
1713
  * {
@@ -1715,6 +1738,11 @@ await new User().relations('posts').findMany()
1715
1738
  */
1716
1739
 
1717
1740
  await new User().relationsExists('posts').findMany()
1741
+ // SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL
1742
+ // AND EXISTS (SELECT 1 FROM `posts` WHERE `users`.`id` = `posts`.`user_id` AND `posts`.`deletedA_at` IS NULL );
1743
+
1744
+ // SELECT * FROM `posts` WHERE `posts`.`user_id` IN (...) AND `posts`.`deleted_at` IS NULL;
1745
+
1718
1746
  /*
1719
1747
  * @returns [
1720
1748
  * {
@@ -1733,6 +1761,130 @@ await new User().relationsExists('posts').findMany()
1733
1761
  * because posts id 1 and id 3 has been removed from database (using soft delete)
1734
1762
  */
1735
1763
 
1764
+ ```
1765
+
1766
+ #### Relation Trashed
1767
+ Relationships can return results only if they are deleted in table, considering soft deletes.
1768
+ Let's illustrate this with an example:
1769
+ ```js
1770
+
1771
+ +-------------+--------------+----------------------------+--------------------+
1772
+ | table users | |
1773
+ +-------------+--------------+----------------------------+--------------------+
1774
+ | id | username | email | deleted_at |
1775
+ |-------------|--------------|----------------------------|--------------------|
1776
+ | 1 | tspace1 | tspace1@gmail.com | |
1777
+ | 2 | tspace2 | tspace2@gmail.com | |
1778
+ | 3 | tspace3 | tspace3@gmail.com |2020-07-15 00:00:00 |
1779
+ +-------------+--------------+----------------------------+--------------------+
1780
+
1781
+ +-------------+--------------+----------------------------+--------------------+
1782
+ | table posts | |
1783
+ +-------------+--------------+----------------------------+--------------------+
1784
+ | id | user_id | title | deleted_at |
1785
+ |-------------|--------------|----------------------------|--------------------|
1786
+ | 1 | 1 | posts 1 |2020-07-15 00:00:00 |
1787
+ | 2 | 2 | posts 2 | |
1788
+ | 3 | 3 | posts 3 |2020-07-15 00:00:00 |
1789
+ +-------------+--------------+----------------------------+--------------------+
1790
+
1791
+ import { Model } from 'tspace-mysql'
1792
+
1793
+ class User extends Model {
1794
+ constructor(){
1795
+ super()
1796
+ this.hasMany({ name : 'posts' , model : Post })
1797
+ this.useSoftDelete()
1798
+ }
1799
+ }
1800
+
1801
+ +--------------------------------------------------------------------------+
1802
+
1803
+ class Post extends Model {
1804
+ constructor(){
1805
+ super()
1806
+ this.hasMany({ name : 'comments' , model : Comment })
1807
+ this.belongsTo({ name : 'user' , model : User })
1808
+ this.useSoftDelete()
1809
+ }
1810
+ }
1811
+
1812
+ // normal relations
1813
+ await new User().relations('posts').findMany()
1814
+ // SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL;
1815
+ // SELECT * FROM `posts` WHERE `posts`.`user_id` IN (...) AND `posts`.`deleted_at` IS NULL;
1816
+
1817
+ /*
1818
+ * @returns [
1819
+ * {
1820
+ * id : 1,
1821
+ * username: "tspace1",
1822
+ * email : "tspace1@gmail.com",
1823
+ * posts : []
1824
+ * }
1825
+ * {
1826
+ * id : 2,
1827
+ * username: "tspace2",
1828
+ * email : "tspace2@gmail.com",
1829
+ * posts : [
1830
+ * {
1831
+ * id : 2,
1832
+ * user_id : 2,
1833
+ * title : "posts 2"
1834
+ * }
1835
+ * ]
1836
+ * }
1837
+ * ]
1838
+ */
1839
+
1840
+ // relationsTrashed
1841
+ await new User().relationsTrashed('posts').findMany()
1842
+ // SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL;
1843
+ // SELECT * FROM `posts` WHERE `posts`.`user_id` IN (...) AND `posts`.`deleted_at` IS NOT NULL;
1844
+
1845
+ /*
1846
+ * @returns [
1847
+ * {
1848
+ * id : 1,
1849
+ * username: "tspace1",
1850
+ * email : "tspace1@gmail.com",
1851
+ * posts : [
1852
+ * {
1853
+ * id : 1,
1854
+ * user_id : 1,
1855
+ * title : "posts 1"
1856
+ * }
1857
+ * ]
1858
+ * }
1859
+ * {
1860
+ * id : 2,
1861
+ * username: "tspace2",
1862
+ * email : "tspace2@gmail.com",
1863
+ * posts : []
1864
+ * }
1865
+ * ]
1866
+ */
1867
+
1868
+ // relationsTrashed + trashed
1869
+ await new User().relationsTrashed('posts').trashed().findMany()
1870
+ // SELECT * FROM `users` WHERE `users`.`deleted_at` IS NOT NULL;
1871
+ // SELECT * FROM `posts` WHERE `posts`.`user_id` IN (...) AND `posts`.`deleted_at` IS NOT NULL;
1872
+ /*
1873
+ * @returns [
1874
+ * {
1875
+ * id : 3,
1876
+ * username: "tspace3",
1877
+ * email : "tspace3@gmail.com",
1878
+ * posts : [
1879
+ * {
1880
+ * id : 3,
1881
+ * user_id : 3,
1882
+ * title : "posts 3"
1883
+ * }
1884
+ * ]
1885
+ * }
1886
+ * ]
1887
+ */
1736
1888
 
1737
1889
  ```
1738
1890
 
@@ -1873,24 +2025,46 @@ using the following:
1873
2025
  #### Schema Model
1874
2026
 
1875
2027
  ```js
1876
- import { Model, Blueprint } from "tspace-mysql";
1877
- class User extends Model {
2028
+ import { Model, Blueprint , TSchema } from "tspace-mysql";
2029
+
2030
+ const schema = {
2031
+ id: new Blueprint().int().notNull().primary().autoIncrement(),
2032
+ uuid: new Blueprint().varchar(50).null(),
2033
+ name: new Blueprint().varchar(191).notNull(),
2034
+ email: new Blueprint().varchar(191).notNull(),
2035
+ createdAt: new Blueprint().timestamp().null().bindColumn('created_at'),
2036
+ updatedAt: new Blueprint().timestamp().null().bindColumn('updated_at'),
2037
+ deletedAt: new Blueprint().timestamp().null().bindColumn('deleted_at')
2038
+ }
2039
+
2040
+
2041
+ // make type in TS
2042
+ type TSchemaUser = TSchema<typeof Schema>
2043
+
2044
+ // the TSchemaUser will be created like that
2045
+ /**
2046
+ {
2047
+ id : number,
2048
+ uuid : string | null,
2049
+ name : string,
2050
+ email : string,
2051
+ createdAt : Date | string | null,
2052
+ updatedAt : Date | string | null,
2053
+ deletedAt : Date | string | null
2054
+ }
2055
+ */
2056
+
2057
+
2058
+ class User extends Model
2059
+ <TSchemaUser> // use the schema for this User model
2060
+ {
1878
2061
  constructor() {
1879
2062
  super();
1880
2063
  this.useCamelCase()
1881
- this.useSchema({
1882
- id: new Blueprint().int().notNull().primary().autoIncrement(),
1883
- uuid: new Blueprint().varchar(50).null(),
1884
- name: new Blueprint().varchar(191).notNull(),
1885
- email: new Blueprint().varchar(191).notNull(),
1886
- createdAt: new Blueprint().timestamp().null().bindColumn('created_at'), // you can fix the column name with 'bindColumn'
1887
- updatedAt: new Blueprint().timestamp().null().bindColumn('updated_at'),
1888
- deletedAt: new Blueprint().timestamp().null().bindColumn('deleted_at')
1889
- })
2064
+ this.useSchema(schema)
1890
2065
  }
1891
2066
  }
1892
2067
 
1893
-
1894
2068
  ```
1895
2069
 
1896
2070
  #### Validation
@@ -2031,7 +2205,7 @@ Type safety still works when you add additional types to your model, using the f
2031
2205
 
2032
2206
  ```js
2033
2207
  // in file User.ts
2034
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2208
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
2035
2209
  import Phone from '../Phone'
2036
2210
 
2037
2211
  const schemaUser = {
@@ -2045,17 +2219,7 @@ const schemaUser = {
2045
2219
  updatedAt :new Blueprint().timestamp().null()
2046
2220
  }
2047
2221
 
2048
- type TSchemaUser = SchemaType<typeof schemaUser>
2049
-
2050
- // You can also reassign the type for the schema
2051
- /**
2052
- type TSchemaUser = SchemaType<typeof schemaUser , {
2053
- id : number,
2054
- uuid : string,
2055
- ...
2056
- }>
2057
-
2058
- */
2222
+ type TSchemaUser = TSchema<typeof schemaUser>
2059
2223
 
2060
2224
  class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate the type for the Model.
2061
2225
  constructor() {
@@ -2066,13 +2230,13 @@ class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate
2066
2230
  }
2067
2231
  }
2068
2232
 
2069
- export { User , TSchemaUser }
2233
+ export { User }
2070
2234
  export default User
2071
2235
 
2072
2236
  +--------------------------------------------------------------------------+
2073
2237
 
2074
2238
  // in file Phone.ts
2075
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2239
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
2076
2240
  import { User } from './User.ts'
2077
2241
  const schemaPhone = {
2078
2242
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2083,7 +2247,7 @@ const schemaPhone = {
2083
2247
  updatedAt :new Blueprint().timestamp().null()
2084
2248
  }
2085
2249
 
2086
- type TSchemaPhone = SchemaType<typeof schemaPhone>
2250
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2087
2251
 
2088
2252
  class Phone extends Model<TSchemaPhone> {
2089
2253
  constructor() {
@@ -2093,7 +2257,7 @@ class Phone extends Model<TSchemaPhone> {
2093
2257
  }
2094
2258
  }
2095
2259
 
2096
- export { Phone , TSchemaPhone }
2260
+ export { Phone }
2097
2261
  export default Phone
2098
2262
 
2099
2263
  +--------------------------------------------------------------------------+
@@ -2102,8 +2266,8 @@ export default Phone
2102
2266
  ### Safety Select
2103
2267
 
2104
2268
  ```js
2105
- import { User , TSchemaUser } from './User.ts'
2106
- import { Phone, TSchemaPhone } from './Phone.ts'
2269
+ import { User } from './User.ts'
2270
+ import { Phone } from './Phone.ts'
2107
2271
 
2108
2272
  const users = await new User().select('id','username').findMany() ✅
2109
2273
  const users = await new User().select('idx','username').findMany() ❌
@@ -2117,8 +2281,8 @@ const users = await new User().except('idx','username').findMany() ❌
2117
2281
 
2118
2282
  ```js
2119
2283
 
2120
- import { User , TSchemaUser } from './User.ts'
2121
- import { Phone, TSchemaPhone } from './Phone.ts'
2284
+ import { User } from './User.ts'
2285
+ import { Phone } from './Phone.ts'
2122
2286
 
2123
2287
  const users = await new User().orderBy('id','DESC').findMany() ✅
2124
2288
  const users = await new User().orderBy('idx','DESC').findMany() ❌
@@ -2134,8 +2298,8 @@ const users = await new User().oldest('idx').findMany() ❌
2134
2298
  ### Safety GroupBy
2135
2299
 
2136
2300
  ```js
2137
- import { User , TSchemaUser } from './User.ts'
2138
- import { Phone, TSchemaPhone } from './Phone.ts'
2301
+ import { User } from './User.ts'
2302
+ import { Phone } from './Phone.ts'
2139
2303
 
2140
2304
  const users = await new User().groupBy('id').findMany() ✅
2141
2305
  const users = await new User().groupBy('idx').findMany() ❌
@@ -2145,8 +2309,8 @@ const users = await new User().groupBy('idx').findMany() ❌
2145
2309
  ### Safety Where
2146
2310
 
2147
2311
  ```js
2148
- import { User , TSchemaUser } from './User.ts'
2149
- import { Phone, TSchemaPhone } from './Phone.ts'
2312
+ import { User } from './User.ts'
2313
+ import { Phone } from './Phone.ts'
2150
2314
 
2151
2315
  const users = await new User().where('id',1).findMany() ✅
2152
2316
  const users = await new User().where('idxx',1).findMany() ❌
@@ -2183,11 +2347,13 @@ const users = await new User()
2183
2347
  ### Safety Insert
2184
2348
 
2185
2349
  ```js
2186
- import { User , TSchemaUser } from './User.ts'
2187
- import { Phone, TSchemaPhone } from './Phone.ts'
2350
+ import { User } from './User.ts'
2351
+ import { Phone } from './Phone.ts'
2188
2352
 
2189
2353
  const users = await new User().create({ id : 10 }).save() ✅
2190
2354
 
2355
+ const users = await new User().create({ id : "10" }).save() ❌
2356
+
2191
2357
  const users = await new User().create({ idx : 10 }).save() ❌
2192
2358
 
2193
2359
  ```
@@ -2195,11 +2361,12 @@ const users = await new User().create({ idx : 10 }).save() ❌
2195
2361
  ### Safety Update
2196
2362
 
2197
2363
  ```js
2198
- import { User , TSchemaUser } from './User.ts'
2199
- import { Phone, TSchemaPhone } from './Phone.ts'
2364
+ import { User } from './User.ts'
2365
+ import { Phone } from './Phone.ts'
2200
2366
 
2201
2367
  const users = await new User().update({ id : 10 }).where('id',1).save() ✅
2202
2368
  const users = await new User().update({ id : 10 }).where('idx',1).save() ❌
2369
+ const users = await new User().update({ id : "10" }).where('id',1).save() ❌
2203
2370
  const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2204
2371
 
2205
2372
  ```
@@ -2207,8 +2374,8 @@ const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2207
2374
  ### Safety Delete
2208
2375
 
2209
2376
  ```js
2210
- import { User , TSchemaUser } from './User.ts'
2211
- import { Phone, TSchemaPhone } from './Phone.ts'
2377
+ import { User } from './User.ts'
2378
+ import { Phone } from './Phone.ts'
2212
2379
 
2213
2380
  const users = await new User().where('id',1).delete() ✅
2214
2381
  const users = await new User().where('idx',1).delete() ❌
@@ -2218,6 +2385,7 @@ const users = await new User().where('idx',1).delete() ❌
2218
2385
  ### Safety Relationships
2219
2386
 
2220
2387
  ```js
2388
+ import { TSchemaModel } from 'tspace-mysql'
2221
2389
  import { User } from './User.ts'
2222
2390
  import { Phone } from './Phone.ts'
2223
2391
  // Case #1 : Relationship with 2 relations 'phone' and 'phones'
@@ -2248,7 +2416,7 @@ import { Phone } from './Phone.ts'
2248
2416
  // good 👍👍👍
2249
2417
  const users = await new User()
2250
2418
  .relations('phone','phones')
2251
- .findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
2419
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2252
2420
 
2253
2421
  for(const user of users) {
2254
2422
  user.phone ✅
@@ -2266,7 +2434,7 @@ for(const user of users) {
2266
2434
  .relations('phone','phones')
2267
2435
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2268
2436
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2269
- .findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
2437
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2270
2438
 
2271
2439
  for(const user of users) {
2272
2440
  user.phone.user ❌
@@ -2294,8 +2462,8 @@ for(const user of users) {
2294
2462
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2295
2463
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2296
2464
  .findMany<{
2297
- phone : Partial<TSchemaPhone> & { user : TSchemaUser};
2298
- phones : (Partial<TSchemaPhone> & { user : TSchemaUser})[];
2465
+ phone : Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>};
2466
+ phones : (Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>})[];
2299
2467
  }>()
2300
2468
 
2301
2469
  for(const user of users) {
@@ -2310,8 +2478,8 @@ for(const user of users) {
2310
2478
  +--------------------------------------------------------------------------+
2311
2479
  // If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
2312
2480
 
2313
- import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
2314
- import { Phone , TSchemaPhone } from '../Phone'
2481
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2482
+ import { Phone } from '../Phone'
2315
2483
 
2316
2484
  const schemaUser = {
2317
2485
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2324,15 +2492,15 @@ const schemaUser = {
2324
2492
  updatedAt :new Blueprint().timestamp().null()
2325
2493
  }
2326
2494
 
2327
- type TSchemaUser = SchemaType<typeof schemaUser>
2495
+ type TSchemaUser = TSchema<typeof schemaUser>
2328
2496
 
2329
- type RelationUserType = RelationType<{
2330
- phones : TSchemaPhone[]
2331
- phone : TSchemaPhone
2497
+ type TRelationUser = TRelation<{
2498
+ phones : TSchemaModel<Phone>[]
2499
+ phone : TSchemaModel<Phone>
2332
2500
  }>
2333
2501
 
2334
2502
  // Add this '<TSchemaUser, RelationUserType>' to activate the type for the Model.
2335
- class User extends Model<TSchemaUser, RelationUserType> {
2503
+ class User extends Model< TSchemaUser, TRelationUser > {
2336
2504
  constructor() {
2337
2505
  super()
2338
2506
  this.useSchema(schemaUser)
@@ -2343,13 +2511,14 @@ class User extends Model<TSchemaUser, RelationUserType> {
2343
2511
  }
2344
2512
  }
2345
2513
 
2346
- export { User , TSchemaUser }
2514
+ export { User }
2347
2515
 
2348
2516
  +--------------------------------------------------------------------------+
2349
2517
 
2350
2518
  // in file Phone.ts
2351
- import { Model , Blueprint , SchemaType , RelationType} from 'tspace-mysql'
2519
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2352
2520
  import { User } from './User.ts'
2521
+
2353
2522
  const schemaPhone = {
2354
2523
  id :new Blueprint().int().notNull().primary().autoIncrement(),
2355
2524
  uuid :new Blueprint().varchar(50).null(),
@@ -2359,13 +2528,16 @@ const schemaPhone = {
2359
2528
  updatedAt :new Blueprint().timestamp().null()
2360
2529
  }
2361
2530
 
2362
- type TSchemaPhone = SchemaType<typeof schemaPhone>
2531
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2363
2532
 
2364
- type RelationPhoneType = RelationType<{
2365
- user : TSchemaUser[]
2533
+ type TRelationPhone = TRelation<{
2534
+ user : TSchemaModel<User>[]
2366
2535
  }>
2367
2536
 
2368
- class Phone extends Model<TSchemaPhone,RelationPhoneType> {
2537
+ class Phone extends Model<
2538
+ TSchemaPhone,
2539
+ TRelationPhone
2540
+ > {
2369
2541
  constructor() {
2370
2542
  super()
2371
2543
  this.useSchema(schemaPhone)
@@ -2374,7 +2546,7 @@ class Phone extends Model<TSchemaPhone,RelationPhoneType> {
2374
2546
  }
2375
2547
  }
2376
2548
 
2377
- export { Phone , TSchemaPhone }
2549
+ export { Phone }
2378
2550
 
2379
2551
  +--------------------------------------------------------------------------+
2380
2552
 
@@ -2415,15 +2587,214 @@ const users = await new User()
2415
2587
 
2416
2588
  ```
2417
2589
 
2590
+ ## Repository
2591
+ ```js
2592
+ Repository is a mechanism that encapsulates all database operations related to a specific model.
2593
+ It provides methods for querying, inserting, updating, and deleting records in the database associated with the model.
2594
+
2595
+ ** The Repository check always type safety if model is used the type of schema
2596
+
2597
+ ```
2598
+ ### Repository Select Statements
2599
+ ```js
2600
+ import { Repository , TRepository , Operator } from 'tspace-mysql'
2601
+ import { User } from '../Models/User'
2602
+ import { Phone } from '../Models/Phone'
2603
+
2604
+ const userRepository = Repository.bind(User)
2605
+ const needPhone = true
2606
+ const user = await userRepository.findOne({
2607
+ select : ['id','name','username'],
2608
+ where : {
2609
+ id: 1
2610
+ },
2611
+ when : {
2612
+ condition : `${needPhone}`,
2613
+ callback: () => ({
2614
+ relations : ['phone']
2615
+ })
2616
+ }
2617
+ })
2618
+
2619
+ const users = await userRepository.findMany({
2620
+ select : ['id','name','username'],
2621
+ limit : 3,
2622
+ orderBy : {
2623
+ id : 'ASC',
2624
+ name : 'DESC'
2625
+ }
2626
+ groupBy : ['id'],
2627
+ where : {
2628
+ id: Operator.in([1,2,3])
2629
+ }
2630
+ })
2631
+
2632
+ const userPaginate = await userRepository.pagination({
2633
+ select : ['id','name','username'],
2634
+ page : 1,
2635
+ limit : 3,
2636
+ where : {
2637
+ id: Operator.in([1,2,3])
2638
+ }
2639
+ })
2640
+
2641
+ const findFullName = await userRepository.findOne({
2642
+ select : ['name',`${DB.raw('CONCAT(firstName," ",lastName) as fullName')}`],
2643
+ whereRaw : [
2644
+ `CONCAT(firstName," ",lastName) LIKE '%${search}%'`
2645
+ ]
2646
+ })
2647
+
2648
+ // ------- relationship -------
2649
+
2650
+ const userHasPhones = await userRepository.findOne({
2651
+ select : ['*'],
2652
+ where : {
2653
+ id: 1
2654
+ },
2655
+ relations : ['post'],
2656
+ relationQuery:{
2657
+ name : 'post',
2658
+ callback: () => ({
2659
+ select: ['id', 'userId', 'name'],
2660
+ relations : ['user']
2661
+ }) as TRepository<Phone> // add type for the callback know to check type of the model
2662
+ }
2663
+ })
2664
+
2665
+ ```
2666
+ ### Repository Insert Statements
2667
+ ```js
2668
+
2669
+ const userRepository = Repository.bind(User)
2670
+
2671
+ const created = await userRepository.create({
2672
+ data : {
2673
+ name : "repository-name",
2674
+ // ....
2675
+ }
2676
+ })
2677
+
2678
+ const createdMultiple = await u.createMultiple({
2679
+ data : [
2680
+ {
2681
+ name: "tspace4",
2682
+ // ....
2683
+ },
2684
+ {
2685
+ name: "tspace5",
2686
+ // ....
2687
+ },
2688
+ {
2689
+ name: "tspace6",
2690
+ // ....
2691
+ }
2692
+ // ....
2693
+ ]
2694
+ })
2695
+
2696
+ const createdNotExists = await userRepository.createNotExists({
2697
+ data : {
2698
+ name : "repository-name",
2699
+ // ....
2700
+ },
2701
+ where : {
2702
+ id : 1
2703
+ }
2704
+ })
2705
+
2706
+ const createdOrSelected = await userRepository.createOrSelect({
2707
+ data : {
2708
+ name : "repository-name",
2709
+ // ....
2710
+ },
2711
+ where : {
2712
+ id : 1
2713
+ }
2714
+ })
2715
+
2716
+
2717
+ ```
2718
+ ### Repository Update Statements
2719
+ ```js
2720
+
2721
+ const userRepository = Repository.bind(User)
2722
+
2723
+ const updated = await userRepository.update({
2724
+ data : {
2725
+ name : "repository-name",
2726
+ // ....
2727
+ },
2728
+ where : {
2729
+ id : 1
2730
+ }
2731
+ })
2732
+
2733
+ ```
2734
+ ### Repository Delete Statements
2735
+ ```js
2736
+
2737
+ const userRepository = Repository.bind(User)
2738
+
2739
+ const deleted = await userRepository.delete({
2740
+ where : {
2741
+ id : 1
2742
+ }
2743
+ })
2744
+
2745
+ ```
2746
+
2747
+ ### Repository Transactions
2748
+
2749
+ ```js
2750
+ import { DB , Repository } from 'tspace-mysql'
2751
+ import { User } from '../Models/User'
2752
+ const userRepository = Repository.bind(User)
2753
+
2754
+ const transaction = await DB.beginTransaction()
2755
+
2756
+ try {
2757
+ await transaction.startTransaction()
2758
+
2759
+ const created = await userRepository.create({
2760
+ data : {
2761
+ name : "repository-name",
2762
+ // ....
2763
+ },
2764
+ transaction // add this for the transaction
2765
+ })
2766
+
2767
+ const updated = await userRepository.update({
2768
+ data : {
2769
+ name : "repository-name",
2770
+ // ....
2771
+ },
2772
+ where : {
2773
+ id : created.id
2774
+ },
2775
+ transaction
2776
+ })
2777
+
2778
+ await transaction.commit()
2779
+
2780
+ } catch (err) {
2781
+
2782
+ await transaction.rollback()
2783
+ }
2784
+
2785
+ ```
2786
+
2418
2787
  ## Blueprint
2419
2788
 
2420
- Schema table created by command make:migration, you may use the:
2789
+ Blueprint is a tool used for defining database schemas programmatically.
2790
+ It allows developers to describe the structure of their database tables using a simple and intuitive syntax rather than writing SQL queries directly., you may use the:
2421
2791
 
2422
2792
  ```js
2423
2793
  import { Schema , Blueprint , DB } from 'tspace-mysql'
2424
2794
  (async () => {
2425
2795
  await new Schema().table('users', {
2426
2796
  id : new Blueprint().int().notNull().primary().autoIncrement(),
2797
+ // or id : new Blueprint().serial().primary(),
2427
2798
  uuid : new Blueprint().varchar(120).null()
2428
2799
  name : new Blueprint().varchar(120).default('name'),
2429
2800
  email : new Blueprint().varchar(255).unique().notNull(),
@@ -2488,7 +2859,7 @@ foreign({ references : ${COLUMN} , on : ${TABLE-NAME OR MODEL CLASSES} })
2488
2859
  * Binding a column in the key of the schema forwards the key to the corresponding column in the database.
2489
2860
  * @BindColumn
2490
2861
  */
2491
- bindColumn('<REAL-NAME-COLUMN-IN-DB>')
2862
+ bindColumn('< real name column in table >')
2492
2863
 
2493
2864
 
2494
2865
  ```
@@ -2505,9 +2876,9 @@ npm install tspace-mysql -g
2505
2876
 
2506
2877
  ## Make Model
2507
2878
 
2508
- Command will be placed Model in the specific directory
2879
+ The command will be placed Model in the specific directory.
2509
2880
 
2510
- ```js
2881
+ ```sh
2511
2882
 
2512
2883
  /**
2513
2884
  *
@@ -2532,9 +2903,9 @@ tspace-mysql make:model User --m --dir=app/Models
2532
2903
 
2533
2904
  ## Make Migration
2534
2905
 
2535
- Command will be placed Migration in the specific directory
2906
+ The command will be placed Migration in the specific directory.
2536
2907
 
2537
- ```js
2908
+ ```sh
2538
2909
  /**
2539
2910
  *
2540
2911
  * @make Migration Table
@@ -2559,7 +2930,7 @@ tspace-mysql make:migration users --dir=app/Models/Migrations
2559
2930
 
2560
2931
  ## Migrate
2561
2932
 
2562
- ```js
2933
+ ```sh
2563
2934
  /**
2564
2935
  *
2565
2936
  * @run Migrate table
@@ -2588,18 +2959,18 @@ tspace-mysql migrate --dir=app/Models/Migrations --type=js
2588
2959
 
2589
2960
  # Query
2590
2961
 
2591
- Command will be execute a query
2962
+ The command will execute a query.
2592
2963
 
2593
- ```js
2964
+ ```sh
2594
2965
  tspace-mysql query "SELECT * FROM users"
2595
2966
 
2596
2967
  ```
2597
2968
 
2598
2969
  # Dump
2599
2970
 
2600
- Command will be dump database or table into file
2971
+ The command will dump the database or table into a file.
2601
2972
 
2602
- ```js
2973
+ ```sh
2603
2974
  tspace-mysql dump:db "database" --values // backup with values in the tables
2604
2975
 
2605
2976
  tspace-mysql dump:table "table" --values // backup with values in the table
@@ -2608,9 +2979,9 @@ tspace-mysql dump:table "table" --values // backup with values in the table
2608
2979
 
2609
2980
  # Generate Models
2610
2981
 
2611
- Command will be generate models from table in database
2982
+ The command will generate models from tables in the database.
2612
2983
 
2613
- ```js
2984
+ ```sh
2614
2985
  tspace-mysql generate:models --dir=<folder for creating>
2615
2986
 
2616
2987
  tspace-mysql generate:models --dir=app/Models --env=development --decorators
@@ -2619,13 +2990,38 @@ tspace-mysql generate:models --dir=app/Models --env=development --decorators
2619
2990
 
2620
2991
  # Migration Models
2621
2992
 
2622
- Command will be generate migrations by schema in your models
2993
+ The command will generate migrations based on the schema in your models to a .sql file,
2994
+ can also push the migration files to the database.
2623
2995
 
2624
- ```js
2625
- tspace-mysql migrations:models --dir=<path to migration> --models=<path to your models> --generate
2626
- tspace-mysql migrations:models --dir=<path to migration> --push
2996
+ ```sh
2997
+ /**
2998
+ *
2999
+ * @arg --push will push the migration files to the database
3000
+ * @arg --generate will generate the migration files
3001
+ */
3002
+ tspace-mysql migrations:models --dir=<path-to-migration> --models=<path to your models> --generate
3003
+ tspace-mysql migrations:models --dir=<path-to-migration> --push
2627
3004
 
2628
3005
  tspace-mysql migrations:models --models=src/app/models --dir=migrations --generate
2629
3006
  tspace-mysql migrations:models --dir=migrations --push
2630
3007
 
2631
3008
  ```
3009
+
3010
+ # Migration DB
3011
+
3012
+ The command will generate migrations based on the schema in your database to a .sql file,
3013
+ can also push the migration files to the database.
3014
+
3015
+ ```sh
3016
+ /**
3017
+ *
3018
+ * @arg --push will push the migration files to the database
3019
+ * @arg --generate will generate the migration files
3020
+ */
3021
+ tspace-mysql migrations:db --dir=<path-to-migration> --generate --db=<db-name>
3022
+ tspace-mysql migrations:db --dir=<path-to-migration> --push
3023
+
3024
+ tspace-mysql migrations:db --dir=migrations --generate --db=db-migratons
3025
+ tspace-mysql migrations:db --dir=migrations --push
3026
+
3027
+ ```