tspace-mysql 1.4.5 → 1.4.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.
- package/README.md +268 -154
- package/dist/cli/generate/model.js +1 -1
- package/dist/lib/connection/index.d.ts +1 -1
- package/dist/lib/connection/index.js +9 -10
- package/dist/lib/connection/options.js +1 -1
- package/dist/lib/constants/index.js +15 -12
- package/dist/lib/tspace/Abstract/AbstractBuilder.d.ts +43 -42
- package/dist/lib/tspace/Abstract/AbstractBuilder.js +5 -1
- package/dist/lib/tspace/Abstract/AbstractDB.d.ts +2 -6
- package/dist/lib/tspace/Abstract/AbstractModel.d.ts +9 -9
- package/dist/lib/tspace/Abstract/AbstractModel.js +2 -5
- package/dist/lib/tspace/Blueprint.d.ts +18 -4
- package/dist/lib/tspace/Blueprint.js +25 -8
- package/dist/lib/tspace/Builder.d.ts +616 -253
- package/dist/lib/tspace/Builder.js +1476 -945
- package/dist/lib/tspace/DB.d.ts +100 -88
- package/dist/lib/tspace/DB.js +134 -212
- package/dist/lib/tspace/Interface.d.ts +24 -4
- package/dist/lib/tspace/Model.d.ts +332 -319
- package/dist/lib/tspace/Model.js +950 -1204
- package/dist/lib/tspace/RelationHandler.d.ts +32 -0
- package/dist/lib/tspace/RelationHandler.js +529 -0
- package/dist/lib/tspace/Schema.d.ts +9 -4
- package/dist/lib/tspace/Schema.js +119 -45
- package/dist/lib/tspace/StateHandler.js +4 -0
- package/dist/lib/utils/index.d.ts +4 -1
- package/dist/lib/utils/index.js +29 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,15 +21,17 @@ npm install tspace-mysql --save
|
|
|
21
21
|
- [Backup](#backup)
|
|
22
22
|
- [Generating Model Classes](#generating-model-classes)
|
|
23
23
|
- [Model Conventions](#model-conventions)
|
|
24
|
-
- [Relationships](#relationships)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- [Schema
|
|
24
|
+
- [Relationships](#relationships)
|
|
25
|
+
- [One To One](#one-to-one)
|
|
26
|
+
- [One To Many](#one-to-many)
|
|
27
|
+
- [Belongs To](#belongs-to)
|
|
28
|
+
- [Many To Many](#many-to-many)
|
|
29
|
+
- [Deeply Nested Relations](#deeply-nested-relations)
|
|
30
|
+
- [Relation Exists](#relation-exists)
|
|
31
|
+
- [Built in Relation Functions](#built-in-relation-functions)
|
|
32
|
+
- [Schema Model](#schema-model)
|
|
33
|
+
- [Validation](#validation)
|
|
34
|
+
- [Sync](#sync)
|
|
33
35
|
- [Query Builder](#query-builder)
|
|
34
36
|
- [Cli](#cli)
|
|
35
37
|
- [Make Model](#make-model)
|
|
@@ -52,7 +54,6 @@ DB_DATABASE = database
|
|
|
52
54
|
/**
|
|
53
55
|
* @default
|
|
54
56
|
* DB_CONNECTION_LIMIT = 10
|
|
55
|
-
* DB_CONNECTION_ERROR = true
|
|
56
57
|
* DB_QUEUE_LIMIT = 0
|
|
57
58
|
* DB_TIMEOUT = 60000
|
|
58
59
|
* DB_DATE_STRINGS = true
|
|
@@ -99,18 +100,27 @@ Once you have configured your database connection, you may run queries is this :
|
|
|
99
100
|
|
|
100
101
|
import { DB } from 'tspace-mysql'
|
|
101
102
|
(async () => {
|
|
102
|
-
await new DB('users').findMany() //
|
|
103
|
-
await new DB('users').findOne()
|
|
103
|
+
await new DB('users').findMany() // SELECT * FROM users => Array
|
|
104
|
+
await new DB('users').findOne() // SELECT * FROM users LIMIT 1 => Object
|
|
104
105
|
})()
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
Running A Raw Query
|
|
109
|
+
```js
|
|
110
|
+
const rawQuery = await new DB().query('SELECT * FROM users')
|
|
111
|
+
|
|
105
112
|
```
|
|
106
113
|
Running A Select Query
|
|
107
114
|
```js
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
115
|
+
const select = await new DB('users').select('id','username').findOne()
|
|
116
|
+
|
|
117
|
+
const selectRaw = await new DB('users').selectRaw('COUNT(id)').findMany()
|
|
112
118
|
|
|
113
|
-
const
|
|
119
|
+
const selectObject = await new DB('posts')
|
|
120
|
+
.join('posts.user_id', 'users.id')
|
|
121
|
+
.select('posts.*')
|
|
122
|
+
.selectObject({ id : 'users.id', name : 'users.name' , email : 'users.email'},'user')
|
|
123
|
+
.findOne()
|
|
114
124
|
|
|
115
125
|
/**
|
|
116
126
|
* @example except
|
|
@@ -138,119 +148,147 @@ await new DB('posts').rightJoin('posts.user_id' , 'users.id').findMany()
|
|
|
138
148
|
|
|
139
149
|
Running A Where Query
|
|
140
150
|
```js
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
151
|
+
const whereEqual = await new DB('users').where('id',1).findOne()
|
|
152
|
+
// SELECT * FROM `users` WHERE `users`.`id` = '1' LIMIT 1;
|
|
153
|
+
|
|
154
|
+
const whereNotEqual = await new DB('users').where('id','!=',1).findMany()
|
|
155
|
+
// SELECT * FROM `users` WHERE `users`.`id` != '1';
|
|
145
156
|
|
|
146
157
|
const whereIn = await new DB('users').whereIn('id',[1,2]).findMany()
|
|
158
|
+
// SELECT * FROM `users` WHERE `users`.`id` BETWEEN '1' AND '2';
|
|
159
|
+
|
|
147
160
|
const whereBetween = await new DB('users').whereBetween('id',[1,2]).findMany()
|
|
148
|
-
|
|
149
|
-
|
|
161
|
+
// SELECT * FROM `users` WHERE `users`.`id` BETWEEN '1' AND '2';
|
|
162
|
+
|
|
163
|
+
const whereSubQuery = await new DB('users').whereSubQuery('id','SELECT id FROM users').findMany()
|
|
164
|
+
// SELECT * FROM `users` WHERE `users`.`id` IN (SELECT id FROM users);
|
|
165
|
+
// or use -> await new DB('users').whereSubQuery('id',new DB('users').select('id').toString()).findMany()
|
|
166
|
+
|
|
150
167
|
const whereNull = await new DB('users').whereNull('username').findOne()
|
|
168
|
+
// SELECT * FROM `users` WHERE `users`.`username` IS NULL LIMIT 1;
|
|
169
|
+
|
|
151
170
|
const whereNotNull = await new DB('users').whereNotNull('username').findOne()
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
171
|
+
// SELECT * FROM `users` WHERE `users`.`username` IS NOT NULL LIMIT 1;
|
|
172
|
+
|
|
173
|
+
const whereQuery = await new DB('users').whereQuery(query => query.where('id',1).where('username','values')).whereIn('id',[1,2]).findOne()
|
|
174
|
+
// SELECT * FROM `users` WHERE ( `users`.`id` = '1' AND `users`.`username` = 'values') AND `users`.`id` IN ('1','2'') LIMIT 1;
|
|
175
|
+
|
|
176
|
+
const whereExists = await new DB('users').whereExists(new DB('users').select('id').where('id',1).toString()).findOne()
|
|
177
|
+
// SELECT * FROM `users` WHERE EXISTS (SELECT `id` FROM `users` WHERE id = 1) LIMIT 1;
|
|
178
|
+
|
|
179
|
+
const whereJSON = await new DB('users').whereJSON('json', { key : 'id', value : '1234' }).findOne()
|
|
180
|
+
// SELECT * FROM `users` WHERE `users`.`json`->>'$.id' = '1234' LIMIT 1;
|
|
181
|
+
|
|
182
|
+
const whereWhenIsTrue = await new DB('users').where('id',1).when(true, (query) => query.where('username','when is actived')).findOne()
|
|
183
|
+
// SELECT * FROM `users` WHERE `users`.`id` = '1' AND `users`.`username` = 'when is actived' LIMIT 1;
|
|
184
|
+
|
|
185
|
+
const whereWhenIsFalse = await new DB('users').where('id',1).when(false, (query) => query.where('username','when is actived')).findOne()
|
|
186
|
+
// SELECT * FROM `users` WHERE `users`.`id` = '1' LIMIT 1;
|
|
155
187
|
```
|
|
156
188
|
|
|
157
189
|
Running A Hook Query
|
|
158
190
|
```js
|
|
159
|
-
const hookResult = (result) => console.log(result)
|
|
191
|
+
const hookResult = (result) => console.log('hook!! result => ',result)
|
|
160
192
|
const user = await new DB('users').where('id',1).hook(hookResult).findOne()
|
|
161
193
|
```
|
|
162
194
|
|
|
163
195
|
Running A Insert Query
|
|
164
196
|
```js
|
|
165
197
|
const user = await new DB('users')
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
198
|
+
.create({
|
|
199
|
+
name : 'tspace3',
|
|
200
|
+
email : 'tspace3@gmail.com'
|
|
201
|
+
})
|
|
202
|
+
.save()
|
|
203
|
+
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
171
204
|
|
|
172
205
|
+--------------------------------------------------------------------------+
|
|
173
206
|
|
|
174
207
|
const reposity = new DB('users')
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
await reposity.save()
|
|
178
|
-
const { result } = reposity
|
|
179
|
-
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
208
|
+
reposity.name = 'tspace4'
|
|
209
|
+
reposity.email = 'tspace4@gmail.com'
|
|
180
210
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
email : 'tspace4@gmail.com'
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
name :'tspace5',
|
|
189
|
-
email : 'tspace5@gmail.com'
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
name :'tspace6',
|
|
193
|
-
email : 'tspace6@gmail.com'
|
|
194
|
-
},
|
|
195
|
-
])
|
|
196
|
-
.save()
|
|
211
|
+
await reposity.save()
|
|
212
|
+
|
|
213
|
+
const { result } = reposity
|
|
214
|
+
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
197
215
|
|
|
198
216
|
const users = await new DB('users')
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
.createNotExists({
|
|
217
|
+
.createMultiple([
|
|
218
|
+
{
|
|
202
219
|
name :'tspace4',
|
|
203
220
|
email : 'tspace4@gmail.com'
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
name :'tspace5',
|
|
224
|
+
email : 'tspace5@gmail.com'
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
name :'tspace6',
|
|
228
|
+
email : 'tspace6@gmail.com'
|
|
229
|
+
},
|
|
230
|
+
])
|
|
231
|
+
.save()
|
|
207
232
|
|
|
208
233
|
const users = await new DB('users')
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
234
|
+
.where('name','tspace4')
|
|
235
|
+
.where('email','tspace4@gmail.com')
|
|
236
|
+
.createNotExists({
|
|
237
|
+
name :'tspace4',
|
|
238
|
+
email : 'tspace4@gmail.com'
|
|
239
|
+
})
|
|
240
|
+
.save()
|
|
241
|
+
// if has exists return null, if not exists created new data
|
|
242
|
+
|
|
243
|
+
const users = await new DB('users')
|
|
244
|
+
.where('name','tspace4')
|
|
245
|
+
.where('email','tspace4@gmail.com')
|
|
246
|
+
.createOrSelect({
|
|
247
|
+
name :'tspace4',
|
|
248
|
+
email : 'tspace4@gmail.com'
|
|
249
|
+
})
|
|
250
|
+
.save()
|
|
251
|
+
// if has exists return data, if not exists created new data
|
|
217
252
|
|
|
218
253
|
```
|
|
219
254
|
Running A Update Query
|
|
220
255
|
```js
|
|
221
256
|
const user = await new DB('users')
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
257
|
+
.where('id',1)
|
|
258
|
+
.update({
|
|
259
|
+
name : 'tspace1**',
|
|
260
|
+
email : 'tspace1@gmail.com'
|
|
261
|
+
})
|
|
262
|
+
.save()
|
|
263
|
+
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
228
264
|
|
|
229
265
|
+--------------------------------------------------------------------------+
|
|
230
266
|
|
|
231
267
|
const reposity = new DB('users').where('id',1)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
268
|
+
reposity.name = 'tspace1++'
|
|
269
|
+
reposity.email = 'tspace1++@gmail.com'
|
|
270
|
+
|
|
271
|
+
await reposity.save()
|
|
272
|
+
|
|
235
273
|
const { result } = reposity
|
|
236
|
-
|
|
237
|
-
```
|
|
274
|
+
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
238
275
|
|
|
276
|
+
```
|
|
239
277
|
Running A Update Or Created Query
|
|
240
278
|
```js
|
|
241
279
|
const user = await new DB('users')
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
280
|
+
.where('id',1)
|
|
281
|
+
.updateOrCreate({
|
|
282
|
+
name : 'tspace1**',
|
|
283
|
+
email : 'tspace1@gmail.com'
|
|
284
|
+
}).save()
|
|
285
|
+
// user => { username : 'tspace1**', email : 'tspace1@gmail.com' }
|
|
248
286
|
```
|
|
249
287
|
|
|
250
288
|
Running A Delete Query
|
|
251
289
|
```js
|
|
252
290
|
const deleted = await new DB('users').where('id',1).delete()
|
|
253
|
-
|
|
291
|
+
// deleted => Boolean
|
|
254
292
|
```
|
|
255
293
|
## Database Transactions
|
|
256
294
|
|
|
@@ -313,7 +351,6 @@ try {
|
|
|
313
351
|
}
|
|
314
352
|
|
|
315
353
|
```
|
|
316
|
-
|
|
317
354
|
## Connection
|
|
318
355
|
When establishing a connection, you may establish options is this:
|
|
319
356
|
```js
|
|
@@ -326,8 +363,8 @@ const connection = await new DB().getConnection({
|
|
|
326
363
|
})
|
|
327
364
|
|
|
328
365
|
const users = await new DB('users')
|
|
329
|
-
|
|
330
|
-
|
|
366
|
+
.bind(connection)
|
|
367
|
+
.findMany()
|
|
331
368
|
// users => [{ .... }]
|
|
332
369
|
```
|
|
333
370
|
|
|
@@ -355,15 +392,15 @@ const backup = await new DB().backup({
|
|
|
355
392
|
* @param {object | null} conection defalut current connection
|
|
356
393
|
*/
|
|
357
394
|
const backupToFile = await new DB().backupToFile({
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
395
|
+
database: 'try-to-backup',
|
|
396
|
+
filePath: 'backup.sql',
|
|
397
|
+
connection ?: {
|
|
361
398
|
host: 'localhost',
|
|
362
399
|
port : 3306,
|
|
363
400
|
database: 'database'
|
|
364
401
|
username: 'username',
|
|
365
402
|
password: 'password',
|
|
366
|
-
|
|
403
|
+
}
|
|
367
404
|
})
|
|
368
405
|
// backupToFile => backup.sql
|
|
369
406
|
```
|
|
@@ -401,7 +438,7 @@ class User extends Model {
|
|
|
401
438
|
*
|
|
402
439
|
* Assign setting global in your model
|
|
403
440
|
* @useMethod
|
|
404
|
-
*
|
|
441
|
+
* this.usePattern('camelCase') // => default 'snake_case'
|
|
405
442
|
* this.useDebug()
|
|
406
443
|
* this.usePrimaryKey('id')
|
|
407
444
|
* this.useTimestamp({
|
|
@@ -411,8 +448,7 @@ class User extends Model {
|
|
|
411
448
|
* this.useSoftDelete('deletedAt') // => default target to colmun deleted_at
|
|
412
449
|
* this.useTable('users')
|
|
413
450
|
* this.useTableSingular() // => 'user'
|
|
414
|
-
* this.useTablePlural() // => 'users'
|
|
415
|
-
* this.usePattern('camelCase') // => default 'snake_case'
|
|
451
|
+
* this.useTablePlural() // => 'users'
|
|
416
452
|
* this.useUUID('uuid') // => runing a uuid (universally unique identifier) when insert new data
|
|
417
453
|
* this.useRegistry() // => build-in functions registry
|
|
418
454
|
* this.useLoadRelationsInRegistry() // => auto generated result from relationship to results
|
|
@@ -421,14 +457,34 @@ class User extends Model {
|
|
|
421
457
|
* this.useSchema ({
|
|
422
458
|
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
423
459
|
* uuid : new Blueprint().varchar(50).null(),
|
|
424
|
-
*
|
|
425
|
-
*
|
|
460
|
+
* name : new Blueprint().varchar(191).notNull(),
|
|
461
|
+
* email : new Blueprint().varchar(191).notNull(),
|
|
426
462
|
* created_at : new Blueprint().timestamp().null(),
|
|
427
463
|
* updated_at : new Blueprint().timestamp().null(),
|
|
428
464
|
* deleted_at : new Blueprint().timestamp().null()
|
|
429
465
|
* }) // auto-generated table when table is not exists and auto-create column when column not exists
|
|
430
466
|
*
|
|
431
|
-
*
|
|
467
|
+
* // validate input when create or update reference to the schema in 'this.useSchema'
|
|
468
|
+
* this.useValidateSchema({
|
|
469
|
+
* id : Number,
|
|
470
|
+
* uuid : Number,
|
|
471
|
+
* name : {
|
|
472
|
+
* type : String,
|
|
473
|
+
* length : 191
|
|
474
|
+
* require : true
|
|
475
|
+
* },
|
|
476
|
+
* email : {
|
|
477
|
+
* type : String,
|
|
478
|
+
* require : true,
|
|
479
|
+
* length : 191,
|
|
480
|
+
* match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
481
|
+
* unique : true,
|
|
482
|
+
* fn : (email : string) => !/^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
483
|
+
* },
|
|
484
|
+
* created_at : Date,
|
|
485
|
+
* updated_at : Date,
|
|
486
|
+
* deleted_at : Date
|
|
487
|
+
* })
|
|
432
488
|
*/
|
|
433
489
|
|
|
434
490
|
|
|
@@ -667,7 +723,7 @@ await new User()
|
|
|
667
723
|
|
|
668
724
|
```
|
|
669
725
|
## Relation Exists
|
|
670
|
-
Relationships can return only result is not empty in relations.
|
|
726
|
+
Relationships can return only result is not empty in relations (soft delete).
|
|
671
727
|
let's example a exists in relations :
|
|
672
728
|
```js
|
|
673
729
|
+-------------+--------------+----------------------------+--------------------+
|
|
@@ -758,12 +814,11 @@ await new User().relationsExists('posts').findMany()
|
|
|
758
814
|
* ]
|
|
759
815
|
* }
|
|
760
816
|
* ]
|
|
761
|
-
* because posts id 1 and id 3 has been removed from database using soft delete
|
|
817
|
+
* because posts id 1 and id 3 has been removed from database (using soft delete)
|
|
762
818
|
*/
|
|
763
819
|
|
|
764
820
|
|
|
765
821
|
```
|
|
766
|
-
|
|
767
822
|
## Built in Relation Functions
|
|
768
823
|
Relationships can using built in function in results
|
|
769
824
|
let's example a built in function :
|
|
@@ -799,16 +854,65 @@ class Comment extends Model {
|
|
|
799
854
|
const user = await new User().findOne()
|
|
800
855
|
const posts = await user.$posts()
|
|
801
856
|
|
|
802
|
-
/** Warning built
|
|
857
|
+
/** Warning built-in function has Big-O effect */
|
|
803
858
|
for (const post of posts) {
|
|
804
859
|
const comments = await post.$comments()
|
|
805
860
|
}
|
|
806
861
|
|
|
862
|
+
```
|
|
863
|
+
## Schema Model
|
|
864
|
+
Define the schema of Model
|
|
865
|
+
let's example a validator model:
|
|
866
|
+
|
|
867
|
+
## Validation
|
|
868
|
+
Validate the schema of Model
|
|
869
|
+
let's example a validator model:
|
|
870
|
+
```js
|
|
871
|
+
|
|
872
|
+
class User extends Model {
|
|
873
|
+
constructor(){
|
|
874
|
+
super()
|
|
875
|
+
this.useCamelCase()
|
|
876
|
+
this.useSchema ({
|
|
877
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
878
|
+
uuid : new Blueprint().varchar(50).null(),
|
|
879
|
+
name : new Blueprint().varchar(191).notNull(),
|
|
880
|
+
email : new Blueprint().varchar(191).notNull(),
|
|
881
|
+
createdAt : new Blueprint().timestamp().null(),
|
|
882
|
+
updatedAt : new Blueprint().timestamp().null(),
|
|
883
|
+
deletedAt : new Blueprint().timestamp().null()
|
|
884
|
+
})
|
|
885
|
+
// validate input when create or update reference to the schema in 'this.useSchema'
|
|
886
|
+
this.useValidateSchema({
|
|
887
|
+
id : Number,
|
|
888
|
+
uuid : Number,
|
|
889
|
+
name : {
|
|
890
|
+
type : String,
|
|
891
|
+
length : 191
|
|
892
|
+
require : true,
|
|
893
|
+
json : true
|
|
894
|
+
},
|
|
895
|
+
email : {
|
|
896
|
+
type : String,
|
|
897
|
+
require : true,
|
|
898
|
+
length : 191,
|
|
899
|
+
match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
900
|
+
unique : true,
|
|
901
|
+
fn : (email : string) => !/^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
902
|
+
},
|
|
903
|
+
createdAt : Date,
|
|
904
|
+
updatedAt : Date,
|
|
905
|
+
deletedAt : Date
|
|
906
|
+
})
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
807
910
|
```
|
|
808
911
|
|
|
809
|
-
##
|
|
810
|
-
Sync schema
|
|
811
|
-
Sync will check for create or update table
|
|
912
|
+
## Sync
|
|
913
|
+
Sync schema with Models setting in your directory,
|
|
914
|
+
Sync will check for create or update table columns and foreign keys,
|
|
915
|
+
Related by method 'useSchema' in your models.
|
|
812
916
|
Let's examine a basic model sync class:
|
|
813
917
|
|
|
814
918
|
```js
|
|
@@ -817,48 +921,50 @@ Let's examine a basic model sync class:
|
|
|
817
921
|
* @Ex directory
|
|
818
922
|
*
|
|
819
923
|
* - node_modules
|
|
820
|
-
* -
|
|
924
|
+
* - src
|
|
925
|
+
* - index.ts
|
|
821
926
|
* - Models
|
|
822
927
|
* - User.ts
|
|
823
928
|
* - Post.ts
|
|
824
|
-
*
|
|
825
|
-
* // file User.ts
|
|
826
|
-
* class User extends Model {
|
|
827
|
-
* constructor(){
|
|
828
|
-
* super()
|
|
829
|
-
* this.hasMany({ name : 'posts' , model : Post })
|
|
830
|
-
* this.useSchema ({
|
|
831
|
-
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
832
|
-
* uuid : new Blueprint().varchar(50).null(),
|
|
833
|
-
* email : new Blueprint().int().notNull().unique(),
|
|
834
|
-
* name : new Blueprint().varchar(255).null(),
|
|
835
|
-
* created_at : new Blueprint().timestamp().null(),
|
|
836
|
-
* updated_at : new Blueprint().timestamp().null(),
|
|
837
|
-
* deleted_at : new Blueprint().timestamp().null()
|
|
838
|
-
* })
|
|
839
|
-
* }
|
|
840
|
-
* }
|
|
841
|
-
*
|
|
842
|
-
* // file Post.ts
|
|
843
|
-
* class Post extends Model {
|
|
844
|
-
* constructor(){
|
|
845
|
-
* super()
|
|
846
|
-
* this.hasMany({ name : 'comments' , model : Comment })
|
|
847
|
-
* this.belongsTo({ name : 'user' , model : User })
|
|
848
|
-
* this.useSchema ({
|
|
849
|
-
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
850
|
-
* uuid : new Blueprint().varchar(50).null(),
|
|
851
|
-
* user_id : new Blueprint().int().notNull(),
|
|
852
|
-
* title : new Blueprint().varchar(255).null(),
|
|
853
|
-
* created_at : new Blueprint().timestamp().null(),
|
|
854
|
-
* updated_at : new Blueprint().timestamp().null(),
|
|
855
|
-
* deleted_at : new Blueprint().timestamp().null()
|
|
856
|
-
* })
|
|
857
|
-
* }
|
|
858
|
-
* }
|
|
859
|
-
*
|
|
860
929
|
*/
|
|
861
|
-
|
|
930
|
+
|
|
931
|
+
// file User
|
|
932
|
+
class User extends Model {
|
|
933
|
+
constructor(){
|
|
934
|
+
super()
|
|
935
|
+
this.hasMany({ name : 'posts' , model : Post })
|
|
936
|
+
this.useSchema ({
|
|
937
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
938
|
+
uuid : new Blueprint().varchar(50).null(),
|
|
939
|
+
email : new Blueprint().int().notNull().unique(),
|
|
940
|
+
name : new Blueprint().varchar(255).null(),
|
|
941
|
+
created_at : new Blueprint().timestamp().null(),
|
|
942
|
+
updated_at : new Blueprint().timestamp().null(),
|
|
943
|
+
deleted_at : new Blueprint().timestamp().null()
|
|
944
|
+
})
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// file Post
|
|
949
|
+
import User from './User'
|
|
950
|
+
class Post extends Model {
|
|
951
|
+
constructor(){
|
|
952
|
+
super()
|
|
953
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
954
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
955
|
+
this.useSchema ({
|
|
956
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
957
|
+
uuid : new Blueprint().varchar(50).null(),
|
|
958
|
+
user_id : new Blueprint().int().notNull()
|
|
959
|
+
.foreign({ references : 'id' , on : User ,onDelete : 'CASCADE' , onUpdate : 'CASCADE' }),
|
|
960
|
+
title : new Blueprint().varchar(255).null(),
|
|
961
|
+
created_at : new Blueprint().timestamp().null(),
|
|
962
|
+
updated_at : new Blueprint().timestamp().null(),
|
|
963
|
+
deleted_at : new Blueprint().timestamp().null()
|
|
964
|
+
})
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
await Schema.sync(`src/Models` , { force : true })
|
|
862
968
|
|
|
863
969
|
```
|
|
864
970
|
## Query Builder
|
|
@@ -875,6 +981,7 @@ whereNull(column)
|
|
|
875
981
|
whereNotNull(column)
|
|
876
982
|
whereBetween (column , [value1 , value2])
|
|
877
983
|
whereQuery(callback)
|
|
984
|
+
whereJson(column, { targetKey, value , operator })
|
|
878
985
|
whereRaw(sql)
|
|
879
986
|
whereExists(sql)
|
|
880
987
|
whereSubQuery(colmn , rawSQL)
|
|
@@ -883,7 +990,10 @@ orWhere(column , operator , value)
|
|
|
883
990
|
orWhereRaw(sql)
|
|
884
991
|
orWhereIn(column , [])
|
|
885
992
|
orWhereSubQuery(colmn , rawSQL)
|
|
993
|
+
when(contition , callback)
|
|
994
|
+
if(contition , callback)
|
|
886
995
|
select(column1 ,column2 ,...N)
|
|
996
|
+
distinct()
|
|
887
997
|
selectRaw(column1 ,column2 ,...N)
|
|
888
998
|
except(column1 ,column2 ,...N)
|
|
889
999
|
only(column1 ,column2 ,...N)
|
|
@@ -905,8 +1015,10 @@ groupByRaw (column)
|
|
|
905
1015
|
create(objects)
|
|
906
1016
|
createMultiple(array objects)
|
|
907
1017
|
update (objects)
|
|
1018
|
+
updateMany (objects)
|
|
908
1019
|
createNotExists(objects)
|
|
909
1020
|
updateOrCreate (objects)
|
|
1021
|
+
onlyTrashed()
|
|
910
1022
|
connection(options)
|
|
911
1023
|
backup({ database , connection })
|
|
912
1024
|
backupToFile({ filePath, database , connection })
|
|
@@ -950,9 +1062,8 @@ findMany()
|
|
|
950
1062
|
findOne()
|
|
951
1063
|
find(id)
|
|
952
1064
|
delelte()
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
toSQL()
|
|
1065
|
+
delelteMany()
|
|
1066
|
+
exists()
|
|
956
1067
|
toString()
|
|
957
1068
|
toJSON()
|
|
958
1069
|
toArray(column)
|
|
@@ -962,7 +1073,7 @@ avg(column)
|
|
|
962
1073
|
max(column)
|
|
963
1074
|
min(column)
|
|
964
1075
|
pagination({ limit , page })
|
|
965
|
-
save() /*for
|
|
1076
|
+
save() /* for actions statements insert or update */
|
|
966
1077
|
```
|
|
967
1078
|
|
|
968
1079
|
## Cli
|
|
@@ -1068,9 +1179,9 @@ tspace-mysql dump:table "table" --values // backup with values in the table
|
|
|
1068
1179
|
# Generate Models
|
|
1069
1180
|
Command will be generate models from table in database
|
|
1070
1181
|
```js
|
|
1071
|
-
tspace-mysql generate:models --dir=<folder for creating>
|
|
1182
|
+
tspace-mysql generate:models --dir=<folder for creating>
|
|
1072
1183
|
or
|
|
1073
|
-
tspace-mysql gen:models --dir=<folder for creating>
|
|
1184
|
+
tspace-mysql gen:models --dir=<folder for creating> --env=development
|
|
1074
1185
|
|
|
1075
1186
|
```
|
|
1076
1187
|
|
|
@@ -1079,13 +1190,14 @@ Schema table created by command make:migration, you may use the:
|
|
|
1079
1190
|
```js
|
|
1080
1191
|
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
1081
1192
|
(async () => {
|
|
1082
|
-
await new Schema().table('users',{
|
|
1083
|
-
id :
|
|
1193
|
+
await new Schema().table('users', {
|
|
1194
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
1084
1195
|
uuid : new Blueprint().varchar(120).null()
|
|
1085
1196
|
name : new Blueprint().varchar(120).default('name'),
|
|
1086
1197
|
email : new Blueprint().varchar(255).unique().notNull(),
|
|
1087
1198
|
email_verify : new Blueprint().tinyInt(),
|
|
1088
1199
|
password : new Blueprint().varchar(255),
|
|
1200
|
+
json : new Blueprint().json(),
|
|
1089
1201
|
created_at : new Blueprint().null().timestamp(),
|
|
1090
1202
|
updated_at : new Blueprint().null().timestamp()
|
|
1091
1203
|
})
|
|
@@ -1097,15 +1209,16 @@ import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
|
1097
1209
|
})()
|
|
1098
1210
|
|
|
1099
1211
|
/**
|
|
1100
|
-
*
|
|
1212
|
+
* add Type of schema in database
|
|
1101
1213
|
* @Types
|
|
1102
1214
|
*
|
|
1103
1215
|
*/
|
|
1104
|
-
int ()
|
|
1216
|
+
int (number)
|
|
1105
1217
|
tinyInt (number)
|
|
1106
1218
|
bigInt (number)
|
|
1107
1219
|
double ()
|
|
1108
1220
|
float ()
|
|
1221
|
+
json ()
|
|
1109
1222
|
varchar (number)
|
|
1110
1223
|
char (number)
|
|
1111
1224
|
longText()
|
|
@@ -1118,7 +1231,7 @@ dateTime()
|
|
|
1118
1231
|
timestamp ()
|
|
1119
1232
|
|
|
1120
1233
|
/**
|
|
1121
|
-
*
|
|
1234
|
+
* add Attrbuites of schema in database
|
|
1122
1235
|
* @Attrbuites
|
|
1123
1236
|
*
|
|
1124
1237
|
*/
|
|
@@ -1130,4 +1243,5 @@ primary()
|
|
|
1130
1243
|
default(string)
|
|
1131
1244
|
defaultTimestamp()
|
|
1132
1245
|
autoIncrement()
|
|
1246
|
+
foreign({ references : <column> , on : <table or model>}),
|
|
1133
1247
|
```
|
|
@@ -14,7 +14,7 @@ class ${model} extends Model {
|
|
|
14
14
|
* createdAt : 'created_at',
|
|
15
15
|
* updatedAt : 'updated_at'
|
|
16
16
|
* }) // runing a timestamp when insert or update
|
|
17
|
-
* this.useSoftDelete('deletedAt') // => default target to
|
|
17
|
+
* this.useSoftDelete('deletedAt') // => default target to column deleted_at
|
|
18
18
|
* this.usePattern('snake_case') // => default 'snake_case'
|
|
19
19
|
* this.useUUID('uuid') // => runing a uuid (universally unique identifier) when insert new data
|
|
20
20
|
* this.useRegistry() // => build-in functions registry
|