tspace-mysql 1.0.6 → 1.0.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 +315 -200
- package/dist/cli/index.js +9 -3
- package/dist/cli/migrate/make.d.ts +1 -1
- package/dist/cli/migrate/make.js +2 -2
- package/dist/cli/models/make.d.ts +1 -1
- package/dist/cli/models/make.js +2 -2
- package/dist/cli/models/model.js +1 -1
- package/dist/cli/tables/make.d.ts +1 -1
- package/dist/cli/tables/make.js +2 -2
- package/dist/cli/tables/table.js +1 -1
- package/dist/lib/config/env.js +7 -11
- package/dist/lib/connection/index.d.ts +27 -0
- package/dist/lib/connection/index.js +79 -0
- package/dist/lib/constants/index.d.ts +3 -3
- package/dist/lib/constants/index.js +89 -8
- package/dist/lib/tspace/AbstractDB.d.ts +1 -1
- package/dist/lib/tspace/AbstractDB.js +2 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +20 -16
- package/dist/lib/tspace/AbstractDatabase.js +9 -19
- package/dist/lib/tspace/AbstractModel.d.ts +5 -4
- package/dist/lib/tspace/AbstractModel.js +2 -0
- package/dist/lib/tspace/Blueprint.d.ts +65 -0
- package/dist/lib/tspace/Blueprint.js +195 -0
- package/dist/lib/tspace/DB.d.ts +15 -10
- package/dist/lib/tspace/DB.js +41 -106
- package/dist/lib/tspace/Database.d.ts +568 -100
- package/dist/lib/tspace/Database.js +1629 -394
- package/dist/lib/tspace/Interface.d.ts +63 -9
- package/dist/lib/tspace/Logger.d.ts +8 -1
- package/dist/lib/tspace/Logger.js +46 -30
- package/dist/lib/tspace/Model.d.ts +392 -119
- package/dist/lib/tspace/Model.js +1349 -743
- package/dist/lib/tspace/ProxyHandler.d.ts +5 -4
- package/dist/lib/tspace/ProxyHandler.js +15 -11
- package/dist/lib/tspace/Schema.d.ts +4 -41
- package/dist/lib/tspace/Schema.js +12 -137
- package/dist/lib/tspace/index.d.ts +2 -1
- package/dist/lib/tspace/index.js +6 -5
- package/dist/lib/utils/index.d.ts +1 -3
- package/dist/lib/utils/index.js +4 -85
- package/package.json +4 -7
- package/dist/lib/connections/index.d.ts +0 -2
- package/dist/lib/connections/index.js +0 -42
- package/dist/lib/connections/options.d.ts +0 -4
- package/dist/lib/connections/options.js +0 -32
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
## tspace-mysql
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com)
|
|
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
|
+
Its goal is to always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.
|
|
7
8
|
|
|
8
9
|
## Install
|
|
9
10
|
|
|
@@ -13,8 +14,18 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
13
14
|
npm install tspace-mysql --save
|
|
14
15
|
|
|
15
16
|
```
|
|
16
|
-
##
|
|
17
|
-
|
|
17
|
+
## Basic Usage
|
|
18
|
+
- [Configuration](#configuration)
|
|
19
|
+
- [Running Queries](#running-queryies)
|
|
20
|
+
- [Database Transactions](#database-transactions)
|
|
21
|
+
- [Accessing Connections](#accessing-connections)
|
|
22
|
+
- [Query Logging](#query-logging)
|
|
23
|
+
- [Generating Model Classes](#generating-model-classes)
|
|
24
|
+
- [Model Conventions](#model-conventions)
|
|
25
|
+
- [Relationships](#relationships)
|
|
26
|
+
|
|
27
|
+
## Configuration
|
|
28
|
+
Created your environment variables is to use a .env file, you may establish a connection is this:
|
|
18
29
|
```js
|
|
19
30
|
DB_HOST = localhost
|
|
20
31
|
DB_PORT = 3306
|
|
@@ -22,180 +33,276 @@ DB_USERNAME = root
|
|
|
22
33
|
DB_PASSWORD = password
|
|
23
34
|
DB_DATABASE = database
|
|
24
35
|
```
|
|
25
|
-
##
|
|
26
|
-
queries
|
|
36
|
+
## Running Queries
|
|
37
|
+
Once you have configured your database connection, you may run queries is this :
|
|
27
38
|
```js
|
|
39
|
+
+-------------+--------------+----------------------------+
|
|
40
|
+
| table users |
|
|
41
|
+
+-------------+--------------+----------------------------+
|
|
42
|
+
| id | username | email |
|
|
43
|
+
|-------------|--------------|----------------------------|
|
|
44
|
+
| 1 | tspace | tspace@gmail.com |
|
|
45
|
+
| 2 | tspace2 | tspace2@gmail.com |
|
|
46
|
+
+-------------+--------------+----------------------------+
|
|
47
|
+
|
|
28
48
|
import { DB } from 'tspace-mysql'
|
|
29
49
|
(async () => {
|
|
30
|
-
await new DB().
|
|
31
|
-
await new DB(
|
|
32
|
-
await new DB().table('users').select('id').where('id',1).findOne()
|
|
33
|
-
await new DB().table('users').select('id').whereIn('id',[1,2,3]).findMany()
|
|
50
|
+
await new DB('users').findMany() // SELECT * FROM users => Array
|
|
51
|
+
await new DB('users').findOne() // SELECT * FROM users LIMIT 1 => Object
|
|
34
52
|
})()
|
|
35
53
|
```
|
|
36
|
-
|
|
37
|
-
|
|
54
|
+
Running A Select Query
|
|
38
55
|
```js
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
name : 'tspace',
|
|
44
|
-
email : 'tspace@gmail.com'
|
|
45
|
-
}).save()
|
|
46
|
-
|
|
47
|
-
console.log(user)
|
|
48
|
-
|
|
49
|
-
*ex pattern 2
|
|
50
|
-
const u = new DB().table('users')
|
|
51
|
-
u.name = 'tspace'
|
|
52
|
-
u.email = 'tspace@gmail.com'
|
|
53
|
-
await u.save()
|
|
54
|
-
const { result : user } = u
|
|
55
|
-
/* or const user = await u.save() */
|
|
56
|
-
console.log(user)
|
|
57
|
-
})()
|
|
56
|
+
const selectQuery = await new DB('users').select('id','username').findOne()
|
|
57
|
+
// selectQuery => { id : 1, username : 'tspace'}
|
|
58
|
+
const selectQueries = await new DB('users').select('id','username').findMany()
|
|
59
|
+
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
58
60
|
```
|
|
59
|
-
|
|
60
|
-
update data
|
|
61
|
+
Running A Where Query
|
|
61
62
|
```js
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
63
|
+
const user = await new DB('users').where('id',1).findOne()
|
|
64
|
+
// user => { id : 1 , username : 'tspace', email : 'tspace@gmail.com'}
|
|
65
|
+
const users = await new DB('users').where('id','!=',1).findMany()
|
|
66
|
+
// users => [{ id : 2 , username : 'tspace2' , email : 'tspace2@gmail.com' }]
|
|
67
|
+
```
|
|
68
|
+
Running A Insert Query
|
|
69
|
+
```js
|
|
70
|
+
const user = await new DB('users').create({
|
|
71
|
+
name : 'tspace3',
|
|
72
|
+
email : 'tspace3@gmail.com'
|
|
73
|
+
}).save()
|
|
74
|
+
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
75
|
+
+----------------------------------------------------------------------------------+
|
|
76
|
+
const reposity = new DB('users')
|
|
77
|
+
reposity.name = 'tspace4'
|
|
78
|
+
reposity.email = 'tspace4@gmail.com'
|
|
79
|
+
await reposity.save()
|
|
80
|
+
const { result } = reposity
|
|
81
|
+
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
82
|
+
```
|
|
83
|
+
Running A Update Query
|
|
84
|
+
```js
|
|
85
|
+
const user = await new DB('users').where('id',1)
|
|
86
|
+
.update({
|
|
87
|
+
name : 'tspace1**',
|
|
88
|
+
email : 'tspace1@gmail.com'
|
|
89
|
+
}).save()
|
|
90
|
+
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
91
|
+
+----------------------------------------------------------------------------------+
|
|
92
|
+
const reposity = new DB('users').where('id',1)
|
|
93
|
+
reposity.name = 'tspace1++'
|
|
94
|
+
reposity.email = 'tspace1++@gmail.com'
|
|
95
|
+
await reposity.save()
|
|
96
|
+
const { result } = reposity
|
|
97
|
+
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
83
98
|
```
|
|
84
|
-
|
|
99
|
+
Running A Delete Query
|
|
85
100
|
```js
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
await new User().where('id',1).delete()
|
|
89
|
-
})()
|
|
101
|
+
const deleted = await new DB('users').where('id',1).delete()
|
|
102
|
+
// deleted => true
|
|
90
103
|
```
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
## Database Transactions
|
|
105
|
+
Within a database transaction, you may use the:
|
|
93
106
|
```js
|
|
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
|
-
|
|
107
|
+
const transaction = await new DB().beginTransaction()
|
|
108
|
+
try {
|
|
109
|
+
const user = await new DB('users').create({
|
|
110
|
+
name : 'tspace5',
|
|
111
|
+
email : 'tspace5@gmail.com'
|
|
112
|
+
})
|
|
113
|
+
.save(transaction)
|
|
114
|
+
// user => { id : 5 , username : 'tspace5', email : 'tspace5@gmail.com'}
|
|
115
|
+
|
|
116
|
+
const userSecond = await new DB('users').create({
|
|
117
|
+
name : 'tspace6',
|
|
118
|
+
email : 'tspace6@gmail.com'
|
|
119
|
+
})
|
|
120
|
+
.save(transaction)
|
|
121
|
+
// userSecond => { id : 6 , username : 'tspace6', email : 'tspace6@gmail.com'}
|
|
122
|
+
|
|
123
|
+
throw new Error('try to using transaction')
|
|
124
|
+
|
|
125
|
+
} catch (err) {
|
|
126
|
+
const rollback = await transaction.rollback()
|
|
127
|
+
// rollback => true
|
|
128
|
+
// * rollback data user, userSecond in your database
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
## Generating Model Classes
|
|
132
|
+
To get started, let's install npm install tspace-mysql -g
|
|
133
|
+
you may use the make:model command to generate a new model:
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
npm install tspace-mysql -g
|
|
137
|
+
tspace-mysql make:model <model name> --dir=<directory>
|
|
138
|
+
|
|
139
|
+
# tspace-mysql make:model User --dir=App/Models
|
|
140
|
+
# => App/Models/User.ts
|
|
123
141
|
```
|
|
124
|
-
|
|
125
142
|
## Model Conventions
|
|
126
|
-
|
|
143
|
+
Models generated by the make:model command will be placed in the specific directory.
|
|
144
|
+
Let's examine a basic model class:
|
|
127
145
|
|
|
128
146
|
```js
|
|
129
147
|
import { Model } from 'tspace-mysql'
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
.findMany()
|
|
149
|
-
|
|
150
|
-
console.log(users)
|
|
151
|
-
})()
|
|
148
|
+
class User extends Model {
|
|
149
|
+
constructor(){
|
|
150
|
+
super()
|
|
151
|
+
this.useTimestamp()
|
|
152
|
+
/*
|
|
153
|
+
* the "snake case", plural name of the class will be used as the table name
|
|
154
|
+
*
|
|
155
|
+
* @param {string} name The table associated with the model.
|
|
156
|
+
*/
|
|
157
|
+
this.useTable('users')
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
export { User }
|
|
161
|
+
export default User
|
|
162
|
+
```
|
|
163
|
+
## Relationships
|
|
164
|
+
Relationships are defined as methods on your Model classes
|
|
165
|
+
Let's examine a basic relations :
|
|
152
166
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
*/
|
|
167
|
+
## One To One
|
|
168
|
+
A one-to-one relationship is used to define relationships where a single model is the parent to one child models
|
|
169
|
+
```js
|
|
157
170
|
import { Model } from 'tspace-mysql'
|
|
171
|
+
import Phone from '../Phone'
|
|
158
172
|
class User extends Model {
|
|
159
173
|
constructor(){
|
|
160
174
|
super()
|
|
161
|
-
this.
|
|
162
|
-
|
|
175
|
+
this.useTimestamp()
|
|
176
|
+
/**
|
|
177
|
+
*
|
|
178
|
+
* @hasOne Get the phone associated with the user.
|
|
179
|
+
* @relationship users.id -> phones.user_id
|
|
180
|
+
*/
|
|
181
|
+
this.hasOne({ name : 'phone' , model : Phone })
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @hasOneQuery Get the phone associated with the user. using function callback
|
|
185
|
+
* @function
|
|
186
|
+
*/
|
|
187
|
+
phone (callback ?: Function) {
|
|
188
|
+
return this.hasOneQuery({ model : Phone }, (query) => {
|
|
189
|
+
return callback ? callback(query) : query
|
|
190
|
+
})
|
|
163
191
|
}
|
|
164
|
-
}
|
|
192
|
+
}
|
|
165
193
|
export default User
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
194
|
+
+----------------------------------------------------------------------------------+
|
|
195
|
+
import User from '../User'
|
|
196
|
+
const user = await new User().with('brand').findOne()
|
|
197
|
+
// user?.phone => {...}
|
|
198
|
+
const userUsingFunction = await new User().phone().findOne()
|
|
199
|
+
// userUsingFunction?.phone => {...}
|
|
200
|
+
```
|
|
171
201
|
|
|
202
|
+
## One To Many
|
|
203
|
+
A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models.
|
|
204
|
+
```js
|
|
205
|
+
import { Model } from 'tspace-mysql'
|
|
206
|
+
import Comment from '../Comment'
|
|
172
207
|
class Post extends Model {
|
|
173
208
|
constructor(){
|
|
174
209
|
super()
|
|
175
|
-
this.
|
|
210
|
+
this.useTimestamp()
|
|
211
|
+
/**
|
|
212
|
+
*
|
|
213
|
+
* @hasMany Get the comments for the post.
|
|
214
|
+
* @relationship posts.id -> comments.post_id
|
|
215
|
+
*/
|
|
176
216
|
this.hasMany({ name : 'comments' , model : Comment })
|
|
177
217
|
}
|
|
178
|
-
|
|
218
|
+
/**
|
|
219
|
+
* @hasManyQuery Get the comments for the post. using function callback
|
|
220
|
+
* @function
|
|
221
|
+
*/
|
|
222
|
+
comments (callback ?: Function) {
|
|
223
|
+
return this.hasManyQuery({ model : Comment }, (query) => {
|
|
224
|
+
return callback ? callback(query) : query
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
}
|
|
179
228
|
export default Post
|
|
229
|
+
+----------------------------------------------------------------------------------+
|
|
230
|
+
import Post from '../Post'
|
|
231
|
+
const posts = await new Post().with('comments').findOne()
|
|
232
|
+
// posts?.comments => [{...}]
|
|
233
|
+
const postsUsingFunction = await new Post().comments().findOne()
|
|
234
|
+
// postsUsingFunction?.comments => [{...}]
|
|
235
|
+
```
|
|
180
236
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
* Comment.ts
|
|
184
|
-
*/
|
|
185
|
-
|
|
237
|
+
## One To One & One To Many (Inverse) / Belongs To
|
|
238
|
+
```js
|
|
186
239
|
import { Model } from 'tspace-mysql'
|
|
187
|
-
|
|
188
|
-
class
|
|
240
|
+
import User from '../User'
|
|
241
|
+
class Phone extends Model {
|
|
189
242
|
constructor(){
|
|
190
243
|
super()
|
|
191
|
-
this.
|
|
192
|
-
|
|
244
|
+
this.useTimestamp()
|
|
245
|
+
/**
|
|
246
|
+
*
|
|
247
|
+
* @belongsTo Get the user that owns the phone.
|
|
248
|
+
* @relationship phones.user_id -> users.id
|
|
249
|
+
*/
|
|
250
|
+
this.belognsTo({ name : 'user' , model : User })
|
|
193
251
|
}
|
|
194
|
-
|
|
195
|
-
|
|
252
|
+
/**
|
|
253
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
254
|
+
* @function
|
|
255
|
+
*/
|
|
256
|
+
user (callback ?: Function) {
|
|
257
|
+
return this.belongsToQuery({ model : User }, (query) => {
|
|
258
|
+
return callback ? callback(query) : query
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
export default Phone
|
|
263
|
+
+----------------------------------------------------------------------------------+
|
|
264
|
+
import Phone from '../Phone'
|
|
265
|
+
const phone = await new Phone().with('user').findOne()
|
|
266
|
+
// phone?.user => {...}
|
|
267
|
+
const phoneUsingFunction = await new Phone().user().findOne()
|
|
268
|
+
// phoneUsingFunction?.user => {...}
|
|
196
269
|
```
|
|
197
270
|
|
|
198
|
-
##
|
|
271
|
+
## Many To Many
|
|
272
|
+
Many-to-many relations are slightly more complicated than hasOne and hasMany relationships.
|
|
273
|
+
```js
|
|
274
|
+
import { Model } from 'tspace-mysql'
|
|
275
|
+
import Role from '../Role'
|
|
276
|
+
class User extends Model {
|
|
277
|
+
constructor(){
|
|
278
|
+
super()
|
|
279
|
+
this.useTimestamp()
|
|
280
|
+
/**
|
|
281
|
+
*
|
|
282
|
+
* @belongsToMany Get The roles that belong to the user.
|
|
283
|
+
* @relationship users.id , roles.id => role_user.user_id , role_user.role_id
|
|
284
|
+
*/
|
|
285
|
+
this.belognsToMany({ name : 'roles' , model : Role })
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
289
|
+
* @function
|
|
290
|
+
*/
|
|
291
|
+
roles (callback ?: Function) {
|
|
292
|
+
return this.belongsToManyQuery({ model : Role }, (query) => {
|
|
293
|
+
return callback ? callback(query) : query
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export default User
|
|
298
|
+
+----------------------------------------------------------------------------------+
|
|
299
|
+
import User from '../User'
|
|
300
|
+
const user = await new User().with('roles').findOne()
|
|
301
|
+
// user?.roles => [{...}]
|
|
302
|
+
const userUsingFunction = await new User().roles().findOne()
|
|
303
|
+
// user?.roles => [{...}]
|
|
304
|
+
```
|
|
305
|
+
## Query Builder Chaining
|
|
199
306
|
method chaining for queries
|
|
200
307
|
```js
|
|
201
308
|
where(column , operator , value)
|
|
@@ -230,25 +337,35 @@ createMultiple(array objects)
|
|
|
230
337
|
update (objects)
|
|
231
338
|
createNotExists(objects)
|
|
232
339
|
updateOrCreate (objects)
|
|
340
|
+
|
|
233
341
|
/**
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
|
|
342
|
+
* registry relation in your models
|
|
343
|
+
* @relationship
|
|
344
|
+
*/
|
|
345
|
+
hasOne({ name , model , localKey , foreignKey , freezeTable , as }
|
|
346
|
+
hasMany({ name , model , localKey , foreignKey , freezeTable , as }
|
|
347
|
+
belongsTo({ name , model , localKey , foreignKey , freezeTable , as }
|
|
348
|
+
belongsToMany({ name , model , localKey , foreignKey , freezeTable , as }
|
|
349
|
+
/**
|
|
350
|
+
* @relation using registry in your models
|
|
237
351
|
*/
|
|
238
352
|
with(name1 , name2,...nameN)
|
|
353
|
+
/**
|
|
354
|
+
* @relation using registry in your models. if exists child data remove this parent data
|
|
355
|
+
*/
|
|
239
356
|
withExists(name1 , name2,...nameN)
|
|
240
|
-
|
|
357
|
+
/**
|
|
358
|
+
* @relation call a relation in registry, callback query of parent data
|
|
359
|
+
*/
|
|
360
|
+
withQuery('relation registry',(callback query))
|
|
241
361
|
|
|
242
362
|
/**
|
|
243
363
|
* queries statements
|
|
244
|
-
*
|
|
245
|
-
* @exec statements
|
|
364
|
+
* @exec statements
|
|
246
365
|
*/
|
|
247
366
|
findMany()
|
|
248
367
|
findOne()
|
|
249
368
|
find(id)
|
|
250
|
-
first()
|
|
251
|
-
get()
|
|
252
369
|
delelte()
|
|
253
370
|
exists ()
|
|
254
371
|
onlyTrashed()
|
|
@@ -266,61 +383,64 @@ save() /*for action statements insert update or delete */
|
|
|
266
383
|
```
|
|
267
384
|
|
|
268
385
|
## Cli
|
|
269
|
-
npm install tspace-mysql -g
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
tspace-mysql make:model <MODEL NAME> --m --dir=... --js
|
|
273
|
-
* optional
|
|
274
|
-
--m /* created table for migrate */
|
|
275
|
-
--dir=directory /* created model in directory */
|
|
276
|
-
--type=js /* extension js default ts */
|
|
277
|
-
|
|
278
|
-
tspace-mysql make:migration <TABLE NAME>
|
|
279
|
-
* optional
|
|
280
|
-
--type=js /* extension js default ts */
|
|
281
|
-
--dir=directory /* created table in directory */
|
|
282
|
-
|
|
283
|
-
tspace-mysql migrate <FOLDER> --js
|
|
284
|
-
* optional
|
|
285
|
-
--type=js /* extension js default ts */
|
|
286
|
-
--dir=directory /* find migrate in directory */
|
|
287
|
-
```
|
|
386
|
+
To get started, let's install npm install tspace-mysql -g
|
|
387
|
+
you may use a basic cli :
|
|
288
388
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
389
|
+
## Make Model
|
|
390
|
+
Command will be placed Model in the specific directory
|
|
391
|
+
```sh
|
|
392
|
+
tspace-mysql make:model <MODEL NAME> --m --dir=.... --type=....
|
|
393
|
+
/**
|
|
394
|
+
* @options
|
|
395
|
+
* --m // created scheme table for migrate
|
|
396
|
+
* --dir=directory // created model in directory
|
|
397
|
+
* --type=js // extension js default ts
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
tspace-mysql make:model User --m --dir=app/Models
|
|
401
|
+
// =>
|
|
292
402
|
- node_modules
|
|
293
|
-
-
|
|
403
|
+
- app
|
|
294
404
|
- Models
|
|
295
405
|
User.ts
|
|
296
406
|
*/
|
|
297
|
-
|
|
298
|
-
/* in App/Models/User.ts */
|
|
299
|
-
import { Model } from 'tspace-mysql'
|
|
300
|
-
class User extends Model{
|
|
301
|
-
constructor(){
|
|
302
|
-
super()
|
|
303
|
-
this.useDebug() /* default false *debug sql */
|
|
304
|
-
this.useTimestamp() /* default false * case created_at & updated_at*/
|
|
305
|
-
this.useSoftDelete() /* default false * case where deleted_at is null */
|
|
306
|
-
this.useTable('users') /* default users */
|
|
307
|
-
this.usePattern('camelCase') /* default snake_case */
|
|
308
|
-
this.useUUID()
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
export default User
|
|
312
407
|
```
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
408
|
+
|
|
409
|
+
## Make Migration
|
|
410
|
+
Command will be placed Migration in the specific directory
|
|
411
|
+
```sh
|
|
412
|
+
tspace-mysql make:migration <TABLE NAME>
|
|
413
|
+
/**
|
|
414
|
+
* @options
|
|
415
|
+
* --type=js /* extension js default ts */
|
|
416
|
+
* --dir=directory /* created table in directory */
|
|
417
|
+
*/
|
|
418
|
+
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
419
|
+
// =>
|
|
316
420
|
- node_modules
|
|
317
|
-
-
|
|
421
|
+
- app
|
|
318
422
|
- Models
|
|
319
|
-
-
|
|
423
|
+
- Migrations
|
|
320
424
|
create_users_table.ts
|
|
321
425
|
User.ts
|
|
322
426
|
*/
|
|
323
|
-
|
|
427
|
+
```
|
|
428
|
+
## Migrate
|
|
429
|
+
```sh
|
|
430
|
+
tspace-mysql migrate <FOLDER> --type=...
|
|
431
|
+
/**
|
|
432
|
+
* @options
|
|
433
|
+
*--type=js /* extension js default ts */
|
|
434
|
+
*--dir=directory /* find migrate in directory */
|
|
435
|
+
*/
|
|
436
|
+
|
|
437
|
+
tspace-mysql migrate --dir=App/Models/Migrations
|
|
438
|
+
// => migrate all schema table in folder into database
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
## Blueprint method
|
|
442
|
+
Schema table created by command make:migration, you may use the:
|
|
443
|
+
```js
|
|
324
444
|
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
325
445
|
(async () => {
|
|
326
446
|
await new Schema().table('users',{
|
|
@@ -333,16 +453,11 @@ import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
|
333
453
|
updated_at : new Blueprint().null().timestamp()
|
|
334
454
|
})
|
|
335
455
|
/**
|
|
336
|
-
*
|
|
337
|
-
* @Faker data
|
|
456
|
+
*
|
|
457
|
+
* @Faker fake data 5 raw
|
|
338
458
|
* await new DB().table('users').faker(5)
|
|
339
459
|
*/
|
|
340
460
|
})()
|
|
341
|
-
```
|
|
342
|
-
tspace-mysql migrate --dir=App/Models/Migrations
|
|
343
|
-
/* migrate all table in folder into database */
|
|
344
|
-
```js
|
|
345
|
-
* Blueprint method
|
|
346
461
|
/**
|
|
347
462
|
*
|
|
348
463
|
* @Types
|
|
@@ -376,4 +491,4 @@ primary()
|
|
|
376
491
|
default (string)
|
|
377
492
|
defaultTimestamp ()
|
|
378
493
|
autoIncrement ()
|
|
379
|
-
```
|
|
494
|
+
```
|
package/dist/cli/index.js
CHANGED
|
@@ -16,10 +16,16 @@ var commands = {
|
|
|
16
16
|
'migrate': make_3.default
|
|
17
17
|
};
|
|
18
18
|
try {
|
|
19
|
-
var name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(function (data) {
|
|
19
|
+
var name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(function (data) {
|
|
20
|
+
return data === null || data === void 0 ? void 0 : data.includes('--name=');
|
|
21
|
+
})) === null || _b === void 0 ? void 0 : _b.replace('--name=', '')) !== null && _c !== void 0 ? _c : null;
|
|
20
22
|
var migrate = (_e = (_d = process.argv.slice(2)) === null || _d === void 0 ? void 0 : _d.includes('--m')) !== null && _e !== void 0 ? _e : false;
|
|
21
|
-
var dir = (_h = (_g = (_f = process.argv.slice(2)) === null || _f === void 0 ? void 0 : _f.find(function (data) {
|
|
22
|
-
|
|
23
|
+
var dir = (_h = (_g = (_f = process.argv.slice(2)) === null || _f === void 0 ? void 0 : _f.find(function (data) {
|
|
24
|
+
return data === null || data === void 0 ? void 0 : data.includes('--dir=');
|
|
25
|
+
})) === null || _g === void 0 ? void 0 : _g.replace('--dir=', '/')) !== null && _h !== void 0 ? _h : null;
|
|
26
|
+
var type = (_l = (_k = (_j = process.argv.slice(2)) === null || _j === void 0 ? void 0 : _j.find(function (data) {
|
|
27
|
+
return data === null || data === void 0 ? void 0 : data.includes('--type=');
|
|
28
|
+
})) === null || _k === void 0 ? void 0 : _k.replace('--type=', '.')) !== null && _l !== void 0 ? _l : '.ts';
|
|
23
29
|
type = ['.js', '.ts'].includes(type) ? type : '.ts';
|
|
24
30
|
var file = (_o = (_m = process.argv.slice(3)) === null || _m === void 0 ? void 0 : _m.shift()) !== null && _o !== void 0 ? _o : '';
|
|
25
31
|
var cmd = {
|
package/dist/cli/migrate/make.js
CHANGED
|
@@ -12,10 +12,10 @@ var __values = (this && this.__values) || function(o) {
|
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
var child_process_1 = require("child_process");
|
|
15
|
-
exports.default = (function (
|
|
15
|
+
exports.default = (function (formCommand) {
|
|
16
16
|
var e_1, _a;
|
|
17
17
|
var _b, _c, _d;
|
|
18
|
-
var type =
|
|
18
|
+
var type = formCommand.type, dir = formCommand.dir, cwd = formCommand.cwd, fs = formCommand.fs;
|
|
19
19
|
try {
|
|
20
20
|
if (dir == null)
|
|
21
21
|
throw new Error('Not found directory');
|