tspace-mysql 1.1.7 → 1.1.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 +200 -45
- package/dist/cli/generate/make.d.ts +4 -0
- package/dist/cli/generate/make.js +46 -0
- package/dist/cli/index.js +20 -13
- package/dist/cli/migrate/make.js +5 -4
- package/dist/cli/models/make.d.ts +1 -1
- package/dist/cli/models/make.js +2 -2
- package/dist/cli/models/model.js +3 -4
- package/dist/cli/query/index.d.ts +4 -0
- package/dist/cli/query/index.js +7 -0
- package/dist/lib/connection/index.d.ts +17 -35
- package/dist/lib/connection/index.js +53 -60
- package/dist/lib/connection/options.d.ts +4 -0
- package/dist/lib/connection/options.js +42 -0
- package/dist/lib/constants/index.js +14 -11
- package/dist/lib/tspace/AbstractDB.d.ts +9 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +62 -57
- package/dist/lib/tspace/AbstractDatabase.js +29 -26
- package/dist/lib/tspace/AbstractModel.d.ts +26 -21
- package/dist/lib/tspace/Blueprint.js +4 -2
- package/dist/lib/tspace/DB.d.ts +32 -7
- package/dist/lib/tspace/DB.js +109 -42
- package/dist/lib/tspace/Database.d.ts +32 -15
- package/dist/lib/tspace/Database.js +1097 -922
- package/dist/lib/tspace/Interface.d.ts +26 -0
- package/dist/lib/tspace/Logger.js +5 -4
- package/dist/lib/tspace/Model.d.ts +121 -70
- package/dist/lib/tspace/Model.js +1314 -1062
- package/dist/lib/tspace/ProxyHandler.d.ts +9 -1
- package/dist/lib/tspace/ProxyHandler.js +8 -9
- package/dist/lib/tspace/Schema.js +35 -22
- package/dist/lib/utils/index.d.ts +1 -0
- package/dist/lib/utils/index.js +45 -30
- package/package.json +7 -5
- package/dist/lib/config/env.d.ts +0 -13
- package/dist/lib/config/env.js +0 -33
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ npm install tspace-mysql --save
|
|
|
15
15
|
```
|
|
16
16
|
## Basic Usage
|
|
17
17
|
- [Configuration](#configuration)
|
|
18
|
-
- [Running Queries](#running-
|
|
18
|
+
- [Running Queries](#running-queries)
|
|
19
19
|
- [Database Transactions](#database-transactions)
|
|
20
20
|
- [Connection](#connection)
|
|
21
21
|
- [Backup](#backup)
|
|
@@ -26,6 +26,7 @@ npm install tspace-mysql --save
|
|
|
26
26
|
- [One To Many](#one-to-many)
|
|
27
27
|
- [Belongs To](#belongs-to)
|
|
28
28
|
- [Many To Many](#many-to-many)
|
|
29
|
+
- [Relation in Relation](#relation-in-relation)
|
|
29
30
|
- [Query Builder](#query-builder)
|
|
30
31
|
- [Cli](#cli)
|
|
31
32
|
- [Make Model](#make-model)
|
|
@@ -36,11 +37,20 @@ npm install tspace-mysql --save
|
|
|
36
37
|
## Configuration
|
|
37
38
|
Created your environment variables is to use a .env file, you may establish a connection is this:
|
|
38
39
|
```js
|
|
39
|
-
DB_HOST
|
|
40
|
-
DB_PORT
|
|
40
|
+
DB_HOST = localhost
|
|
41
|
+
DB_PORT = 3306
|
|
41
42
|
DB_USERNAME = root
|
|
42
43
|
DB_PASSWORD = password
|
|
43
44
|
DB_DATABASE = database
|
|
45
|
+
|
|
46
|
+
/** default
|
|
47
|
+
DB_CONNECTION_LIMIT = 30
|
|
48
|
+
DB_CONNECTION_ERROR = true
|
|
49
|
+
DB_QUEUE_LIMIT = 25
|
|
50
|
+
DB_TIMEOUT = 30000
|
|
51
|
+
DB_DATE_STRINGS = true
|
|
52
|
+
*/
|
|
53
|
+
|
|
44
54
|
```
|
|
45
55
|
## Running Queries
|
|
46
56
|
Once you have configured your database connection, you may run queries is this :
|
|
@@ -54,6 +64,16 @@ Once you have configured your database connection, you may run queries is this :
|
|
|
54
64
|
| 2 | tspace2 | tspace2@gmail.com |
|
|
55
65
|
+-------------+--------------+----------------------------+
|
|
56
66
|
|
|
67
|
+
|
|
68
|
+
+-------------+--------------+----------------------------+
|
|
69
|
+
| table posts |
|
|
70
|
+
+-------------+--------------+----------------------------+
|
|
71
|
+
| id | user_id | title |
|
|
72
|
+
|-------------|--------------|----------------------------|
|
|
73
|
+
| 1 | 1 | posts tspace |
|
|
74
|
+
| 2 | 2 | posts tspace2 |
|
|
75
|
+
+-------------+--------------+----------------------------+
|
|
76
|
+
|
|
57
77
|
import { DB } from 'tspace-mysql'
|
|
58
78
|
(async () => {
|
|
59
79
|
await new DB('users').findMany() // SELECT * FROM users => Array
|
|
@@ -66,14 +86,51 @@ const selectQuery = await new DB('users').select('id','username').findOne()
|
|
|
66
86
|
// selectQuery => { id : 1, username : 'tspace'}
|
|
67
87
|
const selectQueries = await new DB('users').select('id','username').findMany()
|
|
68
88
|
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @example except
|
|
92
|
+
*/
|
|
93
|
+
await new DB('users').except('id','username').findOne()
|
|
69
94
|
```
|
|
95
|
+
|
|
96
|
+
Running A OrderBy & GroupBy Query
|
|
97
|
+
```js
|
|
98
|
+
await new DB('users').orderBy('id','asc').findOne()
|
|
99
|
+
await new DB('users').orderBy('id','desc').findOne()
|
|
100
|
+
await new DB('users').oldest('id').findOne()
|
|
101
|
+
await new DB('users').latest('id').findOne()
|
|
102
|
+
|
|
103
|
+
await new DB('users').groupBy('id').findOne()
|
|
104
|
+
await new DB('users').groupBy('id','usernamename').findOne()
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Running A Join Query
|
|
108
|
+
```js
|
|
109
|
+
await new DB('posts').join('posts.user_id' , 'users.id').findOne()
|
|
110
|
+
await new DB('posts').leftJoin('posts.user_id' , 'users.id').findOne()
|
|
111
|
+
await new DB('posts').rightJoin('posts.user_id' , 'users.id').findOne()
|
|
112
|
+
```
|
|
113
|
+
|
|
70
114
|
Running A Where Query
|
|
71
115
|
```js
|
|
72
116
|
const user = await new DB('users').where('id',1).findOne()
|
|
73
117
|
// user => { id : 1 , username : 'tspace', email : 'tspace@gmail.com'}
|
|
74
118
|
const users = await new DB('users').where('id','!=',1).findMany()
|
|
75
119
|
// users => [{ id : 2 , username : 'tspace2' , email : 'tspace2@gmail.com' }]
|
|
120
|
+
|
|
121
|
+
const whereIn = await new DB('users').whereIn('id',[1,2]).findMany()
|
|
122
|
+
const whereBetween = await new DB('users').whereBetween('id',[1,2]).findMany()
|
|
123
|
+
const whereSubQuery = await new DB('users').whereSubQuery('id','select id from users').findMany()
|
|
124
|
+
// await new DB('users').whereSubQuery('id',new DB('users').select('id').toString()).findMany()
|
|
125
|
+
const whereNull = await new DB('users').whereNull('username').findOne()
|
|
76
126
|
```
|
|
127
|
+
|
|
128
|
+
Running A Hook Query
|
|
129
|
+
```js
|
|
130
|
+
const hookResult = (result) => console.log(result)
|
|
131
|
+
const user = await new DB('users').where('id',1).hook(hookResult).findOne()
|
|
132
|
+
```
|
|
133
|
+
|
|
77
134
|
Running A Insert Query
|
|
78
135
|
```js
|
|
79
136
|
const user = await new DB('users')
|
|
@@ -91,6 +148,33 @@ const reposity = new DB('users')
|
|
|
91
148
|
await reposity.save()
|
|
92
149
|
const { result } = reposity
|
|
93
150
|
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
151
|
+
|
|
152
|
+
const users = await new DB('users')
|
|
153
|
+
.createMultiple([
|
|
154
|
+
{
|
|
155
|
+
name :'tspace4',
|
|
156
|
+
email : 'tspace4@gmail.com'
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
name :'tspace5',
|
|
160
|
+
email : 'tspace5@gmail.com'
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name :'tspace6',
|
|
164
|
+
email : 'tspace6@gmail.com'
|
|
165
|
+
},
|
|
166
|
+
]).save()
|
|
167
|
+
|
|
168
|
+
const users = await new DB('users')
|
|
169
|
+
.where('name','tspace4')
|
|
170
|
+
.where('email','tspace4@gmail.com')
|
|
171
|
+
.createNotExists({
|
|
172
|
+
name :'tspace4',
|
|
173
|
+
email : 'tspace4@gmail.com'
|
|
174
|
+
})
|
|
175
|
+
.save()
|
|
176
|
+
// if has exists return null, if not exists created new data
|
|
177
|
+
|
|
94
178
|
```
|
|
95
179
|
Running A Update Query
|
|
96
180
|
```js
|
|
@@ -110,7 +194,19 @@ const reposity = new DB('users').where('id',1)
|
|
|
110
194
|
await reposity.save()
|
|
111
195
|
const { result } = reposity
|
|
112
196
|
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
113
|
-
```
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Running A Update Or Created Query
|
|
200
|
+
```js
|
|
201
|
+
const user = await new DB('users')
|
|
202
|
+
.where('id',1)
|
|
203
|
+
.updateOrCreate({
|
|
204
|
+
name : 'tspace1**',
|
|
205
|
+
email : 'tspace1@gmail.com'
|
|
206
|
+
}).save()
|
|
207
|
+
// user => { username : 'tspace1**', email : 'tspace1@gmail.com' }
|
|
208
|
+
```
|
|
209
|
+
|
|
114
210
|
Running A Delete Query
|
|
115
211
|
```js
|
|
116
212
|
const deleted = await new DB('users').where('id',1).delete()
|
|
@@ -118,7 +214,7 @@ const deleted = await new DB('users').where('id',1).delete()
|
|
|
118
214
|
```
|
|
119
215
|
## Database Transactions
|
|
120
216
|
|
|
121
|
-
Within a
|
|
217
|
+
Within a Database Transaction, you may use the:
|
|
122
218
|
|
|
123
219
|
```js
|
|
124
220
|
const connection = await new DB().beginTransaction()
|
|
@@ -157,16 +253,9 @@ try {
|
|
|
157
253
|
title : `tspace-post3`
|
|
158
254
|
}
|
|
159
255
|
])
|
|
160
|
-
|
|
161
|
-
*
|
|
162
|
-
* bind method for make sure this connection has same transaction in connection
|
|
163
|
-
* @params {Function} connection
|
|
164
|
-
*/
|
|
165
|
-
.bind(connection)
|
|
256
|
+
.bind(connection) // don't forget this
|
|
166
257
|
.save()
|
|
167
258
|
|
|
168
|
-
// throw new Error('try error')
|
|
169
|
-
|
|
170
259
|
/**
|
|
171
260
|
*
|
|
172
261
|
* @commit commit transaction to database
|
|
@@ -187,14 +276,16 @@ try {
|
|
|
187
276
|
## Connection
|
|
188
277
|
When establishing a connection, you may establish options is this:
|
|
189
278
|
```js
|
|
279
|
+
const connection = await new DB().getConnection({
|
|
280
|
+
host: 'localhost',
|
|
281
|
+
port : 3306,
|
|
282
|
+
database: 'database'
|
|
283
|
+
username: 'username',
|
|
284
|
+
password: 'password',
|
|
285
|
+
})
|
|
286
|
+
|
|
190
287
|
const users = await new DB('users')
|
|
191
|
-
.connection
|
|
192
|
-
host: 'localhost',
|
|
193
|
-
port : 3306,
|
|
194
|
-
database: 'database'
|
|
195
|
-
username: 'username',
|
|
196
|
-
password: 'password',
|
|
197
|
-
})
|
|
288
|
+
.bind(connection)
|
|
198
289
|
.findMany()
|
|
199
290
|
// users => [{ .... }]
|
|
200
291
|
```
|
|
@@ -265,7 +356,17 @@ import { Model } from 'tspace-mysql'
|
|
|
265
356
|
class User extends Model {
|
|
266
357
|
constructor(){
|
|
267
358
|
super()
|
|
268
|
-
this.useTimestamp()
|
|
359
|
+
this.useTimestamp() /** created_at , updated_at
|
|
360
|
+
/**
|
|
361
|
+
*
|
|
362
|
+
* @Custom
|
|
363
|
+
*
|
|
364
|
+
* this.useTimestamp({
|
|
365
|
+
* createdAt : 'created_at',
|
|
366
|
+
* updatedAt : 'updated_at'
|
|
367
|
+
* })
|
|
368
|
+
*/
|
|
369
|
+
|
|
269
370
|
/*
|
|
270
371
|
* the "snake case", plural name of the class will be used as the table name
|
|
271
372
|
*
|
|
@@ -289,7 +390,7 @@ import Phone from '../Phone'
|
|
|
289
390
|
class User extends Model {
|
|
290
391
|
constructor(){
|
|
291
392
|
super()
|
|
292
|
-
this.useTimestamp()
|
|
393
|
+
this.useTimestamp()
|
|
293
394
|
/**
|
|
294
395
|
*
|
|
295
396
|
* @hasOne Get the phone associated with the user.
|
|
@@ -298,13 +399,12 @@ class User extends Model {
|
|
|
298
399
|
this.hasOne({ name : 'phone' , model : Phone })
|
|
299
400
|
}
|
|
300
401
|
/**
|
|
301
|
-
*
|
|
402
|
+
* Mark a method for relationship
|
|
403
|
+
* @hasOne Get the phone associated with the user. using function callback
|
|
302
404
|
* @function
|
|
303
405
|
*/
|
|
304
406
|
phone (callback ?: Function) {
|
|
305
|
-
return
|
|
306
|
-
return callback ? callback(query) : query
|
|
307
|
-
})
|
|
407
|
+
return this.hasOneBuilder({ name : 'phone' , model : Phone } , callback)
|
|
308
408
|
}
|
|
309
409
|
}
|
|
310
410
|
export default User
|
|
@@ -335,13 +435,12 @@ class Post extends Model {
|
|
|
335
435
|
this.hasMany({ name : 'comments' , model : Comment })
|
|
336
436
|
}
|
|
337
437
|
/**
|
|
438
|
+
*
|
|
338
439
|
* @hasManyQuery Get the comments for the post. using function callback
|
|
339
440
|
* @function
|
|
340
441
|
*/
|
|
341
442
|
comments (callback ?: Function) {
|
|
342
|
-
return this.
|
|
343
|
-
return callback ? callback(query) : query
|
|
344
|
-
})
|
|
443
|
+
return this.hasManyBuilder({ name : 'comments' , model : Comment } , callback)
|
|
345
444
|
}
|
|
346
445
|
}
|
|
347
446
|
export default Post
|
|
@@ -372,13 +471,12 @@ class Phone extends Model {
|
|
|
372
471
|
this.belognsTo({ name : 'user' , model : User })
|
|
373
472
|
}
|
|
374
473
|
/**
|
|
474
|
+
*
|
|
375
475
|
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
376
476
|
* @function
|
|
377
477
|
*/
|
|
378
478
|
user (callback ?: Function) {
|
|
379
|
-
return
|
|
380
|
-
return callback ? callback(query) : query
|
|
381
|
-
})
|
|
479
|
+
return this.belongsToBuilder({ name : 'user' , model : User }, callback)
|
|
382
480
|
}
|
|
383
481
|
}
|
|
384
482
|
export default Phone
|
|
@@ -413,9 +511,7 @@ class User extends Model {
|
|
|
413
511
|
* @function
|
|
414
512
|
*/
|
|
415
513
|
roles (callback ?: Function) {
|
|
416
|
-
return
|
|
417
|
-
return callback ? callback(query) : query
|
|
418
|
-
})
|
|
514
|
+
return this.belognsToManyBuilder({ model : Role } , callback)
|
|
419
515
|
}
|
|
420
516
|
}
|
|
421
517
|
export default User
|
|
@@ -428,6 +524,52 @@ const user = await new User().relations('roles').findOne()
|
|
|
428
524
|
const userUsingFunction = await new User().roles().findOne()
|
|
429
525
|
// user?.roles => [{...}]
|
|
430
526
|
```
|
|
527
|
+
|
|
528
|
+
## Relation in Relation
|
|
529
|
+
Relationships can deep relations.
|
|
530
|
+
let's example a deep in relations :
|
|
531
|
+
```js
|
|
532
|
+
import { Model } from 'tspace-mysql'
|
|
533
|
+
|
|
534
|
+
class User extends Model {
|
|
535
|
+
constructor(){
|
|
536
|
+
super()
|
|
537
|
+
this.hasMany({ name : 'posts' , model : Post })
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
+--------------------------------------------------------------------------+
|
|
541
|
+
class Post extends Model {
|
|
542
|
+
constructor(){
|
|
543
|
+
super()
|
|
544
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
545
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
+--------------------------------------------------------------------------+
|
|
549
|
+
class Comment extends Model {
|
|
550
|
+
constructor(){
|
|
551
|
+
super()
|
|
552
|
+
this.hasMany({ name : 'users' , model : User })
|
|
553
|
+
this.belongsTo({ name : 'post' , model : Post })
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
+--------------------------------------------------------------------------+
|
|
557
|
+
await new User().relations('posts')
|
|
558
|
+
.relationQuery('posts', (query : Post) => { // relationQuery return a callback query in model registry
|
|
559
|
+
return query.relations('comments','user')
|
|
560
|
+
.relationQuery('comments', (query : Comment) => {
|
|
561
|
+
return query.relations('user','post')
|
|
562
|
+
})
|
|
563
|
+
.relationQuery('user', (query : User) => {
|
|
564
|
+
return query.relations('posts').relationQuery('posts',(query : Post)=> {
|
|
565
|
+
return query.relations('comments','user')
|
|
566
|
+
// relation n to ...n
|
|
567
|
+
})
|
|
568
|
+
})
|
|
569
|
+
})
|
|
570
|
+
.findMany()
|
|
571
|
+
```
|
|
572
|
+
|
|
431
573
|
## Query Builder
|
|
432
574
|
Methods builder for queries
|
|
433
575
|
```js
|
|
@@ -436,13 +578,20 @@ whereSensitive(column , operator , value)
|
|
|
436
578
|
whereId(id)
|
|
437
579
|
whereUser(userId)
|
|
438
580
|
whereEmail(value)
|
|
439
|
-
orWhere(column , operator , value)
|
|
440
581
|
whereIn(column , [])
|
|
441
582
|
whereNotIn(column , [])
|
|
442
583
|
whereNull(column)
|
|
443
584
|
whereNotNull(column)
|
|
444
585
|
whereBetween (column , [value1 , value2])
|
|
586
|
+
whereQuery(callback)
|
|
587
|
+
whereRaw(sql)
|
|
588
|
+
whereExists(sql)
|
|
445
589
|
whereSubQuery(colmn , rawSQL)
|
|
590
|
+
whereNotSubQuery(colmn , rawSQL)
|
|
591
|
+
orWhere(column , operator , value)
|
|
592
|
+
orWhereRaw(sql)
|
|
593
|
+
orWhereIn(column , [])
|
|
594
|
+
orWhereSubQuery(colmn , rawSQL)
|
|
446
595
|
select(column1 ,column2 ,...N)
|
|
447
596
|
except(column1 ,column2 ,...N)
|
|
448
597
|
only(column1 ,column2 ,...N)
|
|
@@ -451,8 +600,8 @@ join(primary key , table.foreign key)
|
|
|
451
600
|
rightJoin (primary key , table.foreign key)
|
|
452
601
|
leftJoin (primary key , table.foreign key)
|
|
453
602
|
limit (limit)
|
|
454
|
-
orderBy (column ,'ASC' || 'DSCE')
|
|
455
603
|
having (condition)
|
|
604
|
+
orderBy (column ,'ASC' || 'DSCE')
|
|
456
605
|
latest (column)
|
|
457
606
|
oldest (column)
|
|
458
607
|
groupBy (column)
|
|
@@ -464,6 +613,7 @@ updateOrCreate (objects)
|
|
|
464
613
|
connection(options)
|
|
465
614
|
backup({ database , connection })
|
|
466
615
|
backupToFile({ filePath, database , connection })
|
|
616
|
+
hook((result) => ...) // callback result to function
|
|
467
617
|
|
|
468
618
|
/**
|
|
469
619
|
* registry relation in your models
|
|
@@ -510,9 +660,14 @@ save() /*for action statements insert update or delete */
|
|
|
510
660
|
```
|
|
511
661
|
|
|
512
662
|
## Cli
|
|
513
|
-
To get started, let's install
|
|
663
|
+
To get started, let's install tspace-mysql
|
|
514
664
|
you may use a basic cli :
|
|
515
665
|
|
|
666
|
+
```sh
|
|
667
|
+
npm install tspace-mysql -g
|
|
668
|
+
|
|
669
|
+
```
|
|
670
|
+
|
|
516
671
|
## Make Model
|
|
517
672
|
Command will be placed Model in the specific directory
|
|
518
673
|
```js
|
|
@@ -638,12 +793,12 @@ timestamp ()
|
|
|
638
793
|
* @Attrbuites
|
|
639
794
|
*
|
|
640
795
|
*/
|
|
641
|
-
unsigned
|
|
642
|
-
unique
|
|
643
|
-
null
|
|
644
|
-
notNull
|
|
796
|
+
unsigned()
|
|
797
|
+
unique()
|
|
798
|
+
null()
|
|
799
|
+
notNull()
|
|
645
800
|
primary()
|
|
646
|
-
default
|
|
647
|
-
defaultTimestamp
|
|
648
|
-
autoIncrement
|
|
801
|
+
default(string)
|
|
802
|
+
defaultTimestamp()
|
|
803
|
+
autoIncrement()
|
|
649
804
|
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const model_1 = __importDefault(require("../models/model"));
|
|
7
|
+
const pluralize_1 = __importDefault(require("pluralize"));
|
|
8
|
+
const lib_1 = require("../../lib");
|
|
9
|
+
exports.default = (cmd) => {
|
|
10
|
+
const { dir, cwd, type, fs, npm } = cmd;
|
|
11
|
+
if (dir) {
|
|
12
|
+
try {
|
|
13
|
+
fs.accessSync(cwd + `/${dir}`, fs.F_OK, {
|
|
14
|
+
recursive: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
fs.mkdirSync(cwd + `/${dir}`, {
|
|
19
|
+
recursive: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const snake2Pascal = (data) => {
|
|
24
|
+
let str = data.split('_');
|
|
25
|
+
for (let i = 0; i < str.length; i++) {
|
|
26
|
+
str[i] = str[i].slice(0, 1).toUpperCase() + str[i].slice(1, str[i].length);
|
|
27
|
+
}
|
|
28
|
+
return str.join('');
|
|
29
|
+
};
|
|
30
|
+
new lib_1.DB().rawQuery('SHOW TABLES')
|
|
31
|
+
.then(tables => {
|
|
32
|
+
var _a;
|
|
33
|
+
for (let i = 0; i < tables.length; i++) {
|
|
34
|
+
const table = String((_a = Object.values(tables[i])) === null || _a === void 0 ? void 0 : _a.shift());
|
|
35
|
+
const model = snake2Pascal(pluralize_1.default.singular(table));
|
|
36
|
+
const data = (0, model_1.default)(model, npm);
|
|
37
|
+
fs.writeFile(`${dir}/${model}.${type !== null && type !== void 0 ? type : 'ts'}`, data, (err) => {
|
|
38
|
+
if (err)
|
|
39
|
+
throw err;
|
|
40
|
+
});
|
|
41
|
+
console.log(`Model : '${model}' created successfully`);
|
|
42
|
+
}
|
|
43
|
+
console.log('\nGenerate Models has completed');
|
|
44
|
+
})
|
|
45
|
+
.catch(err => console.log(err));
|
|
46
|
+
};
|
package/dist/cli/index.js
CHANGED
|
@@ -3,30 +3,36 @@
|
|
|
3
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
6
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
8
|
const fs_1 = __importDefault(require("fs"));
|
|
8
9
|
const make_1 = __importDefault(require("./models/make"));
|
|
9
10
|
const make_2 = __importDefault(require("./tables/make"));
|
|
10
11
|
const make_3 = __importDefault(require("./migrate/make"));
|
|
12
|
+
const make_4 = __importDefault(require("./generate/make"));
|
|
13
|
+
const query_1 = __importDefault(require("./query"));
|
|
11
14
|
const commands = {
|
|
15
|
+
'query': query_1.default,
|
|
12
16
|
'make:model': make_1.default,
|
|
13
17
|
'make:table': make_2.default,
|
|
14
18
|
'make:migration': make_2.default,
|
|
15
|
-
'migrate': make_3.default
|
|
19
|
+
'migrate': make_3.default,
|
|
20
|
+
'generate:models': make_4.default
|
|
16
21
|
};
|
|
17
22
|
try {
|
|
18
|
-
const name = process.argv.slice(2)
|
|
19
|
-
return data
|
|
20
|
-
})
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(data => {
|
|
24
|
+
return data === null || data === void 0 ? void 0 : data.includes('--name=');
|
|
25
|
+
})) === null || _b === void 0 ? void 0 : _b.replace('--name=', '')) !== null && _c !== void 0 ? _c : null;
|
|
26
|
+
const sql = (_d = process.argv.slice(3)[0]) !== null && _d !== void 0 ? _d : '';
|
|
27
|
+
const migrate = (_f = (_e = process.argv.slice(2)) === null || _e === void 0 ? void 0 : _e.includes('--m')) !== null && _f !== void 0 ? _f : false;
|
|
28
|
+
const dir = (_j = (_h = (_g = process.argv.slice(2)) === null || _g === void 0 ? void 0 : _g.find(data => {
|
|
29
|
+
return data === null || data === void 0 ? void 0 : data.includes('--dir=');
|
|
30
|
+
})) === null || _h === void 0 ? void 0 : _h.replace('--dir=', '/')) !== null && _j !== void 0 ? _j : null;
|
|
31
|
+
let type = (_m = (_l = (_k = process.argv.slice(2)) === null || _k === void 0 ? void 0 : _k.find(data => {
|
|
32
|
+
return data === null || data === void 0 ? void 0 : data.includes('--type=');
|
|
33
|
+
})) === null || _l === void 0 ? void 0 : _l.replace('--type=', '.')) !== null && _m !== void 0 ? _m : '.ts';
|
|
28
34
|
type = ['.js', '.ts'].includes(type) ? type : '.ts';
|
|
29
|
-
const file = process.argv.slice(3)
|
|
35
|
+
const file = (_o = process.argv.slice(3)[0]) !== null && _o !== void 0 ? _o : '';
|
|
30
36
|
const cmd = {
|
|
31
37
|
name,
|
|
32
38
|
file,
|
|
@@ -35,10 +41,11 @@ try {
|
|
|
35
41
|
type,
|
|
36
42
|
cwd: process.cwd(),
|
|
37
43
|
fs: fs_1.default,
|
|
44
|
+
sql,
|
|
38
45
|
npm: 'tspace-mysql'
|
|
39
46
|
};
|
|
40
47
|
commands[process.argv[2]](cmd);
|
|
41
48
|
}
|
|
42
49
|
catch (err) {
|
|
43
|
-
console.log(`
|
|
50
|
+
console.log(`Readme https://www.npmjs.com/package/tspace-mysql`);
|
|
44
51
|
}
|
package/dist/cli/migrate/make.js
CHANGED
|
@@ -2,22 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const child_process_1 = require("child_process");
|
|
4
4
|
exports.default = (formCommand) => {
|
|
5
|
+
var _a, _b, _c;
|
|
5
6
|
const { type, dir, cwd, fs } = formCommand;
|
|
6
7
|
try {
|
|
7
8
|
if (dir == null)
|
|
8
9
|
throw new Error('Not found directory');
|
|
9
10
|
const path = `${cwd}/${dir}`;
|
|
10
|
-
const files = fs.readdirSync(path)
|
|
11
|
-
if (!files
|
|
11
|
+
const files = (_a = fs.readdirSync(path)) !== null && _a !== void 0 ? _a : [];
|
|
12
|
+
if (!(files === null || files === void 0 ? void 0 : files.length))
|
|
12
13
|
console.log('this folder is empty');
|
|
13
14
|
const cmd = type === '.js' ? 'node' : 'ts-node';
|
|
14
15
|
for (const _file of files) {
|
|
15
16
|
const run = (0, child_process_1.exec)(`${cmd} ${path}/${_file}`);
|
|
16
|
-
run
|
|
17
|
+
(_b = run === null || run === void 0 ? void 0 : run.stdout) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
|
|
17
18
|
if (data)
|
|
18
19
|
console.log(data);
|
|
19
20
|
});
|
|
20
|
-
run
|
|
21
|
+
(_c = run === null || run === void 0 ? void 0 : run.stderr) === null || _c === void 0 ? void 0 : _c.on('data', (err) => {
|
|
21
22
|
if (err)
|
|
22
23
|
console.error(err);
|
|
23
24
|
});
|
package/dist/cli/models/make.js
CHANGED
|
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const model_1 = __importDefault(require("./model"));
|
|
7
7
|
const table_1 = __importDefault(require("../tables/table"));
|
|
8
8
|
const pluralize_1 = __importDefault(require("pluralize"));
|
|
9
|
-
exports.default = (
|
|
10
|
-
const { file, migrate, dir, type, cwd, fs, npm } =
|
|
9
|
+
exports.default = (cmd) => {
|
|
10
|
+
const { file, migrate, dir, type, cwd, fs, npm } = cmd;
|
|
11
11
|
if (dir) {
|
|
12
12
|
try {
|
|
13
13
|
fs.accessSync(cwd + `/${dir}`, fs.F_OK, {
|
package/dist/cli/models/model.js
CHANGED
|
@@ -14,10 +14,9 @@ class ${model} extends Model {
|
|
|
14
14
|
* this.usePrimaryKey('id') // => runing a uuid (universally unique identifier) when insert new data
|
|
15
15
|
* this.useTimestamp({ createdAt : 'created_at' , updatedAt : 'updated_at' }) // runing a timestamp when insert or update
|
|
16
16
|
* this.useSoftDelete()
|
|
17
|
-
* this.
|
|
18
|
-
* this.
|
|
19
|
-
* this.
|
|
20
|
-
* this.useTablePlural()
|
|
17
|
+
* this.useTable('users')
|
|
18
|
+
* this.useTableSingular() // 'user'
|
|
19
|
+
* this.useTablePlural() // 'users'
|
|
21
20
|
* this.usePattern('snake_case')
|
|
22
21
|
* this.useUUID('uuid')
|
|
23
22
|
* this.useRegistry()
|
|
@@ -1,48 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
query: (sql: string, callback: (err: any, result: any) => void) => void;
|
|
3
|
-
release: () => void;
|
|
4
|
-
}
|
|
5
|
-
export interface ConnectionTransaction {
|
|
6
|
-
query: (sql: string) => Promise<any[]>;
|
|
7
|
-
startTransaction: () => Promise<any[]>;
|
|
8
|
-
commit: () => Promise<any[]>;
|
|
9
|
-
rollback: () => Promise<any[]>;
|
|
10
|
-
}
|
|
11
|
-
export interface Connection {
|
|
12
|
-
query: (sql: string) => Promise<any[]>;
|
|
13
|
-
connection: () => Promise<ConnectionTransaction>;
|
|
14
|
-
}
|
|
15
|
-
export interface Options {
|
|
16
|
-
[key: string]: any;
|
|
17
|
-
connectionLimit?: number;
|
|
18
|
-
dateStrings?: boolean;
|
|
19
|
-
waitForConnections?: boolean;
|
|
20
|
-
charset?: string;
|
|
21
|
-
host: string;
|
|
22
|
-
port: number;
|
|
23
|
-
database: string;
|
|
24
|
-
user: string;
|
|
25
|
-
password: string;
|
|
26
|
-
}
|
|
1
|
+
import { Connection, Options } from '../tspace/Interface';
|
|
27
2
|
export declare class PoolConnection {
|
|
28
3
|
private OPTIONS;
|
|
29
|
-
constructor(options?: Options);
|
|
30
4
|
/**
|
|
31
5
|
*
|
|
32
|
-
*
|
|
33
|
-
* @return {this} this
|
|
6
|
+
* @Init a options connection pool
|
|
34
7
|
*/
|
|
35
|
-
|
|
8
|
+
constructor(options?: Options);
|
|
36
9
|
/**
|
|
37
10
|
*
|
|
38
|
-
* Get a connection
|
|
11
|
+
* Get a connection to database
|
|
39
12
|
* @return {Connection} Connection
|
|
13
|
+
* @property {Function} Connection.query
|
|
14
|
+
* @property {Function} Connection.connection
|
|
40
15
|
*/
|
|
41
16
|
connection(): Connection;
|
|
42
17
|
private _defaultOptions;
|
|
43
|
-
private
|
|
18
|
+
private _loadOptions;
|
|
44
19
|
private _messageError;
|
|
45
20
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* Connection to database when service is started
|
|
24
|
+
* @return {Connection} Connection
|
|
25
|
+
* @property {Function} Connection.query
|
|
26
|
+
* @property {Function} Connection.connection
|
|
27
|
+
*/
|
|
28
|
+
declare const pool: Connection;
|
|
29
|
+
export { pool as Pool };
|
|
30
|
+
export default pool;
|