tspace-mysql 1.5.5 → 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 +408 -112
  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 +2 -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 +38 -30
  50. package/build/lib/core/Builder.js +163 -88
  51. package/build/lib/core/Builder.js.map +1 -0
  52. package/build/lib/core/DB.d.ts +40 -33
  53. package/build/lib/core/DB.js +88 -48
  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 +96 -76
  64. package/build/lib/core/Handlers/Relation.js.map +1 -0
  65. package/build/lib/core/Handlers/State.js +3 -2
  66. package/build/lib/core/Handlers/State.js.map +1 -0
  67. package/build/lib/core/Model.d.ts +173 -115
  68. package/build/lib/core/Model.js +141 -53
  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 +9 -8
  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 -183
  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
@@ -19,6 +19,7 @@ npm install tspace-mysql -g
19
19
  - [Configuration](#configuration)
20
20
  - [Query Builder](#query-builder)
21
21
  - [Returning Results](#returning-results)
22
+ - [Query Statement](#query-statements)
22
23
  - [Select Statements](#select-statements)
23
24
  - [Raw Expressions](#raw-expressions)
24
25
  - [Ordering, Grouping, Limit and Offset](#ordering-grouping-limit-and-offset)
@@ -32,6 +33,7 @@ npm install tspace-mysql -g
32
33
  - [Basic Where Clauses](#basic-where-clauses)
33
34
  - [Where Clauses](#where-clauses)
34
35
  - [Or Where Clauses](#or-where-clauses)
36
+ - [Where Object Clauses](#where-object-clauses)
35
37
  - [JSON Where Clauses](#json-where-clauses)
36
38
  - [Additional Where Clauses](#additional-where-clauses)
37
39
  - [Logical Grouping](#logical-grouping)
@@ -84,6 +86,12 @@ npm install tspace-mysql -g
84
86
  - [Safety Update](#safety-update)
85
87
  - [Safety Delete](#safety-delete)
86
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)
87
95
  - [Blueprint](#blueprint)
88
96
  - [Cli](#cli)
89
97
  - [Make Model](#make-model)
@@ -93,6 +101,7 @@ npm install tspace-mysql -g
93
101
  - [Dump](#dump)
94
102
  - [Generate Models](#generate-models)
95
103
  - [Migration Models](#migrate-models)
104
+ - [Migration DB](#migration-db)
96
105
 
97
106
  ## Configuration
98
107
 
@@ -207,6 +216,19 @@ const users = await new DB("users").makeUpdateStatement() // query string for up
207
216
  const users = await new DB("users").makeDeleteStatement() // query string for delete statement
208
217
 
209
218
  const users = await new DB("users").makeCreateTableStatement() // query string for create table statement
219
+
220
+ ```
221
+
222
+ ## Query Statements
223
+
224
+ ```js
225
+ const query = await DB.query(
226
+ "SELECT * FROM users WHERE id = :id AND email IS :email AND name IN :username", {
227
+ id : 1,
228
+ email : null,
229
+ username : ['name1','name2']
230
+ })
231
+ // SELECT * FROM users WHERE id = '1' AND email IS NULL AND username in ('name1','name2');
210
232
  ```
211
233
 
212
234
  ## Select Statements
@@ -217,7 +239,7 @@ const select = await new DB("users").select("id", "username").findOne();
217
239
 
218
240
  const selectRaw = await new DB("users").selectRaw("COUNT(id)").findMany();
219
241
  // SELECT COUNT(id) FROM `users`;
220
- // also you can use the DB.raw() function
242
+ // You can also use the DB.raw() function
221
243
  // const selectRaw = await new DB("users").selec(DB.raw("COUNT(id)")).findMany();
222
244
 
223
245
  const selectObject = await new DB("posts")
@@ -256,6 +278,12 @@ const users = await new DB("users")
256
278
  )
257
279
  .findMany();
258
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;
259
287
  ```
260
288
 
261
289
  ## Ordering, Grouping, Limit and Offset
@@ -386,6 +414,32 @@ const users = await new DB("users")
386
414
  // ( `users`.`id` <> '2' AND `users`.`username` = 'try to find' AND `users`.`email` = 'find@example.com');
387
415
  ```
388
416
 
417
+ ### Where Object Clauses
418
+
419
+ ```js
420
+ import { Operator } from 'tspace-mysql'
421
+
422
+ const whereObject = await new DB("users")
423
+ .whereObject({
424
+ id : Operator.eq(1),
425
+ username : Operator.in(['user1','user2']),
426
+ name : Operator.like('%value%')
427
+ })
428
+ .findMany();
429
+
430
+ // You can also use .where as well.
431
+ const where = await new DB("users")
432
+ .where({
433
+ id : 1,
434
+ username : 'username1',
435
+ name : Operator.like('%value%')
436
+ })
437
+ .findMany();
438
+
439
+ // SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` = 'user1' AND `users`.`name` LIKE '%value%';
440
+
441
+ ```
442
+
389
443
  ### JSON Where Clauses
390
444
 
391
445
  ```js
@@ -548,7 +602,7 @@ const pageTwoUsers = await new DB("users").paginate({ page: 2, limit: 5 });
548
602
  */
549
603
  ```
550
604
 
551
- ## Insert Statement
605
+ ## Insert Statements
552
606
 
553
607
  ```js
554
608
  const user = await new DB("users")
@@ -1085,7 +1139,7 @@ tspace-mysql make:model <model name> --dir=< directory >
1085
1139
 
1086
1140
  ## Model Conventions
1087
1141
 
1088
- Models generated by the make:model command will be placed in the specific directory.
1142
+ Your database schema using models. These models represent tables in the database
1089
1143
  Let's example a basic model class:
1090
1144
 
1091
1145
  ```js
@@ -1181,7 +1235,7 @@ class User extends Model {
1181
1235
  super()
1182
1236
  // By default, the model knows that the table name for this User is 'users'
1183
1237
 
1184
- this.useTable('fixtable') // fixtable
1238
+ this.useTable('fix_table') // fixtable
1185
1239
  this.useTablePlural() // users
1186
1240
  this.useTableSingular() // user
1187
1241
  }
@@ -1201,7 +1255,6 @@ class UserPhone extends Model {
1201
1255
  // The table name is user_phones
1202
1256
  this.useSnakeCase()
1203
1257
 
1204
-
1205
1258
  this.useCamelCase()
1206
1259
  // The table name is userPhones
1207
1260
  }
@@ -1220,10 +1273,10 @@ const userPhone = await new UserPhone().where('user_id',1).findOne()
1220
1273
 
1221
1274
  import { Model } from 'tspace-mysql'
1222
1275
  class User extends Model {
1223
- constructor() {
1224
- super()
1225
- this.useUUID() // insert uuid when creating
1226
- }
1276
+ constructor() {
1277
+ super()
1278
+ this.useUUID() // insert uuid when creating
1279
+ }
1227
1280
  }
1228
1281
 
1229
1282
  ```
@@ -1234,21 +1287,21 @@ class User extends Model {
1234
1287
 
1235
1288
  import { Model } from 'tspace-mysql'
1236
1289
  class User extends Model {
1237
- constructor() {
1238
- super()
1239
- // insert created_at and updated_at when creating
1240
- // update updated_at when updating
1241
- // 'created_at' and 'updated_at' still relate to pettern the model
1242
- // this.useCamelCase() will covert 'created_at' to 'createdAt' and 'updated_at' to 'updatedAt'
1243
- this.useTimestamp()
1244
-
1245
- // custom the columns
1246
- this.useTimestamp({
1247
- createdAt : 'createdAtCustom',
1248
- updatedAt : 'updatedAtCustom'
1249
- })
1290
+ constructor() {
1291
+ super()
1292
+ // insert created_at and updated_at when creating
1293
+ // update updated_at when updating
1294
+ // 'created_at' and 'updated_at' still relate to pettern the model
1295
+ // this.useCamelCase() will covert 'created_at' to 'createdAt' and 'updated_at' to 'updatedAt'
1296
+ this.useTimestamp()
1297
+
1298
+ // custom the columns
1299
+ this.useTimestamp({
1300
+ createdAt : 'createdAtCustom',
1301
+ updatedAt : 'updatedAtCustom'
1302
+ })
1250
1303
 
1251
- }
1304
+ }
1252
1305
  }
1253
1306
 
1254
1307
  ```
@@ -1352,8 +1405,8 @@ class User extends Model {
1352
1405
  super()
1353
1406
  this.useSoftDelete() // All query will be where 'deleted_at' is null
1354
1407
 
1355
- // you can also use patterns camelCase to covert the 'deleted_at' to 'deletedAt'
1356
- // you can also customize the column 'deleted_at'
1408
+ // You can also use patterns camelCase to covert the 'deleted_at' to 'deletedAt'
1409
+ // You can also customize the column 'deleted_at'
1357
1410
  this.useSoftDelete('deletedAtCustom')
1358
1411
  }
1359
1412
  }
@@ -1400,7 +1453,7 @@ export default User
1400
1453
  +--------------------------------------------------------------------------+
1401
1454
 
1402
1455
  import User from '../User'
1403
- const user = await new User().relations('phone').findOne() // can use the method .with('roles') also
1456
+ const user = await new User().relations('phone').findOne() // You can also use the method .with('roles').
1404
1457
  // user?.phone => {...}
1405
1458
  const userUsingFunction = await new User().phone().findOne()
1406
1459
  // userUsingFunction?.phone => {...}
@@ -1835,24 +1888,46 @@ using the following:
1835
1888
  #### Schema Model
1836
1889
 
1837
1890
  ```js
1838
- import { Model, Blueprint } from "tspace-mysql";
1839
- 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
+ {
1840
1924
  constructor() {
1841
1925
  super();
1842
1926
  this.useCamelCase()
1843
- this.useSchema({
1844
- id: new Blueprint().int().notNull().primary().autoIncrement(),
1845
- uuid: new Blueprint().varchar(50).null(),
1846
- name: new Blueprint().varchar(191).notNull(),
1847
- email: new Blueprint().varchar(191).notNull(),
1848
- createdAt: new Blueprint().timestamp().null().bindColumn('created_at'), // you can fix the column name with 'bindColumn'
1849
- updatedAt: new Blueprint().timestamp().null().bindColumn('updated_at'),
1850
- deletedAt: new Blueprint().timestamp().null().bindColumn('deleted_at')
1851
- })
1927
+ this.useSchema(schema)
1852
1928
  }
1853
1929
  }
1854
1930
 
1855
-
1856
1931
  ```
1857
1932
 
1858
1933
  #### Validation
@@ -1983,7 +2058,7 @@ await Schema.sync(`/src/Models`, {
1983
2058
  changed: true,
1984
2059
  });
1985
2060
 
1986
- // also you can sync by Model
2061
+ // You can also synchronize using the Model.
1987
2062
  await new User().sync({ force: true, foreign: true, changed: true });
1988
2063
  ```
1989
2064
 
@@ -1993,7 +2068,7 @@ Type safety still works when you add additional types to your model, using the f
1993
2068
 
1994
2069
  ```js
1995
2070
  // in file User.ts
1996
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2071
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
1997
2072
  import Phone from '../Phone'
1998
2073
 
1999
2074
  const schemaUser = {
@@ -2007,19 +2082,9 @@ const schemaUser = {
2007
2082
  updatedAt :new Blueprint().timestamp().null()
2008
2083
  }
2009
2084
 
2010
- type SchemaUserType = SchemaType<typeof schemaUser>
2011
-
2012
- // you can re-assign type for schema
2013
- /**
2014
- type SchemaUserType = SchemaType<typeof schemaUser , {
2015
- id : number,
2016
- uuid : string,
2017
- ...
2018
- }>
2019
-
2020
- */
2085
+ type TSchemaUser = TSchema<typeof schemaUser>
2021
2086
 
2022
- class User extends Model<SchemaUserType> { // add this type for the Model
2087
+ class User extends Model<TSchemaUser> { // Add this '<TSchemaUser>' to activate the type for the Model.
2023
2088
  constructor() {
2024
2089
  super()
2025
2090
  this.useSchema(schemaUser)
@@ -2028,13 +2093,13 @@ class User extends Model<SchemaUserType> { // add this type for the Model
2028
2093
  }
2029
2094
  }
2030
2095
 
2031
- export { User , SchemaUserType }
2096
+ export { User }
2032
2097
  export default User
2033
2098
 
2034
2099
  +--------------------------------------------------------------------------+
2035
2100
 
2036
2101
  // in file Phone.ts
2037
- import { Model , Blueprint , SchemaType } from 'tspace-mysql'
2102
+ import { Model , Blueprint , TSchema } from 'tspace-mysql'
2038
2103
  import { User } from './User.ts'
2039
2104
  const schemaPhone = {
2040
2105
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2045,9 +2110,9 @@ const schemaPhone = {
2045
2110
  updatedAt :new Blueprint().timestamp().null()
2046
2111
  }
2047
2112
 
2048
- type SchemaPhoneType = SchemaType<typeof schemaPhone>
2113
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2049
2114
 
2050
- class Phone extends Model<SchemaPhoneType> {
2115
+ class Phone extends Model<TSchemaPhone> {
2051
2116
  constructor() {
2052
2117
  super()
2053
2118
  this.useSchema(schemaPhone)
@@ -2055,7 +2120,7 @@ class Phone extends Model<SchemaPhoneType> {
2055
2120
  }
2056
2121
  }
2057
2122
 
2058
- export { Phone , SchemaPhoneType }
2123
+ export { Phone }
2059
2124
  export default Phone
2060
2125
 
2061
2126
  +--------------------------------------------------------------------------+
@@ -2064,8 +2129,8 @@ export default Phone
2064
2129
  ### Safety Select
2065
2130
 
2066
2131
  ```js
2067
- import { User , schemaUserType } from './User.ts'
2068
- import { Phone, schemaPhoneType } from './Phone.ts'
2132
+ import { User } from './User.ts'
2133
+ import { Phone } from './Phone.ts'
2069
2134
 
2070
2135
  const users = await new User().select('id','username').findMany() ✅
2071
2136
  const users = await new User().select('idx','username').findMany() ❌
@@ -2079,8 +2144,8 @@ const users = await new User().except('idx','username').findMany() ❌
2079
2144
 
2080
2145
  ```js
2081
2146
 
2082
- import { User , schemaUserType } from './User.ts'
2083
- import { Phone, schemaPhoneType } from './Phone.ts'
2147
+ import { User } from './User.ts'
2148
+ import { Phone } from './Phone.ts'
2084
2149
 
2085
2150
  const users = await new User().orderBy('id','DESC').findMany() ✅
2086
2151
  const users = await new User().orderBy('idx','DESC').findMany() ❌
@@ -2096,8 +2161,8 @@ const users = await new User().oldest('idx').findMany() ❌
2096
2161
  ### Safety GroupBy
2097
2162
 
2098
2163
  ```js
2099
- import { User , schemaUserType } from './User.ts'
2100
- import { Phone, schemaPhoneType } from './Phone.ts'
2164
+ import { User } from './User.ts'
2165
+ import { Phone } from './Phone.ts'
2101
2166
 
2102
2167
  const users = await new User().groupBy('id').findMany() ✅
2103
2168
  const users = await new User().groupBy('idx').findMany() ❌
@@ -2107,8 +2172,8 @@ const users = await new User().groupBy('idx').findMany() ❌
2107
2172
  ### Safety Where
2108
2173
 
2109
2174
  ```js
2110
- import { User , schemaUserType } from './User.ts'
2111
- import { Phone, schemaPhoneType } from './Phone.ts'
2175
+ import { User } from './User.ts'
2176
+ import { Phone } from './Phone.ts'
2112
2177
 
2113
2178
  const users = await new User().where('id',1).findMany() ✅
2114
2179
  const users = await new User().where('idxx',1).findMany() ❌
@@ -2145,11 +2210,13 @@ const users = await new User()
2145
2210
  ### Safety Insert
2146
2211
 
2147
2212
  ```js
2148
- import { User , schemaUserType } from './User.ts'
2149
- import { Phone, schemaPhoneType } from './Phone.ts'
2213
+ import { User } from './User.ts'
2214
+ import { Phone } from './Phone.ts'
2150
2215
 
2151
2216
  const users = await new User().create({ id : 10 }).save() ✅
2152
2217
 
2218
+ const users = await new User().create({ id : "10" }).save() ❌
2219
+
2153
2220
  const users = await new User().create({ idx : 10 }).save() ❌
2154
2221
 
2155
2222
  ```
@@ -2157,11 +2224,12 @@ const users = await new User().create({ idx : 10 }).save() ❌
2157
2224
  ### Safety Update
2158
2225
 
2159
2226
  ```js
2160
- import { User , schemaUserType } from './User.ts'
2161
- import { Phone, schemaPhoneType } from './Phone.ts'
2227
+ import { User } from './User.ts'
2228
+ import { Phone } from './Phone.ts'
2162
2229
 
2163
2230
  const users = await new User().update({ id : 10 }).where('id',1).save() ✅
2164
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() ❌
2165
2233
  const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2166
2234
 
2167
2235
  ```
@@ -2169,8 +2237,8 @@ const users = await new User().update({ idx : 10 }).where('idx',1).save() ❌
2169
2237
  ### Safety Delete
2170
2238
 
2171
2239
  ```js
2172
- import { User , schemaUserType } from './User.ts'
2173
- import { Phone, schemaPhoneType } from './Phone.ts'
2240
+ import { User } from './User.ts'
2241
+ import { Phone } from './Phone.ts'
2174
2242
 
2175
2243
  const users = await new User().where('id',1).delete() ✅
2176
2244
  const users = await new User().where('idx',1).delete() ❌
@@ -2180,6 +2248,7 @@ const users = await new User().where('idx',1).delete() ❌
2180
2248
  ### Safety Relationships
2181
2249
 
2182
2250
  ```js
2251
+ import { TSchemaModel } from 'tspace-mysql'
2183
2252
  import { User } from './User.ts'
2184
2253
  import { Phone } from './Phone.ts'
2185
2254
  // Case #1 : Relationship with 2 relations 'phone' and 'phones'
@@ -2192,7 +2261,7 @@ import { Phone } from './Phone.ts'
2192
2261
  user.phones ❌
2193
2262
  }
2194
2263
 
2195
- // You can also specify the type for the results
2264
+ // You can also specify the type for the results.
2196
2265
  // bad 👎👎👎
2197
2266
  const users = await new User()
2198
2267
  .relations('phone','phones')
@@ -2210,15 +2279,15 @@ import { Phone } from './Phone.ts'
2210
2279
  // good 👍👍👍
2211
2280
  const users = await new User()
2212
2281
  .relations('phone','phones')
2213
- .findMany<{ phone : schemaPhoneType , phones : schemaPhoneType[] }>()
2282
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2214
2283
 
2215
2284
  for(const user of users) {
2216
2285
  user.phone ✅
2217
2286
  user.phones ✅
2218
- user.phone.id ✅
2219
- user.phone.idx ❌
2220
- user.phones.map(phone => phone.id) ✅
2221
- user.phones.map(phone => phone.idx) ❌
2287
+ user.phone?.id ✅
2288
+ user.phone?.idx ❌
2289
+ user.phones.map(phone => phone?.id) ✅
2290
+ user.phones.map(phone => phone?.idx) ❌
2222
2291
  }
2223
2292
 
2224
2293
  +--------------------------------------------------------------------------+
@@ -2228,14 +2297,14 @@ for(const user of users) {
2228
2297
  .relations('phone','phones')
2229
2298
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2230
2299
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2231
- .findMany<{ phone : schemaPhoneType , phones : schemaPhoneType[] }>()
2300
+ .findMany<{ phone : TSchemaModel<Phone> , phones : TSchemaModel<Phone>[] }>()
2232
2301
 
2233
2302
  for(const user of users) {
2234
2303
  user.phone.user ❌
2235
2304
  user.phones.map(phone =>phone.user) ❌
2236
2305
  }
2237
2306
 
2238
- // You can also specify the type for the results
2307
+ // You can also specify the type for the results.
2239
2308
  // bad 👎👎👎
2240
2309
  const users = await new User()
2241
2310
  .relations('phone','phones')
@@ -2256,8 +2325,8 @@ for(const user of users) {
2256
2325
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
2257
2326
  .relationQuery('phones' , (query : Phone) => query.relations('user'))
2258
2327
  .findMany<{
2259
- phone : Partial<SchemaPhoneType> & { user : SchemaUserType};
2260
- phones : (Partial<SchemaPhoneType> & { user : SchemaUserType})[];
2328
+ phone : Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>};
2329
+ phones : (Partial<TSchemaModel<Phone>> & { user : TSchemaModel<User>})[];
2261
2330
  }>()
2262
2331
 
2263
2332
  for(const user of users) {
@@ -2272,8 +2341,8 @@ for(const user of users) {
2272
2341
  +--------------------------------------------------------------------------+
2273
2342
  // If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
2274
2343
 
2275
- import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
2276
- import { Phone , SchemaPhoneType } from '../Phone'
2344
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2345
+ import { Phone } from '../Phone'
2277
2346
 
2278
2347
  const schemaUser = {
2279
2348
  id :new Blueprint().int().notNull().primary().autoIncrement(),
@@ -2286,14 +2355,15 @@ const schemaUser = {
2286
2355
  updatedAt :new Blueprint().timestamp().null()
2287
2356
  }
2288
2357
 
2289
- type SchemaUserType = SchemaType<typeof schemaUser>
2358
+ type TSchemaUser = TSchema<typeof schemaUser>
2290
2359
 
2291
- type RelationUserType = RelationType<{
2292
- phones : SchemaPhoneType[]
2293
- phone : SchemaPhoneType
2360
+ type TRelationUser = TRelation<{
2361
+ phones : TSchemaModel<Phone>[]
2362
+ phone : TSchemaModel<Phone>
2294
2363
  }>
2295
2364
 
2296
- class User extends Model<SchemaUserType, RelationUserType> {
2365
+ // Add this '<TSchemaUser, RelationUserType>' to activate the type for the Model.
2366
+ class User extends Model< TSchemaUser, TRelationUser > {
2297
2367
  constructor() {
2298
2368
  super()
2299
2369
  this.useSchema(schemaUser)
@@ -2304,13 +2374,14 @@ class User extends Model<SchemaUserType, RelationUserType> {
2304
2374
  }
2305
2375
  }
2306
2376
 
2307
- export { User , SchemaUserType }
2377
+ export { User }
2308
2378
 
2309
2379
  +--------------------------------------------------------------------------+
2310
2380
 
2311
2381
  // in file Phone.ts
2312
- import { Model , Blueprint , SchemaType , RelationType} from 'tspace-mysql'
2382
+ import { Model , Blueprint , TSchema , TRelation , TSchemaModel } from 'tspace-mysql'
2313
2383
  import { User } from './User.ts'
2384
+
2314
2385
  const schemaPhone = {
2315
2386
  id :new Blueprint().int().notNull().primary().autoIncrement(),
2316
2387
  uuid :new Blueprint().varchar(50).null(),
@@ -2320,13 +2391,16 @@ const schemaPhone = {
2320
2391
  updatedAt :new Blueprint().timestamp().null()
2321
2392
  }
2322
2393
 
2323
- type SchemaPhoneType = SchemaType<typeof schemaPhone>
2394
+ type TSchemaPhone = TSchema<typeof schemaPhone>
2324
2395
 
2325
- type RelationPhoneType = RelationType<{
2326
- user : SchemaPhoneType[]
2396
+ type TRelationPhone = TRelation<{
2397
+ user : TSchemaModel<User>[]
2327
2398
  }>
2328
2399
 
2329
- class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
2400
+ class Phone extends Model<
2401
+ TSchemaPhone,
2402
+ TRelationPhone
2403
+ > {
2330
2404
  constructor() {
2331
2405
  super()
2332
2406
  this.useSchema(schemaPhone)
@@ -2335,7 +2409,7 @@ class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
2335
2409
  }
2336
2410
  }
2337
2411
 
2338
- export { Phone , SchemaPhoneType }
2412
+ export { Phone }
2339
2413
 
2340
2414
  +--------------------------------------------------------------------------+
2341
2415
 
@@ -2376,9 +2450,206 @@ const users = await new User()
2376
2450
 
2377
2451
  ```
2378
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
+
2379
2649
  ## Blueprint
2380
2650
 
2381
- 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:
2382
2653
 
2383
2654
  ```js
2384
2655
  import { Schema , Blueprint , DB } from 'tspace-mysql'
@@ -2449,7 +2720,7 @@ foreign({ references : ${COLUMN} , on : ${TABLE-NAME OR MODEL CLASSES} })
2449
2720
  * Binding a column in the key of the schema forwards the key to the corresponding column in the database.
2450
2721
  * @BindColumn
2451
2722
  */
2452
- bindColumn('<REAL-NAME-COLUMN-IN-DB>')
2723
+ bindColumn('< real name column in table >')
2453
2724
 
2454
2725
 
2455
2726
  ```
@@ -2466,9 +2737,9 @@ npm install tspace-mysql -g
2466
2737
 
2467
2738
  ## Make Model
2468
2739
 
2469
- Command will be placed Model in the specific directory
2740
+ The command will be placed Model in the specific directory.
2470
2741
 
2471
- ```js
2742
+ ```sh
2472
2743
 
2473
2744
  /**
2474
2745
  *
@@ -2493,9 +2764,9 @@ tspace-mysql make:model User --m --dir=app/Models
2493
2764
 
2494
2765
  ## Make Migration
2495
2766
 
2496
- Command will be placed Migration in the specific directory
2767
+ The command will be placed Migration in the specific directory.
2497
2768
 
2498
- ```js
2769
+ ```sh
2499
2770
  /**
2500
2771
  *
2501
2772
  * @make Migration Table
@@ -2520,7 +2791,7 @@ tspace-mysql make:migration users --dir=app/Models/Migrations
2520
2791
 
2521
2792
  ## Migrate
2522
2793
 
2523
- ```js
2794
+ ```sh
2524
2795
  /**
2525
2796
  *
2526
2797
  * @run Migrate table
@@ -2549,18 +2820,18 @@ tspace-mysql migrate --dir=app/Models/Migrations --type=js
2549
2820
 
2550
2821
  # Query
2551
2822
 
2552
- Command will be execute a query
2823
+ The command will execute a query.
2553
2824
 
2554
- ```js
2825
+ ```sh
2555
2826
  tspace-mysql query "SELECT * FROM users"
2556
2827
 
2557
2828
  ```
2558
2829
 
2559
2830
  # Dump
2560
2831
 
2561
- Command will be dump database or table into file
2832
+ The command will dump the database or table into a file.
2562
2833
 
2563
- ```js
2834
+ ```sh
2564
2835
  tspace-mysql dump:db "database" --values // backup with values in the tables
2565
2836
 
2566
2837
  tspace-mysql dump:table "table" --values // backup with values in the table
@@ -2569,9 +2840,9 @@ tspace-mysql dump:table "table" --values // backup with values in the table
2569
2840
 
2570
2841
  # Generate Models
2571
2842
 
2572
- Command will be generate models from table in database
2843
+ The command will generate models from tables in the database.
2573
2844
 
2574
- ```js
2845
+ ```sh
2575
2846
  tspace-mysql generate:models --dir=<folder for creating>
2576
2847
 
2577
2848
  tspace-mysql generate:models --dir=app/Models --env=development --decorators
@@ -2580,13 +2851,38 @@ tspace-mysql generate:models --dir=app/Models --env=development --decorators
2580
2851
 
2581
2852
  # Migration Models
2582
2853
 
2583
- 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.
2584
2856
 
2585
- ```js
2586
- tspace-mysql migrations:models --dir=<path to migration> --models=<path to your models> --generate
2587
- 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
2588
2865
 
2589
2866
  tspace-mysql migrations:models --models=src/app/models --dir=migrations --generate
2590
2867
  tspace-mysql migrations:models --dir=migrations --push
2591
2868
 
2592
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
+ ```