tspace-mysql 1.4.8 → 1.5.0
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 +248 -75
- package/build/cli/generate/make.js +145 -0
- package/build/cli/generate/model.js +20 -0
- package/build/cli/generate/modelDecorator.d.ts +2 -0
- package/build/cli/generate/modelDecorator.js +15 -0
- package/{dist → build}/cli/index.js +4 -1
- package/{dist → build}/lib/Interface.d.ts +21 -1
- package/{dist → build}/lib/constants/index.js +10 -3
- package/{dist → build}/lib/tspace/Abstracts/AbstractBuilder.d.ts +1 -1
- package/{dist → build}/lib/tspace/Abstracts/AbstractModel.d.ts +25 -1
- package/{dist → build}/lib/tspace/Blueprint.d.ts +8 -4
- package/{dist → build}/lib/tspace/Blueprint.js +30 -18
- package/{dist → build}/lib/tspace/Builder.d.ts +5 -79
- package/{dist → build}/lib/tspace/Builder.js +54 -316
- package/{dist → build}/lib/tspace/DB.d.ts +179 -2
- package/build/lib/tspace/DB.js +766 -0
- package/build/lib/tspace/Decorator.d.ts +26 -0
- package/build/lib/tspace/Decorator.js +174 -0
- package/{dist → build}/lib/tspace/Handlers/Relation.js +8 -9
- package/{dist → build}/lib/tspace/Handlers/State.js +4 -3
- package/{dist → build}/lib/tspace/Model.d.ts +218 -6
- package/{dist → build}/lib/tspace/Model.js +819 -63
- package/{dist → build}/lib/tspace/Schema.js +1 -3
- package/{dist → build}/lib/tspace/index.d.ts +2 -0
- package/{dist → build}/lib/tspace/index.js +17 -1
- package/{dist → build}/lib/utils/index.js +1 -0
- package/build/tests/01-Pool.test.d.ts +1 -0
- package/build/tests/01-Pool.test.js +45 -0
- package/build/tests/02-DB.test.d.ts +1 -0
- package/build/tests/02-DB.test.js +109 -0
- package/build/tests/03-Model.test.d.ts +1 -0
- package/build/tests/03-Model.test.js +73 -0
- package/package.json +18 -7
- package/dist/cli/generate/make.js +0 -83
- package/dist/cli/generate/model.js +0 -37
- package/dist/lib/tspace/DB.js +0 -340
- /package/{dist → build}/cli/dump/db.d.ts +0 -0
- /package/{dist → build}/cli/dump/db.js +0 -0
- /package/{dist → build}/cli/dump/table.d.ts +0 -0
- /package/{dist → build}/cli/dump/table.js +0 -0
- /package/{dist → build}/cli/generate/make.d.ts +0 -0
- /package/{dist → build}/cli/generate/model.d.ts +0 -0
- /package/{dist → build}/cli/index.d.ts +0 -0
- /package/{dist → build}/cli/migrate/make.d.ts +0 -0
- /package/{dist → build}/cli/migrate/make.js +0 -0
- /package/{dist → build}/cli/models/make.d.ts +0 -0
- /package/{dist → build}/cli/models/make.js +0 -0
- /package/{dist → build}/cli/models/model.d.ts +0 -0
- /package/{dist → build}/cli/models/model.js +0 -0
- /package/{dist → build}/cli/query/index.d.ts +0 -0
- /package/{dist → build}/cli/query/index.js +0 -0
- /package/{dist → build}/cli/tables/make.d.ts +0 -0
- /package/{dist → build}/cli/tables/make.js +0 -0
- /package/{dist → build}/cli/tables/table.d.ts +0 -0
- /package/{dist → build}/cli/tables/table.js +0 -0
- /package/{dist → build}/lib/Interface.js +0 -0
- /package/{dist → build}/lib/connection/index.d.ts +0 -0
- /package/{dist → build}/lib/connection/index.js +0 -0
- /package/{dist → build}/lib/connection/options.d.ts +0 -0
- /package/{dist → build}/lib/connection/options.js +0 -0
- /package/{dist → build}/lib/constants/index.d.ts +0 -0
- /package/{dist → build}/lib/index.d.ts +0 -0
- /package/{dist → build}/lib/index.js +0 -0
- /package/{dist → build}/lib/tspace/Abstracts/AbstractBuilder.js +0 -0
- /package/{dist → build}/lib/tspace/Abstracts/AbstractDB.d.ts +0 -0
- /package/{dist → build}/lib/tspace/Abstracts/AbstractDB.js +0 -0
- /package/{dist → build}/lib/tspace/Abstracts/AbstractModel.js +0 -0
- /package/{dist → build}/lib/tspace/Handlers/Proxy.d.ts +0 -0
- /package/{dist → build}/lib/tspace/Handlers/Proxy.js +0 -0
- /package/{dist → build}/lib/tspace/Handlers/Relation.d.ts +0 -0
- /package/{dist → build}/lib/tspace/Handlers/State.d.ts +0 -0
- /package/{dist → build}/lib/tspace/Logger.d.ts +0 -0
- /package/{dist → build}/lib/tspace/Logger.js +0 -0
- /package/{dist → build}/lib/tspace/Schema.d.ts +0 -0
- /package/{dist → build}/lib/utils/index.d.ts +0 -0
package/README.md
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com)
|
|
4
4
|
[](https://www.npmjs.com)
|
|
5
5
|
|
|
6
|
-
tspace-mysql is an ORM
|
|
7
|
-
Its always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.
|
|
6
|
+
tspace-mysql is an Object-Relational Mapping (ORM) tool designed to run seamlessly in Node.js and is fully compatible with TypeScript. It consistently supports the latest features in both TypeScript and JavaScript, providing additional functionalities to enhance your development experience.
|
|
8
7
|
|
|
9
8
|
## Install
|
|
10
9
|
|
|
@@ -19,6 +18,7 @@ npm install tspace-mysql --save
|
|
|
19
18
|
- [Database Transactions](#database-transactions)
|
|
20
19
|
- [Connection](#connection)
|
|
21
20
|
- [Backup](#backup)
|
|
21
|
+
- [Injection](#injection)
|
|
22
22
|
- [Generating Model Classes](#generating-model-classes)
|
|
23
23
|
- [Model Conventions](#model-conventions)
|
|
24
24
|
- [Relationships](#relationships)
|
|
@@ -29,6 +29,7 @@ npm install tspace-mysql --save
|
|
|
29
29
|
- [Deeply Nested Relations](#deeply-nested-relations)
|
|
30
30
|
- [Relation Exists](#relation-exists)
|
|
31
31
|
- [Built in Relation Functions](#built-in-relation-functions)
|
|
32
|
+
- [Decorator](#decorator)
|
|
32
33
|
- [Schema Model](#schema-model)
|
|
33
34
|
- [Validation](#validation)
|
|
34
35
|
- [Sync](#sync)
|
|
@@ -43,7 +44,7 @@ npm install tspace-mysql --save
|
|
|
43
44
|
- [Blueprint](#blueprint)
|
|
44
45
|
|
|
45
46
|
## Configuration
|
|
46
|
-
|
|
47
|
+
To establish a connection, the recommended method for creating your environment variables is by using a '.env' file. using the following:
|
|
47
48
|
```js
|
|
48
49
|
DB_HOST = localhost
|
|
49
50
|
DB_PORT = 3306
|
|
@@ -59,7 +60,7 @@ DB_DATABASE = database
|
|
|
59
60
|
* DB_DATE_STRINGS = true
|
|
60
61
|
*/
|
|
61
62
|
```
|
|
62
|
-
|
|
63
|
+
You can also create a file named 'db.tspace' to configure the connection. using the following:
|
|
63
64
|
```js
|
|
64
65
|
source db {
|
|
65
66
|
host = localhost
|
|
@@ -77,7 +78,7 @@ source db {
|
|
|
77
78
|
|
|
78
79
|
```
|
|
79
80
|
## Running Queries
|
|
80
|
-
Once you have configured your database connection, you
|
|
81
|
+
Once you have configured your database connection, you can execute queries using the following:
|
|
81
82
|
```js
|
|
82
83
|
+-------------+--------------+----------------------------+
|
|
83
84
|
| table users |
|
|
@@ -100,50 +101,78 @@ Once you have configured your database connection, you may run queries is this :
|
|
|
100
101
|
|
|
101
102
|
import { DB } from 'tspace-mysql'
|
|
102
103
|
(async () => {
|
|
103
|
-
await new DB('users').findMany()
|
|
104
|
-
|
|
104
|
+
await new DB('users').findMany()
|
|
105
|
+
// SELECT * FROM users => Array
|
|
106
|
+
await new DB('users').findOne()
|
|
107
|
+
// SELECT * FROM users LIMIT 1 => Object
|
|
105
108
|
})()
|
|
106
109
|
|
|
107
110
|
```
|
|
108
111
|
Running A Raw Query
|
|
109
112
|
```js
|
|
110
|
-
const rawQuery = await new DB().query('SELECT * FROM users')
|
|
113
|
+
const rawQuery = await new DB().query('SELECT * FROM users')
|
|
114
|
+
// SELECT * FROM users;
|
|
111
115
|
|
|
112
116
|
```
|
|
113
117
|
Running A Select Query
|
|
114
118
|
```js
|
|
115
119
|
const select = await new DB('users').select('id','username').findOne()
|
|
120
|
+
// SELECT `users`.`id`, `users`.`username` FROM `users` LIMIT 1;
|
|
116
121
|
|
|
117
122
|
const selectRaw = await new DB('users').selectRaw('COUNT(id)').findMany()
|
|
123
|
+
// SELECT COUNT(id) FROM `users`;
|
|
118
124
|
|
|
119
125
|
const selectObject = await new DB('posts')
|
|
120
126
|
.join('posts.user_id', 'users.id')
|
|
121
127
|
.select('posts.*')
|
|
122
|
-
.selectObject({ id : 'users.id', name : 'users.name' , email : 'users.email'},'user')
|
|
128
|
+
.selectObject({ id : 'users.id', name : 'users.name' , email : 'users.email'} , 'user')
|
|
123
129
|
.findOne()
|
|
124
130
|
|
|
131
|
+
// SELECT posts.*, JSON_OBJECT('id' , `users`.`id` , 'name' , `users`.`name` , 'email' , `users`.`email`) AS `user`
|
|
132
|
+
// FROM `posts` INNER JOIN `users` ON `posts`.`user_id` = `users`.`id` LIMIT 1;
|
|
133
|
+
|
|
125
134
|
/**
|
|
126
135
|
* @example except
|
|
127
136
|
*/
|
|
128
|
-
await new DB('users').except('id'
|
|
137
|
+
await new DB('users').except('id').findOne()
|
|
138
|
+
// SELECT `users`.`email`, `users`.`username` FROM `users` LIMIT 1;
|
|
129
139
|
```
|
|
130
140
|
|
|
131
141
|
Running A OrderBy & GroupBy Query
|
|
132
142
|
```js
|
|
133
143
|
await new DB('users').orderBy('id','asc').findOne()
|
|
144
|
+
// SELECT * FROM `users` ORDER BY `id` ASC LIMIT 1;
|
|
145
|
+
|
|
134
146
|
await new DB('users').orderBy('id','desc').findOne()
|
|
147
|
+
// SELECT * FROM `users` ORDER BY `id` DESC LIMIT 1;
|
|
148
|
+
|
|
135
149
|
await new DB('users').oldest('id').findOne()
|
|
150
|
+
// SELECT * FROM `users` ORDER BY `id` ASC LIMIT 1;
|
|
151
|
+
|
|
136
152
|
await new DB('users').latest('id').findOne()
|
|
153
|
+
// SELECT * FROM `users` ORDER BY `id` DESC LIMIT 1;
|
|
137
154
|
|
|
138
155
|
await new DB('users').groupBy('id').findOne()
|
|
139
|
-
|
|
156
|
+
// SELECT * FROM `users` GROUP BY `id` LIMIT 1;
|
|
157
|
+
|
|
158
|
+
await new DB('users').groupBy('id','username').findOne()
|
|
159
|
+
// SELECT * FROM `users` GROUP BY `id`, `username` LIMIT 1;
|
|
160
|
+
|
|
161
|
+
await new DB('users').orderBy('id').groupBy('id','username').findOne()
|
|
162
|
+
// SELECT * FROM `users` GROUP BY `id`, `username` ORDER BY `id` ASC LIMIT 1;
|
|
163
|
+
|
|
140
164
|
```
|
|
141
165
|
|
|
142
166
|
Running A Join Query
|
|
143
167
|
```js
|
|
144
168
|
await new DB('posts').join('posts.user_id' , 'users.id').findMany()
|
|
169
|
+
// SELECT * FROM `posts` INNER JOIN `users` ON `posts`.`user_id` = `users`.`id`;
|
|
170
|
+
|
|
145
171
|
await new DB('posts').leftJoin('posts.user_id' , 'users.id').findMany()
|
|
172
|
+
// SELECT * FROM `posts` LEFT JOIN `users` ON `posts`.`user_id` = `users`.`id`;
|
|
173
|
+
|
|
146
174
|
await new DB('posts').rightJoin('posts.user_id' , 'users.id').findMany()
|
|
175
|
+
// SELECT * FROM `posts` RIGHT JOIN `users` ON `posts`.`user_id` = `users`.`id`;
|
|
147
176
|
```
|
|
148
177
|
|
|
149
178
|
Running A Where Query
|
|
@@ -162,7 +191,7 @@ const whereBetween = await new DB('users').whereBetween('id',[1,2]).findMany()
|
|
|
162
191
|
|
|
163
192
|
const whereSubQuery = await new DB('users').whereSubQuery('id','SELECT id FROM users').findMany()
|
|
164
193
|
// SELECT * FROM `users` WHERE `users`.`id` IN (SELECT id FROM users);
|
|
165
|
-
//
|
|
194
|
+
// also you can use -> await new DB('users').whereSubQuery('id',new DB('users').select('id').toString()).findMany()
|
|
166
195
|
|
|
167
196
|
const whereNull = await new DB('users').whereNull('username').findOne()
|
|
168
197
|
// SELECT * FROM `users` WHERE `users`.`username` IS NULL LIMIT 1;
|
|
@@ -189,7 +218,21 @@ const whereWhenIsFalse = await new DB('users').where('id',1).when(false, (query)
|
|
|
189
218
|
Running A Hook Query
|
|
190
219
|
```js
|
|
191
220
|
const hookResult = (result) => console.log('hook!! result => ',result)
|
|
192
|
-
const user = await new DB('users').where('id',1).hook(hookResult).
|
|
221
|
+
const user = await new DB('users').where('id',1).hook(hookResult).findMany()
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Running A Faker
|
|
225
|
+
```js
|
|
226
|
+
|
|
227
|
+
await new DB('users').faker(10)
|
|
228
|
+
|
|
229
|
+
// custom faker
|
|
230
|
+
await new DB('users').faker(10 , (row , index) => {
|
|
231
|
+
return {
|
|
232
|
+
...row,
|
|
233
|
+
custom : 'custom' + index
|
|
234
|
+
}
|
|
235
|
+
})
|
|
193
236
|
```
|
|
194
237
|
|
|
195
238
|
Running A Insert Query
|
|
@@ -200,19 +243,9 @@ const user = await new DB('users')
|
|
|
200
243
|
email : 'tspace3@gmail.com'
|
|
201
244
|
})
|
|
202
245
|
.save()
|
|
203
|
-
//
|
|
246
|
+
// INSERT INTO `users` (`name`,`email`) VALUES ('tspace3','tspace3@gmail.com');
|
|
204
247
|
|
|
205
248
|
+--------------------------------------------------------------------------+
|
|
206
|
-
|
|
207
|
-
const reposity = new DB('users')
|
|
208
|
-
reposity.name = 'tspace4'
|
|
209
|
-
reposity.email = 'tspace4@gmail.com'
|
|
210
|
-
|
|
211
|
-
await reposity.save()
|
|
212
|
-
|
|
213
|
-
const { result } = reposity
|
|
214
|
-
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
215
|
-
|
|
216
249
|
const users = await new DB('users')
|
|
217
250
|
.createMultiple([
|
|
218
251
|
{
|
|
@@ -230,6 +263,8 @@ const users = await new DB('users')
|
|
|
230
263
|
])
|
|
231
264
|
.save()
|
|
232
265
|
|
|
266
|
+
// INSERT INTO `users` (`name`,`email`) VALUES ('tspace4','tspace4@gmail.com'),('tspace5','tspace5@gmail.com'),('tspace6','tspace6@gmail.com');
|
|
267
|
+
|
|
233
268
|
const users = await new DB('users')
|
|
234
269
|
.where('name','tspace4')
|
|
235
270
|
.where('email','tspace4@gmail.com')
|
|
@@ -238,7 +273,10 @@ const users = await new DB('users')
|
|
|
238
273
|
email : 'tspace4@gmail.com'
|
|
239
274
|
})
|
|
240
275
|
.save()
|
|
241
|
-
// if
|
|
276
|
+
// if exists return null, if not exists created new data
|
|
277
|
+
// SELECT EXISTS(SELECT 1 FROM `users` WHERE `users`.`name` = 'tspace4' AND `users`.`email` = 'tspace4@gmail.com' LIMIT 1) AS 'exists';
|
|
278
|
+
// INSERT INTO `users` (`name`,`email`) VALUES ('tspace4','tspace4@gmail.com');
|
|
279
|
+
|
|
242
280
|
|
|
243
281
|
const users = await new DB('users')
|
|
244
282
|
.where('name','tspace4')
|
|
@@ -249,6 +287,9 @@ const users = await new DB('users')
|
|
|
249
287
|
})
|
|
250
288
|
.save()
|
|
251
289
|
// if has exists return data, if not exists created new data
|
|
290
|
+
// SELECT EXISTS(SELECT 1 FROM `users` WHERE `users`.`name` = 'tspace4' AND `users`.`email` = 'tspace4@gmail.com' LIMIT 1) AS 'exists';
|
|
291
|
+
// SELECT * FROM `users` WHERE `users`.`name` = 'tspace4' AND `users`.`email` = 'tspace4@gmail.com';
|
|
292
|
+
// INSERT INTO `users` (`name`,`email`) VALUES ('tspace4','tspace4@gmail.com');
|
|
252
293
|
|
|
253
294
|
```
|
|
254
295
|
Running A Update Query
|
|
@@ -271,18 +312,6 @@ const user = await new DB('users')
|
|
|
271
312
|
.save()
|
|
272
313
|
// UPDATE `users` SET `name` = CASE WHEN (`name` = "" OR `name` IS NULL) THEN "tspace1**" ELSE `name` END,`email` = 'tspace1@gmail.com' WHERE `users`.`id` = '1' LIMIT 1;
|
|
273
314
|
|
|
274
|
-
|
|
275
|
-
+--------------------------------------------------------------------------+
|
|
276
|
-
|
|
277
|
-
const reposity = new DB('users').where('id',1)
|
|
278
|
-
reposity.name = 'tspace1++'
|
|
279
|
-
reposity.email = 'tspace1++@gmail.com'
|
|
280
|
-
|
|
281
|
-
await reposity.save()
|
|
282
|
-
// UPDATE `users` SET `name` = 'tspace1**',`email` = 'tspace1@gmail.com' WHERE `users`.`id` = '1' LIMIT 1;
|
|
283
|
-
const { result } = reposity
|
|
284
|
-
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
285
|
-
|
|
286
315
|
```
|
|
287
316
|
Running A Update Or Created Query
|
|
288
317
|
```js
|
|
@@ -304,7 +333,7 @@ const deleted = await new DB('users').where('id',1).delete()
|
|
|
304
333
|
```
|
|
305
334
|
## Database Transactions
|
|
306
335
|
|
|
307
|
-
Within a
|
|
336
|
+
Within a database transaction, you can utilize the following:
|
|
308
337
|
|
|
309
338
|
```js
|
|
310
339
|
const connection = await new DB().beginTransaction()
|
|
@@ -317,17 +346,17 @@ try {
|
|
|
317
346
|
await connection.startTransaction()
|
|
318
347
|
|
|
319
348
|
const user = await new User()
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
349
|
+
.create({
|
|
350
|
+
name : `tspace`,
|
|
351
|
+
email : 'tspace@example.com'
|
|
352
|
+
})
|
|
353
|
+
/**
|
|
354
|
+
*
|
|
355
|
+
* bind method for make sure this connection has same transaction in connection
|
|
356
|
+
* @params {Function} connection
|
|
357
|
+
*/
|
|
358
|
+
.bind(connection)
|
|
359
|
+
.save()
|
|
331
360
|
|
|
332
361
|
const posts = await new Post()
|
|
333
362
|
.createMultiple([
|
|
@@ -364,7 +393,7 @@ try {
|
|
|
364
393
|
|
|
365
394
|
```
|
|
366
395
|
## Connection
|
|
367
|
-
When establishing a connection, you
|
|
396
|
+
When establishing a connection, you can specify options as follows:
|
|
368
397
|
```js
|
|
369
398
|
const connection = await new DB().getConnection({
|
|
370
399
|
host: 'localhost',
|
|
@@ -375,24 +404,23 @@ const connection = await new DB().getConnection({
|
|
|
375
404
|
})
|
|
376
405
|
|
|
377
406
|
const users = await new DB('users')
|
|
378
|
-
.bind(connection)
|
|
407
|
+
.bind(connection) // don't forget this
|
|
379
408
|
.findMany()
|
|
380
|
-
// users => [{ .... }]
|
|
381
409
|
```
|
|
382
410
|
|
|
383
411
|
## Backup
|
|
384
|
-
|
|
412
|
+
To backup a database, you can perform the following steps:
|
|
385
413
|
```js
|
|
386
414
|
/**
|
|
387
415
|
*
|
|
388
|
-
* @param
|
|
416
|
+
* @param {string} database Database selected
|
|
417
|
+
* @param {object | null} to defalut new current connection
|
|
389
418
|
*/
|
|
390
419
|
const backup = await new DB().backup({
|
|
391
|
-
|
|
392
|
-
|
|
420
|
+
database: 'try-to-backup', // clone current database to this database
|
|
421
|
+
to ?: {
|
|
393
422
|
host: 'localhost',
|
|
394
423
|
port : 3306,
|
|
395
|
-
database: 'database'
|
|
396
424
|
username: 'username',
|
|
397
425
|
password: 'password',
|
|
398
426
|
}
|
|
@@ -415,11 +443,32 @@ const backupToFile = await new DB().backupToFile({
|
|
|
415
443
|
}
|
|
416
444
|
})
|
|
417
445
|
// backupToFile => backup.sql
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
*
|
|
449
|
+
* @param {string} database new db name
|
|
450
|
+
*/
|
|
451
|
+
await new DB().cloneDB('try-to-clone')
|
|
452
|
+
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## Injection
|
|
456
|
+
The 'tspace-mysql' library is configured to automatically escape SQL injection by default.
|
|
457
|
+
Let's example a escape SQL injection and XSs injection:
|
|
458
|
+
```js
|
|
459
|
+
const input = "admin' OR '1'='1"
|
|
460
|
+
DB.escape(input)
|
|
461
|
+
// admin OR 1=1
|
|
462
|
+
|
|
463
|
+
//XSS
|
|
464
|
+
const input = "text hello!<script>alert('XSS attack');</script>"
|
|
465
|
+
DB.escapeXSS(input)
|
|
466
|
+
// text hello!
|
|
467
|
+
|
|
418
468
|
```
|
|
419
469
|
|
|
420
470
|
## Generating Model Classes
|
|
421
|
-
To get started,
|
|
422
|
-
you may use the make:model command to generate a new model:
|
|
471
|
+
To get started, install the 'tspace-mysql' package globally using the following npm command:
|
|
423
472
|
|
|
424
473
|
```js
|
|
425
474
|
/**
|
|
@@ -435,14 +484,41 @@ npm install tspace-mysql -g
|
|
|
435
484
|
tspace-mysql make:model <model name> --dir=< directory >
|
|
436
485
|
|
|
437
486
|
# tspace-mysql make:model User --dir=App/Models
|
|
438
|
-
#
|
|
487
|
+
# App/Models/User.ts
|
|
439
488
|
```
|
|
440
489
|
## Model Conventions
|
|
441
490
|
Models generated by the make:model command will be placed in the specific directory.
|
|
442
|
-
Let's
|
|
491
|
+
Let's example a basic model class:
|
|
443
492
|
|
|
444
493
|
```js
|
|
445
494
|
import { Model } from 'tspace-mysql'
|
|
495
|
+
// If you want to specify a global setting for the 'Model'
|
|
496
|
+
Model.global({
|
|
497
|
+
uuid: true,
|
|
498
|
+
softDelete: true,
|
|
499
|
+
timestamp: true
|
|
500
|
+
})
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
class Observe {
|
|
504
|
+
|
|
505
|
+
public selected(results : unknown) {
|
|
506
|
+
console.log({ results , selected : true })
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
public created(results : unknown) {
|
|
510
|
+
console.log({ results , created : true })
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
public updated(results : unknown) {
|
|
514
|
+
console.log({ results , updated : true })
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
public deleted(results : unknown) {
|
|
518
|
+
console.log({ results , deleted : true })
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
*/
|
|
446
522
|
class User extends Model {
|
|
447
523
|
constructor(){
|
|
448
524
|
super()
|
|
@@ -466,6 +542,7 @@ class User extends Model {
|
|
|
466
542
|
* this.useLoadRelationsInRegistry() // => auto generated result from relationship to results
|
|
467
543
|
* this.useBuiltInRelationFunctions() // => build-in functions relationships to results
|
|
468
544
|
* this.useHooks([(r) => console.log(r)])
|
|
545
|
+
* this.useObserver(Observe)
|
|
469
546
|
* this.useSchema ({
|
|
470
547
|
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
471
548
|
* uuid : new Blueprint().varchar(50).null(),
|
|
@@ -512,8 +589,8 @@ export { User }
|
|
|
512
589
|
export default User
|
|
513
590
|
```
|
|
514
591
|
## Relationships
|
|
515
|
-
Relationships are defined as methods on your Model classes
|
|
516
|
-
Let's
|
|
592
|
+
Relationships are defined as methods on your Model classes.
|
|
593
|
+
Let's example a basic relationship:
|
|
517
594
|
|
|
518
595
|
## One To One
|
|
519
596
|
A one-to-one relationship is used to define relationships where a single model is the parent to one child models
|
|
@@ -659,8 +736,8 @@ const userUsingFunction = await new User().roles().findOne()
|
|
|
659
736
|
```
|
|
660
737
|
|
|
661
738
|
## Deeply Nested Relations
|
|
662
|
-
Relationships can
|
|
663
|
-
|
|
739
|
+
Relationships can involve deep connections.
|
|
740
|
+
Let's example of a deep relationship:
|
|
664
741
|
```js
|
|
665
742
|
import { Model } from 'tspace-mysql'
|
|
666
743
|
|
|
@@ -735,8 +812,8 @@ await new User()
|
|
|
735
812
|
|
|
736
813
|
```
|
|
737
814
|
## Relation Exists
|
|
738
|
-
Relationships can return only
|
|
739
|
-
|
|
815
|
+
Relationships can return results only if they are not empty in relations, considering soft deletes.
|
|
816
|
+
Let's illustrate this with an example of an existence check in relations:
|
|
740
817
|
```js
|
|
741
818
|
+-------------+--------------+----------------------------+--------------------+
|
|
742
819
|
| table users | |
|
|
@@ -832,8 +909,7 @@ await new User().relationsExists('posts').findMany()
|
|
|
832
909
|
|
|
833
910
|
```
|
|
834
911
|
## Built in Relation Functions
|
|
835
|
-
|
|
836
|
-
let's example a built in function :
|
|
912
|
+
Certainly, let's illustrate the use of a built-in function in the results of relationships:
|
|
837
913
|
```js
|
|
838
914
|
import { Model } from 'tspace-mysql'
|
|
839
915
|
|
|
@@ -871,16 +947,109 @@ for (const post of posts) {
|
|
|
871
947
|
const comments = await post.$comments()
|
|
872
948
|
}
|
|
873
949
|
|
|
950
|
+
```
|
|
951
|
+
|
|
952
|
+
## Decorator
|
|
953
|
+
Decorators can be used in a Model.
|
|
954
|
+
Let's illustrate this with an example of a decorators:
|
|
955
|
+
```js
|
|
956
|
+
|
|
957
|
+
import {
|
|
958
|
+
Blueprint, Model ,
|
|
959
|
+
Table ,TableSingular, TablePlural,
|
|
960
|
+
UUID, SoftDelete, Timestamp,
|
|
961
|
+
Pattern, CamelCase , snakeCase ,
|
|
962
|
+
Column, Validate, Observer,
|
|
963
|
+
HasMany, HasOne, BelongsTo, BelongsToMany
|
|
964
|
+
|
|
965
|
+
} from 'tspace-mysql'
|
|
966
|
+
import { Post } from './Post'
|
|
967
|
+
import { PostUser } from './PostUser'
|
|
968
|
+
|
|
969
|
+
class UserObserve {
|
|
970
|
+
|
|
971
|
+
public selected(results : any) {
|
|
972
|
+
console.log({ results , selected : true })
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
public created(results : unknown) {
|
|
976
|
+
console.log({ results , created : true })
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
public updated(results : unknown) {
|
|
980
|
+
console.log({ results , updated : true })
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
public deleted(results : unknown) {
|
|
984
|
+
console.log({ results , deleted : true })
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
@Pattern('camelCase')
|
|
989
|
+
@Observer(UserObserve)
|
|
990
|
+
@UUID()
|
|
991
|
+
@SoftDelete()
|
|
992
|
+
@Timestamp()
|
|
993
|
+
@Table('users')
|
|
994
|
+
class User extends Model {
|
|
995
|
+
|
|
996
|
+
@Column(() => new Blueprint().int().notNull().primary().autoIncrement())
|
|
997
|
+
public id!: number
|
|
998
|
+
|
|
999
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
1000
|
+
public uuid!: string
|
|
1001
|
+
|
|
1002
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
1003
|
+
@Validate({
|
|
1004
|
+
type : String,
|
|
1005
|
+
require : true,
|
|
1006
|
+
length : 50,
|
|
1007
|
+
match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
1008
|
+
unique : true,
|
|
1009
|
+
fn : (email : string) => /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
1010
|
+
})
|
|
1011
|
+
public email!: string
|
|
1012
|
+
|
|
1013
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
1014
|
+
public name !: string
|
|
1015
|
+
|
|
1016
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
1017
|
+
public username !: string
|
|
1018
|
+
|
|
1019
|
+
@Column(() => new Blueprint().varchar(50).null())
|
|
1020
|
+
public password !: string
|
|
1021
|
+
|
|
1022
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
1023
|
+
public createdAt!: Date
|
|
1024
|
+
|
|
1025
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
1026
|
+
public updatedAt!: Date
|
|
1027
|
+
|
|
1028
|
+
@Column(() => new Blueprint().timestamp().null())
|
|
1029
|
+
public deletedAt!: Date
|
|
1030
|
+
|
|
1031
|
+
@HasMany({ model : Post })
|
|
1032
|
+
public posts!: Post[]
|
|
1033
|
+
|
|
1034
|
+
@HasOne({ model : Post })
|
|
1035
|
+
public post!: Post
|
|
1036
|
+
|
|
1037
|
+
@BelongsToMany({ model : Post , modelPivot : PostUser })
|
|
1038
|
+
public users!: PostUser[]
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
export { User }
|
|
1042
|
+
export default User
|
|
1043
|
+
|
|
874
1044
|
```
|
|
875
1045
|
## Schema Model
|
|
876
|
-
Define the schema of Model
|
|
877
|
-
let's example a validator model:
|
|
1046
|
+
Define the schema of a Model
|
|
878
1047
|
|
|
879
1048
|
## Validation
|
|
880
1049
|
Validate the schema of Model
|
|
881
1050
|
let's example a validator model:
|
|
882
1051
|
```js
|
|
883
|
-
|
|
1052
|
+
import { Model , Blueprint , Column } from 'tspace-mysql'
|
|
884
1053
|
class User extends Model {
|
|
885
1054
|
constructor(){
|
|
886
1055
|
super()
|
|
@@ -900,7 +1069,7 @@ class User extends Model {
|
|
|
900
1069
|
uuid : Number,
|
|
901
1070
|
name : {
|
|
902
1071
|
type : String,
|
|
903
|
-
length : 191
|
|
1072
|
+
length : 191,
|
|
904
1073
|
require : true,
|
|
905
1074
|
json : true
|
|
906
1075
|
},
|
|
@@ -910,7 +1079,7 @@ class User extends Model {
|
|
|
910
1079
|
length : 191,
|
|
911
1080
|
match: /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
912
1081
|
unique : true,
|
|
913
|
-
fn : (email : string) =>
|
|
1082
|
+
fn : (email : string) => /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email)
|
|
914
1083
|
},
|
|
915
1084
|
createdAt : Date,
|
|
916
1085
|
updatedAt : Date,
|
|
@@ -978,6 +1147,9 @@ class Post extends Model {
|
|
|
978
1147
|
}
|
|
979
1148
|
await Schema.sync(`src/Models` , { force : true })
|
|
980
1149
|
|
|
1150
|
+
// also you can sync by Model
|
|
1151
|
+
await new User().sync({ force : true })
|
|
1152
|
+
|
|
981
1153
|
```
|
|
982
1154
|
## Query Builder
|
|
983
1155
|
Methods builder for queries
|
|
@@ -1003,7 +1175,6 @@ orWhereRaw(sql)
|
|
|
1003
1175
|
orWhereIn(column , [])
|
|
1004
1176
|
orWhereSubQuery(colmn , rawSQL)
|
|
1005
1177
|
when(contition , callback)
|
|
1006
|
-
if(contition , callback)
|
|
1007
1178
|
select(column1 ,column2 ,...N)
|
|
1008
1179
|
distinct()
|
|
1009
1180
|
selectRaw(column1 ,column2 ,...N)
|
|
@@ -1199,6 +1370,8 @@ Command will be generate models from table in database
|
|
|
1199
1370
|
tspace-mysql generate:models --dir=<folder for creating>
|
|
1200
1371
|
or
|
|
1201
1372
|
tspace-mysql gen:models --dir=<folder for creating> --env=development
|
|
1373
|
+
or
|
|
1374
|
+
tspace-mysql generate:models --dir=app/Models --env=development --decorators
|
|
1202
1375
|
|
|
1203
1376
|
```
|
|
1204
1377
|
|