tspace-mysql 1.1.0 → 1.1.2
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 +612 -678
- package/dist/cli/models/model.js +1 -1
- package/dist/cli/tables/table.js +1 -1
- package/dist/lib/constants/index.js +0 -1
- package/dist/lib/tspace/AbstractDatabase.d.ts +0 -6
- package/dist/lib/tspace/AbstractModel.d.ts +16 -31
- package/dist/lib/tspace/Database.d.ts +0 -20
- package/dist/lib/tspace/Database.js +49 -168
- package/dist/lib/tspace/Model.d.ts +14 -41
- package/dist/lib/tspace/Model.js +89 -192
- package/package.json +55 -53
package/README.md
CHANGED
|
@@ -1,679 +1,613 @@
|
|
|
1
|
-
## tspace-mysql
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com)
|
|
4
|
-
[](https://www.npmjs.com)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
Install with [npm](https://www.npmjs.com/):
|
|
12
|
-
|
|
13
|
-
```sh
|
|
14
|
-
npm install tspace-mysql --save
|
|
15
|
-
```
|
|
16
|
-
## Basic Usage
|
|
17
|
-
- [
|
|
18
|
-
- [
|
|
19
|
-
- [
|
|
20
|
-
- [
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
25
|
-
- [
|
|
26
|
-
- [One To
|
|
27
|
-
- [One To Many](#
|
|
28
|
-
- [
|
|
29
|
-
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
- [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
+-------------+--------------+----------------------------+
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// user =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
import
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
*
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
*
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
*
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
*/
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
## Migrate
|
|
614
|
-
```sh
|
|
615
|
-
tspace-mysql migrate <FOLDER> --type=...
|
|
616
|
-
/**
|
|
617
|
-
* @options
|
|
618
|
-
*--type=js /* extension js default ts */
|
|
619
|
-
*--dir=directory /* find migrate in directory */
|
|
620
|
-
*/
|
|
621
|
-
|
|
622
|
-
tspace-mysql migrate --dir=App/Models/Migrations
|
|
623
|
-
// => migrate all schema table in folder into database
|
|
624
|
-
```
|
|
625
|
-
|
|
626
|
-
## Blueprint
|
|
627
|
-
Schema table created by command make:migration. you may use the:
|
|
628
|
-
```js
|
|
629
|
-
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
630
|
-
(async () => {
|
|
631
|
-
await new Schema().table('users',{
|
|
632
|
-
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
633
|
-
name : new Blueprint().varchar(120).default('my name'),
|
|
634
|
-
email : new Blueprint().varchar(255).unique(),
|
|
635
|
-
email_verify : new Blueprint().tinyInt(),
|
|
636
|
-
password : new Blueprint().varchar(255),
|
|
637
|
-
created_at : new Blueprint().null().timestamp(),
|
|
638
|
-
updated_at : new Blueprint().null().timestamp()
|
|
639
|
-
})
|
|
640
|
-
/**
|
|
641
|
-
*
|
|
642
|
-
* @Faker fake data 5 raw
|
|
643
|
-
* await new DB().table('users').faker(5)
|
|
644
|
-
*/
|
|
645
|
-
})()
|
|
646
|
-
```
|
|
647
|
-
|
|
648
|
-
## Types
|
|
649
|
-
|
|
650
|
-
```js
|
|
651
|
-
int ()
|
|
652
|
-
tinyInt (number)
|
|
653
|
-
bigInt (number)
|
|
654
|
-
double ()
|
|
655
|
-
float ()
|
|
656
|
-
varchar (number)
|
|
657
|
-
char (number)
|
|
658
|
-
longText()
|
|
659
|
-
mediumText()
|
|
660
|
-
tinyText()
|
|
661
|
-
text()
|
|
662
|
-
enum(...n)
|
|
663
|
-
date()
|
|
664
|
-
dateTime()
|
|
665
|
-
timestamp()
|
|
666
|
-
```
|
|
667
|
-
|
|
668
|
-
## Attrbuites
|
|
669
|
-
|
|
670
|
-
```js
|
|
671
|
-
unsigned ()
|
|
672
|
-
unique ()
|
|
673
|
-
null ()
|
|
674
|
-
notNull ()
|
|
675
|
-
primary()
|
|
676
|
-
default(string)
|
|
677
|
-
defaultTimestamp()
|
|
678
|
-
autoIncrement()
|
|
1
|
+
## tspace-mysql
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com)
|
|
4
|
+
[](https://www.npmjs.com)
|
|
5
|
+
|
|
6
|
+
tspace-mysql is an ORM that can run in NodeJs and can be used with TypeScript.
|
|
7
|
+
Its always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
Install with [npm](https://www.npmjs.com/):
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install tspace-mysql --save
|
|
15
|
+
```
|
|
16
|
+
## Basic Usage
|
|
17
|
+
- [Configuration](#configuration)
|
|
18
|
+
- [Running Queries](#running-queryies)
|
|
19
|
+
- [Database Transactions](#database-transactions)
|
|
20
|
+
- [Connection](#connection)
|
|
21
|
+
- [Backup](#backup)
|
|
22
|
+
- [Generating Model Classes](#generating-model-classes)
|
|
23
|
+
- [Model Conventions](#model-conventions)
|
|
24
|
+
- [Relationships](#relationships)
|
|
25
|
+
- [One To One](#one-to-one)
|
|
26
|
+
- [One To Many](#one-to-many)
|
|
27
|
+
- [One To One & One To Many (Inverse) / Belongs To](#inverse-belongs-to)
|
|
28
|
+
- [Many To Many](#many-to-many)
|
|
29
|
+
- [Query Builder](#query-builder)
|
|
30
|
+
- [Cli](#cli)
|
|
31
|
+
- [Make Model](#make-model)
|
|
32
|
+
- [Make Migration](#make-migration)
|
|
33
|
+
- [Migrate](#migrate)
|
|
34
|
+
- [Blueprint](#blueprint)
|
|
35
|
+
|
|
36
|
+
## Configuration
|
|
37
|
+
Created your environment variables is to use a .env file, you may establish a connection is this:
|
|
38
|
+
```js
|
|
39
|
+
DB_HOST = localhost
|
|
40
|
+
DB_PORT = 3306
|
|
41
|
+
DB_USERNAME = root
|
|
42
|
+
DB_PASSWORD = password
|
|
43
|
+
DB_DATABASE = database
|
|
44
|
+
```
|
|
45
|
+
## Running Queries
|
|
46
|
+
Once you have configured your database connection, you may run queries is this :
|
|
47
|
+
```js
|
|
48
|
+
+-------------+--------------+----------------------------+
|
|
49
|
+
| table users |
|
|
50
|
+
+-------------+--------------+----------------------------+
|
|
51
|
+
| id | username | email |
|
|
52
|
+
|-------------|--------------|----------------------------|
|
|
53
|
+
| 1 | tspace | tspace@gmail.com |
|
|
54
|
+
| 2 | tspace2 | tspace2@gmail.com |
|
|
55
|
+
+-------------+--------------+----------------------------+
|
|
56
|
+
|
|
57
|
+
import { DB } from 'tspace-mysql'
|
|
58
|
+
(async () => {
|
|
59
|
+
await new DB('users').findMany() // SELECT * FROM users => Array
|
|
60
|
+
await new DB('users').findOne() // SELECT * FROM users LIMIT 1 => Object
|
|
61
|
+
})()
|
|
62
|
+
```
|
|
63
|
+
Running A Select Query
|
|
64
|
+
```js
|
|
65
|
+
const selectQuery = await new DB('users').select('id','username').findOne()
|
|
66
|
+
// selectQuery => { id : 1, username : 'tspace'}
|
|
67
|
+
const selectQueries = await new DB('users').select('id','username').findMany()
|
|
68
|
+
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
69
|
+
```
|
|
70
|
+
Running A Where Query
|
|
71
|
+
```js
|
|
72
|
+
const user = await new DB('users').where('id',1).findOne()
|
|
73
|
+
// user => { id : 1 , username : 'tspace', email : 'tspace@gmail.com'}
|
|
74
|
+
const users = await new DB('users').where('id','!=',1).findMany()
|
|
75
|
+
// users => [{ id : 2 , username : 'tspace2' , email : 'tspace2@gmail.com' }]
|
|
76
|
+
```
|
|
77
|
+
Running A Insert Query
|
|
78
|
+
```js
|
|
79
|
+
const user = await new DB('users')
|
|
80
|
+
.create({
|
|
81
|
+
name : 'tspace3',
|
|
82
|
+
email : 'tspace3@gmail.com'
|
|
83
|
+
}).save()
|
|
84
|
+
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
85
|
+
|
|
86
|
+
+--------------------------------------------------------------------------+
|
|
87
|
+
|
|
88
|
+
const reposity = new DB('users')
|
|
89
|
+
reposity.name = 'tspace4'
|
|
90
|
+
reposity.email = 'tspace4@gmail.com'
|
|
91
|
+
await reposity.save()
|
|
92
|
+
const { result } = reposity
|
|
93
|
+
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
94
|
+
```
|
|
95
|
+
Running A Update Query
|
|
96
|
+
```js
|
|
97
|
+
const user = await new DB('users')
|
|
98
|
+
.where('id',1)
|
|
99
|
+
.update({
|
|
100
|
+
name : 'tspace1**',
|
|
101
|
+
email : 'tspace1@gmail.com'
|
|
102
|
+
}).save()
|
|
103
|
+
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
104
|
+
|
|
105
|
+
+--------------------------------------------------------------------------+
|
|
106
|
+
|
|
107
|
+
const reposity = new DB('users').where('id',1)
|
|
108
|
+
reposity.name = 'tspace1++'
|
|
109
|
+
reposity.email = 'tspace1++@gmail.com'
|
|
110
|
+
await reposity.save()
|
|
111
|
+
const { result } = reposity
|
|
112
|
+
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
113
|
+
```
|
|
114
|
+
Running A Delete Query
|
|
115
|
+
```js
|
|
116
|
+
const deleted = await new DB('users').where('id',1).delete()
|
|
117
|
+
// deleted => true
|
|
118
|
+
```
|
|
119
|
+
## Database Transactions
|
|
120
|
+
|
|
121
|
+
Within a database transaction, you may use the:
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
const connection = await new DB().beginTransaction()
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* @startTransaction start transaction in scopes function
|
|
130
|
+
*/
|
|
131
|
+
await connection.startTransaction()
|
|
132
|
+
|
|
133
|
+
const user = await new User()
|
|
134
|
+
.create({
|
|
135
|
+
name : `tspace`,
|
|
136
|
+
email : 'tspace@example.com'
|
|
137
|
+
})
|
|
138
|
+
/**
|
|
139
|
+
*
|
|
140
|
+
* bind method for make sure this connection has same transaction in connection
|
|
141
|
+
* @params {Function} connection
|
|
142
|
+
*/
|
|
143
|
+
.bind(connection)
|
|
144
|
+
.save()
|
|
145
|
+
|
|
146
|
+
const posts = await new Post().createMultiple([
|
|
147
|
+
{
|
|
148
|
+
user_id : user.id,
|
|
149
|
+
title : `tspace-post1`
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
user_id : user.id,
|
|
153
|
+
title : `tspace-post2`
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
user_id : user.id,
|
|
157
|
+
title : `tspace-post3`
|
|
158
|
+
}
|
|
159
|
+
])
|
|
160
|
+
/**
|
|
161
|
+
*
|
|
162
|
+
* bind method for make sure this connection has same transaction in connection
|
|
163
|
+
* @params {Function} connection
|
|
164
|
+
*/
|
|
165
|
+
.bind(connection)
|
|
166
|
+
.save()
|
|
167
|
+
|
|
168
|
+
// throw new Error('try error')
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
* @commit commit transaction to database
|
|
173
|
+
*/
|
|
174
|
+
await connection.commit()
|
|
175
|
+
|
|
176
|
+
} catch (err) {
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
*
|
|
180
|
+
* @rollback rollback transaction
|
|
181
|
+
*/
|
|
182
|
+
await connection.rollback()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Connection
|
|
188
|
+
When establishing a connection, you may establish options is this:
|
|
189
|
+
```js
|
|
190
|
+
const users = await new DB('users')
|
|
191
|
+
.connection({
|
|
192
|
+
host: 'localhost',
|
|
193
|
+
port : 3306,
|
|
194
|
+
database: 'database'
|
|
195
|
+
username: 'username',
|
|
196
|
+
password: 'password',
|
|
197
|
+
})
|
|
198
|
+
.findMany()
|
|
199
|
+
// users => [{ .... }]
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Backup
|
|
203
|
+
Backup database, you may backup is this:
|
|
204
|
+
```js
|
|
205
|
+
/**
|
|
206
|
+
*
|
|
207
|
+
* @param conection defalut current connection
|
|
208
|
+
*/
|
|
209
|
+
const backup = await new DB().backup({
|
|
210
|
+
database: 'try-to-backup',
|
|
211
|
+
connection ?: {
|
|
212
|
+
host: 'localhost',
|
|
213
|
+
port : 3306,
|
|
214
|
+
database: 'database'
|
|
215
|
+
username: 'username',
|
|
216
|
+
password: 'password',
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
/**
|
|
220
|
+
*
|
|
221
|
+
* @param {string} database Database selected
|
|
222
|
+
* @param {string} filePath file path
|
|
223
|
+
* @param {object | null} conection defalut current connection
|
|
224
|
+
*/
|
|
225
|
+
const backupToFile = await new DB().backupToFile({
|
|
226
|
+
database: 'try-to-backup',
|
|
227
|
+
filePath: 'backup.sql',
|
|
228
|
+
connection ?: {
|
|
229
|
+
host: 'localhost',
|
|
230
|
+
port : 3306,
|
|
231
|
+
database: 'database'
|
|
232
|
+
username: 'username',
|
|
233
|
+
password: 'password',
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
// backupToFile => backup.sql
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Generating Model Classes
|
|
240
|
+
To get started, let's install npm install tspace-mysql -g
|
|
241
|
+
you may use the make:model command to generate a new model:
|
|
242
|
+
|
|
243
|
+
```sh
|
|
244
|
+
npm install tspace-mysql -g
|
|
245
|
+
tspace-mysql make:model <model name> --dir=<directory>
|
|
246
|
+
|
|
247
|
+
# tspace-mysql make:model User --dir=App/Models
|
|
248
|
+
# => App/Models/User.ts
|
|
249
|
+
```
|
|
250
|
+
## Model Conventions
|
|
251
|
+
Models generated by the make:model command will be placed in the specific directory.
|
|
252
|
+
Let's examine a basic model class:
|
|
253
|
+
|
|
254
|
+
```js
|
|
255
|
+
import { Model } from 'tspace-mysql'
|
|
256
|
+
class User extends Model {
|
|
257
|
+
constructor(){
|
|
258
|
+
super()
|
|
259
|
+
this.useTimestamp()
|
|
260
|
+
/*
|
|
261
|
+
* the "snake case", plural name of the class will be used as the table name
|
|
262
|
+
*
|
|
263
|
+
* @param {string} name The table associated with the model.
|
|
264
|
+
*/
|
|
265
|
+
this.useTable('users')
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
export { User }
|
|
269
|
+
export default User
|
|
270
|
+
```
|
|
271
|
+
## Relationships
|
|
272
|
+
Relationships are defined as methods on your Model classes
|
|
273
|
+
Let's examine a basic relations :
|
|
274
|
+
|
|
275
|
+
## One To One
|
|
276
|
+
A one-to-one relationship is used to define relationships where a single model is the parent to one child models
|
|
277
|
+
```js
|
|
278
|
+
import { Model } from 'tspace-mysql'
|
|
279
|
+
import Phone from '../Phone'
|
|
280
|
+
class User extends Model {
|
|
281
|
+
constructor(){
|
|
282
|
+
super()
|
|
283
|
+
this.useTimestamp()
|
|
284
|
+
/**
|
|
285
|
+
*
|
|
286
|
+
* @hasOne Get the phone associated with the user.
|
|
287
|
+
* @relationship users.id -> phones.user_id
|
|
288
|
+
*/
|
|
289
|
+
this.hasOne({ name : 'phone' , model : Phone })
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* @hasOneQuery Get the phone associated with the user. using function callback
|
|
293
|
+
* @function
|
|
294
|
+
*/
|
|
295
|
+
phone (callback ?: Function) {
|
|
296
|
+
return this.hasOneQuery({ model : Phone }, (query) => {
|
|
297
|
+
return callback ? callback(query) : query
|
|
298
|
+
})
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
export default User
|
|
302
|
+
|
|
303
|
+
+--------------------------------------------------------------------------+
|
|
304
|
+
|
|
305
|
+
import User from '../User'
|
|
306
|
+
const user = await new User().relations('brand').findOne()
|
|
307
|
+
// user?.phone => {...}
|
|
308
|
+
const userUsingFunction = await new User().phone().findOne()
|
|
309
|
+
// userUsingFunction?.phone => {...}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## One To Many
|
|
313
|
+
A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models.
|
|
314
|
+
```js
|
|
315
|
+
import { Model } from 'tspace-mysql'
|
|
316
|
+
import Comment from '../Comment'
|
|
317
|
+
class Post extends Model {
|
|
318
|
+
constructor(){
|
|
319
|
+
super()
|
|
320
|
+
this.useTimestamp()
|
|
321
|
+
/**
|
|
322
|
+
*
|
|
323
|
+
* @hasMany Get the comments for the post.
|
|
324
|
+
* @relationship posts.id -> comments.post_id
|
|
325
|
+
*/
|
|
326
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* @hasManyQuery Get the comments for the post. using function callback
|
|
330
|
+
* @function
|
|
331
|
+
*/
|
|
332
|
+
comments (callback ?: Function) {
|
|
333
|
+
return this.hasManyQuery({ model : Comment }, (query) => {
|
|
334
|
+
return callback ? callback(query) : query
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
export default Post
|
|
339
|
+
|
|
340
|
+
+--------------------------------------------------------------------------+
|
|
341
|
+
|
|
342
|
+
import Post from '../Post'
|
|
343
|
+
const posts = await new Post().relations('comments').findOne()
|
|
344
|
+
// posts?.comments => [{...}]
|
|
345
|
+
const postsUsingFunction = await new Post().comments().findOne()
|
|
346
|
+
// postsUsingFunction?.comments => [{...}]
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## One To One & One To Many (Inverse) / Belongs To
|
|
350
|
+
```js
|
|
351
|
+
import { Model } from 'tspace-mysql'
|
|
352
|
+
import User from '../User'
|
|
353
|
+
class Phone extends Model {
|
|
354
|
+
constructor(){
|
|
355
|
+
super()
|
|
356
|
+
this.useTimestamp()
|
|
357
|
+
/**
|
|
358
|
+
*
|
|
359
|
+
* @belongsTo Get the user that owns the phone.
|
|
360
|
+
* @relationship phones.user_id -> users.id
|
|
361
|
+
*/
|
|
362
|
+
this.belognsTo({ name : 'user' , model : User })
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
366
|
+
* @function
|
|
367
|
+
*/
|
|
368
|
+
user (callback ?: Function) {
|
|
369
|
+
return this.belongsToQuery({ model : User }, (query) => {
|
|
370
|
+
return callback ? callback(query) : query
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
export default Phone
|
|
375
|
+
|
|
376
|
+
+--------------------------------------------------------------------------+
|
|
377
|
+
|
|
378
|
+
import Phone from '../Phone'
|
|
379
|
+
const phone = await new Phone().relations('user').findOne()
|
|
380
|
+
// phone?.user => {...}
|
|
381
|
+
const phoneUsingFunction = await new Phone().user().findOne()
|
|
382
|
+
// phoneUsingFunction?.user => {...}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## Many To Many
|
|
386
|
+
Many-to-many relations are slightly more complicated than hasOne and hasMany relationships.
|
|
387
|
+
```js
|
|
388
|
+
import { Model } from 'tspace-mysql'
|
|
389
|
+
import Role from '../Role'
|
|
390
|
+
class User extends Model {
|
|
391
|
+
constructor(){
|
|
392
|
+
super()
|
|
393
|
+
this.useTimestamp()
|
|
394
|
+
/**
|
|
395
|
+
*
|
|
396
|
+
* @belongsToMany Get The roles that belong to the user.
|
|
397
|
+
* @relationship users.id , roles.id => role_user.user_id , role_user.role_id
|
|
398
|
+
*/
|
|
399
|
+
this.belognsToMany({ name : 'roles' , model : Role })
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
403
|
+
* @function
|
|
404
|
+
*/
|
|
405
|
+
roles (callback ?: Function) {
|
|
406
|
+
return this.belongsToManyQuery({ model : Role }, (query) => {
|
|
407
|
+
return callback ? callback(query) : query
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
export default User
|
|
412
|
+
|
|
413
|
+
+--------------------------------------------------------------------------+
|
|
414
|
+
|
|
415
|
+
import User from '../User'
|
|
416
|
+
const user = await new User().relations('roles').findOne()
|
|
417
|
+
// user?.roles => [{...}]
|
|
418
|
+
const userUsingFunction = await new User().roles().findOne()
|
|
419
|
+
// user?.roles => [{...}]
|
|
420
|
+
```
|
|
421
|
+
## Query Builder
|
|
422
|
+
method chaining for queries
|
|
423
|
+
```js
|
|
424
|
+
where(column , operator , value)
|
|
425
|
+
whereSensitive(column , operator , value)
|
|
426
|
+
whereId(id)
|
|
427
|
+
whereUser(userId)
|
|
428
|
+
whereEmail(value)
|
|
429
|
+
orWhere(column , operator , value)
|
|
430
|
+
whereIn(column , [])
|
|
431
|
+
whereNotIn(column , [])
|
|
432
|
+
whereNull(column)
|
|
433
|
+
whereNotNull(column)
|
|
434
|
+
whereBetween (column , [value1 , value2])
|
|
435
|
+
whereSubQuery(colmn , rawSQL)
|
|
436
|
+
select(column1 ,column2 ,...N)
|
|
437
|
+
except(column1 ,column2 ,...N)
|
|
438
|
+
only(column1 ,column2 ,...N)
|
|
439
|
+
hidden(column1 ,column2 ,...N)
|
|
440
|
+
join(primary key , table.foreign key)
|
|
441
|
+
rightJoin (primary key , table.foreign key)
|
|
442
|
+
leftJoin (primary key , table.foreign key)
|
|
443
|
+
limit (limit)
|
|
444
|
+
orderBy (column ,'ASC' || 'DSCE')
|
|
445
|
+
having (condition)
|
|
446
|
+
latest (column)
|
|
447
|
+
oldest (column)
|
|
448
|
+
groupBy (column)
|
|
449
|
+
create(objects)
|
|
450
|
+
createMultiple(array objects)
|
|
451
|
+
update (objects)
|
|
452
|
+
createNotExists(objects)
|
|
453
|
+
updateOrCreate (objects)
|
|
454
|
+
connection(options)
|
|
455
|
+
backup({ database , connection })
|
|
456
|
+
backupToFile({ filePath, database , connection })
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* registry relation in your models
|
|
460
|
+
* @relationship
|
|
461
|
+
*/
|
|
462
|
+
hasOne({ name , model , localKey , foreignKey , freezeTable , as })
|
|
463
|
+
hasMany({ name , model , localKey , foreignKey , freezeTable , as })
|
|
464
|
+
belongsTo({ name , model , localKey , foreignKey , freezeTable , as })
|
|
465
|
+
belongsToMany({ name , model , localKey , foreignKey , freezeTable , as })
|
|
466
|
+
/**
|
|
467
|
+
* @relation using registry in your models
|
|
468
|
+
*/
|
|
469
|
+
relations(name1 , name2,...nameN)
|
|
470
|
+
/**
|
|
471
|
+
* @relation using registry in your models. if exists child data remove this parent data
|
|
472
|
+
*/
|
|
473
|
+
relationsExists(name1 , name2,...nameN)
|
|
474
|
+
/**
|
|
475
|
+
* @relation call a relation in registry, callback query of parent data
|
|
476
|
+
*/
|
|
477
|
+
relationQuery(name,(callback))
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* queries statements
|
|
481
|
+
* @execute data statements
|
|
482
|
+
*/
|
|
483
|
+
findMany()
|
|
484
|
+
findOne()
|
|
485
|
+
find(id)
|
|
486
|
+
delelte()
|
|
487
|
+
exists ()
|
|
488
|
+
onlyTrashed()
|
|
489
|
+
toSQL()
|
|
490
|
+
toString()
|
|
491
|
+
toJSON()
|
|
492
|
+
toArray(column)
|
|
493
|
+
count(column)
|
|
494
|
+
sum(column)
|
|
495
|
+
avg(column)
|
|
496
|
+
max(column)
|
|
497
|
+
min(column)
|
|
498
|
+
pagination({ limit , page })
|
|
499
|
+
save() /*for action statements insert update or delete */
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
## Cli
|
|
503
|
+
To get started, let's install npm install tspace-mysql -g
|
|
504
|
+
you may use a basic cli :
|
|
505
|
+
|
|
506
|
+
## Make Model
|
|
507
|
+
Command will be placed Model in the specific directory
|
|
508
|
+
```sh
|
|
509
|
+
tspace-mysql make:model <MODEL NAME> --m --dir=.... --type=....
|
|
510
|
+
/**
|
|
511
|
+
* @options
|
|
512
|
+
* --m // created scheme table for migrate
|
|
513
|
+
* --dir=directory // created model in directory
|
|
514
|
+
* --type=js // extension js default ts
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
tspace-mysql make:model User --m --dir=app/Models
|
|
518
|
+
// =>
|
|
519
|
+
- node_modules
|
|
520
|
+
- app
|
|
521
|
+
- Models
|
|
522
|
+
User.ts
|
|
523
|
+
*/
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
## Make Migration
|
|
527
|
+
Command will be placed Migration in the specific directory
|
|
528
|
+
```sh
|
|
529
|
+
tspace-mysql make:migration <TABLE NAME>
|
|
530
|
+
/**
|
|
531
|
+
* @options
|
|
532
|
+
* --type=js /* extension js default ts */
|
|
533
|
+
* --dir=directory /* created table in directory */
|
|
534
|
+
*/
|
|
535
|
+
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
536
|
+
// =>
|
|
537
|
+
- node_modules
|
|
538
|
+
- app
|
|
539
|
+
- Models
|
|
540
|
+
- Migrations
|
|
541
|
+
create_users_table.ts
|
|
542
|
+
User.ts
|
|
543
|
+
*/
|
|
544
|
+
```
|
|
545
|
+
## Migrate
|
|
546
|
+
```sh
|
|
547
|
+
tspace-mysql migrate <FOLDER> --type=...
|
|
548
|
+
/**
|
|
549
|
+
* @options
|
|
550
|
+
*--type=js /* extension js default ts */
|
|
551
|
+
*--dir=directory /* find migrate in directory */
|
|
552
|
+
*/
|
|
553
|
+
|
|
554
|
+
tspace-mysql migrate --dir=App/Models/Migrations
|
|
555
|
+
// => migrate all schema table in folder into database
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
## Blueprint
|
|
559
|
+
Schema table created by command make:migration, you may use the:
|
|
560
|
+
```js
|
|
561
|
+
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
562
|
+
(async () => {
|
|
563
|
+
await new Schema().table('users',{
|
|
564
|
+
id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
565
|
+
name : new Blueprint().varchar(120).default('my name'),
|
|
566
|
+
email : new Blueprint().varchar(255).unique(),
|
|
567
|
+
email_verify : new Blueprint().tinyInt(),
|
|
568
|
+
password : new Blueprint().varchar(255),
|
|
569
|
+
created_at : new Blueprint().null().timestamp(),
|
|
570
|
+
updated_at : new Blueprint().null().timestamp()
|
|
571
|
+
})
|
|
572
|
+
/**
|
|
573
|
+
*
|
|
574
|
+
* @Faker fake data 5 raw
|
|
575
|
+
* await new DB().table('users').faker(5)
|
|
576
|
+
*/
|
|
577
|
+
})()
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
*
|
|
581
|
+
* @Types
|
|
582
|
+
*
|
|
583
|
+
*/
|
|
584
|
+
int ()
|
|
585
|
+
tinyInt (number)
|
|
586
|
+
bigInt (number)
|
|
587
|
+
double ()
|
|
588
|
+
float ()
|
|
589
|
+
varchar (number)
|
|
590
|
+
char (number)
|
|
591
|
+
longText()
|
|
592
|
+
mediumText()
|
|
593
|
+
tinyText()
|
|
594
|
+
text()
|
|
595
|
+
enum(...n)
|
|
596
|
+
date()
|
|
597
|
+
dateTime()
|
|
598
|
+
timestamp ()
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
*
|
|
602
|
+
* @Attrbuites
|
|
603
|
+
*
|
|
604
|
+
*/
|
|
605
|
+
unsigned ()
|
|
606
|
+
unique ()
|
|
607
|
+
null ()
|
|
608
|
+
notNull ()
|
|
609
|
+
primary()
|
|
610
|
+
default (string)
|
|
611
|
+
defaultTimestamp ()
|
|
612
|
+
autoIncrement ()
|
|
679
613
|
```
|