tspace-mysql 1.0.7 → 1.0.9
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 +233 -48
- package/dist/cli/models/model.js +1 -1
- package/dist/cli/tables/table.js +1 -1
- package/dist/lib/constants/index.js +1 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +6 -0
- package/dist/lib/tspace/AbstractModel.d.ts +31 -16
- package/dist/lib/tspace/Database.d.ts +20 -0
- package/dist/lib/tspace/Database.js +159 -39
- package/dist/lib/tspace/Model.d.ts +41 -14
- package/dist/lib/tspace/Model.js +191 -85
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com)
|
|
5
5
|
|
|
6
6
|
TspaceMySQL is an ORM that can run in NodeJs and can be used with TypeScript.
|
|
7
|
-
|
|
7
|
+
The goal is to always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -12,30 +12,46 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
14
|
npm install tspace-mysql --save
|
|
15
|
-
|
|
16
15
|
```
|
|
17
16
|
## Basic Usage
|
|
17
|
+
- [Install](#install)
|
|
18
18
|
- [Configuration](#configuration)
|
|
19
19
|
- [Running Queries](#running-queryies)
|
|
20
20
|
- [Database Transactions](#database-transactions)
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
21
|
+
- [Connection](#connection)
|
|
22
|
+
- [Backup](#backup)
|
|
23
23
|
- [Generating Model Classes](#generating-model-classes)
|
|
24
24
|
- [Model Conventions](#model-conventions)
|
|
25
25
|
- [Relationships](#relationships)
|
|
26
|
+
- [One To One](#one-to-one)
|
|
27
|
+
- [One To Many](#one-to-many)
|
|
28
|
+
- [Belongs To](#belongs-to)
|
|
29
|
+
- [Many To Many](#many-to-many)
|
|
30
|
+
- [Deep Relationships](#deep-relationships)
|
|
31
|
+
- [Query Builder](#query-builder)
|
|
32
|
+
- [Cli](#cli)
|
|
33
|
+
- [Make Model](#make-model)
|
|
34
|
+
- [Make Migration](#make-migration)
|
|
35
|
+
- [Migrate](#migrate)
|
|
36
|
+
- [Blueprint](#blueprint)
|
|
37
|
+
- [Types](#types)
|
|
38
|
+
- [Attrbuites](#attrbuites)
|
|
26
39
|
|
|
27
40
|
## Configuration
|
|
28
41
|
Created your environment variables is to use a .env file, you may establish a connection is this:
|
|
42
|
+
|
|
29
43
|
```js
|
|
30
|
-
DB_HOST
|
|
31
|
-
DB_PORT
|
|
44
|
+
DB_HOST = localhost
|
|
45
|
+
DB_PORT = 3306
|
|
32
46
|
DB_USERNAME = root
|
|
33
47
|
DB_PASSWORD = password
|
|
34
48
|
DB_DATABASE = database
|
|
35
49
|
```
|
|
36
50
|
## Running Queries
|
|
37
51
|
Once you have configured your database connection, you may run queries is this :
|
|
52
|
+
|
|
38
53
|
```js
|
|
54
|
+
|
|
39
55
|
+-------------+--------------+----------------------------+
|
|
40
56
|
| table users |
|
|
41
57
|
+-------------+--------------+----------------------------+
|
|
@@ -51,14 +67,18 @@ import { DB } from 'tspace-mysql'
|
|
|
51
67
|
await new DB('users').findOne() // SELECT * FROM users LIMIT 1 => Object
|
|
52
68
|
})()
|
|
53
69
|
```
|
|
70
|
+
|
|
54
71
|
Running A Select Query
|
|
72
|
+
|
|
55
73
|
```js
|
|
56
74
|
const selectQuery = await new DB('users').select('id','username').findOne()
|
|
57
75
|
// selectQuery => { id : 1, username : 'tspace'}
|
|
58
76
|
const selectQueries = await new DB('users').select('id','username').findMany()
|
|
59
77
|
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
60
78
|
```
|
|
79
|
+
|
|
61
80
|
Running A Where Query
|
|
81
|
+
|
|
62
82
|
```js
|
|
63
83
|
const user = await new DB('users').where('id',1).findOne()
|
|
64
84
|
// user => { id : 1 , username : 'tspace', email : 'tspace@gmail.com'}
|
|
@@ -66,13 +86,16 @@ const users = await new DB('users').where('id','!=',1).findMany()
|
|
|
66
86
|
// users => [{ id : 2 , username : 'tspace2' , email : 'tspace2@gmail.com' }]
|
|
67
87
|
```
|
|
68
88
|
Running A Insert Query
|
|
89
|
+
|
|
69
90
|
```js
|
|
70
91
|
const user = await new DB('users').create({
|
|
71
92
|
name : 'tspace3',
|
|
72
93
|
email : 'tspace3@gmail.com'
|
|
73
94
|
}).save()
|
|
74
95
|
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
75
|
-
|
|
96
|
+
|
|
97
|
+
+--------------------------------------------------------------------------+
|
|
98
|
+
|
|
76
99
|
const reposity = new DB('users')
|
|
77
100
|
reposity.name = 'tspace4'
|
|
78
101
|
reposity.email = 'tspace4@gmail.com'
|
|
@@ -81,6 +104,7 @@ const { result } = reposity
|
|
|
81
104
|
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
82
105
|
```
|
|
83
106
|
Running A Update Query
|
|
107
|
+
|
|
84
108
|
```js
|
|
85
109
|
const user = await new DB('users').where('id',1)
|
|
86
110
|
.update({
|
|
@@ -88,7 +112,9 @@ const user = await new DB('users').where('id',1)
|
|
|
88
112
|
email : 'tspace1@gmail.com'
|
|
89
113
|
}).save()
|
|
90
114
|
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
91
|
-
|
|
115
|
+
|
|
116
|
+
+--------------------------------------------------------------------------+
|
|
117
|
+
|
|
92
118
|
const reposity = new DB('users').where('id',1)
|
|
93
119
|
reposity.name = 'tspace1++'
|
|
94
120
|
reposity.email = 'tspace1++@gmail.com'
|
|
@@ -97,12 +123,15 @@ const { result } = reposity
|
|
|
97
123
|
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
98
124
|
```
|
|
99
125
|
Running A Delete Query
|
|
126
|
+
|
|
100
127
|
```js
|
|
101
128
|
const deleted = await new DB('users').where('id',1).delete()
|
|
102
129
|
// deleted => true
|
|
103
130
|
```
|
|
104
131
|
## Database Transactions
|
|
132
|
+
|
|
105
133
|
Within a database transaction, you may use the:
|
|
134
|
+
|
|
106
135
|
```js
|
|
107
136
|
const transaction = await new DB().beginTransaction()
|
|
108
137
|
try {
|
|
@@ -128,6 +157,59 @@ try {
|
|
|
128
157
|
// * rollback data user, userSecond in your database
|
|
129
158
|
}
|
|
130
159
|
```
|
|
160
|
+
|
|
161
|
+
## Connection
|
|
162
|
+
When establishing a connection, you may establish options is this:
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
const users = await new DB('users')
|
|
166
|
+
.connection({
|
|
167
|
+
host : 'localhost..',
|
|
168
|
+
port : 3306,
|
|
169
|
+
database : 'database'
|
|
170
|
+
username : 'username',
|
|
171
|
+
password : 'password',
|
|
172
|
+
})
|
|
173
|
+
.findMany()
|
|
174
|
+
// users => [{ .... }]
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Backup
|
|
178
|
+
Backup database, you may backup is this:
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
/**
|
|
182
|
+
* @param {string} database created new database
|
|
183
|
+
* @param {object?} connection [connection=defalut current connection]
|
|
184
|
+
*/
|
|
185
|
+
const backup = await new DB().backup({
|
|
186
|
+
database: 'try-to-backup',
|
|
187
|
+
connection : {
|
|
188
|
+
host : 'localhost..',
|
|
189
|
+
port : 3306,
|
|
190
|
+
database : 'database'
|
|
191
|
+
username : 'username',
|
|
192
|
+
password : 'password',
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
/**
|
|
196
|
+
* @param {string} database new database
|
|
197
|
+
* @param {object?} connection [connection=defalut current connection]
|
|
198
|
+
*/
|
|
199
|
+
const backupToFile = await new DB().backupToFile({
|
|
200
|
+
database: 'try-to-backup',
|
|
201
|
+
filePath: 'backup.sql',
|
|
202
|
+
connection : {
|
|
203
|
+
host : 'localhost..',
|
|
204
|
+
port : 3306,
|
|
205
|
+
database : 'database'
|
|
206
|
+
username : 'username',
|
|
207
|
+
password : 'password',
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
// backupToFile => backup.sql
|
|
211
|
+
```
|
|
212
|
+
|
|
131
213
|
## Generating Model Classes
|
|
132
214
|
To get started, let's install npm install tspace-mysql -g
|
|
133
215
|
you may use the make:model command to generate a new model:
|
|
@@ -148,20 +230,31 @@ import { Model } from 'tspace-mysql'
|
|
|
148
230
|
class User extends Model {
|
|
149
231
|
constructor(){
|
|
150
232
|
super()
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* the "snake case", plural name of the class will be used as the table name
|
|
233
|
+
/**
|
|
234
|
+
* The "snake case", plural name of the class will be used as the table name // => users
|
|
154
235
|
*
|
|
155
|
-
*
|
|
156
|
-
|
|
157
|
-
|
|
236
|
+
* Assign setting global in your model
|
|
237
|
+
* @useMethod
|
|
238
|
+
*
|
|
239
|
+
* this.useDebug() // => runing a uuid (universally unique identifier) when insert new data
|
|
240
|
+
* this.usePrimaryKey('id') // => runing a uuid (universally unique identifier) when insert new data
|
|
241
|
+
* this.useTimestamp({ createdAt : 'created_at' , updatedAt : 'updated_at' }) // runing a timestamp when insert or update
|
|
242
|
+
* this.useSoftDelete()
|
|
243
|
+
* this.useDisableSoftDeleteInRelations()
|
|
244
|
+
* this.useTable('users')
|
|
245
|
+
* this.useTableSingular()
|
|
246
|
+
* this.useTablePlural()
|
|
247
|
+
* this.usePattern('snake_case')
|
|
248
|
+
* this.useUUID('uuid')
|
|
249
|
+
* this.useRegistry()
|
|
250
|
+
*/
|
|
158
251
|
}
|
|
159
252
|
}
|
|
160
253
|
export { User }
|
|
161
254
|
export default User
|
|
162
255
|
```
|
|
163
256
|
## Relationships
|
|
164
|
-
Relationships are defined as methods on your Model classes
|
|
257
|
+
Relationships are defined as methods on your Model classes.
|
|
165
258
|
Let's examine a basic relations :
|
|
166
259
|
|
|
167
260
|
## One To One
|
|
@@ -191,7 +284,9 @@ class User extends Model {
|
|
|
191
284
|
}
|
|
192
285
|
}
|
|
193
286
|
export default User
|
|
194
|
-
|
|
287
|
+
|
|
288
|
+
+--------------------------------------------------------------------------+
|
|
289
|
+
|
|
195
290
|
import User from '../User'
|
|
196
291
|
const user = await new User().with('brand').findOne()
|
|
197
292
|
// user?.phone => {...}
|
|
@@ -201,6 +296,7 @@ const userUsingFunction = await new User().phone().findOne()
|
|
|
201
296
|
|
|
202
297
|
## One To Many
|
|
203
298
|
A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models.
|
|
299
|
+
|
|
204
300
|
```js
|
|
205
301
|
import { Model } from 'tspace-mysql'
|
|
206
302
|
import Comment from '../Comment'
|
|
@@ -226,7 +322,9 @@ class Post extends Model {
|
|
|
226
322
|
}
|
|
227
323
|
}
|
|
228
324
|
export default Post
|
|
229
|
-
|
|
325
|
+
|
|
326
|
+
+--------------------------------------------------------------------------+
|
|
327
|
+
|
|
230
328
|
import Post from '../Post'
|
|
231
329
|
const posts = await new Post().with('comments').findOne()
|
|
232
330
|
// posts?.comments => [{...}]
|
|
@@ -234,7 +332,7 @@ const postsUsingFunction = await new Post().comments().findOne()
|
|
|
234
332
|
// postsUsingFunction?.comments => [{...}]
|
|
235
333
|
```
|
|
236
334
|
|
|
237
|
-
##
|
|
335
|
+
## Belongs To
|
|
238
336
|
```js
|
|
239
337
|
import { Model } from 'tspace-mysql'
|
|
240
338
|
import User from '../User'
|
|
@@ -260,7 +358,9 @@ class Phone extends Model {
|
|
|
260
358
|
}
|
|
261
359
|
}
|
|
262
360
|
export default Phone
|
|
263
|
-
|
|
361
|
+
|
|
362
|
+
+--------------------------------------------------------------------------+
|
|
363
|
+
|
|
264
364
|
import Phone from '../Phone'
|
|
265
365
|
const phone = await new Phone().with('user').findOne()
|
|
266
366
|
// phone?.user => {...}
|
|
@@ -269,7 +369,8 @@ const phoneUsingFunction = await new Phone().user().findOne()
|
|
|
269
369
|
```
|
|
270
370
|
|
|
271
371
|
## Many To Many
|
|
272
|
-
Many-to-many
|
|
372
|
+
Many-to-many relationships are slightly more complicated than hasOne and hasMany relationships.
|
|
373
|
+
|
|
273
374
|
```js
|
|
274
375
|
import { Model } from 'tspace-mysql'
|
|
275
376
|
import Role from '../Role'
|
|
@@ -295,15 +396,91 @@ class User extends Model {
|
|
|
295
396
|
}
|
|
296
397
|
}
|
|
297
398
|
export default User
|
|
298
|
-
|
|
399
|
+
|
|
400
|
+
+--------------------------------------------------------------------------+
|
|
401
|
+
|
|
299
402
|
import User from '../User'
|
|
300
403
|
const user = await new User().with('roles').findOne()
|
|
301
404
|
// user?.roles => [{...}]
|
|
302
405
|
const userUsingFunction = await new User().roles().findOne()
|
|
303
406
|
// user?.roles => [{...}]
|
|
304
407
|
```
|
|
305
|
-
|
|
306
|
-
|
|
408
|
+
|
|
409
|
+
## Deep Relationships
|
|
410
|
+
Deep Relationships using methods on your child Model classes.
|
|
411
|
+
Let's examine a basic relations :
|
|
412
|
+
|
|
413
|
+
```js
|
|
414
|
+
import { Model } from 'tspace-mysql'
|
|
415
|
+
import Post from '../Post'
|
|
416
|
+
class User extends Model {
|
|
417
|
+
constructor(){
|
|
418
|
+
super()
|
|
419
|
+
this.hasMany({ name : 'posts' , model : Post })
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
export default User
|
|
423
|
+
|
|
424
|
+
+--------------------------------------------------------------------------+
|
|
425
|
+
|
|
426
|
+
import { Model } from 'tspace-mysql'
|
|
427
|
+
import User from '../User'
|
|
428
|
+
class Comment extends Model {
|
|
429
|
+
constructor(){
|
|
430
|
+
super()
|
|
431
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
export default Comment
|
|
435
|
+
|
|
436
|
+
+--------------------------------------------------------------------------+
|
|
437
|
+
|
|
438
|
+
import { Model } from 'tspace-mysql'
|
|
439
|
+
import Comment from '../Comment'
|
|
440
|
+
class Post extends Model {
|
|
441
|
+
constructor(){
|
|
442
|
+
super()
|
|
443
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
export default Post
|
|
447
|
+
|
|
448
|
+
+--------------------------------------------------------------------------+
|
|
449
|
+
|
|
450
|
+
import Post from '../Post'
|
|
451
|
+
const post = await new Post()
|
|
452
|
+
.with('comments','user') // => has many comments in posts , => write by user ...
|
|
453
|
+
.withQuery('comments',(query) => {
|
|
454
|
+
/**
|
|
455
|
+
*
|
|
456
|
+
* @callback query registry in Comment classes
|
|
457
|
+
*/
|
|
458
|
+
return query
|
|
459
|
+
.with('user') // relation this comment by user?
|
|
460
|
+
.withQuery('user' , (query) => {
|
|
461
|
+
return query
|
|
462
|
+
.with('posts') // relation this user has many posts ...
|
|
463
|
+
})
|
|
464
|
+
})
|
|
465
|
+
.findOne()
|
|
466
|
+
/* post => {
|
|
467
|
+
* ....post details,
|
|
468
|
+
* comments : [
|
|
469
|
+
* {
|
|
470
|
+
* ...comment details,
|
|
471
|
+
* user : {
|
|
472
|
+
* ...user detils
|
|
473
|
+
* posts : [{ ...post details }]
|
|
474
|
+
* }
|
|
475
|
+
* }
|
|
476
|
+
* ],
|
|
477
|
+
* user : { ...user detils }
|
|
478
|
+
* }
|
|
479
|
+
*/
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## Query Builder
|
|
483
|
+
Method chaining for queries
|
|
307
484
|
```js
|
|
308
485
|
where(column , operator , value)
|
|
309
486
|
whereSensitive(column , operator , value)
|
|
@@ -317,7 +494,6 @@ whereNull(column)
|
|
|
317
494
|
whereNotNull(column)
|
|
318
495
|
whereBetween (column , [value1 , value2])
|
|
319
496
|
whereSubQuery(colmn , rawSQL)
|
|
320
|
-
|
|
321
497
|
select(column1 ,column2 ,...N)
|
|
322
498
|
except(column1 ,column2 ,...N)
|
|
323
499
|
only(column1 ,column2 ,...N)
|
|
@@ -331,23 +507,31 @@ having (condition)
|
|
|
331
507
|
latest (column)
|
|
332
508
|
oldest (column)
|
|
333
509
|
groupBy (column)
|
|
334
|
-
|
|
335
510
|
create(objects)
|
|
336
511
|
createMultiple(array objects)
|
|
337
512
|
update (objects)
|
|
338
513
|
createNotExists(objects)
|
|
339
514
|
updateOrCreate (objects)
|
|
515
|
+
connection(options)
|
|
516
|
+
backup({ database , connection })
|
|
517
|
+
backupToFile({ filePath, database , connection })
|
|
340
518
|
|
|
341
519
|
/**
|
|
342
520
|
* registry relation in your models
|
|
343
521
|
* @relationship
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
522
|
+
* @param {string} relation.name
|
|
523
|
+
* @param {class} relation.model
|
|
524
|
+
* @param {string?} relation.as
|
|
525
|
+
* @param {string?} relation.localKey
|
|
526
|
+
* @param {string?} relation.foreignKey
|
|
527
|
+
* @param {string?} relation.freezeTable
|
|
528
|
+
*/
|
|
529
|
+
hasOne({ name , model , localKey , foreignKey , freezeTable , as })
|
|
530
|
+
hasMany({ name , model , localKey , foreignKey , freezeTable , as })
|
|
531
|
+
belongsTo({ name , model , localKey , foreignKey , freezeTable , as })
|
|
532
|
+
belongsToMany({ name , model , localKey , foreignKey , freezeTable , as })
|
|
349
533
|
/**
|
|
350
|
-
* @relation using registry in your models
|
|
534
|
+
* @relation using registry lists in your models
|
|
351
535
|
*/
|
|
352
536
|
with(name1 , name2,...nameN)
|
|
353
537
|
/**
|
|
@@ -355,7 +539,7 @@ with(name1 , name2,...nameN)
|
|
|
355
539
|
*/
|
|
356
540
|
withExists(name1 , name2,...nameN)
|
|
357
541
|
/**
|
|
358
|
-
* @relation call
|
|
542
|
+
* @relation call name relation in registry, callback query this relation
|
|
359
543
|
*/
|
|
360
544
|
withQuery('relation registry',(callback query))
|
|
361
545
|
|
|
@@ -365,6 +549,7 @@ withQuery('relation registry',(callback query))
|
|
|
365
549
|
*/
|
|
366
550
|
findMany()
|
|
367
551
|
findOne()
|
|
552
|
+
findOneOrError(message , options?)
|
|
368
553
|
find(id)
|
|
369
554
|
delelte()
|
|
370
555
|
exists ()
|
|
@@ -438,8 +623,8 @@ tspace-mysql migrate --dir=App/Models/Migrations
|
|
|
438
623
|
// => migrate all schema table in folder into database
|
|
439
624
|
```
|
|
440
625
|
|
|
441
|
-
## Blueprint
|
|
442
|
-
Schema table created by command make:migration
|
|
626
|
+
## Blueprint
|
|
627
|
+
Schema table created by command make:migration. you may use the:
|
|
443
628
|
```js
|
|
444
629
|
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
445
630
|
(async () => {
|
|
@@ -458,11 +643,11 @@ import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
|
458
643
|
* await new DB().table('users').faker(5)
|
|
459
644
|
*/
|
|
460
645
|
})()
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
## Types
|
|
649
|
+
|
|
650
|
+
```js
|
|
466
651
|
int ()
|
|
467
652
|
tinyInt (number)
|
|
468
653
|
bigInt (number)
|
|
@@ -477,18 +662,18 @@ text()
|
|
|
477
662
|
enum(...n)
|
|
478
663
|
date()
|
|
479
664
|
dateTime()
|
|
480
|
-
timestamp
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
665
|
+
timestamp()
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
## Attrbuites
|
|
669
|
+
|
|
670
|
+
```js
|
|
486
671
|
unsigned ()
|
|
487
672
|
unique ()
|
|
488
673
|
null ()
|
|
489
674
|
notNull ()
|
|
490
675
|
primary()
|
|
491
|
-
default
|
|
492
|
-
defaultTimestamp
|
|
493
|
-
autoIncrement
|
|
676
|
+
default(string)
|
|
677
|
+
defaultTimestamp()
|
|
678
|
+
autoIncrement()
|
|
494
679
|
```
|
package/dist/cli/models/model.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var Model = function (model, npm) {
|
|
4
|
-
return "import { Model } from '".concat(npm, "'\nclass ").concat(model, " extends Model {\n constructor(){\n super()\n this.
|
|
4
|
+
return "import { Model } from '".concat(npm, "'\nclass ").concat(model, " extends Model {\n constructor(){\n super()\n /**\n * \n * Assign setting global in your model\n * @useMethod\n *\n * this.useDebug() // => runing a uuid (universally unique identifier) when insert new data\n * this.usePrimaryKey('id') // => runing a uuid (universally unique identifier) when insert new data\n * this.useTimestamp({ createdAt : 'created_at' , updatedAt : 'updated_at' }) // runing a timestamp when insert or update\n * this.useSoftDelete()\n * this.useDisableSoftDeleteInRelations()\n * this.useTable('Users')\n * this.useTableSingular()\n * this.useTablePlural()\n * this.usePattern('snake_case') \n * this.useUUID('uuid')\n * this.useRegistry()\n */\n }\n}\nexport { ").concat(model, " }\nexport default ").concat(model, "\n");
|
|
5
5
|
};
|
|
6
6
|
exports.default = Model;
|
package/dist/cli/tables/table.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var Table = function (table, npm) {
|
|
4
|
-
return "import { Schema , Blueprint , DB } from '".concat(npm, "'\n(async () => {\n await new Schema().table('").concat(table, "',{ \n id : new Blueprint().int().notNull().primary().autoIncrement(),\n
|
|
4
|
+
return "import { Schema , Blueprint , DB } from '".concat(npm, "'\n(async () => {\n await new Schema().table('").concat(table, "',{ \n id : new Blueprint().int().notNull().primary().autoIncrement(),\n uuid : new Blueprint().varchar(50),\n name : new Blueprint().varchar(120).default('my name'),\n email : new Blueprint().varchar(120).unique(),\n email_verify : new Blueprint().tinyInt(),\n password : new Blueprint().varchar(120),\n birthdate : new Blueprint().date(),\n created_at : new Blueprint().null().timestamp(),\n updated_at : new Blueprint().null().timestamp()\n })\n\n /**\n * \n * @Faker data\n * await new DB().table('").concat(table, "').faker(5)\n */\n})()\n");
|
|
5
5
|
};
|
|
6
6
|
exports.default = Table;
|
|
@@ -82,6 +82,12 @@ declare abstract class AbstractDatabase {
|
|
|
82
82
|
page: number;
|
|
83
83
|
}): Promise<Pagination>;
|
|
84
84
|
abstract first(): Promise<any>;
|
|
85
|
+
abstract firstOrError(message: string, options?: {
|
|
86
|
+
[key: string]: any;
|
|
87
|
+
}): Promise<any>;
|
|
88
|
+
abstract findOneOrError(message: string, options?: {
|
|
89
|
+
[key: string]: any;
|
|
90
|
+
}): Promise<any>;
|
|
85
91
|
abstract get(): Promise<any>;
|
|
86
92
|
abstract findOne(): Promise<any>;
|
|
87
93
|
abstract findMany(): Promise<any>;
|
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
import { Relation } from './Interface';
|
|
1
|
+
import { Relation, RelationQuery } from './Interface';
|
|
2
2
|
import Database from './Database';
|
|
3
3
|
declare abstract class AbstractModel extends Database {
|
|
4
|
-
abstract
|
|
5
|
-
abstract
|
|
6
|
-
abstract
|
|
7
|
-
abstract
|
|
8
|
-
abstract
|
|
9
|
-
abstract
|
|
10
|
-
abstract
|
|
11
|
-
abstract
|
|
12
|
-
abstract
|
|
13
|
-
abstract
|
|
14
|
-
abstract
|
|
15
|
-
abstract
|
|
16
|
-
abstract
|
|
17
|
-
abstract
|
|
18
|
-
abstract
|
|
4
|
+
abstract useUUID(): this;
|
|
5
|
+
abstract usePrimaryKey(primaryKey: string): this;
|
|
6
|
+
abstract useDisableSoftDeleteInRelations(): this;
|
|
7
|
+
abstract useRegistry(): this;
|
|
8
|
+
abstract useDebug(): this;
|
|
9
|
+
abstract useTable(table: string): this;
|
|
10
|
+
abstract useTablePlural(): this;
|
|
11
|
+
abstract useTableSingular(): this;
|
|
12
|
+
abstract useTimestamp(): this;
|
|
13
|
+
abstract useSoftDelete(): this;
|
|
14
|
+
abstract usePattern(pattern: string): this;
|
|
15
|
+
abstract onlyTrashed(): Promise<any>;
|
|
16
|
+
abstract trashed(): Promise<any>;
|
|
17
|
+
abstract restore(): Promise<any>;
|
|
18
|
+
abstract ignoreSoftDelete(): this;
|
|
19
|
+
abstract disableSoftDelete(): this;
|
|
20
|
+
abstract registry(func: {
|
|
21
|
+
[key: string]: Function;
|
|
22
|
+
}): this;
|
|
23
|
+
abstract with(...nameRelations: string[]): this;
|
|
24
|
+
abstract withQuery(nameRelations: string, callback: Function): this;
|
|
25
|
+
abstract withExists(...nameRelations: string[]): this;
|
|
26
|
+
abstract hasOne({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
27
|
+
abstract hasMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
28
|
+
abstract belongsTo({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
29
|
+
abstract belongsToMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
|
|
30
|
+
abstract hasOneQuery({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
31
|
+
abstract hasManyQuery({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
32
|
+
abstract belongsToQuery({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
33
|
+
abstract belongsToManyQuery({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
|
|
19
34
|
}
|
|
20
35
|
export { AbstractModel };
|
|
21
36
|
export default AbstractModel;
|
|
@@ -446,6 +446,26 @@ declare class Database extends AbstractDatabase {
|
|
|
446
446
|
findOne(): Promise<{
|
|
447
447
|
[key: string]: any;
|
|
448
448
|
} | null>;
|
|
449
|
+
/**
|
|
450
|
+
*
|
|
451
|
+
* execute data return object | null
|
|
452
|
+
* @return {promise<object | null>}
|
|
453
|
+
*/
|
|
454
|
+
firstOrError(message: string, options?: {
|
|
455
|
+
[key: string]: any;
|
|
456
|
+
}): Promise<{
|
|
457
|
+
[key: string]: any;
|
|
458
|
+
}>;
|
|
459
|
+
/**
|
|
460
|
+
*
|
|
461
|
+
* execute data return object | null
|
|
462
|
+
* @return {promise<object | null>}
|
|
463
|
+
*/
|
|
464
|
+
findOneOrError(message: string, options?: {
|
|
465
|
+
[key: string]: any;
|
|
466
|
+
}): Promise<{
|
|
467
|
+
[key: string]: any;
|
|
468
|
+
}>;
|
|
449
469
|
/**
|
|
450
470
|
*
|
|
451
471
|
* execute data return Array
|