tspace-mysql 1.2.9 → 1.3.1
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 +64 -14
- package/dist/cli/dump/db.d.ts +4 -4
- package/dist/cli/dump/db.js +25 -25
- package/dist/cli/generate/make.d.ts +4 -4
- package/dist/cli/generate/make.js +45 -45
- package/dist/cli/generate/model.d.ts +2 -2
- package/dist/cli/generate/model.js +6 -6
- package/dist/cli/index.d.ts +2 -2
- package/dist/cli/index.js +58 -58
- package/dist/cli/migrate/make.d.ts +4 -4
- package/dist/cli/migrate/make.js +30 -30
- package/dist/cli/models/make.d.ts +4 -4
- package/dist/cli/models/make.js +51 -51
- package/dist/cli/models/model.d.ts +2 -2
- package/dist/cli/models/model.js +20 -11
- package/dist/cli/query/index.d.ts +4 -4
- package/dist/cli/query/index.js +7 -7
- package/dist/cli/tables/make.d.ts +4 -4
- package/dist/cli/tables/make.js +26 -26
- package/dist/cli/tables/table.d.ts +2 -2
- package/dist/cli/tables/table.js +6 -6
- package/dist/lib/connection/index.d.ts +30 -30
- package/dist/lib/connection/index.js +144 -143
- package/dist/lib/connection/options.d.ts +15 -4
- package/dist/lib/connection/options.js +43 -42
- package/dist/lib/constants/index.d.ts +5 -8
- package/dist/lib/constants/index.js +162 -158
- package/dist/lib/index.d.ts +8 -8
- package/dist/lib/index.js +36 -36
- package/dist/lib/tspace/{AbstractDatabase.d.ts → AbstractBuilder.d.ts} +124 -116
- package/dist/lib/tspace/{AbstractDatabase.js → AbstractBuilder.js} +36 -34
- package/dist/lib/tspace/AbstractDB.d.ts +18 -18
- package/dist/lib/tspace/AbstractDB.js +11 -11
- package/dist/lib/tspace/AbstractModel.d.ts +41 -41
- package/dist/lib/tspace/AbstractModel.js +11 -11
- package/dist/lib/tspace/Blueprint.d.ts +136 -136
- package/dist/lib/tspace/Blueprint.js +228 -228
- package/dist/lib/tspace/{Database.d.ts → Builder.d.ts} +833 -706
- package/dist/lib/tspace/{Database.js → Builder.js} +2556 -2363
- package/dist/lib/tspace/DB.d.ts +152 -126
- package/dist/lib/tspace/DB.js +373 -264
- package/dist/lib/tspace/Interface.d.ts +110 -109
- package/dist/lib/tspace/Interface.js +2 -2
- package/dist/lib/tspace/Logger.d.ts +8 -8
- package/dist/lib/tspace/Logger.js +49 -49
- package/dist/lib/tspace/Model.d.ts +708 -609
- package/dist/lib/tspace/Model.js +2520 -2477
- package/dist/lib/tspace/ProxyHandler.d.ts +14 -14
- package/dist/lib/tspace/ProxyHandler.js +31 -31
- package/dist/lib/tspace/Schema.d.ts +8 -8
- package/dist/lib/tspace/Schema.js +45 -44
- package/dist/lib/tspace/index.d.ts +15 -15
- package/dist/lib/tspace/index.js +20 -20
- package/dist/lib/utils/index.d.ts +15 -15
- package/dist/lib/utils/index.js +155 -165
- package/package.json +2 -4
package/dist/lib/tspace/DB.js
CHANGED
|
@@ -1,264 +1,373 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.DB = void 0;
|
|
24
|
-
const AbstractDB_1 = require("./AbstractDB");
|
|
25
|
-
const ProxyHandler_1 = require("./ProxyHandler");
|
|
26
|
-
const connection_1 = require("../connection");
|
|
27
|
-
class DB extends AbstractDB_1.AbstractDB {
|
|
28
|
-
constructor(table) {
|
|
29
|
-
super();
|
|
30
|
-
this._initialDB();
|
|
31
|
-
if (table)
|
|
32
|
-
this.table(table);
|
|
33
|
-
return new Proxy(this, ProxyHandler_1.proxyHandler);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
* @param {string
|
|
149
|
-
* @return {
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
* @
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*
|
|
223
|
-
* @
|
|
224
|
-
* @
|
|
225
|
-
* @
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.DB = void 0;
|
|
24
|
+
const AbstractDB_1 = require("./AbstractDB");
|
|
25
|
+
const ProxyHandler_1 = require("./ProxyHandler");
|
|
26
|
+
const connection_1 = require("../connection");
|
|
27
|
+
class DB extends AbstractDB_1.AbstractDB {
|
|
28
|
+
constructor(table) {
|
|
29
|
+
super();
|
|
30
|
+
this._initialDB();
|
|
31
|
+
if (table)
|
|
32
|
+
this.table(table);
|
|
33
|
+
return new Proxy(this, ProxyHandler_1.proxyHandler);
|
|
34
|
+
}
|
|
35
|
+
union(a, b) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
return yield this.queryStatement([
|
|
38
|
+
a,
|
|
39
|
+
'UNION',
|
|
40
|
+
b
|
|
41
|
+
].join(' '));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Covert result to array
|
|
46
|
+
* @param {any} result table name
|
|
47
|
+
* @return {Array} array
|
|
48
|
+
*/
|
|
49
|
+
makeArray(result) {
|
|
50
|
+
switch (this._typeOf(result)) {
|
|
51
|
+
case 'object': {
|
|
52
|
+
if (Object.keys(result).length)
|
|
53
|
+
return [result];
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
case 'array': {
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
case 'undefined':
|
|
60
|
+
case 'null': {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
default: return [].concat(result);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Covert result to object or null
|
|
68
|
+
* @param {any} result table name
|
|
69
|
+
* @return {Record | null} object or null
|
|
70
|
+
*/
|
|
71
|
+
makeObject(result) {
|
|
72
|
+
switch (this._typeOf(result)) {
|
|
73
|
+
case 'object': {
|
|
74
|
+
if (Object.keys(result).length)
|
|
75
|
+
return result;
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
case 'array': {
|
|
79
|
+
if (result[0] == null)
|
|
80
|
+
return null;
|
|
81
|
+
return result[0];
|
|
82
|
+
}
|
|
83
|
+
case 'undefined':
|
|
84
|
+
case 'null': {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
default: return {
|
|
88
|
+
"0": result
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Assign table name
|
|
94
|
+
* @param {string} table table name
|
|
95
|
+
* @return {this} this
|
|
96
|
+
*/
|
|
97
|
+
table(table) {
|
|
98
|
+
this.$state.set('SELECT', `${this.$constants('SELECT')} *`);
|
|
99
|
+
this.$state.set('TABLE_NAME', `\`${table}\``);
|
|
100
|
+
this.$state.set('FROM', `${this.$constants('FROM')}`);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get constant
|
|
105
|
+
* @param {string} constant
|
|
106
|
+
* @return {string | object} string || object
|
|
107
|
+
*/
|
|
108
|
+
constants(constant) {
|
|
109
|
+
return this.$constants(constant);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* cases query
|
|
113
|
+
* @param {arrayObject} cases array object {when , then }
|
|
114
|
+
* @param {string?} final else condition
|
|
115
|
+
* @return {string} string
|
|
116
|
+
*/
|
|
117
|
+
caseUpdate(cases, final) {
|
|
118
|
+
if (!cases.length)
|
|
119
|
+
return [];
|
|
120
|
+
let query = [];
|
|
121
|
+
for (const c of cases) {
|
|
122
|
+
if (c.when == null)
|
|
123
|
+
throw new Error(`can't find when condition`);
|
|
124
|
+
if (c.then == null)
|
|
125
|
+
throw new Error(`can't find then condition`);
|
|
126
|
+
query = [
|
|
127
|
+
...query,
|
|
128
|
+
`${this.$constants('WHEN')} ${c.when} ${this.$constants('THEN')} ${c.then}`
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
return [
|
|
132
|
+
this.$constants('RAW'),
|
|
133
|
+
this.$constants('CASE'),
|
|
134
|
+
query.join(' '),
|
|
135
|
+
final == null ? '' : `ELSE ${final}`,
|
|
136
|
+
this.$constants('END')
|
|
137
|
+
].join(' ');
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* generate UUID
|
|
141
|
+
* @return {string} string
|
|
142
|
+
*/
|
|
143
|
+
generateUUID() {
|
|
144
|
+
return this.$utils.generateUUID();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Assign raw query for schema validation
|
|
148
|
+
* @param {string} sql
|
|
149
|
+
* @return {string} string
|
|
150
|
+
*/
|
|
151
|
+
raw(sql) {
|
|
152
|
+
return `${this.$constants('RAW')} ${sql}`;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get a pool connection
|
|
156
|
+
* @param {Object} options options for connection database with credentials
|
|
157
|
+
* @param {string} option.host
|
|
158
|
+
* @param {number} option.port
|
|
159
|
+
* @param {string} option.database
|
|
160
|
+
* @param {string} option.username
|
|
161
|
+
* @param {string} option.password
|
|
162
|
+
* @return {Connection}
|
|
163
|
+
*/
|
|
164
|
+
getConnection(options) {
|
|
165
|
+
const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
|
|
166
|
+
const pool = new connection_1.PoolConnection(Object.assign({ host,
|
|
167
|
+
port,
|
|
168
|
+
database,
|
|
169
|
+
user,
|
|
170
|
+
password }, others));
|
|
171
|
+
return pool.connection();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Covert result to array
|
|
175
|
+
* @param {any} result table name
|
|
176
|
+
* @return {Array} array
|
|
177
|
+
*/
|
|
178
|
+
static makeArray(result) {
|
|
179
|
+
switch (new this()._typeOf(result)) {
|
|
180
|
+
case 'object': {
|
|
181
|
+
if (Object.keys(result).length)
|
|
182
|
+
return [result];
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
case 'array': {
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
case 'undefined':
|
|
189
|
+
case 'null': {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
default: return [].concat(result);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Covert result to object | null
|
|
197
|
+
* @param {any} result table name
|
|
198
|
+
* @return {Record | null} object | null
|
|
199
|
+
*/
|
|
200
|
+
static makeObject(result) {
|
|
201
|
+
switch (new this()._typeOf(result)) {
|
|
202
|
+
case 'object': {
|
|
203
|
+
if (Object.keys(result).length)
|
|
204
|
+
return result;
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
case 'array': {
|
|
208
|
+
if (result[0] == null)
|
|
209
|
+
return null;
|
|
210
|
+
return result[0];
|
|
211
|
+
}
|
|
212
|
+
case 'undefined':
|
|
213
|
+
case 'null': {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
default: return {
|
|
217
|
+
"0": result
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a connection
|
|
223
|
+
* @return {ConnectionTransaction} object - Connection for the transaction
|
|
224
|
+
* @type {object} connection
|
|
225
|
+
* @property {function} connection.query - execute query sql then release connection to pool
|
|
226
|
+
* @property {function} connection.startTransaction - start transaction of query
|
|
227
|
+
* @property {function} connection.commit - commit transaction of query
|
|
228
|
+
* @property {function} connection.rollback - rollback transaction of query
|
|
229
|
+
*/
|
|
230
|
+
beginTransaction() {
|
|
231
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
const pool = yield this.$pool.get();
|
|
233
|
+
return yield pool.connection();
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Assign table name
|
|
238
|
+
* @static
|
|
239
|
+
* @param {string} table table name
|
|
240
|
+
* @return {DB} DB
|
|
241
|
+
*/
|
|
242
|
+
static table(table) {
|
|
243
|
+
const self = new this();
|
|
244
|
+
self.$state.set('SELECT', `${self.$constants('SELECT')} *`);
|
|
245
|
+
self.$state.set('TABLE_NAME', `\`${table}\``);
|
|
246
|
+
self.$state.set('FROM', `${self.$constants('FROM')}`);
|
|
247
|
+
return self;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* select by cases
|
|
251
|
+
* @static
|
|
252
|
+
* @param {arrayObject} cases array object {when , then }
|
|
253
|
+
* @param {string?} final else condition
|
|
254
|
+
* @return {this}
|
|
255
|
+
*/
|
|
256
|
+
static caseUpdate(cases, final) {
|
|
257
|
+
if (!cases.length)
|
|
258
|
+
return [];
|
|
259
|
+
const self = new this();
|
|
260
|
+
let query = [];
|
|
261
|
+
for (const c of cases) {
|
|
262
|
+
if (c.when == null)
|
|
263
|
+
throw new Error(`can't find when condition`);
|
|
264
|
+
if (c.then == null)
|
|
265
|
+
throw new Error(`can't find then condition`);
|
|
266
|
+
query = [
|
|
267
|
+
...query,
|
|
268
|
+
`${self.$constants('WHEN')} ${c.when} ${self.$constants('THEN')} ${c.then}`
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
return [
|
|
272
|
+
self.$constants('RAW'),
|
|
273
|
+
self.$constants('CASE'),
|
|
274
|
+
query.join(' '),
|
|
275
|
+
final == null ? '' : `ELSE ${final}`,
|
|
276
|
+
self.$constants('END'),
|
|
277
|
+
].join(' ');
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Assign raw query for schema validation
|
|
281
|
+
* @static
|
|
282
|
+
* @param {string} sql
|
|
283
|
+
* @return {string} string
|
|
284
|
+
*/
|
|
285
|
+
static raw(sql) {
|
|
286
|
+
return `${new this().$constants('RAW')} ${sql}`;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* generate UUID
|
|
290
|
+
* @static
|
|
291
|
+
* @return {string} string
|
|
292
|
+
*/
|
|
293
|
+
static generateUUID() {
|
|
294
|
+
return new this().$utils.generateUUID();
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Get constant
|
|
298
|
+
* @static
|
|
299
|
+
* @param {string} constant
|
|
300
|
+
* @return {string | object} string || object
|
|
301
|
+
*/
|
|
302
|
+
static constants(constant) {
|
|
303
|
+
return new this().$constants(constant);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get a connection
|
|
307
|
+
* @static
|
|
308
|
+
* @return {ConnectionTransaction} object - Connection for the transaction
|
|
309
|
+
* @type {object} connection
|
|
310
|
+
* @property {function} connection.query - execute query sql then release connection to pool
|
|
311
|
+
* @property {function} connection.startTransaction - start transaction of query
|
|
312
|
+
* @property {function} connection.commit - commit transaction of query
|
|
313
|
+
* @property {function} connection.rollback - rollback transaction of query
|
|
314
|
+
*/
|
|
315
|
+
static beginTransaction() {
|
|
316
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
+
const self = new this();
|
|
318
|
+
const pool = yield self.$pool.get();
|
|
319
|
+
return yield pool.connection();
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Get a pool connection
|
|
324
|
+
* @param {Object} options options for connection database with credentials
|
|
325
|
+
* @param {string} option.host
|
|
326
|
+
* @param {number} option.port
|
|
327
|
+
* @param {string} option.database
|
|
328
|
+
* @param {string} option.username
|
|
329
|
+
* @param {string} option.password
|
|
330
|
+
* @return {Connection}
|
|
331
|
+
*/
|
|
332
|
+
static getConnection(options) {
|
|
333
|
+
const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
|
|
334
|
+
const pool = new connection_1.PoolConnection(Object.assign({ host,
|
|
335
|
+
port,
|
|
336
|
+
database,
|
|
337
|
+
user,
|
|
338
|
+
password }, others));
|
|
339
|
+
return pool.connection();
|
|
340
|
+
}
|
|
341
|
+
_typeOf(data) {
|
|
342
|
+
return Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
|
|
343
|
+
}
|
|
344
|
+
_initialDB() {
|
|
345
|
+
this.$state = (() => {
|
|
346
|
+
let db = new Map(Object.entries(Object.assign({}, this.$constants('DB'))));
|
|
347
|
+
let original = new Map(Object.entries(Object.assign({}, this.$constants('DB'))));
|
|
348
|
+
return {
|
|
349
|
+
original: () => original,
|
|
350
|
+
get: (key) => {
|
|
351
|
+
if (key == null)
|
|
352
|
+
return db;
|
|
353
|
+
if (!db.has(key))
|
|
354
|
+
throw new Error(`can't get this [${key}]`);
|
|
355
|
+
return db.get(key);
|
|
356
|
+
},
|
|
357
|
+
set: (key, value) => {
|
|
358
|
+
if (!db.has(key))
|
|
359
|
+
throw new Error(`can't set this [${key}]`);
|
|
360
|
+
db.set(key, value);
|
|
361
|
+
return;
|
|
362
|
+
},
|
|
363
|
+
clone: (data) => {
|
|
364
|
+
db = new Map(Object.entries(Object.assign({}, data)));
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
})();
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
exports.DB = DB;
|
|
373
|
+
exports.default = DB;
|