tspace-mysql 1.0.6 → 1.0.8
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 +390 -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
|
|
|
@@ -11,10 +12,29 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
11
12
|
|
|
12
13
|
```sh
|
|
13
14
|
npm install tspace-mysql --save
|
|
14
|
-
|
|
15
15
|
```
|
|
16
|
-
##
|
|
17
|
-
|
|
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:
|
|
18
38
|
```js
|
|
19
39
|
DB_HOST = localhost
|
|
20
40
|
DB_PORT = 3306
|
|
@@ -22,180 +42,341 @@ DB_USERNAME = root
|
|
|
22
42
|
DB_PASSWORD = password
|
|
23
43
|
DB_DATABASE = database
|
|
24
44
|
```
|
|
25
|
-
##
|
|
26
|
-
queries
|
|
45
|
+
## Running Queries
|
|
46
|
+
Once you have configured your database connection, you may run queries is this :
|
|
27
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
|
+
|
|
28
57
|
import { DB } from 'tspace-mysql'
|
|
29
58
|
(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()
|
|
59
|
+
await new DB('users').findMany() // SELECT * FROM users => Array
|
|
60
|
+
await new DB('users').findOne() // SELECT * FROM users LIMIT 1 => Object
|
|
34
61
|
})()
|
|
35
62
|
```
|
|
36
|
-
|
|
37
|
-
|
|
63
|
+
Running A Select Query
|
|
38
64
|
```js
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
})()
|
|
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' }]
|
|
58
76
|
```
|
|
77
|
+
Running A Insert Query
|
|
78
|
+
```js
|
|
79
|
+
const user = await new DB('users').create({
|
|
80
|
+
name : 'tspace3',
|
|
81
|
+
email : 'tspace3@gmail.com'
|
|
82
|
+
}).save()
|
|
83
|
+
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
84
|
+
|
|
85
|
+
+--------------------------------------------------------------------------+
|
|
59
86
|
|
|
60
|
-
|
|
87
|
+
const reposity = new DB('users')
|
|
88
|
+
reposity.name = 'tspace4'
|
|
89
|
+
reposity.email = 'tspace4@gmail.com'
|
|
90
|
+
await reposity.save()
|
|
91
|
+
const { result } = reposity
|
|
92
|
+
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
93
|
+
```
|
|
94
|
+
Running A Update Query
|
|
61
95
|
```js
|
|
62
|
-
|
|
63
|
-
(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
u.email = 'tspace@gmail.com'
|
|
78
|
-
await u.save()
|
|
79
|
-
const { result : user } = u
|
|
80
|
-
/* or const user = await u.save() */
|
|
81
|
-
console.log(user)
|
|
82
|
-
})()
|
|
96
|
+
const user = await new DB('users').where('id',1)
|
|
97
|
+
.update({
|
|
98
|
+
name : 'tspace1**',
|
|
99
|
+
email : 'tspace1@gmail.com'
|
|
100
|
+
}).save()
|
|
101
|
+
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
102
|
+
|
|
103
|
+
+--------------------------------------------------------------------------+
|
|
104
|
+
|
|
105
|
+
const reposity = new DB('users').where('id',1)
|
|
106
|
+
reposity.name = 'tspace1++'
|
|
107
|
+
reposity.email = 'tspace1++@gmail.com'
|
|
108
|
+
await reposity.save()
|
|
109
|
+
const { result } = reposity
|
|
110
|
+
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
83
111
|
```
|
|
84
|
-
|
|
112
|
+
Running A Delete Query
|
|
85
113
|
```js
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
await new User().where('id',1).delete()
|
|
89
|
-
})()
|
|
114
|
+
const deleted = await new DB('users').where('id',1).delete()
|
|
115
|
+
// deleted => true
|
|
90
116
|
```
|
|
117
|
+
## Database Transactions
|
|
118
|
+
|
|
119
|
+
Within a database transaction, you may use the:
|
|
91
120
|
|
|
92
|
-
## Transactions & Rollback
|
|
93
121
|
```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
|
-
} catch (err) {
|
|
119
|
-
const rollback = await transaction.rollback()
|
|
120
|
-
console.log(rollback ,'rollback !')
|
|
121
|
-
}
|
|
122
|
-
})()
|
|
122
|
+
const transaction = await new DB().beginTransaction()
|
|
123
|
+
try {
|
|
124
|
+
const user = await new DB('users').create({
|
|
125
|
+
name : 'tspace5',
|
|
126
|
+
email : 'tspace5@gmail.com'
|
|
127
|
+
})
|
|
128
|
+
.save(transaction)
|
|
129
|
+
// user => { id : 5 , username : 'tspace5', email : 'tspace5@gmail.com'}
|
|
130
|
+
|
|
131
|
+
const userSecond = await new DB('users').create({
|
|
132
|
+
name : 'tspace6',
|
|
133
|
+
email : 'tspace6@gmail.com'
|
|
134
|
+
})
|
|
135
|
+
.save(transaction)
|
|
136
|
+
// userSecond => { id : 6 , username : 'tspace6', email : 'tspace6@gmail.com'}
|
|
137
|
+
|
|
138
|
+
throw new Error('try to using transaction')
|
|
139
|
+
|
|
140
|
+
} catch (err) {
|
|
141
|
+
const rollback = await transaction.rollback()
|
|
142
|
+
// rollback => true
|
|
143
|
+
// * rollback data user, userSecond in your database
|
|
144
|
+
}
|
|
123
145
|
```
|
|
124
|
-
|
|
125
|
-
## Model Conventions
|
|
126
|
-
basic model class and discuss some relations:
|
|
127
146
|
|
|
147
|
+
## Connection
|
|
148
|
+
When establishing a connection, you may establish options is this:
|
|
128
149
|
```js
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
const users = await new DB('users')
|
|
151
|
+
.connection({
|
|
152
|
+
host: 'localhost..',
|
|
153
|
+
port : 3306,
|
|
154
|
+
database: 'database'
|
|
155
|
+
username: 'username',
|
|
156
|
+
password: 'password',
|
|
157
|
+
})
|
|
158
|
+
.findMany()
|
|
159
|
+
// users => [{ .... }]
|
|
160
|
+
```
|
|
133
161
|
|
|
162
|
+
## Backup
|
|
163
|
+
Backup database, you may backup is this:
|
|
164
|
+
```js
|
|
134
165
|
/**
|
|
135
|
-
Folder directory example
|
|
136
|
-
- App
|
|
137
|
-
- Model
|
|
138
|
-
Post.ts
|
|
139
|
-
User.ts
|
|
140
|
-
Comment.ts
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
(async () => {
|
|
144
|
-
const users = await new User()
|
|
145
|
-
.with('posts','comments') /* relations -> hasMany: posts & comments */
|
|
146
|
-
.withQuery('posts', (query) => query.with('user')) /* relation -> belongsTo: post by user */
|
|
147
|
-
.withQuery('comments', (query) => query.with('user','post')) /* relation -> belongsTo: comment by user? & comment in post*/
|
|
148
|
-
.findMany()
|
|
149
|
-
|
|
150
|
-
console.log(users)
|
|
151
|
-
})()
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
166
|
*
|
|
155
|
-
*
|
|
156
|
-
*/
|
|
167
|
+
* @param conection defalut current connection
|
|
168
|
+
*/
|
|
169
|
+
const backup = await new DB().backup({
|
|
170
|
+
database: 'try-to-backup',
|
|
171
|
+
connection ?: {
|
|
172
|
+
host: 'localhost..',
|
|
173
|
+
port : 3306,
|
|
174
|
+
database: 'database'
|
|
175
|
+
username: 'username',
|
|
176
|
+
password: 'password',
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
/**
|
|
180
|
+
*
|
|
181
|
+
* @param conection defalut current connection
|
|
182
|
+
*/
|
|
183
|
+
const backupToFile = await new DB().backupToFile({
|
|
184
|
+
database: 'try-to-backup',
|
|
185
|
+
filePath: 'backup.sql',
|
|
186
|
+
connection ?: {
|
|
187
|
+
host: 'localhost..',
|
|
188
|
+
port : 3306,
|
|
189
|
+
database: 'database'
|
|
190
|
+
username: 'username',
|
|
191
|
+
password: 'password',
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
// backupToFile => backup.sql
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Generating Model Classes
|
|
198
|
+
To get started, let's install npm install tspace-mysql -g
|
|
199
|
+
you may use the make:model command to generate a new model:
|
|
200
|
+
|
|
201
|
+
```sh
|
|
202
|
+
npm install tspace-mysql -g
|
|
203
|
+
tspace-mysql make:model <model name> --dir=<directory>
|
|
204
|
+
|
|
205
|
+
# tspace-mysql make:model User --dir=App/Models
|
|
206
|
+
# => App/Models/User.ts
|
|
207
|
+
```
|
|
208
|
+
## Model Conventions
|
|
209
|
+
Models generated by the make:model command will be placed in the specific directory.
|
|
210
|
+
Let's examine a basic model class:
|
|
211
|
+
|
|
212
|
+
```js
|
|
157
213
|
import { Model } from 'tspace-mysql'
|
|
214
|
+
class User extends Model {
|
|
215
|
+
constructor(){
|
|
216
|
+
super()
|
|
217
|
+
this.useTimestamp()
|
|
218
|
+
/*
|
|
219
|
+
* the "snake case", plural name of the class will be used as the table name
|
|
220
|
+
*
|
|
221
|
+
* @param {string} name The table associated with the model.
|
|
222
|
+
*/
|
|
223
|
+
this.useTable('users')
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
export { User }
|
|
227
|
+
export default User
|
|
228
|
+
```
|
|
229
|
+
## Relationships
|
|
230
|
+
Relationships are defined as methods on your Model classes
|
|
231
|
+
Let's examine a basic relations :
|
|
232
|
+
|
|
233
|
+
## One To One
|
|
234
|
+
A one-to-one relationship is used to define relationships where a single model is the parent to one child models
|
|
235
|
+
```js
|
|
236
|
+
import { Model } from 'tspace-mysql'
|
|
237
|
+
import Phone from '../Phone'
|
|
158
238
|
class User extends Model {
|
|
159
239
|
constructor(){
|
|
160
240
|
super()
|
|
161
|
-
this.
|
|
162
|
-
|
|
241
|
+
this.useTimestamp()
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* @hasOne Get the phone associated with the user.
|
|
245
|
+
* @relationship users.id -> phones.user_id
|
|
246
|
+
*/
|
|
247
|
+
this.hasOne({ name : 'phone' , model : Phone })
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* @hasOneQuery Get the phone associated with the user. using function callback
|
|
251
|
+
* @function
|
|
252
|
+
*/
|
|
253
|
+
phone (callback ?: Function) {
|
|
254
|
+
return this.hasOneQuery({ model : Phone }, (query) => {
|
|
255
|
+
return callback ? callback(query) : query
|
|
256
|
+
})
|
|
163
257
|
}
|
|
164
|
-
}
|
|
258
|
+
}
|
|
165
259
|
export default User
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
168
|
-
* Post.ts
|
|
169
|
-
*/
|
|
170
|
-
import { Model } from 'tspace-mysql'
|
|
171
260
|
|
|
261
|
+
+--------------------------------------------------------------------------+
|
|
262
|
+
|
|
263
|
+
import User from '../User'
|
|
264
|
+
const user = await new User().with('brand').findOne()
|
|
265
|
+
// user?.phone => {...}
|
|
266
|
+
const userUsingFunction = await new User().phone().findOne()
|
|
267
|
+
// userUsingFunction?.phone => {...}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## One To Many
|
|
271
|
+
A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models.
|
|
272
|
+
```js
|
|
273
|
+
import { Model } from 'tspace-mysql'
|
|
274
|
+
import Comment from '../Comment'
|
|
172
275
|
class Post extends Model {
|
|
173
276
|
constructor(){
|
|
174
277
|
super()
|
|
175
|
-
this.
|
|
278
|
+
this.useTimestamp()
|
|
279
|
+
/**
|
|
280
|
+
*
|
|
281
|
+
* @hasMany Get the comments for the post.
|
|
282
|
+
* @relationship posts.id -> comments.post_id
|
|
283
|
+
*/
|
|
176
284
|
this.hasMany({ name : 'comments' , model : Comment })
|
|
177
285
|
}
|
|
178
|
-
|
|
286
|
+
/**
|
|
287
|
+
* @hasManyQuery Get the comments for the post. using function callback
|
|
288
|
+
* @function
|
|
289
|
+
*/
|
|
290
|
+
comments (callback ?: Function) {
|
|
291
|
+
return this.hasManyQuery({ model : Comment }, (query) => {
|
|
292
|
+
return callback ? callback(query) : query
|
|
293
|
+
})
|
|
294
|
+
}
|
|
295
|
+
}
|
|
179
296
|
export default Post
|
|
180
297
|
|
|
181
|
-
|
|
182
|
-
*
|
|
183
|
-
* Comment.ts
|
|
184
|
-
*/
|
|
298
|
+
+--------------------------------------------------------------------------+
|
|
185
299
|
|
|
186
|
-
import
|
|
300
|
+
import Post from '../Post'
|
|
301
|
+
const posts = await new Post().with('comments').findOne()
|
|
302
|
+
// posts?.comments => [{...}]
|
|
303
|
+
const postsUsingFunction = await new Post().comments().findOne()
|
|
304
|
+
// postsUsingFunction?.comments => [{...}]
|
|
305
|
+
```
|
|
187
306
|
|
|
188
|
-
|
|
307
|
+
## One To One & One To Many (Inverse) / Belongs To
|
|
308
|
+
```js
|
|
309
|
+
import { Model } from 'tspace-mysql'
|
|
310
|
+
import User from '../User'
|
|
311
|
+
class Phone extends Model {
|
|
189
312
|
constructor(){
|
|
190
313
|
super()
|
|
191
|
-
this.
|
|
192
|
-
|
|
314
|
+
this.useTimestamp()
|
|
315
|
+
/**
|
|
316
|
+
*
|
|
317
|
+
* @belongsTo Get the user that owns the phone.
|
|
318
|
+
* @relationship phones.user_id -> users.id
|
|
319
|
+
*/
|
|
320
|
+
this.belognsTo({ name : 'user' , model : User })
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
324
|
+
* @function
|
|
325
|
+
*/
|
|
326
|
+
user (callback ?: Function) {
|
|
327
|
+
return this.belongsToQuery({ model : User }, (query) => {
|
|
328
|
+
return callback ? callback(query) : query
|
|
329
|
+
})
|
|
193
330
|
}
|
|
194
|
-
}
|
|
195
|
-
export default
|
|
331
|
+
}
|
|
332
|
+
export default Phone
|
|
333
|
+
|
|
334
|
+
+--------------------------------------------------------------------------+
|
|
335
|
+
|
|
336
|
+
import Phone from '../Phone'
|
|
337
|
+
const phone = await new Phone().with('user').findOne()
|
|
338
|
+
// phone?.user => {...}
|
|
339
|
+
const phoneUsingFunction = await new Phone().user().findOne()
|
|
340
|
+
// phoneUsingFunction?.user => {...}
|
|
196
341
|
```
|
|
197
342
|
|
|
198
|
-
##
|
|
343
|
+
## Many To Many
|
|
344
|
+
Many-to-many relations are slightly more complicated than hasOne and hasMany relationships.
|
|
345
|
+
```js
|
|
346
|
+
import { Model } from 'tspace-mysql'
|
|
347
|
+
import Role from '../Role'
|
|
348
|
+
class User extends Model {
|
|
349
|
+
constructor(){
|
|
350
|
+
super()
|
|
351
|
+
this.useTimestamp()
|
|
352
|
+
/**
|
|
353
|
+
*
|
|
354
|
+
* @belongsToMany Get The roles that belong to the user.
|
|
355
|
+
* @relationship users.id , roles.id => role_user.user_id , role_user.role_id
|
|
356
|
+
*/
|
|
357
|
+
this.belognsToMany({ name : 'roles' , model : Role })
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* @belongsToQuery Get the user that owns the phone.. using function callback
|
|
361
|
+
* @function
|
|
362
|
+
*/
|
|
363
|
+
roles (callback ?: Function) {
|
|
364
|
+
return this.belongsToManyQuery({ model : Role }, (query) => {
|
|
365
|
+
return callback ? callback(query) : query
|
|
366
|
+
})
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
export default User
|
|
370
|
+
|
|
371
|
+
+--------------------------------------------------------------------------+
|
|
372
|
+
|
|
373
|
+
import User from '../User'
|
|
374
|
+
const user = await new User().with('roles').findOne()
|
|
375
|
+
// user?.roles => [{...}]
|
|
376
|
+
const userUsingFunction = await new User().roles().findOne()
|
|
377
|
+
// user?.roles => [{...}]
|
|
378
|
+
```
|
|
379
|
+
## Query Builder
|
|
199
380
|
method chaining for queries
|
|
200
381
|
```js
|
|
201
382
|
where(column , operator , value)
|
|
@@ -210,7 +391,6 @@ whereNull(column)
|
|
|
210
391
|
whereNotNull(column)
|
|
211
392
|
whereBetween (column , [value1 , value2])
|
|
212
393
|
whereSubQuery(colmn , rawSQL)
|
|
213
|
-
|
|
214
394
|
select(column1 ,column2 ,...N)
|
|
215
395
|
except(column1 ,column2 ,...N)
|
|
216
396
|
only(column1 ,column2 ,...N)
|
|
@@ -224,31 +404,43 @@ having (condition)
|
|
|
224
404
|
latest (column)
|
|
225
405
|
oldest (column)
|
|
226
406
|
groupBy (column)
|
|
227
|
-
|
|
228
407
|
create(objects)
|
|
229
408
|
createMultiple(array objects)
|
|
230
409
|
update (objects)
|
|
231
410
|
createNotExists(objects)
|
|
232
411
|
updateOrCreate (objects)
|
|
412
|
+
connection(options)
|
|
413
|
+
backup({ database , connection })
|
|
414
|
+
backupToFile({ filePath, database , connection })
|
|
415
|
+
|
|
233
416
|
/**
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
|
|
417
|
+
* registry relation in your models
|
|
418
|
+
* @relationship
|
|
419
|
+
*/
|
|
420
|
+
hasOne({ name , model , localKey , foreignKey , freezeTable , as }
|
|
421
|
+
hasMany({ name , model , localKey , foreignKey , freezeTable , as }
|
|
422
|
+
belongsTo({ name , model , localKey , foreignKey , freezeTable , as }
|
|
423
|
+
belongsToMany({ name , model , localKey , foreignKey , freezeTable , as }
|
|
424
|
+
/**
|
|
425
|
+
* @relation using registry in your models
|
|
237
426
|
*/
|
|
238
427
|
with(name1 , name2,...nameN)
|
|
428
|
+
/**
|
|
429
|
+
* @relation using registry in your models. if exists child data remove this parent data
|
|
430
|
+
*/
|
|
239
431
|
withExists(name1 , name2,...nameN)
|
|
240
|
-
|
|
432
|
+
/**
|
|
433
|
+
* @relation call a relation in registry, callback query of parent data
|
|
434
|
+
*/
|
|
435
|
+
withQuery('relation registry',(callback query))
|
|
241
436
|
|
|
242
437
|
/**
|
|
243
438
|
* queries statements
|
|
244
|
-
*
|
|
245
|
-
* @exec statements
|
|
439
|
+
* @exec statements
|
|
246
440
|
*/
|
|
247
441
|
findMany()
|
|
248
442
|
findOne()
|
|
249
443
|
find(id)
|
|
250
|
-
first()
|
|
251
|
-
get()
|
|
252
444
|
delelte()
|
|
253
445
|
exists ()
|
|
254
446
|
onlyTrashed()
|
|
@@ -266,61 +458,64 @@ save() /*for action statements insert update or delete */
|
|
|
266
458
|
```
|
|
267
459
|
|
|
268
460
|
## Cli
|
|
269
|
-
npm install tspace-mysql -g
|
|
270
|
-
|
|
461
|
+
To get started, let's install npm install tspace-mysql -g
|
|
462
|
+
you may use a basic cli :
|
|
271
463
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
*
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
--type=js /* extension js default ts */
|
|
286
|
-
--dir=directory /* find migrate in directory */
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
tspace-mysql make:model User --m --dir=App/Models
|
|
290
|
-
```js
|
|
291
|
-
/* Ex folder
|
|
464
|
+
## Make Model
|
|
465
|
+
Command will be placed Model in the specific directory
|
|
466
|
+
```sh
|
|
467
|
+
tspace-mysql make:model <MODEL NAME> --m --dir=.... --type=....
|
|
468
|
+
/**
|
|
469
|
+
* @options
|
|
470
|
+
* --m // created scheme table for migrate
|
|
471
|
+
* --dir=directory // created model in directory
|
|
472
|
+
* --type=js // extension js default ts
|
|
473
|
+
*/
|
|
474
|
+
|
|
475
|
+
tspace-mysql make:model User --m --dir=app/Models
|
|
476
|
+
// =>
|
|
292
477
|
- node_modules
|
|
293
|
-
-
|
|
478
|
+
- app
|
|
294
479
|
- Models
|
|
295
480
|
User.ts
|
|
296
481
|
*/
|
|
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
482
|
```
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
483
|
+
|
|
484
|
+
## Make Migration
|
|
485
|
+
Command will be placed Migration in the specific directory
|
|
486
|
+
```sh
|
|
487
|
+
tspace-mysql make:migration <TABLE NAME>
|
|
488
|
+
/**
|
|
489
|
+
* @options
|
|
490
|
+
* --type=js /* extension js default ts */
|
|
491
|
+
* --dir=directory /* created table in directory */
|
|
492
|
+
*/
|
|
493
|
+
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
494
|
+
// =>
|
|
316
495
|
- node_modules
|
|
317
|
-
-
|
|
496
|
+
- app
|
|
318
497
|
- Models
|
|
319
|
-
-
|
|
498
|
+
- Migrations
|
|
320
499
|
create_users_table.ts
|
|
321
500
|
User.ts
|
|
322
501
|
*/
|
|
323
|
-
|
|
502
|
+
```
|
|
503
|
+
## Migrate
|
|
504
|
+
```sh
|
|
505
|
+
tspace-mysql migrate <FOLDER> --type=...
|
|
506
|
+
/**
|
|
507
|
+
* @options
|
|
508
|
+
*--type=js /* extension js default ts */
|
|
509
|
+
*--dir=directory /* find migrate in directory */
|
|
510
|
+
*/
|
|
511
|
+
|
|
512
|
+
tspace-mysql migrate --dir=App/Models/Migrations
|
|
513
|
+
// => migrate all schema table in folder into database
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
## Blueprint
|
|
517
|
+
Schema table created by command make:migration, you may use the:
|
|
518
|
+
```js
|
|
324
519
|
import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
325
520
|
(async () => {
|
|
326
521
|
await new Schema().table('users',{
|
|
@@ -333,16 +528,11 @@ import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
|
333
528
|
updated_at : new Blueprint().null().timestamp()
|
|
334
529
|
})
|
|
335
530
|
/**
|
|
336
|
-
*
|
|
337
|
-
* @Faker data
|
|
531
|
+
*
|
|
532
|
+
* @Faker fake data 5 raw
|
|
338
533
|
* await new DB().table('users').faker(5)
|
|
339
534
|
*/
|
|
340
535
|
})()
|
|
341
|
-
```
|
|
342
|
-
tspace-mysql migrate --dir=App/Models/Migrations
|
|
343
|
-
/* migrate all table in folder into database */
|
|
344
|
-
```js
|
|
345
|
-
* Blueprint method
|
|
346
536
|
/**
|
|
347
537
|
*
|
|
348
538
|
* @Types
|
|
@@ -376,4 +566,4 @@ primary()
|
|
|
376
566
|
default (string)
|
|
377
567
|
defaultTimestamp ()
|
|
378
568
|
autoIncrement ()
|
|
379
|
-
```
|
|
569
|
+
```
|