tspace-mysql 1.1.8 → 1.2.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 +115 -38
- package/dist/cli/generate/make.d.ts +4 -0
- package/dist/cli/generate/make.js +45 -0
- package/dist/cli/index.js +27 -13
- package/dist/cli/migrate/make.js +5 -4
- package/dist/cli/models/make.d.ts +1 -1
- package/dist/cli/models/make.js +2 -2
- package/dist/cli/models/model.js +3 -4
- package/dist/cli/query/index.d.ts +4 -0
- package/dist/cli/query/index.js +7 -0
- package/dist/lib/connection/index.d.ts +8 -32
- package/dist/lib/connection/index.js +50 -37
- package/dist/lib/connection/options.d.ts +4 -0
- package/dist/lib/{config/env.js → connection/options.js} +10 -7
- package/dist/lib/constants/index.d.ts +2 -2
- package/dist/lib/constants/index.js +14 -11
- package/dist/lib/tspace/AbstractDB.d.ts +2 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +23 -19
- package/dist/lib/tspace/AbstractDatabase.js +29 -26
- package/dist/lib/tspace/AbstractModel.d.ts +5 -4
- package/dist/lib/tspace/Blueprint.js +4 -2
- package/dist/lib/tspace/DB.d.ts +23 -2
- package/dist/lib/tspace/DB.js +93 -30
- package/dist/lib/tspace/Database.d.ts +26 -13
- package/dist/lib/tspace/Database.js +920 -777
- package/dist/lib/tspace/Interface.d.ts +26 -0
- package/dist/lib/tspace/Logger.js +5 -4
- package/dist/lib/tspace/Model.d.ts +73 -23
- package/dist/lib/tspace/Model.js +1208 -867
- package/dist/lib/tspace/ProxyHandler.d.ts +9 -1
- package/dist/lib/tspace/ProxyHandler.js +8 -9
- package/dist/lib/tspace/Schema.js +35 -22
- package/dist/lib/utils/index.d.ts +0 -1
- package/dist/lib/utils/index.js +15 -44
- package/package.json +4 -2
- package/dist/lib/config/env.d.ts +0 -4
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
declare const proxyHandler: {
|
|
2
2
|
set: (self: any, name: string, value: any) => boolean;
|
|
3
|
-
get: (self:
|
|
3
|
+
get: (self: {
|
|
4
|
+
[x: string]: any;
|
|
5
|
+
$db: {
|
|
6
|
+
get: (arg: string) => string;
|
|
7
|
+
};
|
|
8
|
+
$logger: {
|
|
9
|
+
get: () => any;
|
|
10
|
+
};
|
|
11
|
+
}, prop: string, value: unknown) => any;
|
|
4
12
|
};
|
|
5
13
|
export { proxyHandler };
|
|
6
14
|
export default proxyHandler;
|
|
@@ -4,27 +4,26 @@ exports.proxyHandler = void 0;
|
|
|
4
4
|
const Logger_1 = require("./Logger");
|
|
5
5
|
const proxyHandler = {
|
|
6
6
|
set: (self, name, value) => {
|
|
7
|
-
|
|
7
|
+
var _a;
|
|
8
|
+
if ((_a = self.$setters) === null || _a === void 0 ? void 0 : _a.includes(name))
|
|
8
9
|
throw new Error(`no allow to set this ${name}`);
|
|
9
|
-
self.$attributes = {
|
|
10
|
-
...self.$attributes,
|
|
11
|
-
[name]: value
|
|
12
|
-
};
|
|
10
|
+
self.$attributes = Object.assign(Object.assign({}, self.$attributes), { [name]: value });
|
|
13
11
|
return true;
|
|
14
12
|
},
|
|
15
13
|
get: (self, prop, value) => {
|
|
14
|
+
var _a, _b, _c, _d;
|
|
16
15
|
try {
|
|
17
16
|
new Logger_1.Logger(self, prop);
|
|
18
17
|
switch (prop) {
|
|
19
|
-
case 'tableName': return self.$db
|
|
18
|
+
case 'tableName': return (_b = (_a = self.$db) === null || _a === void 0 ? void 0 : _a.get('TABLE_NAME')) === null || _b === void 0 ? void 0 : _b.replace(/`/g, '');
|
|
20
19
|
case 'attributes': return self[`$${prop}`];
|
|
21
|
-
case 'logger': return self.$logger
|
|
22
|
-
case 'result': return self.$db
|
|
20
|
+
case 'logger': return (_c = self.$logger) === null || _c === void 0 ? void 0 : _c.get();
|
|
21
|
+
case 'result': return (_d = self.$db) === null || _d === void 0 ? void 0 : _d.get('RESULT');
|
|
23
22
|
default: return Reflect.get(self, prop, value);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
catch (e) {
|
|
27
|
-
throw new Error(e
|
|
26
|
+
throw new Error(e === null || e === void 0 ? void 0 : e.message);
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
29
|
};
|
|
@@ -1,31 +1,44 @@
|
|
|
1
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Schema = void 0;
|
|
4
13
|
const Database_1 = require("./Database");
|
|
5
14
|
class Schema extends Database_1.Database {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this.table = (table, schemas) => __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
var _a;
|
|
19
|
+
try {
|
|
20
|
+
let columns = [];
|
|
21
|
+
for (const key in schemas) {
|
|
22
|
+
const data = schemas[key];
|
|
23
|
+
const { type, attrbuites } = data;
|
|
24
|
+
columns = [
|
|
25
|
+
...columns,
|
|
26
|
+
`${key} ${type} ${attrbuites === null || attrbuites === void 0 ? void 0 : attrbuites.join(' ')}`
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
const sql = [
|
|
30
|
+
`CREATE TABLE IF NOT EXISTS`,
|
|
31
|
+
`${table} (${columns === null || columns === void 0 ? void 0 : columns.join(',')})`,
|
|
32
|
+
`ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8`
|
|
33
|
+
].join(' ');
|
|
34
|
+
yield this.rawQuery(sql);
|
|
35
|
+
console.log(`Migrats : '${table}' created successfully`);
|
|
16
36
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
await this.rawQuery(sql);
|
|
23
|
-
console.log(`Migrats : '${table}' created successfully`);
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
console.log(err.message?.replace("ER_TABLE_EXISTS_ERROR: ", ""));
|
|
27
|
-
}
|
|
28
|
-
};
|
|
37
|
+
catch (err) {
|
|
38
|
+
console.log((_a = err.message) === null || _a === void 0 ? void 0 : _a.replace(/ER_TABLE_EXISTS_ERROR:/g, ""));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
29
42
|
}
|
|
30
43
|
exports.Schema = Schema;
|
|
31
44
|
exports.default = Schema;
|
package/dist/lib/utils/index.js
CHANGED
|
@@ -34,16 +34,13 @@ const escape = (str) => {
|
|
|
34
34
|
const check = str == null || str === true || str === false || Number.isInteger(str);
|
|
35
35
|
if (check)
|
|
36
36
|
return str;
|
|
37
|
-
const regx = /[+#$&*=;\\|,\?~]/;
|
|
38
|
-
let res = str.split(regx).join("");
|
|
39
|
-
const temp = str.split(regx).join("").toLocaleLowerCase();
|
|
40
37
|
const regxs = ['DROP TABLE', 'UPDATE ', 'DELETE FROM ', 'OR', 'SELECT ', 'FROM ', 'WHERE ', 'OR'];
|
|
41
38
|
for (let i in regxs) {
|
|
42
|
-
if (
|
|
43
|
-
|
|
39
|
+
if (str.includes(regxs[i].toLocaleLowerCase())) {
|
|
40
|
+
str = str.split(regxs[i]).join("");
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
|
-
return
|
|
43
|
+
return str;
|
|
47
44
|
}
|
|
48
45
|
catch (e) {
|
|
49
46
|
return str;
|
|
@@ -53,25 +50,20 @@ const escapeSubQuery = (str) => {
|
|
|
53
50
|
const check = str == null || str === true || str === false || Number.isInteger(str);
|
|
54
51
|
if (check)
|
|
55
52
|
return str;
|
|
56
|
-
const regx = /[+#$&;\\|\?~]/;
|
|
57
|
-
let result = str.split(regx).join("");
|
|
58
53
|
const regxs = ['DROP TABLE', 'UPDATE ', 'DELETE FROM ', 'TRUNCATE'];
|
|
59
54
|
for (let i in regxs) {
|
|
60
|
-
if (
|
|
61
|
-
|
|
55
|
+
if (str.includes(regxs[i])) {
|
|
56
|
+
str = str.split(regxs[i]).join("");
|
|
62
57
|
}
|
|
63
58
|
}
|
|
64
|
-
return
|
|
59
|
+
return str;
|
|
65
60
|
};
|
|
66
61
|
const columnRelation = (name) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
return `${name.toLocaleLowerCase()}`;
|
|
62
|
+
var _a;
|
|
63
|
+
const matches = (_a = name === null || name === void 0 ? void 0 : name.match(/[A-Z]/g)) !== null && _a !== void 0 ? _a : [];
|
|
64
|
+
if (matches.length < 1)
|
|
65
|
+
return `${name.toLocaleLowerCase()}`;
|
|
66
|
+
return name.replace(matches[0], `_${matches[0].toUpperCase()}`);
|
|
75
67
|
};
|
|
76
68
|
const generateUUID = () => {
|
|
77
69
|
const date = +new Date();
|
|
@@ -94,9 +86,7 @@ const snakeCase = (obj) => {
|
|
|
94
86
|
const newName = oldName.replace(/([A-Z])/g, (str) => `_${str.toLocaleLowerCase()}`);
|
|
95
87
|
if (newName !== oldName) {
|
|
96
88
|
if (obj.hasOwnProperty(oldName)) {
|
|
97
|
-
obj = {
|
|
98
|
-
[newName]: obj[oldName]
|
|
99
|
-
};
|
|
89
|
+
obj = Object.assign(Object.assign({}, obj), { [newName]: obj[oldName] });
|
|
100
90
|
delete obj[oldName];
|
|
101
91
|
}
|
|
102
92
|
}
|
|
@@ -117,9 +107,7 @@ const camelCase = (obj) => {
|
|
|
117
107
|
const newName = oldName.replace(/(.(\_|-|\s)+.)/g, (str) => str[0] + (str[str.length - 1].toUpperCase()));
|
|
118
108
|
if (newName !== oldName) {
|
|
119
109
|
if (obj.hasOwnProperty(oldName)) {
|
|
120
|
-
obj = {
|
|
121
|
-
[newName]: obj[oldName]
|
|
122
|
-
};
|
|
110
|
+
obj = Object.assign(Object.assign({}, obj), { [newName]: obj[oldName] });
|
|
123
111
|
delete obj[oldName];
|
|
124
112
|
}
|
|
125
113
|
}
|
|
@@ -135,7 +123,7 @@ const camelCase = (obj) => {
|
|
|
135
123
|
const consoleDebug = (debug) => {
|
|
136
124
|
if (debug == null)
|
|
137
125
|
return;
|
|
138
|
-
console.log(
|
|
126
|
+
console.log(`-----\n\x1b[33m${debug.replace(/(\r\n|\n|\r|\t)/gm, "").trim()}\x1b[0m`);
|
|
139
127
|
};
|
|
140
128
|
const faker = (value) => {
|
|
141
129
|
if (!value.search('timestamp'))
|
|
@@ -160,22 +148,6 @@ const faker = (value) => {
|
|
|
160
148
|
return Buffer.from(Math.random().toString(36).substring(7)).toString('base64');
|
|
161
149
|
return 'fake data';
|
|
162
150
|
};
|
|
163
|
-
const test = async (testName, callback) => {
|
|
164
|
-
const startTime = process.hrtime();
|
|
165
|
-
const diffTimeSs = (hrtime) => {
|
|
166
|
-
if (hrtime == null)
|
|
167
|
-
return;
|
|
168
|
-
const [start, end] = process.hrtime(hrtime);
|
|
169
|
-
return (start + (end / 1e9)).toFixed(4);
|
|
170
|
-
};
|
|
171
|
-
try {
|
|
172
|
-
await callback();
|
|
173
|
-
console.log(`Test : \x1b[34m ${testName} \x1b[0m ==> \x1b[32m PASSED \x1b[0m ${diffTimeSs(startTime)} s`);
|
|
174
|
-
}
|
|
175
|
-
catch (err) {
|
|
176
|
-
console.log(`Test : \x1b[34m ${testName} \x1b[0m ==> \x1b[31m FAILED \x1b[0m (${err.message}) , ${diffTimeSs(startTime)} s`);
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
151
|
const utils = {
|
|
180
152
|
consoleDebug,
|
|
181
153
|
faker,
|
|
@@ -187,8 +159,7 @@ const utils = {
|
|
|
187
159
|
generateUUID,
|
|
188
160
|
covertBooleanToNumber,
|
|
189
161
|
snakeCase,
|
|
190
|
-
camelCase
|
|
191
|
-
test
|
|
162
|
+
camelCase
|
|
192
163
|
};
|
|
193
164
|
exports.utils = utils;
|
|
194
165
|
exports.default = utils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tspace-mysql",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "mysql query builder object relational mapping",
|
|
5
5
|
"main": "dist/lib/index.js",
|
|
6
6
|
"types": "dist/lib/index.d.ts",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"keywords": [
|
|
18
18
|
"tspace-mysql",
|
|
19
19
|
"tspace",
|
|
20
|
+
"mysql",
|
|
20
21
|
"orm",
|
|
21
22
|
"relations",
|
|
22
23
|
"object relation model",
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
"devDependencies": {
|
|
51
52
|
"@types/mysql": "^2.15.19",
|
|
52
53
|
"@types/node": "^17.0.29",
|
|
53
|
-
"@types/pluralize": "0.0.29"
|
|
54
|
+
"@types/pluralize": "0.0.29",
|
|
55
|
+
"tspace-mysql": "^1.1.8"
|
|
54
56
|
}
|
|
55
57
|
}
|
package/dist/lib/config/env.d.ts
DELETED