tspace-mysql 1.5.0 → 1.5.2
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 +1856 -701
- package/build/cli/dump/db.js +3 -2
- package/build/cli/generate/make.js +17 -15
- package/build/cli/generate/modelDecorator.d.ts +1 -1
- package/build/cli/generate/modelDecorator.js +2 -3
- package/build/cli/index.js +45 -27
- package/build/cli/migrate/make.d.ts +1 -1
- package/build/cli/migrate/make.js +2 -2
- package/build/cli/migrations/make-db.d.ts +4 -0
- package/build/cli/migrations/make-db.js +58 -0
- package/build/cli/migrations/make.d.ts +2 -0
- package/build/cli/migrations/make.js +201 -0
- package/build/lib/Interface.d.ts +24 -5
- package/build/lib/connection/index.d.ts +5 -1
- package/build/lib/connection/index.js +65 -8
- package/build/lib/connection/options.js +2 -2
- package/build/lib/constants/index.js +13 -3
- package/build/lib/{tspace → core}/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/{tspace → core}/Abstracts/AbstractModel.d.ts +15 -13
- package/build/lib/{tspace → core}/Blueprint.d.ts +14 -3
- package/build/lib/{tspace → core}/Blueprint.js +30 -4
- package/build/lib/{tspace → core}/Builder.d.ts +140 -44
- package/build/lib/{tspace → core}/Builder.js +772 -459
- package/build/lib/{tspace → core}/DB.d.ts +20 -4
- package/build/lib/{tspace → core}/DB.js +73 -39
- package/build/lib/{tspace → core}/Decorator.js +2 -2
- package/build/lib/{tspace → core/Handlers}/Logger.d.ts +3 -3
- package/build/lib/{tspace → core/Handlers}/Logger.js +4 -4
- package/build/lib/{tspace → core}/Handlers/Proxy.js +2 -2
- package/build/lib/{tspace → core}/Handlers/Relation.d.ts +2 -4
- package/build/lib/{tspace → core}/Handlers/Relation.js +69 -48
- package/build/lib/{tspace → core}/Handlers/State.d.ts +1 -1
- package/build/lib/{tspace → core}/Handlers/State.js +3 -3
- package/build/lib/{tspace → core}/Model.d.ts +358 -133
- package/build/lib/{tspace → core}/Model.js +1316 -558
- package/build/lib/core/Schema.d.ts +137 -0
- package/build/lib/{tspace → core}/Schema.js +147 -52
- package/build/lib/core/Type.d.ts +6 -0
- package/build/lib/core/Type.js +2 -0
- package/build/lib/{tspace → core}/index.d.ts +2 -1
- package/build/lib/{tspace → core}/index.js +3 -2
- package/build/lib/index.d.ts +2 -2
- package/build/lib/index.js +2 -2
- package/build/lib/utils/index.d.ts +1 -0
- package/build/lib/utils/index.js +17 -7
- package/package.json +6 -4
- package/build/lib/tspace/Schema.d.ts +0 -70
- /package/build/lib/{tspace → core}/Abstracts/AbstractBuilder.js +0 -0
- /package/build/lib/{tspace → core}/Abstracts/AbstractDB.d.ts +0 -0
- /package/build/lib/{tspace → core}/Abstracts/AbstractDB.js +0 -0
- /package/build/lib/{tspace → core}/Abstracts/AbstractModel.js +0 -0
- /package/build/lib/{tspace → core}/Decorator.d.ts +0 -0
- /package/build/lib/{tspace → core}/Handlers/Proxy.d.ts +0 -0
package/build/cli/dump/db.js
CHANGED
|
@@ -16,7 +16,7 @@ exports.default = (cmd) => {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
if (sql == null || sql === '') {
|
|
19
|
-
console.log(`Example
|
|
19
|
+
console.log(`Example tspace-mysql dump:db "table" --dir=app/table`);
|
|
20
20
|
process.exit(0);
|
|
21
21
|
}
|
|
22
22
|
if (!values) {
|
|
@@ -30,7 +30,8 @@ exports.default = (cmd) => {
|
|
|
30
30
|
.finally(() => process.exit(0));
|
|
31
31
|
}
|
|
32
32
|
const directory = `${cwd}/${dir}/dump_${+new Date()}.sql`;
|
|
33
|
-
new lib_1.DB().loadEnv(env)
|
|
33
|
+
new lib_1.DB().loadEnv(env)
|
|
34
|
+
.backupToFile({
|
|
34
35
|
filePath: directory,
|
|
35
36
|
database: sql
|
|
36
37
|
})
|
|
@@ -24,17 +24,17 @@ const formatSchema = (data) => {
|
|
|
24
24
|
};
|
|
25
25
|
exports.default = (cmd) => {
|
|
26
26
|
const { dir, cwd, type, fs, decorator, env, npm } = cmd;
|
|
27
|
-
if (dir)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
27
|
+
if (dir == null)
|
|
28
|
+
throw new Error("Cannot find directory please specify the directory : '--dir=${directory}'");
|
|
29
|
+
try {
|
|
30
|
+
fs.accessSync(`${cwd}/${dir}`, fs.F_OK, {
|
|
31
|
+
recursive: true
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
fs.mkdirSync(`${cwd}/${dir}`, {
|
|
36
|
+
recursive: true
|
|
37
|
+
});
|
|
38
38
|
}
|
|
39
39
|
if (decorator) {
|
|
40
40
|
new lib_1.DB()
|
|
@@ -84,8 +84,8 @@ exports.default = (cmd) => {
|
|
|
84
84
|
str += ` ${s.schemaColumn} \n`;
|
|
85
85
|
str += ` ${s.publicColumn} ${isLast ? '' : '\n\n'}`;
|
|
86
86
|
}
|
|
87
|
-
const data = (0, modelDecorator_1.default)(model, npm, str
|
|
88
|
-
fs.writeFile(
|
|
87
|
+
const data = (0, modelDecorator_1.default)(model, npm, str);
|
|
88
|
+
fs.writeFile(`${cwd}/${dir}/${model}${type !== null && type !== void 0 ? type : '.ts'}`, data, (err) => {
|
|
89
89
|
if (err)
|
|
90
90
|
throw err;
|
|
91
91
|
});
|
|
@@ -94,7 +94,8 @@ exports.default = (cmd) => {
|
|
|
94
94
|
}
|
|
95
95
|
console.log('\nGenerate Models has completed');
|
|
96
96
|
})
|
|
97
|
-
.catch(err => console.log(err))
|
|
97
|
+
.catch(err => console.log(err))
|
|
98
|
+
.finally(() => process.exit(0));
|
|
98
99
|
return;
|
|
99
100
|
}
|
|
100
101
|
new lib_1.DB()
|
|
@@ -141,5 +142,6 @@ exports.default = (cmd) => {
|
|
|
141
142
|
}
|
|
142
143
|
console.log('\nGenerate Models has completed');
|
|
143
144
|
})
|
|
144
|
-
.catch(err => console.log(err))
|
|
145
|
+
.catch(err => console.log(err))
|
|
146
|
+
.finally(() => process.exit(0));
|
|
145
147
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const Model: (model: string, npm: string, schema: string
|
|
1
|
+
declare const Model: (model: string, npm: string, schema: string) => string;
|
|
2
2
|
export default Model;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const Model = (model, npm, schema
|
|
4
|
-
return `import { Model , Blueprint , Column
|
|
3
|
+
const Model = (model, npm, schema) => {
|
|
4
|
+
return `import { Model , Blueprint , Column } from '${npm}'
|
|
5
5
|
|
|
6
|
-
@Table('${table}')
|
|
7
6
|
class ${model} extends Model {
|
|
8
7
|
|
|
9
8
|
${schema}
|
package/build/cli/index.js
CHANGED
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3;
|
|
6
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10;
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const make_1 = __importDefault(require("./models/make"));
|
|
10
10
|
const make_2 = __importDefault(require("./tables/make"));
|
|
11
11
|
const make_3 = __importDefault(require("./migrate/make"));
|
|
12
12
|
const make_4 = __importDefault(require("./generate/make"));
|
|
13
|
+
const make_5 = __importDefault(require("./migrations/make"));
|
|
14
|
+
const make_db_1 = __importDefault(require("./migrations/make-db"));
|
|
13
15
|
const query_1 = __importDefault(require("./query"));
|
|
14
16
|
const db_1 = __importDefault(require("./dump/db"));
|
|
15
17
|
const table_1 = __importDefault(require("./dump/table"));
|
|
@@ -22,7 +24,28 @@ const commands = {
|
|
|
22
24
|
'generate:models': make_4.default,
|
|
23
25
|
'gen:models': make_4.default,
|
|
24
26
|
'dump:db': db_1.default,
|
|
25
|
-
'dump:table': table_1.default
|
|
27
|
+
'dump:table': table_1.default,
|
|
28
|
+
'migrations:models': make_5.default,
|
|
29
|
+
'migrations:db': make_db_1.default,
|
|
30
|
+
'help': () => {
|
|
31
|
+
console.log(`
|
|
32
|
+
\x1b[31m
|
|
33
|
+
tspace-mysql make:model User --m --dir=app/Models
|
|
34
|
+
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
35
|
+
tspace-mysql migrate --dir=App/Models/Migrations --type=js
|
|
36
|
+
tspace-mysql query "SELECT * FROM users" --env=development
|
|
37
|
+
tspace-mysql generate:models --dir=app/Models --env=development
|
|
38
|
+
tspace-mysql generate:models --dir=app/Models --env=development --decorators
|
|
39
|
+
tspace-mysql dump:db "database" --dir=app/db --v --env=development
|
|
40
|
+
tspace-mysql dump:table "table" --dir=app/table --v --env=development
|
|
41
|
+
tspace-mysql migrations:models --dir=migrations --models=src/models --generate
|
|
42
|
+
tspace-mysql migrations:models --dir=migrations --models=src/models --push
|
|
43
|
+
tspace-mysql migrations:db --dir=migrations --generate
|
|
44
|
+
tspace-mysql migrations:db --dir=migrations --push
|
|
45
|
+
\x1b[0m
|
|
46
|
+
`);
|
|
47
|
+
console.log(`Read more https://www.npmjs.com/package/tspace-mysql`);
|
|
48
|
+
}
|
|
26
49
|
};
|
|
27
50
|
try {
|
|
28
51
|
const name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(data => {
|
|
@@ -33,28 +56,34 @@ try {
|
|
|
33
56
|
const dir = (_j = (_h = (_g = process.argv.slice(2)) === null || _g === void 0 ? void 0 : _g.find(data => {
|
|
34
57
|
return data === null || data === void 0 ? void 0 : data.includes('--dir=');
|
|
35
58
|
})) === null || _h === void 0 ? void 0 : _h.replace('--dir=', '/')) !== null && _j !== void 0 ? _j : null;
|
|
36
|
-
const
|
|
59
|
+
const models = (_m = (_l = (_k = process.argv.slice(2)) === null || _k === void 0 ? void 0 : _k.find(data => {
|
|
60
|
+
return data === null || data === void 0 ? void 0 : data.includes('--models=');
|
|
61
|
+
})) === null || _l === void 0 ? void 0 : _l.replace('--models=', '/')) !== null && _m !== void 0 ? _m : null;
|
|
62
|
+
const db = (_q = (_p = (_o = process.argv.slice(2)) === null || _o === void 0 ? void 0 : _o.find(data => {
|
|
37
63
|
return data === null || data === void 0 ? void 0 : data.includes('--db=');
|
|
38
|
-
})) === null ||
|
|
39
|
-
const table = (
|
|
64
|
+
})) === null || _p === void 0 ? void 0 : _p.replace('--db=', '')) !== null && _q !== void 0 ? _q : null;
|
|
65
|
+
const table = (_t = (_s = (_r = process.argv.slice(2)) === null || _r === void 0 ? void 0 : _r.find(data => {
|
|
40
66
|
return data === null || data === void 0 ? void 0 : data.includes('--table=');
|
|
41
|
-
})) === null ||
|
|
42
|
-
let type = ((
|
|
67
|
+
})) === null || _s === void 0 ? void 0 : _s.replace('--table=', '')) !== null && _t !== void 0 ? _t : null;
|
|
68
|
+
let type = ((_w = (_v = (_u = process.argv.slice(2)) === null || _u === void 0 ? void 0 : _u.find(data => {
|
|
43
69
|
return data === null || data === void 0 ? void 0 : data.includes('--type=');
|
|
44
|
-
})) === null ||
|
|
70
|
+
})) === null || _v === void 0 ? void 0 : _v.replace('--type=', '.')) !== null && _w !== void 0 ? _w : '.ts');
|
|
45
71
|
type = ['.js', '.ts'].includes(type) ? type : '.ts';
|
|
46
|
-
const file = (
|
|
47
|
-
const env = (
|
|
72
|
+
const file = (_x = process.argv.slice(3)[0]) !== null && _x !== void 0 ? _x : '';
|
|
73
|
+
const env = (_0 = (_z = (_y = process.argv.slice(2)) === null || _y === void 0 ? void 0 : _y.find(data => {
|
|
48
74
|
return data === null || data === void 0 ? void 0 : data.includes('--env=');
|
|
49
|
-
})) === null ||
|
|
50
|
-
const values = (
|
|
51
|
-
const decorator = (
|
|
75
|
+
})) === null || _z === void 0 ? void 0 : _z.replace('--env=', '')) !== null && _0 !== void 0 ? _0 : null;
|
|
76
|
+
const values = (_3 = (((_1 = process.argv.slice(2)) === null || _1 === void 0 ? void 0 : _1.includes('--values')) || ((_2 = process.argv.slice(2)) === null || _2 === void 0 ? void 0 : _2.includes('--v')))) !== null && _3 !== void 0 ? _3 : false;
|
|
77
|
+
const decorator = (_6 = (((_4 = process.argv.slice(2)) === null || _4 === void 0 ? void 0 : _4.includes('--decorator')) || ((_5 = process.argv.slice(2)) === null || _5 === void 0 ? void 0 : _5.includes('--decorators')))) !== null && _6 !== void 0 ? _6 : false;
|
|
78
|
+
const push = (_8 = (_7 = process.argv.slice(2)) === null || _7 === void 0 ? void 0 : _7.includes('--push')) !== null && _8 !== void 0 ? _8 : false;
|
|
79
|
+
const generate = (_10 = (_9 = process.argv.slice(2)) === null || _9 === void 0 ? void 0 : _9.includes('--generate')) !== null && _10 !== void 0 ? _10 : false;
|
|
52
80
|
if (env != null)
|
|
53
81
|
process.env.NODE_ENV = env;
|
|
54
82
|
const cmd = {
|
|
55
83
|
name,
|
|
56
84
|
file,
|
|
57
85
|
dir,
|
|
86
|
+
models,
|
|
58
87
|
migrate,
|
|
59
88
|
type,
|
|
60
89
|
cwd: process.cwd(),
|
|
@@ -65,23 +94,12 @@ try {
|
|
|
65
94
|
values,
|
|
66
95
|
decorator,
|
|
67
96
|
env,
|
|
97
|
+
push,
|
|
98
|
+
generate,
|
|
68
99
|
npm: 'tspace-mysql'
|
|
69
100
|
};
|
|
70
101
|
commands[process.argv[2]](cmd);
|
|
71
102
|
}
|
|
72
103
|
catch (err) {
|
|
73
|
-
console.log(
|
|
74
|
-
\x1b[31m
|
|
75
|
-
tspace-mysql make:model User --m --dir=app/Models
|
|
76
|
-
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
77
|
-
tspace-mysql migrate --dir=App/Models/Migrations --type=js
|
|
78
|
-
tspace-mysql query "SELECT * FROM users" --env=development
|
|
79
|
-
tspace-mysql generate:models --dir=app/Models --env=development
|
|
80
|
-
tspace-mysql generate:models --dir=app/Models --env=development --decorators
|
|
81
|
-
tspace-mysql dump:db "database" --dir=app/db --v --env=development
|
|
82
|
-
tspace-mysql dump:table "table" --dir=app/table --v --env=development
|
|
83
|
-
\x1b[0m
|
|
84
|
-
`);
|
|
85
|
-
console.log(`Read more https://www.npmjs.com/package/tspace-mysql`);
|
|
86
|
-
process.exit(0);
|
|
104
|
+
console.log("The input command failed. Please try again using 'tspace-mysql help'");
|
|
87
105
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const child_process_1 = require("child_process");
|
|
4
|
-
exports.default = (
|
|
4
|
+
exports.default = (cmd) => {
|
|
5
5
|
var _a, _b, _c;
|
|
6
|
-
const { type, dir, cwd, fs } =
|
|
6
|
+
const { type, dir, cwd, fs } = cmd;
|
|
7
7
|
try {
|
|
8
8
|
if (dir == null)
|
|
9
9
|
throw new Error('Not found directory');
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const lib_1 = require("../../lib");
|
|
4
|
+
exports.default = (cmd) => {
|
|
5
|
+
const { dir, cwd, fs, env, sql, push, generate } = cmd;
|
|
6
|
+
if (dir) {
|
|
7
|
+
try {
|
|
8
|
+
fs.accessSync(`${cwd}/${dir}`, fs.F_OK, {
|
|
9
|
+
recursive: true
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
fs.mkdirSync(`${cwd}/${dir}`, {
|
|
14
|
+
recursive: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (push) {
|
|
19
|
+
const filePath = `${cwd}/${dir}/migrations.sql`;
|
|
20
|
+
const sqlString = fs.readFileSync(filePath, 'utf8');
|
|
21
|
+
const sqlStatements = sqlString.split(';');
|
|
22
|
+
for (const sql of sqlStatements) {
|
|
23
|
+
if (sql.trim() === '')
|
|
24
|
+
continue;
|
|
25
|
+
const createTableStatements = sql
|
|
26
|
+
.split(';')
|
|
27
|
+
.filter((statement) => statement.trim().startsWith('CREATE TABLE IF NOT EXISTS'));
|
|
28
|
+
const table = createTableStatements.map((statement) => {
|
|
29
|
+
const tableNameMatch = statement.match(/`([^`]+)`/);
|
|
30
|
+
return tableNameMatch && tableNameMatch[1];
|
|
31
|
+
})[0];
|
|
32
|
+
new lib_1.DB()
|
|
33
|
+
.loadEnv(env)
|
|
34
|
+
.rawQuery(`${sql}`)
|
|
35
|
+
.then(_ => console.log(table == null
|
|
36
|
+
? `The query '${sql}' has been successfully`
|
|
37
|
+
: `The table '${table}' has been successfully`))
|
|
38
|
+
.catch(e => console.log(`Failed to push changes errors: '${e.message}'`));
|
|
39
|
+
}
|
|
40
|
+
return process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
if (sql == null || sql === '') {
|
|
43
|
+
return process.exit(0);
|
|
44
|
+
}
|
|
45
|
+
if (generate) {
|
|
46
|
+
const directory = `${cwd}/${dir}/migrations.sql`;
|
|
47
|
+
new lib_1.DB()
|
|
48
|
+
.loadEnv(env)
|
|
49
|
+
.backupToFile({
|
|
50
|
+
filePath: directory,
|
|
51
|
+
database: sql
|
|
52
|
+
})
|
|
53
|
+
.then(_ => console.log(`Migrations are migrating successfully`))
|
|
54
|
+
.catch(e => console.log(`Failed to migrate errors: '${e.message}'`))
|
|
55
|
+
.finally(() => process.exit(0));
|
|
56
|
+
}
|
|
57
|
+
throw new Error("Do you want to generate or push changes ? use '--generate' or '--push");
|
|
58
|
+
};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const lib_1 = require("../../lib");
|
|
39
|
+
const child_process_1 = require("child_process");
|
|
40
|
+
const sql_formatter_1 = require("sql-formatter");
|
|
41
|
+
const path_1 = __importDefault(require("path"));
|
|
42
|
+
exports.default = (cmd) => {
|
|
43
|
+
const { dir, cwd, fs, generate, push, models, env } = cmd;
|
|
44
|
+
if (models == null)
|
|
45
|
+
throw new Error("Cannot find directory to your models please specify the directory : '--models=src/app/models'");
|
|
46
|
+
if (dir == null)
|
|
47
|
+
throw new Error("Cannot find directory please specify the directory : '--dir=${directory}'");
|
|
48
|
+
try {
|
|
49
|
+
fs.accessSync(`${cwd}/${dir}`, fs.F_OK, {
|
|
50
|
+
recursive: true
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
fs.mkdirSync(`${cwd}/${dir}`, {
|
|
55
|
+
recursive: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const migrations = (pathFolders) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
const directories = fs.readdirSync(pathFolders, { withFileTypes: true });
|
|
60
|
+
const files = (yield Promise.all(directories.map((directory) => {
|
|
61
|
+
const newDir = path_1.default.resolve(String(pathFolders), directory.name);
|
|
62
|
+
if (directory.isDirectory() && directory.name.toLocaleLowerCase().includes('migrations'))
|
|
63
|
+
return null;
|
|
64
|
+
return directory.isDirectory() ? lib_1.Schema.sync(newDir) : newDir;
|
|
65
|
+
})));
|
|
66
|
+
let pathModels = [].concat(...files).filter(d => d != null || d === '');
|
|
67
|
+
yield new Promise(r => setTimeout(r, 3000));
|
|
68
|
+
const isFileTs = pathModels.some(pathModel => /\.ts$/.test(pathModel) && !(/\.d\.ts$/.test(pathModel)));
|
|
69
|
+
const outDirForBuildTs = '__tmp-migrations-ts__';
|
|
70
|
+
if (isFileTs) {
|
|
71
|
+
for (const pathModel of pathModels) {
|
|
72
|
+
const command = `tsc "${pathModel}" --outDir ${outDirForBuildTs} --target es6 --esModuleInterop --module commonjs --allowJs`;
|
|
73
|
+
try {
|
|
74
|
+
(0, child_process_1.execSync)(command, { stdio: 'inherit' });
|
|
75
|
+
}
|
|
76
|
+
catch (error) { }
|
|
77
|
+
}
|
|
78
|
+
const directories = fs.readdirSync(outDirForBuildTs, { withFileTypes: true });
|
|
79
|
+
const files = (yield Promise.all(directories.map((directory) => {
|
|
80
|
+
const newDir = path_1.default.resolve(String(outDirForBuildTs), directory.name);
|
|
81
|
+
if (directory.isDirectory() && directory.name.toLocaleLowerCase().includes('migrations'))
|
|
82
|
+
return null;
|
|
83
|
+
return directory.isDirectory() ? lib_1.Schema.sync(newDir) : newDir;
|
|
84
|
+
})));
|
|
85
|
+
pathModels = [].concat(...files).filter(d => d != null || d === '');
|
|
86
|
+
}
|
|
87
|
+
const models = yield Promise.all(pathModels.map((pathModel) => _import(pathModel)).filter(d => d != null));
|
|
88
|
+
if (isFileTs) {
|
|
89
|
+
const removeFolderRecursive = (folderPath) => {
|
|
90
|
+
if (fs.existsSync(folderPath)) {
|
|
91
|
+
fs.readdirSync(folderPath).forEach((file) => {
|
|
92
|
+
const curPath = path_1.default.join(folderPath, file);
|
|
93
|
+
if (fs.lstatSync(curPath).isDirectory())
|
|
94
|
+
removeFolderRecursive(curPath);
|
|
95
|
+
fs.unlinkSync(curPath);
|
|
96
|
+
});
|
|
97
|
+
fs.rmdirSync(folderPath);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
removeFolderRecursive(outDirForBuildTs);
|
|
101
|
+
}
|
|
102
|
+
if (!models.length)
|
|
103
|
+
return;
|
|
104
|
+
const createTableQueries = [];
|
|
105
|
+
const foreignKeyQueries = [];
|
|
106
|
+
for (const model of models) {
|
|
107
|
+
if (model == null)
|
|
108
|
+
continue;
|
|
109
|
+
const schemaModel = model.getSchemaModel();
|
|
110
|
+
if (!schemaModel)
|
|
111
|
+
continue;
|
|
112
|
+
const createTable = new lib_1.Schema().createTable(`\`${model.getTableName()}\``, schemaModel);
|
|
113
|
+
createTableQueries.push(createTable);
|
|
114
|
+
const foreignKey = _foreignKey({
|
|
115
|
+
schemaModel,
|
|
116
|
+
model
|
|
117
|
+
});
|
|
118
|
+
if (foreignKey == null)
|
|
119
|
+
continue;
|
|
120
|
+
foreignKeyQueries.push(foreignKey);
|
|
121
|
+
}
|
|
122
|
+
let sql = [
|
|
123
|
+
`${createTableQueries.map(c => {
|
|
124
|
+
return (0, sql_formatter_1.format)(c, {
|
|
125
|
+
language: 'spark',
|
|
126
|
+
tabWidth: 2,
|
|
127
|
+
linesBetweenQueries: 1,
|
|
128
|
+
}) + ";";
|
|
129
|
+
}).join('\n\n')}\n`,
|
|
130
|
+
`${foreignKeyQueries.map(f => `${f};`).join('\n')}`,
|
|
131
|
+
];
|
|
132
|
+
fs.writeFileSync(`${cwd}/${dir}/migrations.sql`, sql.join('\n'));
|
|
133
|
+
return;
|
|
134
|
+
});
|
|
135
|
+
const _import = (pathModel) => __awaiter(void 0, void 0, void 0, function* () {
|
|
136
|
+
try {
|
|
137
|
+
const loadModel = yield Promise.resolve(`${pathModel}`).then(s => __importStar(require(s)));
|
|
138
|
+
const model = new loadModel.default();
|
|
139
|
+
return model;
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
console.log(`Check your 'Model' from path : '${pathModel}' is not instance of Model`);
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
const _foreignKey = ({ schemaModel, model }) => {
|
|
147
|
+
var _a;
|
|
148
|
+
for (const key in schemaModel) {
|
|
149
|
+
if (((_a = schemaModel[key]) === null || _a === void 0 ? void 0 : _a.foreignKey) == null)
|
|
150
|
+
continue;
|
|
151
|
+
const foreign = schemaModel[key].foreignKey;
|
|
152
|
+
if (foreign.on == null)
|
|
153
|
+
continue;
|
|
154
|
+
const onReference = typeof foreign.on === "string" ? foreign.on : new foreign.on;
|
|
155
|
+
const table = typeof onReference === "string" ? onReference : onReference.getTableName();
|
|
156
|
+
const constraintName = `\`${model.getTableName()}(${key})_${table}(${foreign.references})\``;
|
|
157
|
+
const sql = [
|
|
158
|
+
"ALTER TABLE",
|
|
159
|
+
`\`${model.getTableName()}\``,
|
|
160
|
+
'ADD CONSTRAINT',
|
|
161
|
+
`${constraintName}`,
|
|
162
|
+
`FOREIGN KEY(\`${key}\`)`,
|
|
163
|
+
`REFERENCES \`${table}\`(\`${foreign.references}\`)`,
|
|
164
|
+
`ON DELETE ${foreign.onDelete} ON UPDATE ${foreign.onUpdate}`
|
|
165
|
+
].join(' ');
|
|
166
|
+
return sql;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
if (push) {
|
|
170
|
+
const filePath = `${cwd}/${dir}/migrations.sql`;
|
|
171
|
+
const sqlString = fs.readFileSync(filePath, 'utf8');
|
|
172
|
+
const sqlStatements = sqlString.split(';');
|
|
173
|
+
for (const sql of sqlStatements) {
|
|
174
|
+
if (sql.trim() === '')
|
|
175
|
+
continue;
|
|
176
|
+
const createTableStatements = sql
|
|
177
|
+
.split(';')
|
|
178
|
+
.filter((statement) => statement.trim().startsWith('CREATE TABLE IF NOT EXISTS'));
|
|
179
|
+
const table = createTableStatements.map((statement) => {
|
|
180
|
+
const tableNameMatch = statement.match(/`([^`]+)`/);
|
|
181
|
+
return tableNameMatch && tableNameMatch[1];
|
|
182
|
+
})[0];
|
|
183
|
+
new lib_1.DB()
|
|
184
|
+
.loadEnv(env)
|
|
185
|
+
.rawQuery(`${sql}`)
|
|
186
|
+
.then(_ => console.log(table == null
|
|
187
|
+
? `The query '${sql}' has been successfully`
|
|
188
|
+
: `The table '${table}' has been successfully`))
|
|
189
|
+
.catch(e => console.log(`Failed to push changes errors: '${e.message}'`));
|
|
190
|
+
}
|
|
191
|
+
return process.exit(0);
|
|
192
|
+
}
|
|
193
|
+
if (generate) {
|
|
194
|
+
migrations(`${cwd}/${models}`)
|
|
195
|
+
.then(_ => console.log(`Migrations are migrating successfully`))
|
|
196
|
+
.catch(e => console.log(`Failed to migrate errors: '${e.message}'`))
|
|
197
|
+
.finally(() => process.exit(0));
|
|
198
|
+
return process.exit(0);
|
|
199
|
+
}
|
|
200
|
+
throw new Error("Do you want to generate or push changes ? use '--generate' or '--push");
|
|
201
|
+
};
|
package/build/lib/Interface.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Model from "./
|
|
2
|
-
export interface Relation {
|
|
3
|
-
name:
|
|
1
|
+
import Model from "./core/Model";
|
|
2
|
+
export interface Relation<K = any> {
|
|
3
|
+
name: K extends void ? never : K;
|
|
4
4
|
model: new () => Model;
|
|
5
5
|
as?: string | undefined;
|
|
6
6
|
localKey?: string | undefined;
|
|
@@ -16,8 +16,8 @@ export interface Relation {
|
|
|
16
16
|
oldVersion?: boolean | undefined;
|
|
17
17
|
modelPivot?: new () => Model | undefined;
|
|
18
18
|
}
|
|
19
|
-
export interface RelationQuery {
|
|
20
|
-
name?:
|
|
19
|
+
export interface RelationQuery<K = any> {
|
|
20
|
+
name?: K extends void ? never : K;
|
|
21
21
|
model: new () => Model;
|
|
22
22
|
as?: string | undefined;
|
|
23
23
|
localKey?: string | undefined;
|
|
@@ -99,12 +99,14 @@ export interface PoolCallback {
|
|
|
99
99
|
release: () => void;
|
|
100
100
|
}
|
|
101
101
|
export interface ConnectionTransaction {
|
|
102
|
+
on: (event: PoolEvent, data: any) => void;
|
|
102
103
|
query: (sql: string) => Promise<any>;
|
|
103
104
|
startTransaction: () => Promise<any>;
|
|
104
105
|
commit: () => Promise<any>;
|
|
105
106
|
rollback: () => Promise<any>;
|
|
106
107
|
}
|
|
107
108
|
export interface Connection {
|
|
109
|
+
on: (event: PoolEvent, data: any) => void;
|
|
108
110
|
query: (sql: string) => Promise<any>;
|
|
109
111
|
connection: () => Promise<ConnectionTransaction>;
|
|
110
112
|
}
|
|
@@ -157,8 +159,25 @@ export type ValidateSchemaDecorator = NumberConstructor | StringConstructor | Da
|
|
|
157
159
|
json?: boolean;
|
|
158
160
|
fn?: Function;
|
|
159
161
|
};
|
|
162
|
+
export type NonEmptyArray<T> = [T, ...T[]];
|
|
160
163
|
export interface GlobalSetting {
|
|
161
164
|
softDelete?: boolean;
|
|
162
165
|
uuid?: boolean;
|
|
163
166
|
timestamp?: boolean;
|
|
167
|
+
logger?: boolean;
|
|
164
168
|
}
|
|
169
|
+
export interface Operator {
|
|
170
|
+
equals: string;
|
|
171
|
+
notEquals: string;
|
|
172
|
+
greaterThan: string;
|
|
173
|
+
lessThan: string;
|
|
174
|
+
greaterThanOrEqual: string;
|
|
175
|
+
lessThanOrEqual: string;
|
|
176
|
+
like: string;
|
|
177
|
+
notLike: string;
|
|
178
|
+
in: string;
|
|
179
|
+
notIn: string;
|
|
180
|
+
isNull: string;
|
|
181
|
+
isNotNull: string;
|
|
182
|
+
}
|
|
183
|
+
export type PoolEvent = 'CONNECTION' | 'RELEASE' | 'QUERY' | 'SLOW_QUERY' | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
1
3
|
import { loadOptionsEnvironment } from './options';
|
|
2
4
|
import { Connection, Options } from '../Interface';
|
|
3
|
-
export declare class PoolConnection {
|
|
5
|
+
export declare class PoolConnection extends EventEmitter {
|
|
4
6
|
private OPTIONS;
|
|
5
7
|
/**
|
|
6
8
|
*
|
|
@@ -15,6 +17,8 @@ export declare class PoolConnection {
|
|
|
15
17
|
* @property {Function} Connection.connection
|
|
16
18
|
*/
|
|
17
19
|
connection(): Connection;
|
|
20
|
+
private _detectEventQuery;
|
|
21
|
+
private _detectQueryType;
|
|
18
22
|
private _defaultOptions;
|
|
19
23
|
private _loadOptions;
|
|
20
24
|
private _convertStringToObject;
|