tspace-mysql 1.0.7 → 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 +88 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,17 +12,26 @@ 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
|
|
18
17
|
- [Configuration](#configuration)
|
|
19
18
|
- [Running Queries](#running-queryies)
|
|
20
19
|
- [Database Transactions](#database-transactions)
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
20
|
+
- [Connection](#connection)
|
|
21
|
+
- [Backup](#backup)
|
|
23
22
|
- [Generating Model Classes](#generating-model-classes)
|
|
24
23
|
- [Model Conventions](#model-conventions)
|
|
25
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)
|
|
26
35
|
|
|
27
36
|
## Configuration
|
|
28
37
|
Created your environment variables is to use a .env file, you may establish a connection is this:
|
|
@@ -72,7 +81,9 @@ const user = await new DB('users').create({
|
|
|
72
81
|
email : 'tspace3@gmail.com'
|
|
73
82
|
}).save()
|
|
74
83
|
// user => { id : 3 , username : 'tspace3', email : 'tspace3@gmail.com'}
|
|
75
|
-
|
|
84
|
+
|
|
85
|
+
+--------------------------------------------------------------------------+
|
|
86
|
+
|
|
76
87
|
const reposity = new DB('users')
|
|
77
88
|
reposity.name = 'tspace4'
|
|
78
89
|
reposity.email = 'tspace4@gmail.com'
|
|
@@ -88,7 +99,9 @@ const user = await new DB('users').where('id',1)
|
|
|
88
99
|
email : 'tspace1@gmail.com'
|
|
89
100
|
}).save()
|
|
90
101
|
// user => { id : 1 , username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
91
|
-
|
|
102
|
+
|
|
103
|
+
+--------------------------------------------------------------------------+
|
|
104
|
+
|
|
92
105
|
const reposity = new DB('users').where('id',1)
|
|
93
106
|
reposity.name = 'tspace1++'
|
|
94
107
|
reposity.email = 'tspace1++@gmail.com'
|
|
@@ -102,7 +115,9 @@ const deleted = await new DB('users').where('id',1).delete()
|
|
|
102
115
|
// deleted => true
|
|
103
116
|
```
|
|
104
117
|
## Database Transactions
|
|
118
|
+
|
|
105
119
|
Within a database transaction, you may use the:
|
|
120
|
+
|
|
106
121
|
```js
|
|
107
122
|
const transaction = await new DB().beginTransaction()
|
|
108
123
|
try {
|
|
@@ -128,6 +143,57 @@ try {
|
|
|
128
143
|
// * rollback data user, userSecond in your database
|
|
129
144
|
}
|
|
130
145
|
```
|
|
146
|
+
|
|
147
|
+
## Connection
|
|
148
|
+
When establishing a connection, you may establish options is this:
|
|
149
|
+
```js
|
|
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
|
+
```
|
|
161
|
+
|
|
162
|
+
## Backup
|
|
163
|
+
Backup database, you may backup is this:
|
|
164
|
+
```js
|
|
165
|
+
/**
|
|
166
|
+
*
|
|
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
|
+
|
|
131
197
|
## Generating Model Classes
|
|
132
198
|
To get started, let's install npm install tspace-mysql -g
|
|
133
199
|
you may use the make:model command to generate a new model:
|
|
@@ -191,7 +257,9 @@ class User extends Model {
|
|
|
191
257
|
}
|
|
192
258
|
}
|
|
193
259
|
export default User
|
|
194
|
-
|
|
260
|
+
|
|
261
|
+
+--------------------------------------------------------------------------+
|
|
262
|
+
|
|
195
263
|
import User from '../User'
|
|
196
264
|
const user = await new User().with('brand').findOne()
|
|
197
265
|
// user?.phone => {...}
|
|
@@ -226,7 +294,9 @@ class Post extends Model {
|
|
|
226
294
|
}
|
|
227
295
|
}
|
|
228
296
|
export default Post
|
|
229
|
-
|
|
297
|
+
|
|
298
|
+
+--------------------------------------------------------------------------+
|
|
299
|
+
|
|
230
300
|
import Post from '../Post'
|
|
231
301
|
const posts = await new Post().with('comments').findOne()
|
|
232
302
|
// posts?.comments => [{...}]
|
|
@@ -260,7 +330,9 @@ class Phone extends Model {
|
|
|
260
330
|
}
|
|
261
331
|
}
|
|
262
332
|
export default Phone
|
|
263
|
-
|
|
333
|
+
|
|
334
|
+
+--------------------------------------------------------------------------+
|
|
335
|
+
|
|
264
336
|
import Phone from '../Phone'
|
|
265
337
|
const phone = await new Phone().with('user').findOne()
|
|
266
338
|
// phone?.user => {...}
|
|
@@ -295,14 +367,16 @@ class User extends Model {
|
|
|
295
367
|
}
|
|
296
368
|
}
|
|
297
369
|
export default User
|
|
298
|
-
|
|
370
|
+
|
|
371
|
+
+--------------------------------------------------------------------------+
|
|
372
|
+
|
|
299
373
|
import User from '../User'
|
|
300
374
|
const user = await new User().with('roles').findOne()
|
|
301
375
|
// user?.roles => [{...}]
|
|
302
376
|
const userUsingFunction = await new User().roles().findOne()
|
|
303
377
|
// user?.roles => [{...}]
|
|
304
378
|
```
|
|
305
|
-
## Query Builder
|
|
379
|
+
## Query Builder
|
|
306
380
|
method chaining for queries
|
|
307
381
|
```js
|
|
308
382
|
where(column , operator , value)
|
|
@@ -317,7 +391,6 @@ whereNull(column)
|
|
|
317
391
|
whereNotNull(column)
|
|
318
392
|
whereBetween (column , [value1 , value2])
|
|
319
393
|
whereSubQuery(colmn , rawSQL)
|
|
320
|
-
|
|
321
394
|
select(column1 ,column2 ,...N)
|
|
322
395
|
except(column1 ,column2 ,...N)
|
|
323
396
|
only(column1 ,column2 ,...N)
|
|
@@ -331,12 +404,14 @@ having (condition)
|
|
|
331
404
|
latest (column)
|
|
332
405
|
oldest (column)
|
|
333
406
|
groupBy (column)
|
|
334
|
-
|
|
335
407
|
create(objects)
|
|
336
408
|
createMultiple(array objects)
|
|
337
409
|
update (objects)
|
|
338
410
|
createNotExists(objects)
|
|
339
411
|
updateOrCreate (objects)
|
|
412
|
+
connection(options)
|
|
413
|
+
backup({ database , connection })
|
|
414
|
+
backupToFile({ filePath, database , connection })
|
|
340
415
|
|
|
341
416
|
/**
|
|
342
417
|
* registry relation in your models
|
|
@@ -438,7 +513,7 @@ tspace-mysql migrate --dir=App/Models/Migrations
|
|
|
438
513
|
// => migrate all schema table in folder into database
|
|
439
514
|
```
|
|
440
515
|
|
|
441
|
-
## Blueprint
|
|
516
|
+
## Blueprint
|
|
442
517
|
Schema table created by command make:migration, you may use the:
|
|
443
518
|
```js
|
|
444
519
|
import { Schema , Blueprint , DB } from 'tspace-mysql'
|