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.
Files changed (36) hide show
  1. package/README.md +115 -38
  2. package/dist/cli/generate/make.d.ts +4 -0
  3. package/dist/cli/generate/make.js +45 -0
  4. package/dist/cli/index.js +27 -13
  5. package/dist/cli/migrate/make.js +5 -4
  6. package/dist/cli/models/make.d.ts +1 -1
  7. package/dist/cli/models/make.js +2 -2
  8. package/dist/cli/models/model.js +3 -4
  9. package/dist/cli/query/index.d.ts +4 -0
  10. package/dist/cli/query/index.js +7 -0
  11. package/dist/lib/connection/index.d.ts +8 -32
  12. package/dist/lib/connection/index.js +50 -37
  13. package/dist/lib/connection/options.d.ts +4 -0
  14. package/dist/lib/{config/env.js → connection/options.js} +10 -7
  15. package/dist/lib/constants/index.d.ts +2 -2
  16. package/dist/lib/constants/index.js +14 -11
  17. package/dist/lib/tspace/AbstractDB.d.ts +2 -0
  18. package/dist/lib/tspace/AbstractDatabase.d.ts +23 -19
  19. package/dist/lib/tspace/AbstractDatabase.js +29 -26
  20. package/dist/lib/tspace/AbstractModel.d.ts +5 -4
  21. package/dist/lib/tspace/Blueprint.js +4 -2
  22. package/dist/lib/tspace/DB.d.ts +23 -2
  23. package/dist/lib/tspace/DB.js +93 -30
  24. package/dist/lib/tspace/Database.d.ts +26 -13
  25. package/dist/lib/tspace/Database.js +920 -777
  26. package/dist/lib/tspace/Interface.d.ts +26 -0
  27. package/dist/lib/tspace/Logger.js +5 -4
  28. package/dist/lib/tspace/Model.d.ts +73 -23
  29. package/dist/lib/tspace/Model.js +1208 -867
  30. package/dist/lib/tspace/ProxyHandler.d.ts +9 -1
  31. package/dist/lib/tspace/ProxyHandler.js +8 -9
  32. package/dist/lib/tspace/Schema.js +35 -22
  33. package/dist/lib/utils/index.d.ts +0 -1
  34. package/dist/lib/utils/index.js +15 -44
  35. package/package.json +4 -2
  36. 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: any, prop: any, value: any) => any;
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
- if (self.$setters?.includes(name))
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?.get('TABLE_NAME')?.replace(/`/g, '');
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?.get();
22
- case 'result': return self.$db?.get('RESULT');
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?.message);
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
- table = async (table, schemas) => {
7
- try {
8
- let columns = [];
9
- for (const key in schemas) {
10
- const data = schemas[key];
11
- const { type, attrbuites } = data;
12
- columns = [
13
- ...columns,
14
- `${key} ${type} ${attrbuites?.join(' ')}`
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
- const sql = [
18
- `CREATE TABLE IF NOT EXISTS`,
19
- `${table} (${columns?.join(',')})`,
20
- `ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8`
21
- ].join(' ');
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;
@@ -10,7 +10,6 @@ declare const utils: {
10
10
  covertBooleanToNumber: (data: any) => any;
11
11
  snakeCase: (obj: any) => any;
12
12
  camelCase: (obj: any) => any;
13
- test: (testName: string, callback: Function) => Promise<void>;
14
13
  };
15
14
  export { utils };
16
15
  export default utils;
@@ -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 (temp.includes(regxs[i].toLocaleLowerCase())) {
43
- res = res.split(regxs[i]).join("");
39
+ if (str.includes(regxs[i].toLocaleLowerCase())) {
40
+ str = str.split(regxs[i]).join("");
44
41
  }
45
42
  }
46
- return res;
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 (result.includes(regxs[i])) {
61
- result = result.split(regxs[i]).join("");
55
+ if (str.includes(regxs[i])) {
56
+ str = str.split(regxs[i]).join("");
62
57
  }
63
58
  }
64
- return result;
59
+ return str;
65
60
  };
66
61
  const columnRelation = (name) => {
67
- const matches = name?.match(/[A-Z]/g) ?? [];
68
- if (matches.length > 1) {
69
- matches.forEach((matche, i) => {
70
- if (i > 0)
71
- name = name.replace(matche, `_${matche.toUpperCase()}`);
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 = { ...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 = { ...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(`\x1b[33m${debug}\x1b[0m`);
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.1.8",
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
  }
@@ -1,4 +0,0 @@
1
- declare const _default: Readonly<{
2
- [x: string]: string | number | boolean | null | undefined;
3
- }>;
4
- export default _default;