tspace-mysql 1.1.9 → 1.2.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 +19 -4
- package/dist/cli/generate/make.js +4 -5
- package/dist/cli/index.js +8 -1
- package/dist/cli/query/index.js +1 -1
- package/dist/lib/connection/index.js +16 -0
- package/dist/lib/constants/index.d.ts +2 -2
- package/dist/lib/tspace/AbstractDB.d.ts +2 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +1 -1
- package/dist/lib/tspace/AbstractDatabase.js +1 -1
- package/dist/lib/tspace/AbstractModel.d.ts +1 -0
- package/dist/lib/tspace/Database.d.ts +1 -2
- package/dist/lib/tspace/Database.js +16 -44
- package/dist/lib/tspace/Model.d.ts +1 -0
- package/dist/lib/tspace/Model.js +134 -69
- package/dist/lib/utils/index.d.ts +0 -1
- package/dist/lib/utils/index.js +1 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,8 @@ npm install tspace-mysql --save
|
|
|
32
32
|
- [Make Model](#make-model)
|
|
33
33
|
- [Make Migration](#make-migration)
|
|
34
34
|
- [Migrate](#migrate)
|
|
35
|
+
- [Query](#query)
|
|
36
|
+
- [Menerate Models](#generate-models)
|
|
35
37
|
- [Blueprint](#blueprint)
|
|
36
38
|
|
|
37
39
|
## Configuration
|
|
@@ -210,7 +212,7 @@ const user = await new DB('users')
|
|
|
210
212
|
Running A Delete Query
|
|
211
213
|
```js
|
|
212
214
|
const deleted = await new DB('users').where('id',1).delete()
|
|
213
|
-
// deleted =>
|
|
215
|
+
// deleted => Boolean
|
|
214
216
|
```
|
|
215
217
|
## Database Transactions
|
|
216
218
|
|
|
@@ -298,7 +300,7 @@ Backup database, you may backup is this:
|
|
|
298
300
|
* @param conection defalut current connection
|
|
299
301
|
*/
|
|
300
302
|
const backup = await new DB().backup({
|
|
301
|
-
database: 'try-to-backup', // clone current database to this
|
|
303
|
+
database: 'try-to-backup', // clone current database to this database
|
|
302
304
|
connection ?: {
|
|
303
305
|
host: 'localhost',
|
|
304
306
|
port : 3306,
|
|
@@ -563,7 +565,7 @@ await new User().relations('posts')
|
|
|
563
565
|
.relationQuery('user', (query : User) => {
|
|
564
566
|
return query.relations('posts').relationQuery('posts',(query : Post)=> {
|
|
565
567
|
return query.relations('comments','user')
|
|
566
|
-
// relation n
|
|
568
|
+
// relation n, n, ...n
|
|
567
569
|
})
|
|
568
570
|
})
|
|
569
571
|
})
|
|
@@ -667,7 +669,6 @@ you may use a basic cli :
|
|
|
667
669
|
npm install tspace-mysql -g
|
|
668
670
|
|
|
669
671
|
```
|
|
670
|
-
|
|
671
672
|
## Make Model
|
|
672
673
|
Command will be placed Model in the specific directory
|
|
673
674
|
```js
|
|
@@ -745,6 +746,20 @@ tspace-mysql migrate --dir=App/Models/Migrations --type=js
|
|
|
745
746
|
// => migrate all schemas in folder <Migrations>. created into database
|
|
746
747
|
```
|
|
747
748
|
|
|
749
|
+
# Query
|
|
750
|
+
Command will be execute a query
|
|
751
|
+
```js
|
|
752
|
+
tspace-mysql query "SELECT * FROM users"
|
|
753
|
+
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
# Generate Models
|
|
757
|
+
Command will be generate models from table in database
|
|
758
|
+
```js
|
|
759
|
+
tspace-mysql generate:models
|
|
760
|
+
|
|
761
|
+
```
|
|
762
|
+
|
|
748
763
|
## Blueprint
|
|
749
764
|
Schema table created by command make:migration, you may use the:
|
|
750
765
|
```js
|
|
@@ -20,21 +20,20 @@ exports.default = (cmd) => {
|
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
const
|
|
23
|
+
const snakeCaseToPascal = (data) => {
|
|
24
24
|
let str = data.split('_');
|
|
25
25
|
for (let i = 0; i < str.length; i++) {
|
|
26
26
|
str[i] = str[i].slice(0, 1).toUpperCase() + str[i].slice(1, str[i].length);
|
|
27
27
|
}
|
|
28
28
|
return str.join('');
|
|
29
29
|
};
|
|
30
|
-
new lib_1.DB().rawQuery('SHOW TABLES')
|
|
31
|
-
.then(tables => {
|
|
30
|
+
new lib_1.DB().rawQuery('SHOW TABLES').then(tables => {
|
|
32
31
|
var _a;
|
|
33
32
|
for (let i = 0; i < tables.length; i++) {
|
|
34
33
|
const table = String((_a = Object.values(tables[i])) === null || _a === void 0 ? void 0 : _a.shift());
|
|
35
|
-
const model =
|
|
34
|
+
const model = snakeCaseToPascal(pluralize_1.default.singular(table));
|
|
36
35
|
const data = (0, model_1.default)(model, npm);
|
|
37
|
-
fs.writeFile(`${dir}/${model}
|
|
36
|
+
fs.writeFile(`${cwd}/${dir}/${model}${type !== null && type !== void 0 ? type : '.ts'}`, data, (err) => {
|
|
38
37
|
if (err)
|
|
39
38
|
throw err;
|
|
40
39
|
});
|
package/dist/cli/index.js
CHANGED
|
@@ -47,5 +47,12 @@ try {
|
|
|
47
47
|
commands[process.argv[2]](cmd);
|
|
48
48
|
}
|
|
49
49
|
catch (err) {
|
|
50
|
-
console.log(`
|
|
50
|
+
console.log(`
|
|
51
|
+
tspace-mysql make:model User --m --dir=app/Models
|
|
52
|
+
tspace-mysql make:migration users --dir=app/Models/Migrations
|
|
53
|
+
tspace-mysql migrate --dir=App/Models/Migrations --type=js
|
|
54
|
+
tspace-mysql query "SELECT * FROM users"
|
|
55
|
+
tspace-mysql generate:models --dir=app/Models
|
|
56
|
+
`);
|
|
57
|
+
console.log(`Read more https://www.npmjs.com/package/tspace-mysql`);
|
|
51
58
|
}
|
package/dist/cli/query/index.js
CHANGED
|
@@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const lib_1 = require("../../lib");
|
|
4
4
|
exports.default = (cmd) => {
|
|
5
5
|
const { sql } = cmd;
|
|
6
|
-
new lib_1.DB().rawQuery(sql).then(result => console.log(result));
|
|
6
|
+
new lib_1.DB().rawQuery(sql === null || sql === void 0 ? void 0 : sql.replace(/`/g, '')).then(result => console.log(result));
|
|
7
7
|
};
|
|
@@ -98,6 +98,22 @@ class PoolConnection {
|
|
|
98
98
|
}
|
|
99
99
|
_loadOptions() {
|
|
100
100
|
try {
|
|
101
|
+
/**
|
|
102
|
+
*
|
|
103
|
+
* @Json connection
|
|
104
|
+
*
|
|
105
|
+
"host" : "",
|
|
106
|
+
"port" : "",
|
|
107
|
+
"database" : "",
|
|
108
|
+
"user" : "",
|
|
109
|
+
"password" : "",
|
|
110
|
+
"connectionLimit" : "",
|
|
111
|
+
"dateStrings" : "",
|
|
112
|
+
"connectTimeout" : "",
|
|
113
|
+
"waitForConnections" : "",
|
|
114
|
+
"queueLimit" : "",
|
|
115
|
+
"charset" : ""
|
|
116
|
+
*/
|
|
101
117
|
const jsonPath = path_1.default.join(path_1.default.resolve(), 'tspace-mysql.json');
|
|
102
118
|
const jsonOptionsExists = fs_1.default.existsSync(jsonPath);
|
|
103
119
|
if (!jsonOptionsExists)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Database from './Database';
|
|
2
|
+
import { Connection, ConnectionOptions } from './Interface';
|
|
2
3
|
declare abstract class AbstractDB extends Database {
|
|
3
4
|
abstract table(tableName: string): void;
|
|
4
5
|
abstract beginTransaction(): Promise<any>;
|
|
@@ -11,6 +12,7 @@ declare abstract class AbstractDB extends Database {
|
|
|
11
12
|
when: string;
|
|
12
13
|
then: string;
|
|
13
14
|
}[], final?: string): string | [];
|
|
15
|
+
abstract getConnection(options: ConnectionOptions): Connection;
|
|
14
16
|
}
|
|
15
17
|
export { AbstractDB };
|
|
16
18
|
export default AbstractDB;
|
|
@@ -32,6 +32,7 @@ declare abstract class AbstractModel extends Database {
|
|
|
32
32
|
abstract with(...nameRelations: string[]): this;
|
|
33
33
|
abstract withQuery(nameRelations: string, callback: Function): this;
|
|
34
34
|
abstract withExists(...nameRelations: string[]): this;
|
|
35
|
+
abstract has(...nameRelations: string[]): this;
|
|
35
36
|
abstract relations(...nameRelations: string[]): this;
|
|
36
37
|
abstract relationQuery(nameRelations: string, callback: Function): this;
|
|
37
38
|
abstract relationsExists(...nameRelations: string[]): this;
|
|
@@ -336,7 +336,7 @@ declare class Database extends AbstractDatabase {
|
|
|
336
336
|
*/
|
|
337
337
|
dd(debug?: boolean): this;
|
|
338
338
|
/**
|
|
339
|
-
* hook when execute returned result to callback function
|
|
339
|
+
* hook function when execute returned result to callback function
|
|
340
340
|
* @param {Function} func function for callback result
|
|
341
341
|
* @return {this}
|
|
342
342
|
*/
|
|
@@ -654,7 +654,6 @@ declare class Database extends AbstractDatabase {
|
|
|
654
654
|
* @return {Promise<boolean>}
|
|
655
655
|
*/
|
|
656
656
|
backupToFile({ filePath, database, connection }: BackupToFile): Promise<void>;
|
|
657
|
-
genrateModel(): Promise<void>;
|
|
658
657
|
/**
|
|
659
658
|
*
|
|
660
659
|
* fake data
|
|
@@ -25,7 +25,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.Database = void 0;
|
|
27
27
|
const fs_1 = __importDefault(require("fs"));
|
|
28
|
-
const pluralize_1 = __importDefault(require("pluralize"));
|
|
29
28
|
const sql_formatter_1 = require("sql-formatter");
|
|
30
29
|
const AbstractDatabase_1 = __importDefault(require("./AbstractDatabase"));
|
|
31
30
|
const utils_1 = __importDefault(require("../utils"));
|
|
@@ -938,7 +937,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
938
937
|
return this;
|
|
939
938
|
}
|
|
940
939
|
/**
|
|
941
|
-
* hook when execute returned result to callback function
|
|
940
|
+
* hook function when execute returned result to callback function
|
|
942
941
|
* @param {Function} func function for callback result
|
|
943
942
|
* @return {this}
|
|
944
943
|
*/
|
|
@@ -1285,13 +1284,15 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1285
1284
|
const r = newData[pluck] || null;
|
|
1286
1285
|
const hook = this.$state.get('HOOK');
|
|
1287
1286
|
if (hook)
|
|
1288
|
-
|
|
1287
|
+
for (let i in hook)
|
|
1288
|
+
yield hook[i](r);
|
|
1289
1289
|
return r;
|
|
1290
1290
|
}
|
|
1291
1291
|
const r = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
1292
1292
|
const hook = this.$state.get('HOOK');
|
|
1293
1293
|
if (hook)
|
|
1294
|
-
|
|
1294
|
+
for (let i in hook)
|
|
1295
|
+
yield hook[i](r);
|
|
1295
1296
|
return r;
|
|
1296
1297
|
});
|
|
1297
1298
|
}
|
|
@@ -1338,7 +1339,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1338
1339
|
}
|
|
1339
1340
|
const hook = this.$state.get('HOOK');
|
|
1340
1341
|
if (hook)
|
|
1341
|
-
|
|
1342
|
+
for (let i in hook)
|
|
1343
|
+
yield hook[i](data);
|
|
1342
1344
|
return data;
|
|
1343
1345
|
}
|
|
1344
1346
|
const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
@@ -1350,7 +1352,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1350
1352
|
}
|
|
1351
1353
|
const hook = this.$state.get('HOOK');
|
|
1352
1354
|
if (hook)
|
|
1353
|
-
|
|
1355
|
+
for (let i in hook)
|
|
1356
|
+
yield hook[i](data);
|
|
1354
1357
|
return data;
|
|
1355
1358
|
});
|
|
1356
1359
|
}
|
|
@@ -1388,7 +1391,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1388
1391
|
}, []);
|
|
1389
1392
|
const hook = this.$state.get('HOOK');
|
|
1390
1393
|
if (hook)
|
|
1391
|
-
|
|
1394
|
+
for (let i in hook)
|
|
1395
|
+
yield hook[i](data || []);
|
|
1392
1396
|
return data || [];
|
|
1393
1397
|
}
|
|
1394
1398
|
if (this.$state.get('PLUCK')) {
|
|
@@ -1399,12 +1403,14 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1399
1403
|
}
|
|
1400
1404
|
const hook = this.$state.get('HOOK');
|
|
1401
1405
|
if (hook)
|
|
1402
|
-
|
|
1406
|
+
for (let i in hook)
|
|
1407
|
+
yield hook[i](newData || []);
|
|
1403
1408
|
return newData || [];
|
|
1404
1409
|
}
|
|
1405
1410
|
const hook = this.$state.get('HOOK');
|
|
1406
1411
|
if (hook)
|
|
1407
|
-
|
|
1412
|
+
for (let i in hook)
|
|
1413
|
+
yield hook[i](result || []);
|
|
1408
1414
|
return result || [];
|
|
1409
1415
|
});
|
|
1410
1416
|
}
|
|
@@ -1654,10 +1660,9 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1654
1660
|
* @return {Promise<any>} promise
|
|
1655
1661
|
*/
|
|
1656
1662
|
save() {
|
|
1657
|
-
var _a;
|
|
1658
1663
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1659
1664
|
const attributes = this.$attributes;
|
|
1660
|
-
if (
|
|
1665
|
+
if (attributes != null) {
|
|
1661
1666
|
while (true) {
|
|
1662
1667
|
if (this.$state.get('WHERE')) {
|
|
1663
1668
|
const query = this._queryUpdate(attributes);
|
|
@@ -1903,39 +1908,6 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1903
1908
|
return;
|
|
1904
1909
|
});
|
|
1905
1910
|
}
|
|
1906
|
-
genrateModel() {
|
|
1907
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1908
|
-
const tables = yield this.queryStatement(this.$constants('SHOW_TABLES'));
|
|
1909
|
-
fs_1.default.mkdirSync('Model', {
|
|
1910
|
-
recursive: true
|
|
1911
|
-
});
|
|
1912
|
-
const snake2Pascal = (data) => {
|
|
1913
|
-
let str = data.split('_');
|
|
1914
|
-
for (let i = 0; i < str.length; i++) {
|
|
1915
|
-
str[i] = str[i].slice(0, 1).toUpperCase() + str[i].slice(1, str[i].length);
|
|
1916
|
-
}
|
|
1917
|
-
return str.join('');
|
|
1918
|
-
};
|
|
1919
|
-
new DB_1.default().rawQuery('SHOW TABLES')
|
|
1920
|
-
.then(tables => {
|
|
1921
|
-
var _a;
|
|
1922
|
-
const models = [];
|
|
1923
|
-
for (let i = 0; i < tables.length; i++) {
|
|
1924
|
-
const table = String((_a = Object.values(tables[i])) === null || _a === void 0 ? void 0 : _a.shift());
|
|
1925
|
-
const model = snake2Pascal(pluralize_1.default.singular(table));
|
|
1926
|
-
fs_1.default.writeFileSync(`Model/${model}.ts`, `import { ${model} } from 'tspace-mysql'`);
|
|
1927
|
-
// console.log(`Model : '${model}' created successfully`)
|
|
1928
|
-
models.push(`'${model}' created successfully`);
|
|
1929
|
-
}
|
|
1930
|
-
console.table(models.map((m, i) => {
|
|
1931
|
-
return { model: i, name: m };
|
|
1932
|
-
}));
|
|
1933
|
-
console.log('\nGenerate Models has completed');
|
|
1934
|
-
})
|
|
1935
|
-
.catch(err => console.log(err));
|
|
1936
|
-
return;
|
|
1937
|
-
});
|
|
1938
|
-
}
|
|
1939
1911
|
/**
|
|
1940
1912
|
*
|
|
1941
1913
|
* fake data
|
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -37,7 +37,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
37
37
|
* define for initialize of models
|
|
38
38
|
* @return {void} void
|
|
39
39
|
*/
|
|
40
|
-
define() {
|
|
40
|
+
define() {
|
|
41
|
+
}
|
|
41
42
|
/**
|
|
42
43
|
*
|
|
43
44
|
* Assign function callback in model
|
|
@@ -863,6 +864,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
863
864
|
first() {
|
|
864
865
|
var _a;
|
|
865
866
|
return __awaiter(this, void 0, void 0, function* () {
|
|
867
|
+
this._validateMethod('first');
|
|
866
868
|
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
867
869
|
this.select(yield this.exceptColumns());
|
|
868
870
|
let sql = this._buildQueryModel();
|
|
@@ -902,6 +904,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
902
904
|
firstOrError(message, options) {
|
|
903
905
|
var _a;
|
|
904
906
|
return __awaiter(this, void 0, void 0, function* () {
|
|
907
|
+
this._validateMethod('firstOrError');
|
|
905
908
|
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
906
909
|
this.select(yield this.exceptColumns());
|
|
907
910
|
let sql = this._buildQueryModel();
|
|
@@ -922,8 +925,21 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
922
925
|
* @return {promise<any>}
|
|
923
926
|
*/
|
|
924
927
|
findOneOrError(message, options) {
|
|
928
|
+
var _a;
|
|
925
929
|
return __awaiter(this, void 0, void 0, function* () {
|
|
926
|
-
|
|
930
|
+
this._validateMethod('findOneOrError');
|
|
931
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
932
|
+
this.select(yield this.exceptColumns());
|
|
933
|
+
let sql = this._buildQueryModel();
|
|
934
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
935
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
936
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
937
|
+
}
|
|
938
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
939
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
940
|
+
sql = this._queryRelationsExists();
|
|
941
|
+
}
|
|
942
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
927
943
|
});
|
|
928
944
|
}
|
|
929
945
|
/**
|
|
@@ -950,6 +966,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
950
966
|
*/
|
|
951
967
|
find(id) {
|
|
952
968
|
return __awaiter(this, void 0, void 0, function* () {
|
|
969
|
+
this._validateMethod('find');
|
|
970
|
+
this._handleSoftDelete();
|
|
953
971
|
const sql = [
|
|
954
972
|
`${this.$constants('SELECT')}`,
|
|
955
973
|
`*`,
|
|
@@ -970,6 +988,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
970
988
|
get() {
|
|
971
989
|
var _a;
|
|
972
990
|
return __awaiter(this, void 0, void 0, function* () {
|
|
991
|
+
this._validateMethod('get');
|
|
973
992
|
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
974
993
|
this.select(yield this.exceptColumns());
|
|
975
994
|
let sql = this._buildQueryModel();
|
|
@@ -989,6 +1008,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
989
1008
|
findMany() {
|
|
990
1009
|
var _a;
|
|
991
1010
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1011
|
+
this._validateMethod('findMany');
|
|
992
1012
|
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
993
1013
|
this.select(yield this.exceptColumns());
|
|
994
1014
|
const sql = this._buildQueryModel();
|
|
@@ -1006,6 +1026,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1006
1026
|
pagination(paginationOptions) {
|
|
1007
1027
|
var _a, _b;
|
|
1008
1028
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1029
|
+
this._validateMethod('pagination');
|
|
1009
1030
|
let limit = 15;
|
|
1010
1031
|
let page = 1;
|
|
1011
1032
|
if (paginationOptions != null) {
|
|
@@ -1050,14 +1071,42 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1050
1071
|
* @return {promise<Pagination>}
|
|
1051
1072
|
*/
|
|
1052
1073
|
paginate(paginationOptions) {
|
|
1074
|
+
var _a, _b;
|
|
1053
1075
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1076
|
+
this._validateMethod('paginate');
|
|
1054
1077
|
let limit = 15;
|
|
1055
1078
|
let page = 1;
|
|
1056
1079
|
if (paginationOptions != null) {
|
|
1057
1080
|
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
1058
1081
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1059
1082
|
}
|
|
1060
|
-
|
|
1083
|
+
this._assertError((_a = this.$logger) === null || _a === void 0 ? void 0 : _a.check('limit'), `this '[pagination]' can't support '[limit]' method`);
|
|
1084
|
+
if ((_b = this.$state.get('EXCEPT')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1085
|
+
this.select(yield this.exceptColumns());
|
|
1086
|
+
const offset = (page - 1) * limit;
|
|
1087
|
+
this.$state.set('PER_PAGE', limit);
|
|
1088
|
+
this.$state.set('PAGE', page);
|
|
1089
|
+
let sql = this._buildQueryModel();
|
|
1090
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
1091
|
+
sql = this._queryRelationsExists();
|
|
1092
|
+
}
|
|
1093
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
1094
|
+
sql = [
|
|
1095
|
+
`${sql}`,
|
|
1096
|
+
`${this.$constants('LIMIT')}`,
|
|
1097
|
+
`${limit}`,
|
|
1098
|
+
`${this.$constants('OFFSET')}`,
|
|
1099
|
+
`${offset}`
|
|
1100
|
+
].join(' ');
|
|
1101
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1102
|
+
}
|
|
1103
|
+
sql = sql.replace(this.$state.get('LIMIT'), [
|
|
1104
|
+
`${this.$constants('LIMIT')}`,
|
|
1105
|
+
`${limit}`,
|
|
1106
|
+
`${this.$constants('OFFSET')}`,
|
|
1107
|
+
`${offset}`
|
|
1108
|
+
].join(' '));
|
|
1109
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1061
1110
|
});
|
|
1062
1111
|
}
|
|
1063
1112
|
/**
|
|
@@ -1264,10 +1313,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1264
1313
|
* @return {Promise<any>}
|
|
1265
1314
|
*/
|
|
1266
1315
|
save() {
|
|
1267
|
-
var _a;
|
|
1268
1316
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1269
1317
|
const attributes = this.$attributes;
|
|
1270
|
-
if (
|
|
1318
|
+
if (attributes != null) {
|
|
1271
1319
|
while (true) {
|
|
1272
1320
|
if (this.$state.get('WHERE')) {
|
|
1273
1321
|
const query = this._queryUpdateModel(attributes);
|
|
@@ -1371,54 +1419,48 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1371
1419
|
}
|
|
1372
1420
|
_valueInRelation(relationModel) {
|
|
1373
1421
|
var _a, _b;
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
`${this.$state.get('PRIMARY_KEY')}`
|
|
1403
|
-
].join('_'));
|
|
1404
|
-
}
|
|
1405
|
-
const checkRelationIsBelongsToMany = [
|
|
1406
|
-
relationModel.localKey == null,
|
|
1407
|
-
relationModel.foreignKey == null,
|
|
1408
|
-
relation === this.$constants('RELATIONSHIP').belongsToMany
|
|
1409
|
-
].every(r => r);
|
|
1410
|
-
if (checkRelationIsBelongsToMany) {
|
|
1411
|
-
localKey = this._valuePattern([
|
|
1412
|
-
`${pluralize_1.default.singular(table !== null && table !== void 0 ? table : '')}`,
|
|
1413
|
-
`${this.$state.get('PRIMARY_KEY')}`
|
|
1414
|
-
].join('_'));
|
|
1415
|
-
foreignKey = 'id';
|
|
1416
|
-
}
|
|
1417
|
-
return { name, as, relation, table, localKey, foreignKey, model };
|
|
1422
|
+
const relation = relationModel.relation;
|
|
1423
|
+
const model = (_a = relationModel.model) === null || _a === void 0 ? void 0 : _a.name;
|
|
1424
|
+
const table = relationModel.freezeTable
|
|
1425
|
+
? relationModel.freezeTable
|
|
1426
|
+
: (_b = relationModel.query) === null || _b === void 0 ? void 0 : _b._tableName();
|
|
1427
|
+
const name = relationModel.name;
|
|
1428
|
+
const as = relationModel.as;
|
|
1429
|
+
this._assertError(!model || model == null, 'not found model');
|
|
1430
|
+
let localKey = relationModel.localKey
|
|
1431
|
+
? relationModel.localKey
|
|
1432
|
+
: this.$state.get('PRIMARY_KEY');
|
|
1433
|
+
let foreignKey = relationModel.foreignKey
|
|
1434
|
+
? relationModel.foreignKey
|
|
1435
|
+
: this._valuePattern([
|
|
1436
|
+
`${pluralize_1.default.singular(this.$state.get('TABLE_NAME').replace(/\`/g, ''))}`,
|
|
1437
|
+
`${this.$state.get('PRIMARY_KEY')}`
|
|
1438
|
+
].join('_'));
|
|
1439
|
+
const checkRelationIsBelongsTo = [
|
|
1440
|
+
relationModel.localKey == null,
|
|
1441
|
+
relationModel.foreignKey == null,
|
|
1442
|
+
relation === this.$constants('RELATIONSHIP').belongsTo
|
|
1443
|
+
].every(r => r);
|
|
1444
|
+
if (checkRelationIsBelongsTo) {
|
|
1445
|
+
foreignKey = localKey;
|
|
1446
|
+
localKey = this._valuePattern([
|
|
1447
|
+
`${pluralize_1.default.singular(table !== null && table !== void 0 ? table : '')}`,
|
|
1448
|
+
`${this.$state.get('PRIMARY_KEY')}`
|
|
1449
|
+
].join('_'));
|
|
1418
1450
|
}
|
|
1419
|
-
|
|
1420
|
-
|
|
1451
|
+
const checkRelationIsBelongsToMany = [
|
|
1452
|
+
relationModel.localKey == null,
|
|
1453
|
+
relationModel.foreignKey == null,
|
|
1454
|
+
relation === this.$constants('RELATIONSHIP').belongsToMany
|
|
1455
|
+
].every(r => r);
|
|
1456
|
+
if (checkRelationIsBelongsToMany) {
|
|
1457
|
+
localKey = this._valuePattern([
|
|
1458
|
+
`${pluralize_1.default.singular(table !== null && table !== void 0 ? table : '')}`,
|
|
1459
|
+
`${this.$state.get('PRIMARY_KEY')}`
|
|
1460
|
+
].join('_'));
|
|
1461
|
+
foreignKey = 'id';
|
|
1421
1462
|
}
|
|
1463
|
+
return { name, as, relation, table, localKey, foreignKey, model };
|
|
1422
1464
|
}
|
|
1423
1465
|
_handleSoftDelete() {
|
|
1424
1466
|
if (this.$state.get('SOFT_DELETE')) {
|
|
@@ -1617,7 +1659,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1617
1659
|
const localKeyId = parents.map((parent) => {
|
|
1618
1660
|
const data = parent[localKey];
|
|
1619
1661
|
if (!parent.hasOwnProperty(localKey)) {
|
|
1620
|
-
this._assertError(data == null,
|
|
1662
|
+
this._assertError(data == null, `unknown relationship without primary or foreign key in relation : [${relation === null || relation === void 0 ? void 0 : relation.name}]`);
|
|
1621
1663
|
}
|
|
1622
1664
|
return data;
|
|
1623
1665
|
}).filter((data) => data != null);
|
|
@@ -1625,7 +1667,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1625
1667
|
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1626
1668
|
return [];
|
|
1627
1669
|
const query = yield relation.query;
|
|
1628
|
-
this._assertError(query == null, `unknown callback query in [relation :
|
|
1670
|
+
this._assertError(query == null, `unknown callback query in [relation : ${relation.name}]`);
|
|
1629
1671
|
const dataFromRelation = yield query
|
|
1630
1672
|
.bind(this.$pool.get())
|
|
1631
1673
|
.whereIn(foreignKey, dataPerentId)
|
|
@@ -1855,14 +1897,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1855
1897
|
const empty = this.$utils.snakeCase(this._result(emptyData));
|
|
1856
1898
|
const hook = this.$state.get('HOOK');
|
|
1857
1899
|
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1858
|
-
for (let i
|
|
1900
|
+
for (let i in hook)
|
|
1859
1901
|
yield hook[i](empty);
|
|
1860
1902
|
return empty;
|
|
1861
1903
|
}
|
|
1862
1904
|
const empty = this._result(emptyData);
|
|
1863
1905
|
const hook = this.$state.get('HOOK');
|
|
1864
1906
|
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1865
|
-
for (let i
|
|
1907
|
+
for (let i in hook)
|
|
1866
1908
|
yield hook[i](empty);
|
|
1867
1909
|
return empty;
|
|
1868
1910
|
});
|
|
@@ -1871,12 +1913,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1871
1913
|
var _a, _b, _c, _d, _e;
|
|
1872
1914
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1873
1915
|
if ((_a = Object.keys(this.$state.get('REGISTRY'))) === null || _a === void 0 ? void 0 : _a.length) {
|
|
1874
|
-
|
|
1916
|
+
for (const d of data) {
|
|
1875
1917
|
for (const name in this.$state.get('REGISTRY')) {
|
|
1876
1918
|
const registry = this.$state.get('REGISTRY');
|
|
1877
1919
|
d[name] = registry[name];
|
|
1878
1920
|
}
|
|
1879
|
-
}
|
|
1921
|
+
}
|
|
1880
1922
|
}
|
|
1881
1923
|
if ((_b = this.$state.get('ONLY')) === null || _b === void 0 ? void 0 : _b.length) {
|
|
1882
1924
|
data = this._showOnly(data);
|
|
@@ -1939,21 +1981,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1939
1981
|
}
|
|
1940
1982
|
const hook = this.$state.get('HOOK');
|
|
1941
1983
|
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1942
|
-
for (let i
|
|
1984
|
+
for (let i in hook)
|
|
1943
1985
|
yield hook[i](result);
|
|
1944
1986
|
return result;
|
|
1945
1987
|
});
|
|
1946
1988
|
}
|
|
1947
|
-
_hiddenColumnModel(
|
|
1948
|
-
const
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
});
|
|
1954
|
-
});
|
|
1989
|
+
_hiddenColumnModel(data) {
|
|
1990
|
+
const hiddens = this.$state.get('HIDDEN');
|
|
1991
|
+
for (const hidden of hiddens) {
|
|
1992
|
+
for (const objColumn of data) {
|
|
1993
|
+
delete objColumn[hidden];
|
|
1994
|
+
}
|
|
1955
1995
|
}
|
|
1956
|
-
return
|
|
1996
|
+
return data;
|
|
1957
1997
|
}
|
|
1958
1998
|
_attach(name, dataId, fields) {
|
|
1959
1999
|
var _a;
|
|
@@ -2183,13 +2223,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2183
2223
|
this.$state.set('RESULT', result);
|
|
2184
2224
|
const hook = this.$state.get('HOOK');
|
|
2185
2225
|
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
2186
|
-
for (let i
|
|
2226
|
+
for (let i in hook)
|
|
2187
2227
|
yield hook[i](result);
|
|
2188
2228
|
return result;
|
|
2189
2229
|
}
|
|
2190
2230
|
const hook = this.$state.get('HOOK');
|
|
2191
2231
|
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
2192
|
-
for (let i
|
|
2232
|
+
for (let i in hook)
|
|
2193
2233
|
yield hook[i](result || []);
|
|
2194
2234
|
return null;
|
|
2195
2235
|
});
|
|
@@ -2316,6 +2356,31 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2316
2356
|
this._assertError(!Object.values(this.$constants('RELATIONSHIP')).includes(r.relation), `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
2317
2357
|
return r;
|
|
2318
2358
|
}
|
|
2359
|
+
_validateMethod(method) {
|
|
2360
|
+
switch (method.toLocaleLowerCase()) {
|
|
2361
|
+
case 'paginate':
|
|
2362
|
+
case 'pagination':
|
|
2363
|
+
case 'findOneOrError':
|
|
2364
|
+
case 'firstOrError':
|
|
2365
|
+
case 'findOne':
|
|
2366
|
+
case 'findMany':
|
|
2367
|
+
case 'first':
|
|
2368
|
+
case 'get': {
|
|
2369
|
+
const methodCallings = this.$logger.get();
|
|
2370
|
+
const methodsNotAllowed = [
|
|
2371
|
+
'create',
|
|
2372
|
+
'createNotExists',
|
|
2373
|
+
'updateOrCreate',
|
|
2374
|
+
'updateOrInsert',
|
|
2375
|
+
'insertOrUpdate',
|
|
2376
|
+
'update'
|
|
2377
|
+
];
|
|
2378
|
+
const findMethodNotAllowed = methodCallings.find((methodCalling) => methodsNotAllowed.includes(methodCalling));
|
|
2379
|
+
this._assertError(methodCallings.some((methodCalling) => methodsNotAllowed.includes(methodCalling)), `this method ${method} can't using method : [ ${findMethodNotAllowed} ]`);
|
|
2380
|
+
break;
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2319
2384
|
_initialModel() {
|
|
2320
2385
|
this.$state = (() => {
|
|
2321
2386
|
let db = new Map(Object.entries(Object.assign({}, this.$constants('MODEL'))));
|
package/dist/lib/utils/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
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
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.utils = void 0;
|
|
13
4
|
const timestamp = () => {
|
|
@@ -157,22 +148,6 @@ const faker = (value) => {
|
|
|
157
148
|
return Buffer.from(Math.random().toString(36).substring(7)).toString('base64');
|
|
158
149
|
return 'fake data';
|
|
159
150
|
};
|
|
160
|
-
const test = (testName, callback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
-
const startTime = process.hrtime();
|
|
162
|
-
const diffTimeSs = (hrtime) => {
|
|
163
|
-
if (hrtime == null)
|
|
164
|
-
return;
|
|
165
|
-
const [start, end] = process.hrtime(hrtime);
|
|
166
|
-
return (start + (end / 1e9)).toFixed(4);
|
|
167
|
-
};
|
|
168
|
-
try {
|
|
169
|
-
yield callback();
|
|
170
|
-
console.log(`Test : \x1b[34m ${testName} \x1b[0m ==> \x1b[32m PASSED \x1b[0m ${diffTimeSs(startTime)} s`);
|
|
171
|
-
}
|
|
172
|
-
catch (err) {
|
|
173
|
-
console.log(`Test : \x1b[34m ${testName} \x1b[0m ==> \x1b[31m FAILED \x1b[0m (${err.message}) , ${diffTimeSs(startTime)} s`);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
151
|
const utils = {
|
|
177
152
|
consoleDebug,
|
|
178
153
|
faker,
|
|
@@ -184,8 +159,7 @@ const utils = {
|
|
|
184
159
|
generateUUID,
|
|
185
160
|
covertBooleanToNumber,
|
|
186
161
|
snakeCase,
|
|
187
|
-
camelCase
|
|
188
|
-
test
|
|
162
|
+
camelCase
|
|
189
163
|
};
|
|
190
164
|
exports.utils = utils;
|
|
191
165
|
exports.default = utils;
|