tspace-mysql 1.5.6 → 1.5.7

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 +343 -86
  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 +27 -28
  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 +182 -39
  47. package/build/lib/core/Blueprint.js +362 -63
  48. package/build/lib/core/Blueprint.js.map +1 -0
  49. package/build/lib/core/Builder.d.ts +28 -23
  50. package/build/lib/core/Builder.js +84 -68
  51. package/build/lib/core/Builder.js.map +1 -0
  52. package/build/lib/core/DB.d.ts +17 -33
  53. package/build/lib/core/DB.js +37 -63
  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 +144 -93
  68. package/build/lib/core/Model.js +72 -38
  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 +164 -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
@@ -86,6 +86,12 @@ npm install tspace-mysql -g
86
86
  - [Safety Update](#safety-update)
87
87
  - [Safety Delete](#safety-delete)
88
88
  - [Safety Relationships](#safety-relationships)
89
+ - [Repository](#repository)
90
+ - [Repository Select Statements](#repository-select-statements)
91
+ - [Repository Insert Statements](#repository-insert-statements)
92
+ - [Repository Update Statements](#repository-update-statements)
93
+ - [Repository Delete Statements](#repository-delete-statements)
94
+ - [Repository Transactions](#repository-transactions)
89
95
  - [Blueprint](#blueprint)
90
96
  - [Cli](#cli)
91
97
  - [Make Model](#make-model)
@@ -95,6 +101,7 @@ npm install tspace-mysql -g
95
101
  - [Dump](#dump)
96
102
  - [Generate Models](#generate-models)
97
103
  - [Migration Models](#migrate-models)
104
+ - [Migration DB](#migration-db)
98
105
 
99
106
  ## Configuration
100
107
 
@@ -232,7 +239,7 @@ const select = await new DB("users").select("id", "username").findOne();
232
239
 
233
240
  const selectRaw = await new DB("users").selectRaw("COUNT(id)").findMany();
234
241
  // SELECT COUNT(id) FROM `users`;
235
- // also you can use the DB.raw() function
242
+ // You can also use the DB.raw() function
236
243
  // const selectRaw = await new DB("users").selec(DB.raw("COUNT(id)")).findMany();
237
244
 
238
245
  const selectObject = await new DB("posts")
@@ -271,6 +278,12 @@ const users = await new DB("users")
271
278
  )
272
279
  .findMany();
273
280
  // SELECT * FROM `users` WHERE `users`.`id` = (SELECT `users`.`id` FROM `users` WHERE `users`.`id` = '1' LIMIT 1);
281
+
282
+ const findFullName = await new User()
283
+ .select('name',`${DB.raw('CONCAT(firstName," ",lastName) as fullName')}`)
284
+ .whereRaw(`CONCAT(firstName," ",lastName) LIKE '%${search}%'`)
285
+ .findOne()
286
+ // SELECT `users`.`name`, CONCAT(firstName," ",lastName) as fullName FROM `users` WHERE CONCAT(firstName," ",lastName) LIKE '%search%' LIMIT 1;
274
287
  ```
275
288
 
276
289
  ## Ordering, Grouping, Limit and Offset
@@ -404,24 +417,26 @@ const users = await new DB("users")
404
417
  ### Where Object Clauses
405
418
 
406
419
  ```js
420
+ import { Operator } from 'tspace-mysql'
421
+
407
422
  const whereObject = await new DB("users")
408
423
  .whereObject({
409
- id : DB.op('eq',1),
410
- username : DB.op('in',['user1','user2']),
411
- name : DB.op('like','%value%')
424
+ id : Operator.eq(1),
425
+ username : Operator.in(['user1','user2']),
426
+ name : Operator.like('%value%')
412
427
  })
413
428
  .findMany();
414
429
 
415
430
  // You can also use .where as well.
416
431
  const where = await new DB("users")
417
432
  .where({
418
- id : DB.op('eq',1),
419
- username : DB.op('in',['user1','user2']),
420
- name : DB.op('like','%value%')
433
+ id : 1,
434
+ username : 'username1',
435
+ name : Operator.like('%value%')
421
436
  })
422
437
  .findMany();
423
438
 
424
- // SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` IN ('user1','user2') AND `users`.`name` LIKE '%value%';
439
+ // SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` = 'user1' AND `users`.`name` LIKE '%value%';
425
440
 
426
441
  ```
427
442
 
@@ -587,7 +602,7 @@ const pageTwoUsers = await new DB("users").paginate({ page: 2, limit: 5 });
587
602
  */
588
603
  ```
589
604
 
590
- ## Insert Statement
605
+ ## Insert Statements
591
606
 
592
607
  ```js
593
608
  const user = await new DB("users")
@@ -1873,24 +1888,46 @@ using the following:
1873
1888
  #### Schema Model
1874
1889
 
1875
1890
  ```js
1876
- import { Model, Blueprint } from "tspace-mysql";
1877
- class User extends Model {
1891
+ import { Model, Blueprint , TSchema } from "tspace-mysql";
1892
+
1893
+ const schema = {
1894
+ id: new Blueprint().int().notNull().primary().autoIncrement(),
1895
+ uuid: new Blueprint().varchar(50).null(),
1896
+ name: new Blueprint().varchar(191).notNull(),
1897
+ email: new Blueprint().varchar(191).notNull(),
1898
+ createdAt: new Blueprint().timestamp().null().bindColumn('created_at'),
1899
+ updatedAt: new Blueprint().timestamp().null().bindColumn('updated_at'),
1900
+ deletedAt: new Blueprint().timestamp().null().bindColumn('deleted_at')
1901
+ }
1902
+
1903
+
1904
+ // make type in TS
1905
+ type TSchemaUser = TSchema<typeof Schema>
1906
+
1907
+ // the TSchemaUser will be created like that
1908
+ /**
1909
+ {
1910
+ id : number,
1911
+ uuid : string | null,
1912
+ name : string,
1913
+ email : string,
1914
+ createdAt : Date | string | null,
1915
+ updatedAt : Date | string | null,
1916
+ deletedAt : Date | string | null
1917
+ }
1918
+ */
1919
+
1920
+
1921
+ class User extends Model
1922
+ <TSchemaUser> // use the schema for this User model
1923
+ {
1878
1924
  constructor() {
1879
1925
  super();
1880
1926
  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
- })
1927
+ this.useSchema(schema)
1890
1928
  }
1891
1929
  }
1892
1930
 
1893
-
1894
1931
  ```
1895
1932
 
1896
1933
  #### Validation
@@ -2031,7 +2068,7 @@ Type safety still works when you add additional types to your model, using the f
2031
2068
 
2032
2069
  ```js
2033
2070
  // in file User.ts
2034
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2071
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
2035
2072
  import Phone from '../Phone'
2036
2073
 
2037
2074
  const schemaUser = {
@@ -2045,17 +2082,7 @@ const schemaUser = {
2045
2082
  updatedAt :new Blueprint().timestamp().null()
2046
2083
  }
2047
2084
 
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
- */
2085
+ type TSchemaUser = TSchema<typeof schemaUser>
2059
2086
 
2060
2087
  class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate the type for the Model.
2061
2088
  constructor() {
@@ -2066,13 +2093,13 @@ class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate
2066
2093
  }
2067
2094
  }
2068
2095
 
2069
- export { User , TSchemaUser }
2096
+ export { User }
2070
2097
  export default User
2071
2098
 
2072
2099
  +--------------------------------------------------------------------------+
2073
2100
 
2074
2101
  // in file Phone.ts
2075
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2102
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
2076
2103
  import { User } from './User.ts'
2077
2104
  const schemaPhone = {
2078
2105
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2083,7 +2110,7 @@ const schemaPhone = {
2083
2110
  updatedAt :new Blueprint().timestamp().null()
2084
2111
  }
2085
2112
 
2086
- type TSchemaPhone = SchemaType<typeof schemaPhone>
2113
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2087
2114
 
2088
2115
  class Phone extends Model<TSchemaPhone> {
2089
2116
  constructor() {
@@ -2093,7 +2120,7 @@ class Phone extends Model<TSchemaPhone> {
2093
2120
  }
2094
2121
  }
2095
2122
 
2096
- export { Phone , TSchemaPhone }
2123
+ export { Phone }
2097
2124
  export default Phone
2098
2125
 
2099
2126
  +--------------------------------------------------------------------------+
@@ -2102,8 +2129,8 @@ export default Phone
2102
2129
  ### Safety Select
2103
2130
 
2104
2131
  ```js
2105
- import { User , TSchemaUser } from './User.ts'
2106
- import { Phone, TSchemaPhone } from './Phone.ts'
2132
+ import { User } from './User.ts'
2133
+ import { Phone } from './Phone.ts'
2107
2134
 
2108
2135
  const users = await new User().select('id','username').findMany() ✅
2109
2136
  const users = await new User().select('idx','username').findMany() ❌
@@ -2117,8 +2144,8 @@ const users = await new User().except('idx','username').findMany() ❌
2117
2144
 
2118
2145
  ```js
2119
2146
 
2120
- import { User , TSchemaUser } from './User.ts'
2121
- import { Phone, TSchemaPhone } from './Phone.ts'
2147
+ import { User } from './User.ts'
2148
+ import { Phone } from './Phone.ts'
2122
2149
 
2123
2150
  const users = await new User().orderBy('id','DESC').findMany() ✅
2124
2151
  const users = await new User().orderBy('idx','DESC').findMany() ❌
@@ -2134,8 +2161,8 @@ const users = await new User().oldest('idx').findMany() ❌
2134
2161
  ### Safety GroupBy
2135
2162
 
2136
2163
  ```js
2137
- import { User , TSchemaUser } from './User.ts'
2138
- import { Phone, TSchemaPhone } from './Phone.ts'
2164
+ import { User } from './User.ts'
2165
+ import { Phone } from './Phone.ts'
2139
2166
 
2140
2167
  const users = await new User().groupBy('id').findMany() ✅
2141
2168
  const users = await new User().groupBy('idx').findMany() ❌
@@ -2145,8 +2172,8 @@ const users = await new User().groupBy('idx').findMany() ❌
2145
2172
  ### Safety Where
2146
2173
 
2147
2174
  ```js
2148
- import { User , TSchemaUser } from './User.ts'
2149
- import { Phone, TSchemaPhone } from './Phone.ts'
2175
+ import { User } from './User.ts'
2176
+ import { Phone } from './Phone.ts'
2150
2177
 
2151
2178
  const users = await new User().where('id',1).findMany() ✅
2152
2179
  const users = await new User().where('idxx',1).findMany() ❌
@@ -2183,11 +2210,13 @@ const users = await new User()
2183
2210
  ### Safety Insert
2184
2211
 
2185
2212
  ```js
2186
- import { User , TSchemaUser } from './User.ts'
2187
- import { Phone, TSchemaPhone } from './Phone.ts'
2213
+ import { User } from './User.ts'
2214
+ import { Phone } from './Phone.ts'
2188
2215
 
2189
2216
  const users = await new User().create({ id : 10 }).save() ✅
2190
2217
 
2218
+ const users = await new User().create({ id : "10" }).save() ❌
2219
+
2191
2220
  const users = await new User().create({ idx : 10 }).save() ❌
2192
2221
 
2193
2222
  ```
@@ -2195,11 +2224,12 @@ const users = await new User().create({ idx : 10 }).save() ❌
2195
2224
  ### Safety Update
2196
2225
 
2197
2226
  ```js
2198
- import { User , TSchemaUser } from './User.ts'
2199
- import { Phone, TSchemaPhone } from './Phone.ts'
2227
+ import { User } from './User.ts'
2228
+ import { Phone } from './Phone.ts'
2200
2229
 
2201
2230
  const users = await new User().update({ id : 10 }).where('id',1).save() ✅
2202
2231
  const users = await new User().update({ id : 10 }).where('idx',1).save() ❌
2232
+ const users = await new User().update({ id : "10" }).where('id',1).save() ❌
2203
2233
  const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2204
2234
 
2205
2235
  ```
@@ -2207,8 +2237,8 @@ const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2207
2237
  ### Safety Delete
2208
2238
 
2209
2239
  ```js
2210
- import { User , TSchemaUser } from './User.ts'
2211
- import { Phone, TSchemaPhone } from './Phone.ts'
2240
+ import { User } from './User.ts'
2241
+ import { Phone } from './Phone.ts'
2212
2242
 
2213
2243
  const users = await new User().where('id',1).delete() ✅
2214
2244
  const users = await new User().where('idx',1).delete() ❌
@@ -2218,6 +2248,7 @@ const users = await new User().where('idx',1).delete() ❌
2218
2248
  ### Safety Relationships
2219
2249
 
2220
2250
  ```js
2251
+ import { TSchemaModel } from 'tspace-mysql'
2221
2252
  import { User } from './User.ts'
2222
2253
  import { Phone } from './Phone.ts'
2223
2254
  // Case #1 : Relationship with 2 relations 'phone' and 'phones'
@@ -2248,7 +2279,7 @@ import { Phone } from './Phone.ts'
2248
2279
  // good 👍👍👍
2249
2280
  const users = await new User()
2250
2281
  .relations('phone','phones')
2251
- .findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
2282
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2252
2283
 
2253
2284
  for(const user of users) {
2254
2285
  user.phone ✅
@@ -2266,7 +2297,7 @@ for(const user of users) {
2266
2297
  .relations('phone','phones')
2267
2298
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2268
2299
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2269
- .findMany<{ phone : TSchemaPhone , phones : TSchemaPhone[] }>()
2300
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2270
2301
 
2271
2302
  for(const user of users) {
2272
2303
  user.phone.user ❌
@@ -2294,8 +2325,8 @@ for(const user of users) {
2294
2325
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2295
2326
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2296
2327
  .findMany<{
2297
- phone : Partial<TSchemaPhone> & { user : TSchemaUser};
2298
- phones : (Partial<TSchemaPhone> & { user : TSchemaUser})[];
2328
+ phone : Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>};
2329
+ phones : (Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>})[];
2299
2330
  }>()
2300
2331
 
2301
2332
  for(const user of users) {
@@ -2310,8 +2341,8 @@ for(const user of users) {
2310
2341
  +--------------------------------------------------------------------------+
2311
2342
  // If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
2312
2343
 
2313
- import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
2314
- import { Phone , TSchemaPhone } from '../Phone'
2344
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2345
+ import { Phone } from '../Phone'
2315
2346
 
2316
2347
  const schemaUser = {
2317
2348
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2324,15 +2355,15 @@ const schemaUser = {
2324
2355
  updatedAt :new Blueprint().timestamp().null()
2325
2356
  }
2326
2357
 
2327
- type TSchemaUser = SchemaType<typeof schemaUser>
2358
+ type TSchemaUser = TSchema<typeof schemaUser>
2328
2359
 
2329
- type RelationUserType = RelationType<{
2330
- phones : TSchemaPhone[]
2331
- phone : TSchemaPhone
2360
+ type TRelationUser = TRelation<{
2361
+ phones : TSchemaModel<Phone>[]
2362
+ phone : TSchemaModel<Phone>
2332
2363
  }>
2333
2364
 
2334
2365
  // Add this '<TSchemaUser, RelationUserType>' to activate the type for the Model.
2335
- class User extends Model<TSchemaUser, RelationUserType> {
2366
+ class User extends Model< TSchemaUser, TRelationUser > {
2336
2367
  constructor() {
2337
2368
  super()
2338
2369
  this.useSchema(schemaUser)
@@ -2343,13 +2374,14 @@ class User extends Model<TSchemaUser, RelationUserType> {
2343
2374
  }
2344
2375
  }
2345
2376
 
2346
- export { User , TSchemaUser }
2377
+ export { User }
2347
2378
 
2348
2379
  +--------------------------------------------------------------------------+
2349
2380
 
2350
2381
  // in file Phone.ts
2351
- import { Model , Blueprint , SchemaType , RelationType} from 'tspace-mysql'
2382
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2352
2383
  import { User } from './User.ts'
2384
+
2353
2385
  const schemaPhone = {
2354
2386
  id :new Blueprint().int().notNull().primary().autoIncrement(),
2355
2387
  uuid :new Blueprint().varchar(50).null(),
@@ -2359,13 +2391,16 @@ const schemaPhone = {
2359
2391
  updatedAt :new Blueprint().timestamp().null()
2360
2392
  }
2361
2393
 
2362
- type TSchemaPhone = SchemaType<typeof schemaPhone>
2394
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2363
2395
 
2364
- type RelationPhoneType = RelationType<{
2365
- user : TSchemaUser[]
2396
+ type TRelationPhone = TRelation<{
2397
+ user : TSchemaModel<User>[]
2366
2398
  }>
2367
2399
 
2368
- class Phone extends Model<TSchemaPhone,RelationPhoneType> {
2400
+ class Phone extends Model<
2401
+ TSchemaPhone,
2402
+ TRelationPhone
2403
+ > {
2369
2404
  constructor() {
2370
2405
  super()
2371
2406
  this.useSchema(schemaPhone)
@@ -2374,7 +2409,7 @@ class Phone extends Model<TSchemaPhone,RelationPhoneType> {
2374
2409
  }
2375
2410
  }
2376
2411
 
2377
- export { Phone , TSchemaPhone }
2412
+ export { Phone }
2378
2413
 
2379
2414
  +--------------------------------------------------------------------------+
2380
2415
 
@@ -2415,9 +2450,206 @@ const users = await new User()
2415
2450
 
2416
2451
  ```
2417
2452
 
2453
+ ## Repository
2454
+ ```js
2455
+ Repository is a mechanism that encapsulates all database operations related to a specific model. It provides methods for querying, inserting, updating, and deleting records in the database associated with the model.
2456
+
2457
+ ** The Repository check always type safety if model is used the type of schema
2458
+
2459
+ ```
2460
+ ### Repository Select Statements
2461
+ ```js
2462
+ import { Repository , TRepository , Operator } from 'tspace-mysql'
2463
+ import { User } from '../Models/User'
2464
+ import { Phone } from '../Models/Phone'
2465
+
2466
+ const userRepository = Repository.bind(User)
2467
+ const needPhone = true
2468
+ const user = await userRepository.findOne({
2469
+ select : ['id','name','username'],
2470
+ where : {
2471
+ id: 1
2472
+ },
2473
+ when : {
2474
+ condition : `${needPhone}`,
2475
+ callback: () => ({
2476
+ relations : ['phone']
2477
+ })
2478
+ }
2479
+ })
2480
+
2481
+ const users = await userRepository.findMany({
2482
+ select : ['id','name','username'],
2483
+ limit : 3,
2484
+ orderBy : {
2485
+ id : 'ASC',
2486
+ name : 'DESC'
2487
+ }
2488
+ groupBy : ['id'],
2489
+ where : {
2490
+ id: Operator.in([1,2,3])
2491
+ }
2492
+ })
2493
+
2494
+ const userPaginate = await userRepository.pagination({
2495
+ select : ['id','name','username'],
2496
+ page : 1,
2497
+ limit : 3,
2498
+ where : {
2499
+ id: Operator.in([1,2,3])
2500
+ }
2501
+ })
2502
+
2503
+ const findFullName = await userRepository.findOne({
2504
+ select : ['name',`${DB.raw('CONCAT(firstName," ",lastName) as fullName')}`],
2505
+ whereRaw : [
2506
+ `CONCAT(firstName," ",lastName) LIKE '%${search}%'`
2507
+ ]
2508
+ })
2509
+
2510
+ // ------- relationship -------
2511
+
2512
+ const userHasPhones = await userRepository.findOne({
2513
+ select : ['*'],
2514
+ where : {
2515
+ id: 1
2516
+ },
2517
+ relations : ['post'],
2518
+ relationQuery:{
2519
+ name : 'post',
2520
+ callback: () => ({
2521
+ select: ['id', 'userId', 'name'],
2522
+ relations : ['user']
2523
+ }) as TRepository<Phone> // add type for the callback know to check type of the model
2524
+ }
2525
+ })
2526
+
2527
+ ```
2528
+ ### Repository Insert Statements
2529
+ ```js
2530
+
2531
+ const userRepository = Repository.bind(User)
2532
+
2533
+ const created = await userRepository.create({
2534
+ data : {
2535
+ name : "repository-name",
2536
+ // ....
2537
+ }
2538
+ })
2539
+
2540
+ const createdMultiple = await u.createMultiple({
2541
+ data : [
2542
+ {
2543
+ name: "tspace4",
2544
+ // ....
2545
+ },
2546
+ {
2547
+ name: "tspace5",
2548
+ // ....
2549
+ },
2550
+ {
2551
+ name: "tspace6",
2552
+ // ....
2553
+ }
2554
+ // ....
2555
+ ]
2556
+ })
2557
+
2558
+ const createdNotExists = await userRepository.createNotExists({
2559
+ data : {
2560
+ name : "repository-name",
2561
+ // ....
2562
+ },
2563
+ where : {
2564
+ id : 1
2565
+ }
2566
+ })
2567
+
2568
+ const createdOrSelected = await userRepository.createOrSelect({
2569
+ data : {
2570
+ name : "repository-name",
2571
+ // ....
2572
+ },
2573
+ where : {
2574
+ id : 1
2575
+ }
2576
+ })
2577
+
2578
+
2579
+ ```
2580
+ ### Repository Update Statements
2581
+ ```js
2582
+
2583
+ const userRepository = Repository.bind(User)
2584
+
2585
+ const updated = await userRepository.update({
2586
+ data : {
2587
+ name : "repository-name",
2588
+ // ....
2589
+ },
2590
+ where : {
2591
+ id : 1
2592
+ }
2593
+ })
2594
+
2595
+ ```
2596
+ ### Repository Delete Statements
2597
+ ```js
2598
+
2599
+ const userRepository = Repository.bind(User)
2600
+
2601
+ const deleted = await userRepository.delete({
2602
+ where : {
2603
+ id : 1
2604
+ }
2605
+ })
2606
+
2607
+ ```
2608
+
2609
+ ### Repository Transactions
2610
+
2611
+ ```js
2612
+ import { DB , Repository } from 'tspace-mysql'
2613
+ import { User } from '../Models/User'
2614
+ const userRepository = Repository.bind(User)
2615
+
2616
+ const transaction = await DB.beginTransaction()
2617
+
2618
+ try {
2619
+ await transaction.startTransaction()
2620
+
2621
+ const created = await userRepository.create({
2622
+ data : {
2623
+ name : "repository-name",
2624
+ // ....
2625
+ },
2626
+ transaction // add this for the transaction
2627
+ })
2628
+
2629
+ const updated = await userRepository.update({
2630
+ data : {
2631
+ name : "repository-name",
2632
+ // ....
2633
+ },
2634
+ where : {
2635
+ id : created.id
2636
+ },
2637
+ transaction
2638
+ })
2639
+
2640
+ await transaction.commit()
2641
+
2642
+ } catch (err) {
2643
+
2644
+ await transaction.rollback()
2645
+ }
2646
+
2647
+ ```
2648
+
2418
2649
  ## Blueprint
2419
2650
 
2420
- Schema table created by command make:migration, you may use the:
2651
+ Blueprint is a tool used for defining database schemas programmatically.
2652
+ 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
2653
 
2422
2654
  ```js
2423
2655
  import { Schema , Blueprint , DB } from 'tspace-mysql'
@@ -2488,7 +2720,7 @@ foreign({ references : ${COLUMN} , on : ${TABLE-NAME OR MODEL CLASSES} })
2488
2720
  * Binding a column in the key of the schema forwards the key to the corresponding column in the database.
2489
2721
  * @BindColumn
2490
2722
  */
2491
- bindColumn('<REAL-NAME-COLUMN-IN-DB>')
2723
+ bindColumn('< real name column in table >')
2492
2724
 
2493
2725
 
2494
2726
  ```
@@ -2505,9 +2737,9 @@ npm install tspace-mysql -g
2505
2737
 
2506
2738
  ## Make Model
2507
2739
 
2508
- Command will be placed Model in the specific directory
2740
+ The command will be placed Model in the specific directory.
2509
2741
 
2510
- ```js
2742
+ ```sh
2511
2743
 
2512
2744
  /**
2513
2745
  *
@@ -2532,9 +2764,9 @@ tspace-mysql make:model User --m --dir=app/Models
2532
2764
 
2533
2765
  ## Make Migration
2534
2766
 
2535
- Command will be placed Migration in the specific directory
2767
+ The command will be placed Migration in the specific directory.
2536
2768
 
2537
- ```js
2769
+ ```sh
2538
2770
  /**
2539
2771
  *
2540
2772
  * @make Migration Table
@@ -2559,7 +2791,7 @@ tspace-mysql make:migration users --dir=app/Models/Migrations
2559
2791
 
2560
2792
  ## Migrate
2561
2793
 
2562
- ```js
2794
+ ```sh
2563
2795
  /**
2564
2796
  *
2565
2797
  * @run Migrate table
@@ -2588,18 +2820,18 @@ tspace-mysql migrate --dir=app/Models/Migrations --type=js
2588
2820
 
2589
2821
  # Query
2590
2822
 
2591
- Command will be execute a query
2823
+ The command will execute a query.
2592
2824
 
2593
- ```js
2825
+ ```sh
2594
2826
  tspace-mysql query "SELECT * FROM users"
2595
2827
 
2596
2828
  ```
2597
2829
 
2598
2830
  # Dump
2599
2831
 
2600
- Command will be dump database or table into file
2832
+ The command will dump the database or table into a file.
2601
2833
 
2602
- ```js
2834
+ ```sh
2603
2835
  tspace-mysql dump:db "database" --values // backup with values in the tables
2604
2836
 
2605
2837
  tspace-mysql dump:table "table" --values // backup with values in the table
@@ -2608,9 +2840,9 @@ tspace-mysql dump:table "table" --values // backup with values in the table
2608
2840
 
2609
2841
  # Generate Models
2610
2842
 
2611
- Command will be generate models from table in database
2843
+ The command will generate models from tables in the database.
2612
2844
 
2613
- ```js
2845
+ ```sh
2614
2846
  tspace-mysql generate:models --dir=<folder for creating>
2615
2847
 
2616
2848
  tspace-mysql generate:models --dir=app/Models --env=development --decorators
@@ -2619,13 +2851,38 @@ tspace-mysql generate:models --dir=app/Models --env=development --decorators
2619
2851
 
2620
2852
  # Migration Models
2621
2853
 
2622
- Command will be generate migrations by schema in your models
2854
+ The command will generate migrations based on the schema in your models to a .sql file,
2855
+ can also push the migration files to the database.
2623
2856
 
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
2857
+ ```sh
2858
+ /**
2859
+ *
2860
+ * @arg --push will push the migration files to the database
2861
+ * @arg --generate will generate the migration files
2862
+ */
2863
+ tspace-mysql migrations:models --dir=<path-to-migration> --models=<path to your models> --generate
2864
+ tspace-mysql migrations:models --dir=<path-to-migration> --push
2627
2865
 
2628
2866
  tspace-mysql migrations:models --models=src/app/models --dir=migrations --generate
2629
2867
  tspace-mysql migrations:models --dir=migrations --push
2630
2868
 
2631
2869
  ```
2870
+
2871
+ # Migration DB
2872
+
2873
+ The command will generate migrations based on the schema in your database to a .sql file,
2874
+ can also push the migration files to the database.
2875
+
2876
+ ```sh
2877
+ /**
2878
+ *
2879
+ * @arg --push will push the migration files to the database
2880
+ * @arg --generate will generate the migration files
2881
+ */
2882
+ tspace-mysql migrations:db --dir=<path-to-migration> --generate --db=<db-name>
2883
+ tspace-mysql migrations:db --dir=<path-to-migration> --push
2884
+
2885
+ tspace-mysql migrations:db --dir=migrations --generate --db=db-migratons
2886
+ tspace-mysql migrations:db --dir=migrations --push
2887
+
2888
+ ```