tspace-mysql 1.1.7 → 1.1.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 +123 -30
- package/dist/lib/config/env.d.ts +3 -12
- package/dist/lib/config/env.js +20 -14
- package/dist/lib/connection/index.d.ts +16 -10
- package/dist/lib/connection/index.js +45 -49
- package/dist/lib/tspace/AbstractDB.d.ts +9 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +47 -46
- package/dist/lib/tspace/AbstractDatabase.js +2 -2
- package/dist/lib/tspace/AbstractModel.d.ts +26 -21
- package/dist/lib/tspace/DB.d.ts +9 -5
- package/dist/lib/tspace/DB.js +17 -13
- package/dist/lib/tspace/Database.d.ts +6 -3
- package/dist/lib/tspace/Database.js +333 -336
- package/dist/lib/tspace/Model.d.ts +74 -72
- package/dist/lib/tspace/Model.js +375 -399
- package/dist/lib/utils/index.d.ts +1 -0
- package/dist/lib/utils/index.js +27 -9
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
TSPACE_HOST = localhost
|
|
41
|
+
TSPACE_PORT = 3306
|
|
42
|
+
TSPACE_USERNAME = root
|
|
43
|
+
TSPACE_PASSWORD = password
|
|
44
|
+
TSPACE_DATABASE = database
|
|
45
|
+
|
|
46
|
+
/** default
|
|
47
|
+
TSPACE_CONNECTION_LIMIT = 10
|
|
48
|
+
TSPACE_CONNECTION_ERROR = true
|
|
49
|
+
TSPACE_QUEUE_LIMIT = 25
|
|
50
|
+
TSPACE_TIMEOUT = 30000
|
|
51
|
+
TSPACE_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 :
|
|
@@ -66,6 +76,11 @@ const selectQuery = await new DB('users').select('id','username').findOne()
|
|
|
66
76
|
// selectQuery => { id : 1, username : 'tspace'}
|
|
67
77
|
const selectQueries = await new DB('users').select('id','username').findMany()
|
|
68
78
|
// selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @example except
|
|
82
|
+
*/
|
|
83
|
+
await new DB('users').except('id','username').findOne()
|
|
69
84
|
```
|
|
70
85
|
Running A Where Query
|
|
71
86
|
```js
|
|
@@ -73,6 +88,14 @@ const user = await new DB('users').where('id',1).findOne()
|
|
|
73
88
|
// user => { id : 1 , username : 'tspace', email : 'tspace@gmail.com'}
|
|
74
89
|
const users = await new DB('users').where('id','!=',1).findMany()
|
|
75
90
|
// users => [{ id : 2 , username : 'tspace2' , email : 'tspace2@gmail.com' }]
|
|
91
|
+
/**
|
|
92
|
+
* @example where
|
|
93
|
+
*/
|
|
94
|
+
const whereIn = await new DB('users').whereIn('id',[1,2]).findMany()
|
|
95
|
+
const whereBetween = await new DB('users').whereBetween('id',[1,2]).findMany()
|
|
96
|
+
const whereSubQuery = await new DB('users').whereSubQuery('id','select id from users').findMany()
|
|
97
|
+
// await new DB('users').whereSubQuery('id',new DB('users').select('id').toString()).findMany()
|
|
98
|
+
const whereNull = await new DB('users').whereNull('username').findOne()
|
|
76
99
|
```
|
|
77
100
|
Running A Insert Query
|
|
78
101
|
```js
|
|
@@ -91,6 +114,23 @@ const reposity = new DB('users')
|
|
|
91
114
|
await reposity.save()
|
|
92
115
|
const { result } = reposity
|
|
93
116
|
// result => { id : 4 , username : 'tspace4', email : 'tspace4@gmail.com'}
|
|
117
|
+
|
|
118
|
+
const users = await new DB('users')
|
|
119
|
+
.createMultiple([
|
|
120
|
+
{
|
|
121
|
+
name :'tspace4',
|
|
122
|
+
email : 'tspace4@gmail.com'
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name :'tspace5',
|
|
126
|
+
email : 'tspace5@gmail.com'
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name :'tspace6',
|
|
130
|
+
email : 'tspace6@gmail.com'
|
|
131
|
+
},
|
|
132
|
+
]).save()
|
|
133
|
+
|
|
94
134
|
```
|
|
95
135
|
Running A Update Query
|
|
96
136
|
```js
|
|
@@ -110,7 +150,19 @@ const reposity = new DB('users').where('id',1)
|
|
|
110
150
|
await reposity.save()
|
|
111
151
|
const { result } = reposity
|
|
112
152
|
// result => { id : 1 , username : 'tspace1++', email : 'tspace1++@gmail.com'}
|
|
113
|
-
```
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Running A Update Or Created Query
|
|
156
|
+
```js
|
|
157
|
+
const user = await new DB('users')
|
|
158
|
+
.where('id',1)
|
|
159
|
+
.updateOrCreate({
|
|
160
|
+
name : 'tspace1**',
|
|
161
|
+
email : 'tspace1@gmail.com'
|
|
162
|
+
}).save()
|
|
163
|
+
// user => { username : 'tspace1**', email : 'tspace1@gmail.com'}
|
|
164
|
+
```
|
|
165
|
+
|
|
114
166
|
Running A Delete Query
|
|
115
167
|
```js
|
|
116
168
|
const deleted = await new DB('users').where('id',1).delete()
|
|
@@ -157,16 +209,9 @@ try {
|
|
|
157
209
|
title : `tspace-post3`
|
|
158
210
|
}
|
|
159
211
|
])
|
|
160
|
-
/**
|
|
161
|
-
*
|
|
162
|
-
* bind method for make sure this connection has same transaction in connection
|
|
163
|
-
* @params {Function} connection
|
|
164
|
-
*/
|
|
165
|
-
.bind(connection)
|
|
212
|
+
.bind(connection) /** don't forget this /*
|
|
166
213
|
.save()
|
|
167
214
|
|
|
168
|
-
// throw new Error('try error')
|
|
169
|
-
|
|
170
215
|
/**
|
|
171
216
|
*
|
|
172
217
|
* @commit commit transaction to database
|
|
@@ -265,7 +310,17 @@ import { Model } from 'tspace-mysql'
|
|
|
265
310
|
class User extends Model {
|
|
266
311
|
constructor(){
|
|
267
312
|
super()
|
|
268
|
-
this.useTimestamp()
|
|
313
|
+
this.useTimestamp() /** created_at , updated_at
|
|
314
|
+
/**
|
|
315
|
+
* @custom
|
|
316
|
+
*
|
|
317
|
+
*
|
|
318
|
+
* this.useTimestamp({
|
|
319
|
+
* createdAt : 'created_at',
|
|
320
|
+
* updatedAt : 'updated_at'
|
|
321
|
+
* })
|
|
322
|
+
*/
|
|
323
|
+
|
|
269
324
|
/*
|
|
270
325
|
* the "snake case", plural name of the class will be used as the table name
|
|
271
326
|
*
|
|
@@ -289,7 +344,7 @@ import Phone from '../Phone'
|
|
|
289
344
|
class User extends Model {
|
|
290
345
|
constructor(){
|
|
291
346
|
super()
|
|
292
|
-
this.useTimestamp()
|
|
347
|
+
this.useTimestamp()
|
|
293
348
|
/**
|
|
294
349
|
*
|
|
295
350
|
* @hasOne Get the phone associated with the user.
|
|
@@ -298,13 +353,11 @@ class User extends Model {
|
|
|
298
353
|
this.hasOne({ name : 'phone' , model : Phone })
|
|
299
354
|
}
|
|
300
355
|
/**
|
|
301
|
-
* @
|
|
356
|
+
* @hasOne Get the phone associated with the user. using function callback
|
|
302
357
|
* @function
|
|
303
358
|
*/
|
|
304
359
|
phone (callback ?: Function) {
|
|
305
|
-
return
|
|
306
|
-
return callback ? callback(query) : query
|
|
307
|
-
})
|
|
360
|
+
return this.hasOneQuery({ name : 'phone' , model : Phone } , callback)
|
|
308
361
|
}
|
|
309
362
|
}
|
|
310
363
|
export default User
|
|
@@ -339,9 +392,7 @@ class Post extends Model {
|
|
|
339
392
|
* @function
|
|
340
393
|
*/
|
|
341
394
|
comments (callback ?: Function) {
|
|
342
|
-
return this.hasManyQuery({ model : Comment },
|
|
343
|
-
return callback ? callback(query) : query
|
|
344
|
-
})
|
|
395
|
+
return this.hasManyQuery({ name : 'comments' , model : Comment } , callback)
|
|
345
396
|
}
|
|
346
397
|
}
|
|
347
398
|
export default Post
|
|
@@ -376,9 +427,7 @@ class Phone extends Model {
|
|
|
376
427
|
* @function
|
|
377
428
|
*/
|
|
378
429
|
user (callback ?: Function) {
|
|
379
|
-
return
|
|
380
|
-
return callback ? callback(query) : query
|
|
381
|
-
})
|
|
430
|
+
return this.belongsToQuery({ name : 'user' , model : User }, callback)
|
|
382
431
|
}
|
|
383
432
|
}
|
|
384
433
|
export default Phone
|
|
@@ -413,9 +462,7 @@ class User extends Model {
|
|
|
413
462
|
* @function
|
|
414
463
|
*/
|
|
415
464
|
roles (callback ?: Function) {
|
|
416
|
-
return
|
|
417
|
-
return callback ? callback(query) : query
|
|
418
|
-
})
|
|
465
|
+
return this.belognsToManyQuery({ model : Role } , callback)
|
|
419
466
|
}
|
|
420
467
|
}
|
|
421
468
|
export default User
|
|
@@ -428,6 +475,52 @@ const user = await new User().relations('roles').findOne()
|
|
|
428
475
|
const userUsingFunction = await new User().roles().findOne()
|
|
429
476
|
// user?.roles => [{...}]
|
|
430
477
|
```
|
|
478
|
+
|
|
479
|
+
## Relation in Relation
|
|
480
|
+
Relationships can deep relations.
|
|
481
|
+
let's example a deep in relations :
|
|
482
|
+
```js
|
|
483
|
+
import { Model } from 'tspace-mysql'
|
|
484
|
+
|
|
485
|
+
class User extends Model {
|
|
486
|
+
constructor(){
|
|
487
|
+
super()
|
|
488
|
+
this.hasMany({ name : 'posts' , model : Post })
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
+--------------------------------------------------------------------------+
|
|
492
|
+
class Post extends Model {
|
|
493
|
+
constructor(){
|
|
494
|
+
super()
|
|
495
|
+
this.hasMany({ name : 'comments' , model : Comment })
|
|
496
|
+
this.belongsTo({ name : 'user' , model : User })
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
+--------------------------------------------------------------------------+
|
|
500
|
+
class Comment extends Model {
|
|
501
|
+
constructor(){
|
|
502
|
+
super()
|
|
503
|
+
this.hasMany({ name : 'users' , model : User })
|
|
504
|
+
this.belongsTo({ name : 'post' , model : Post })
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
+--------------------------------------------------------------------------+
|
|
508
|
+
await new User().relations('posts')
|
|
509
|
+
.relationQuery('posts', (query : Post) => { // relationQuery return a callback query in model registry
|
|
510
|
+
return query.relations('comments','user')
|
|
511
|
+
.relationQuery('comments', (query : Comment) => {
|
|
512
|
+
return query.relations('user','post')
|
|
513
|
+
})
|
|
514
|
+
.relationQuery('user', (query : User) => {
|
|
515
|
+
return query.relations('posts').relationQuery('posts',(query : Post)=> {
|
|
516
|
+
return query.relations('comments','user')
|
|
517
|
+
// relation n to ...n
|
|
518
|
+
})
|
|
519
|
+
})
|
|
520
|
+
})
|
|
521
|
+
.findMany()
|
|
522
|
+
```
|
|
523
|
+
|
|
431
524
|
## Query Builder
|
|
432
525
|
Methods builder for queries
|
|
433
526
|
```js
|
|
@@ -451,8 +544,8 @@ join(primary key , table.foreign key)
|
|
|
451
544
|
rightJoin (primary key , table.foreign key)
|
|
452
545
|
leftJoin (primary key , table.foreign key)
|
|
453
546
|
limit (limit)
|
|
454
|
-
orderBy (column ,'ASC' || 'DSCE')
|
|
455
547
|
having (condition)
|
|
548
|
+
orderBy (column ,'ASC' || 'DSCE')
|
|
456
549
|
latest (column)
|
|
457
550
|
oldest (column)
|
|
458
551
|
groupBy (column)
|
package/dist/lib/config/env.d.ts
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
|
|
3
|
-
DB_PORT: string | undefined;
|
|
4
|
-
DB_USERNAME: string | undefined;
|
|
5
|
-
DB_PASSWORD: string | undefined;
|
|
6
|
-
DB_DATABASE: string | undefined;
|
|
7
|
-
DB_CONNECTION_LIMIT: string | undefined;
|
|
8
|
-
DB_DATE_STRING: string | undefined;
|
|
9
|
-
DB_TIMEOUT: string | undefined;
|
|
10
|
-
DB_QUEUE_LIMIT: string | undefined;
|
|
1
|
+
declare const _default: Readonly<{
|
|
2
|
+
[x: string]: string | number | boolean | null | undefined;
|
|
11
3
|
}>;
|
|
12
|
-
export
|
|
13
|
-
export default env;
|
|
4
|
+
export default _default;
|
package/dist/lib/config/env.js
CHANGED
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.env = void 0;
|
|
7
6
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
8
7
|
const path_1 = __importDefault(require("path"));
|
|
9
8
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -18,16 +17,23 @@ const environment = () => {
|
|
|
18
17
|
return envWithoutEnviroment;
|
|
19
18
|
};
|
|
20
19
|
dotenv_1.default.config({ path: environment() });
|
|
21
|
-
const env =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
const env = {
|
|
21
|
+
HOST: process.env.DB_HOST || process.env.TSPACE_HOST,
|
|
22
|
+
PORT: process.env.DB_PORT || process.env.TSPACE_PORT || 3306,
|
|
23
|
+
USERNAME: process.env.DB_USERNAME || process.env.TSPACE_USERNAME,
|
|
24
|
+
PASSWORD: process.env.DB_PASSWORD || process.env.TSPACE_PASSWORD || '',
|
|
25
|
+
DATABASE: process.env.DB_DATABASE || process.env.TSPACE_DATABASE,
|
|
26
|
+
CONNECTION_LIMIT: process.env.DB_CONNECTION_LIMIT || process.env.TSPACE_CONNECTION_LIMIT || 10,
|
|
27
|
+
CONNECTION_ERROR: process.env.DB_CONNECTION_ERROR || process.env.TSPACE_CONNECTION_ERROR || true,
|
|
28
|
+
QUEUE_LIMIT: process.env.DB_QUEUE_LIMIT || process.env.TSPACE_QUEUE_LIMIT || 25,
|
|
29
|
+
TIMEOUT: process.env.DB_TIMEOUT || process.env.TSPACE_TIMEOUT || 1000 * 30,
|
|
30
|
+
DATE_STRINGS: process.env.DB_DATE_STRINGS || process.env.TSPACE_DATE_STRINGS || true
|
|
31
|
+
};
|
|
32
|
+
for (const [key, value] of Object.entries(env)) {
|
|
33
|
+
if (value == null)
|
|
34
|
+
continue;
|
|
35
|
+
if (typeof value === 'string' && ['true', 'false'].some(v => value.toLowerCase() === v)) {
|
|
36
|
+
env[key] = JSON.parse(value.toLowerCase());
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.default = Object.freeze({ ...env });
|
|
@@ -3,13 +3,13 @@ export interface PoolCallback {
|
|
|
3
3
|
release: () => void;
|
|
4
4
|
}
|
|
5
5
|
export interface ConnectionTransaction {
|
|
6
|
-
query: (sql: string) => Promise<any
|
|
7
|
-
startTransaction: () => Promise<any
|
|
8
|
-
commit: () => Promise<any
|
|
9
|
-
rollback: () => Promise<any
|
|
6
|
+
query: (sql: string) => Promise<any>;
|
|
7
|
+
startTransaction: () => Promise<any>;
|
|
8
|
+
commit: () => Promise<any>;
|
|
9
|
+
rollback: () => Promise<any>;
|
|
10
10
|
}
|
|
11
11
|
export interface Connection {
|
|
12
|
-
query: (sql: string) => Promise<any
|
|
12
|
+
query: (sql: string) => Promise<any>;
|
|
13
13
|
connection: () => Promise<ConnectionTransaction>;
|
|
14
14
|
}
|
|
15
15
|
export interface Options {
|
|
@@ -26,6 +26,10 @@ export interface Options {
|
|
|
26
26
|
}
|
|
27
27
|
export declare class PoolConnection {
|
|
28
28
|
private OPTIONS;
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @Init a options connection pool
|
|
32
|
+
*/
|
|
29
33
|
constructor(options?: Options);
|
|
30
34
|
/**
|
|
31
35
|
*
|
|
@@ -35,14 +39,16 @@ export declare class PoolConnection {
|
|
|
35
39
|
options(options: Options): this;
|
|
36
40
|
/**
|
|
37
41
|
*
|
|
38
|
-
* Get a connection
|
|
42
|
+
* Get a connection to database
|
|
39
43
|
* @return {Connection} Connection
|
|
44
|
+
* @property {Function} Connection.query
|
|
45
|
+
* @property {Function} Connection.connection
|
|
40
46
|
*/
|
|
41
47
|
connection(): Connection;
|
|
42
48
|
private _defaultOptions;
|
|
43
|
-
private
|
|
49
|
+
private _loadOptions;
|
|
44
50
|
private _messageError;
|
|
45
51
|
}
|
|
46
|
-
declare const
|
|
47
|
-
export { Pool };
|
|
48
|
-
export default
|
|
52
|
+
declare const pool: Connection;
|
|
53
|
+
export { pool as Pool };
|
|
54
|
+
export default pool;
|
|
@@ -6,10 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Pool = exports.PoolConnection = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const env_1 = require("../config/env");
|
|
10
|
-
const
|
|
9
|
+
const env_1 = __importDefault(require("../config/env"));
|
|
10
|
+
const mysql2_1 = require("mysql2");
|
|
11
11
|
class PoolConnection {
|
|
12
|
-
OPTIONS = this.
|
|
12
|
+
OPTIONS = this._loadOptions();
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @Init a options connection pool
|
|
16
|
+
*/
|
|
13
17
|
constructor(options) {
|
|
14
18
|
if (options) {
|
|
15
19
|
this.OPTIONS = new Map(Object.entries({
|
|
@@ -29,21 +33,23 @@ class PoolConnection {
|
|
|
29
33
|
}
|
|
30
34
|
/**
|
|
31
35
|
*
|
|
32
|
-
* Get a connection
|
|
36
|
+
* Get a connection to database
|
|
33
37
|
* @return {Connection} Connection
|
|
38
|
+
* @property {Function} Connection.query
|
|
39
|
+
* @property {Function} Connection.connection
|
|
34
40
|
*/
|
|
35
41
|
connection() {
|
|
36
|
-
const pool = (0,
|
|
37
|
-
pool.getConnection((err,
|
|
38
|
-
if (err)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
const pool = (0, mysql2_1.createPool)(Object.fromEntries(this.OPTIONS));
|
|
43
|
+
const conn = pool.getConnection((err, _) => {
|
|
44
|
+
if (!err)
|
|
45
|
+
return;
|
|
46
|
+
const message = this._messageError.bind(this);
|
|
47
|
+
if (!env_1.default.CONNECTION_ERROR)
|
|
48
|
+
return;
|
|
49
|
+
process.nextTick(() => {
|
|
50
|
+
console.log(message());
|
|
51
|
+
return process.exit();
|
|
52
|
+
});
|
|
47
53
|
});
|
|
48
54
|
return {
|
|
49
55
|
query: (sql) => {
|
|
@@ -85,40 +91,30 @@ class PoolConnection {
|
|
|
85
91
|
}
|
|
86
92
|
_defaultOptions() {
|
|
87
93
|
return new Map(Object.entries({
|
|
88
|
-
connectionLimit: Number
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
dateStrings: true,
|
|
92
|
-
connectTimeout: Number.isNaN(Number(env_1.env.DB_TIMEOUT))
|
|
93
|
-
? 1000 * 30
|
|
94
|
-
: Number.isNaN(Number(env_1.env.DB_TIMEOUT)),
|
|
95
|
-
acquireTimeout: Number.isNaN(Number(env_1.env.DB_TIMEOUT))
|
|
96
|
-
? 1000 * 30
|
|
97
|
-
: Number.isNaN(Number(env_1.env.DB_TIMEOUT)),
|
|
94
|
+
connectionLimit: Number(env_1.default.CONNECTION_LIMIT),
|
|
95
|
+
dateStrings: Boolean(env_1.default.DATE_STRINGS),
|
|
96
|
+
connectTimeout: Number(env_1.default.TIMEOUT),
|
|
98
97
|
waitForConnections: true,
|
|
99
|
-
queueLimit: Number
|
|
100
|
-
? 25
|
|
101
|
-
: Number.isNaN(Number(env_1.env.DB_QUEUE_LIMIT)),
|
|
98
|
+
queueLimit: Number(env_1.default.QUEUE_LIMIT),
|
|
102
99
|
charset: 'utf8mb4',
|
|
103
|
-
host: String(env_1.
|
|
104
|
-
port: Number.isNaN(Number(env_1.
|
|
100
|
+
host: String(env_1.default.HOST),
|
|
101
|
+
port: Number.isNaN(Number(env_1.default.PORT))
|
|
105
102
|
? 3306
|
|
106
|
-
: Number(env_1.
|
|
107
|
-
database: String(env_1.
|
|
108
|
-
user: String(env_1.
|
|
109
|
-
password: String(env_1.
|
|
103
|
+
: Number(env_1.default.PORT),
|
|
104
|
+
database: String(env_1.default.DATABASE),
|
|
105
|
+
user: String(env_1.default.USERNAME),
|
|
106
|
+
password: String(env_1.default.PASSWORD) === '' ? '' : String(env_1.default.PASSWORD)
|
|
110
107
|
}));
|
|
111
108
|
}
|
|
112
|
-
|
|
109
|
+
_loadOptions() {
|
|
113
110
|
try {
|
|
114
111
|
const jsonPath = path_1.default.join(path_1.default.resolve(), 'tspace-mysql.json');
|
|
115
112
|
const jsonOptionsExists = fs_1.default.existsSync(jsonPath);
|
|
116
|
-
if (jsonOptionsExists)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return this._defaultOptions();
|
|
113
|
+
if (!jsonOptionsExists)
|
|
114
|
+
return this._defaultOptions();
|
|
115
|
+
const jsonOptions = fs_1.default.readFileSync(jsonPath, 'utf8');
|
|
116
|
+
const options = JSON.parse(jsonOptions);
|
|
117
|
+
return new Map(Object.entries(options));
|
|
122
118
|
}
|
|
123
119
|
catch (e) {
|
|
124
120
|
return this._defaultOptions();
|
|
@@ -129,16 +125,16 @@ class PoolConnection {
|
|
|
129
125
|
\x1b[1m\x1b[31m
|
|
130
126
|
Connection lost to database ! \x1b[0m
|
|
131
127
|
--------------------------------- \x1b[33m
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
HOST : ${this.OPTIONS.get('host')}
|
|
129
|
+
PORT : ${this.OPTIONS.get('port')}
|
|
130
|
+
DATABASE : ${this.OPTIONS.get('database')}
|
|
131
|
+
USERNAME : ${this.OPTIONS.get('user')}
|
|
132
|
+
PASSWORD : ${this.OPTIONS.get('password')} \x1b[0m
|
|
137
133
|
---------------------------------
|
|
138
134
|
`;
|
|
139
135
|
}
|
|
140
136
|
}
|
|
141
137
|
exports.PoolConnection = PoolConnection;
|
|
142
|
-
const
|
|
143
|
-
exports.Pool =
|
|
144
|
-
exports.default =
|
|
138
|
+
const pool = new PoolConnection().connection();
|
|
139
|
+
exports.Pool = pool;
|
|
140
|
+
exports.default = pool;
|
|
@@ -2,6 +2,15 @@ import Database from './Database';
|
|
|
2
2
|
declare abstract class AbstractDB extends Database {
|
|
3
3
|
abstract table(tableName: string): void;
|
|
4
4
|
abstract beginTransaction(): Promise<any>;
|
|
5
|
+
abstract generateUUID(): string;
|
|
6
|
+
abstract raw(sql: string): string;
|
|
7
|
+
abstract constants(constants?: string): string | {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
abstract caseUpdate(cases: {
|
|
11
|
+
when: string;
|
|
12
|
+
then: string;
|
|
13
|
+
}[], final?: string): string | [];
|
|
5
14
|
}
|
|
6
15
|
export { AbstractDB };
|
|
7
16
|
export default AbstractDB;
|
|
@@ -5,7 +5,7 @@ declare abstract class AbstractDatabase {
|
|
|
5
5
|
[key: string]: Function;
|
|
6
6
|
};
|
|
7
7
|
protected $constants: Function;
|
|
8
|
-
protected $
|
|
8
|
+
protected $state: {
|
|
9
9
|
get: Function;
|
|
10
10
|
set: Function;
|
|
11
11
|
clone: Function;
|
|
@@ -23,51 +23,52 @@ declare abstract class AbstractDatabase {
|
|
|
23
23
|
protected $attributes: {
|
|
24
24
|
[key: string]: any;
|
|
25
25
|
};
|
|
26
|
-
abstract
|
|
27
|
-
abstract
|
|
28
|
-
abstract
|
|
29
|
-
abstract
|
|
30
|
-
abstract
|
|
31
|
-
abstract
|
|
32
|
-
abstract
|
|
33
|
-
abstract
|
|
34
|
-
abstract
|
|
35
|
-
abstract
|
|
36
|
-
abstract
|
|
37
|
-
abstract
|
|
38
|
-
abstract
|
|
39
|
-
abstract
|
|
40
|
-
abstract
|
|
41
|
-
abstract
|
|
42
|
-
abstract
|
|
43
|
-
abstract
|
|
44
|
-
abstract
|
|
45
|
-
abstract
|
|
46
|
-
abstract
|
|
47
|
-
abstract
|
|
48
|
-
abstract
|
|
49
|
-
abstract
|
|
50
|
-
abstract
|
|
51
|
-
abstract
|
|
52
|
-
abstract
|
|
53
|
-
abstract
|
|
54
|
-
abstract
|
|
55
|
-
abstract
|
|
56
|
-
abstract
|
|
57
|
-
abstract
|
|
58
|
-
abstract
|
|
59
|
-
abstract
|
|
60
|
-
abstract
|
|
61
|
-
abstract
|
|
62
|
-
abstract
|
|
63
|
-
abstract
|
|
64
|
-
abstract
|
|
65
|
-
abstract
|
|
66
|
-
abstract
|
|
67
|
-
abstract
|
|
68
|
-
abstract
|
|
69
|
-
abstract
|
|
70
|
-
abstract
|
|
26
|
+
abstract void(): this;
|
|
27
|
+
abstract debug(): this;
|
|
28
|
+
abstract dd(): this;
|
|
29
|
+
abstract select(...params: string[]): this;
|
|
30
|
+
abstract distinct(...params: string[]): this;
|
|
31
|
+
abstract whereNull(column: string): this;
|
|
32
|
+
abstract whereNotNull(column: string): this;
|
|
33
|
+
abstract where(column: string, operator: string, value: string): this;
|
|
34
|
+
abstract whereSensitive(column: string, operator: string, value: string): this;
|
|
35
|
+
abstract whereRaw(sql: string): this;
|
|
36
|
+
abstract whereId(id: number): this;
|
|
37
|
+
abstract whereUser(id: number): this;
|
|
38
|
+
abstract whereEmail(value: string): this;
|
|
39
|
+
abstract whereQuery(callback: Function): this;
|
|
40
|
+
abstract orWhere(column: string, operator: string, value: string): this;
|
|
41
|
+
abstract whereIn(column: string, arrayValues: Array<any>): this;
|
|
42
|
+
abstract orWhereIn(column: string, arrayValues: Array<any>): this;
|
|
43
|
+
abstract whereNotIn(column: string, arrayValues: Array<any>): this;
|
|
44
|
+
abstract whereSubQuery(column: string, subQuery: string): this;
|
|
45
|
+
abstract whereNotSubQuery(column: string, subQuery: string): this;
|
|
46
|
+
abstract orWhereSubQuery(column: string, subQuery: string): this;
|
|
47
|
+
abstract whereBetween(column: string, arrayValue: Array<any>): this;
|
|
48
|
+
abstract having(condition: string): this;
|
|
49
|
+
abstract join(pk: string, fk: string): this;
|
|
50
|
+
abstract rightJoin(pk: string, fk: string): this;
|
|
51
|
+
abstract leftJoin(pk: string, fk: string): this;
|
|
52
|
+
abstract crossJoin(pk: string, fk: string): this;
|
|
53
|
+
abstract orderBy(column: string, order: string): this;
|
|
54
|
+
abstract latest(column: string): this;
|
|
55
|
+
abstract oldest(column: string): this;
|
|
56
|
+
abstract groupBy(column: string): this;
|
|
57
|
+
abstract limit(number: number): this;
|
|
58
|
+
abstract hidden(...columns: string[]): this;
|
|
59
|
+
abstract insert(objects: object): this;
|
|
60
|
+
abstract create(objects: object): this;
|
|
61
|
+
abstract update(objects: object): this;
|
|
62
|
+
abstract insertNotExists(objects: object): this;
|
|
63
|
+
abstract createNotExists(objects: object): this;
|
|
64
|
+
abstract insertOrUpdate(objects: object): this;
|
|
65
|
+
abstract createOrUpdate(objects: object): this;
|
|
66
|
+
abstract updateOrInsert(objects: object): this;
|
|
67
|
+
abstract updateOrCreate(objects: object): this;
|
|
68
|
+
abstract createMultiple(objects: object): this;
|
|
69
|
+
abstract insertMultiple(objects: object): this;
|
|
70
|
+
abstract except(...params: string[]): this;
|
|
71
|
+
abstract only(...params: string[]): this;
|
|
71
72
|
abstract drop(): Promise<any>;
|
|
72
73
|
abstract truncate(): Promise<any>;
|
|
73
74
|
abstract all(): Promise<any>;
|