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,4 +1,24 @@
|
|
|
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
|
+
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
|
+
};
|
|
2
22
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
24
|
};
|
|
@@ -35,7 +55,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
35
55
|
return this;
|
|
36
56
|
}
|
|
37
57
|
/**
|
|
38
|
-
* data will return void
|
|
58
|
+
* data alaways will return void
|
|
39
59
|
* @return {this} this
|
|
40
60
|
*/
|
|
41
61
|
void() {
|
|
@@ -71,7 +91,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
71
91
|
*/
|
|
72
92
|
select(...columns) {
|
|
73
93
|
let select = '*';
|
|
74
|
-
if (columns
|
|
94
|
+
if (columns === null || columns === void 0 ? void 0 : columns.length)
|
|
75
95
|
select = columns.join(', ');
|
|
76
96
|
this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
|
|
77
97
|
return this;
|
|
@@ -95,6 +115,17 @@ class Database extends AbstractDatabase_1.default {
|
|
|
95
115
|
callback(this);
|
|
96
116
|
return this;
|
|
97
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* if has 2 arguments default operator '='
|
|
120
|
+
* @param {string} column
|
|
121
|
+
* @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
|
|
122
|
+
* @param {any?} value
|
|
123
|
+
* @return {this}
|
|
124
|
+
*/
|
|
125
|
+
resetWhere() {
|
|
126
|
+
this.$state.set('WHERE', '');
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
98
129
|
/**
|
|
99
130
|
* if has 2 arguments default operator '='
|
|
100
131
|
* @param {string} column
|
|
@@ -543,12 +574,13 @@ class Database extends AbstractDatabase_1.default {
|
|
|
543
574
|
* @return {this}
|
|
544
575
|
*/
|
|
545
576
|
whereQuery(callback) {
|
|
546
|
-
|
|
577
|
+
var _a;
|
|
578
|
+
const db = new DB_1.default((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
|
|
547
579
|
const repository = callback(db);
|
|
548
580
|
if (!(repository instanceof DB_1.default)) {
|
|
549
581
|
throw new Error(`unknown callback query: '[${repository}]'`);
|
|
550
582
|
}
|
|
551
|
-
const where = repository
|
|
583
|
+
const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
|
|
552
584
|
if (where === '') {
|
|
553
585
|
throw new Error(`unknown callback query with where condition`);
|
|
554
586
|
}
|
|
@@ -618,7 +650,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
618
650
|
* @return {this}
|
|
619
651
|
*/
|
|
620
652
|
join(pk, fk) {
|
|
621
|
-
|
|
653
|
+
var _a;
|
|
654
|
+
const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
|
|
622
655
|
if (this.$state.get('JOIN')) {
|
|
623
656
|
this.$state.set('JOIN', [
|
|
624
657
|
`${this.$state.get('JOIN')}`,
|
|
@@ -640,7 +673,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
640
673
|
* @return {this}
|
|
641
674
|
*/
|
|
642
675
|
rightJoin(pk, fk) {
|
|
643
|
-
|
|
676
|
+
var _a;
|
|
677
|
+
const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
|
|
644
678
|
if (this.$state.get('JOIN')) {
|
|
645
679
|
this.$state.set('JOIN', [
|
|
646
680
|
`${this.$state.get('JOIN')}`,
|
|
@@ -652,7 +686,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
652
686
|
this.$state.set('JOIN', [
|
|
653
687
|
`${this.$constants('RIGHT_JOIN')}`,
|
|
654
688
|
`${table} ${this.$constants('ON')} ${pk} = ${fk}`
|
|
655
|
-
].join(''));
|
|
689
|
+
].join(' '));
|
|
656
690
|
return this;
|
|
657
691
|
}
|
|
658
692
|
/**
|
|
@@ -662,7 +696,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
662
696
|
* @return {this}
|
|
663
697
|
*/
|
|
664
698
|
leftJoin(pk, fk) {
|
|
665
|
-
|
|
699
|
+
var _a;
|
|
700
|
+
const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
|
|
666
701
|
if (this.$state.get('JOIN')) {
|
|
667
702
|
this.$state.set('JOIN', [
|
|
668
703
|
`${this.$state.get('JOIN')}`,
|
|
@@ -684,7 +719,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
684
719
|
* @return {this}
|
|
685
720
|
*/
|
|
686
721
|
crossJoin(pk, fk) {
|
|
687
|
-
|
|
722
|
+
var _a;
|
|
723
|
+
const table = (_a = fk.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
|
|
688
724
|
if (this.$state.get('JOIN')) {
|
|
689
725
|
this.$state.set('JOIN', [
|
|
690
726
|
`${this.$state.get('JOIN')}`,
|
|
@@ -706,6 +742,13 @@ class Database extends AbstractDatabase_1.default {
|
|
|
706
742
|
* @return {this}
|
|
707
743
|
*/
|
|
708
744
|
orderBy(column, order = this.$constants('ASC')) {
|
|
745
|
+
if (this.$state.get('ORDER_BY')) {
|
|
746
|
+
this.$state.set('ORDER_BY', [
|
|
747
|
+
`${this.$state.get('ORDER_BY')}`,
|
|
748
|
+
`,${column} ${order.toUpperCase()}`
|
|
749
|
+
].join(' '));
|
|
750
|
+
return this;
|
|
751
|
+
}
|
|
709
752
|
this.$state.set('ORDER_BY', [
|
|
710
753
|
`${this.$constants('ORDER_BY')}`,
|
|
711
754
|
`${column} ${order.toUpperCase()}`
|
|
@@ -717,17 +760,13 @@ class Database extends AbstractDatabase_1.default {
|
|
|
717
760
|
* @param {string?} column [column=id]
|
|
718
761
|
* @return {this}
|
|
719
762
|
*/
|
|
720
|
-
latest(
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
`,${column} ${this.$constants('DESC')}`
|
|
725
|
-
].join(' '));
|
|
726
|
-
return this;
|
|
727
|
-
}
|
|
763
|
+
latest(...columns) {
|
|
764
|
+
let orderByDefault = 'id';
|
|
765
|
+
if (columns === null || columns === void 0 ? void 0 : columns.length)
|
|
766
|
+
orderByDefault = columns.join(', ');
|
|
728
767
|
this.$state.set('ORDER_BY', [
|
|
729
768
|
`${this.$constants('ORDER_BY')}`,
|
|
730
|
-
`${
|
|
769
|
+
`${orderByDefault} ${this.$constants('DESC')}`
|
|
731
770
|
].join(' '));
|
|
732
771
|
return this;
|
|
733
772
|
}
|
|
@@ -736,17 +775,13 @@ class Database extends AbstractDatabase_1.default {
|
|
|
736
775
|
* @param {string?} column [column=id]
|
|
737
776
|
* @return {this}
|
|
738
777
|
*/
|
|
739
|
-
oldest(
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
`,${column} ${this.$constants('ASC')}`
|
|
744
|
-
].join(' '));
|
|
745
|
-
return this;
|
|
746
|
-
}
|
|
778
|
+
oldest(...columns) {
|
|
779
|
+
let orderByDefault = 'id';
|
|
780
|
+
if (columns === null || columns === void 0 ? void 0 : columns.length)
|
|
781
|
+
orderByDefault = columns.join(', ');
|
|
747
782
|
this.$state.set('ORDER_BY', [
|
|
748
783
|
`${this.$constants('ORDER_BY')}`,
|
|
749
|
-
`${
|
|
784
|
+
`${orderByDefault} ${this.$constants('ASC')}`
|
|
750
785
|
].join(' '));
|
|
751
786
|
return this;
|
|
752
787
|
}
|
|
@@ -755,8 +790,11 @@ class Database extends AbstractDatabase_1.default {
|
|
|
755
790
|
* @param {string?} column [column=id]
|
|
756
791
|
* @return {this}
|
|
757
792
|
*/
|
|
758
|
-
groupBy(
|
|
759
|
-
|
|
793
|
+
groupBy(...columns) {
|
|
794
|
+
let groupBy = 'id';
|
|
795
|
+
if (columns === null || columns === void 0 ? void 0 : columns.length)
|
|
796
|
+
groupBy = columns.join(', ');
|
|
797
|
+
this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
|
|
760
798
|
return this;
|
|
761
799
|
}
|
|
762
800
|
/**
|
|
@@ -898,6 +936,17 @@ class Database extends AbstractDatabase_1.default {
|
|
|
898
936
|
this.$state.set('DEBUG', debug);
|
|
899
937
|
return this;
|
|
900
938
|
}
|
|
939
|
+
/**
|
|
940
|
+
* hook function when execute returned result to callback function
|
|
941
|
+
* @param {Function} func function for callback result
|
|
942
|
+
* @return {this}
|
|
943
|
+
*/
|
|
944
|
+
hook(func) {
|
|
945
|
+
if (typeof func !== "function")
|
|
946
|
+
throw new Error(`this '${func}' is not a function`);
|
|
947
|
+
this.$state.set('HOOK', [...this.$state.get('HOOK'), func]);
|
|
948
|
+
return this;
|
|
949
|
+
}
|
|
901
950
|
/**
|
|
902
951
|
*
|
|
903
952
|
* @param {object} data create not exists data
|
|
@@ -985,15 +1034,12 @@ class Database extends AbstractDatabase_1.default {
|
|
|
985
1034
|
* @return {this} this
|
|
986
1035
|
*/
|
|
987
1036
|
connection(options) {
|
|
988
|
-
const { host, port, database, username: user, password,
|
|
989
|
-
const pool = new connection_1.PoolConnection({
|
|
990
|
-
host,
|
|
1037
|
+
const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
|
|
1038
|
+
const pool = new connection_1.PoolConnection(Object.assign({ host,
|
|
991
1039
|
port,
|
|
992
1040
|
database,
|
|
993
1041
|
user,
|
|
994
|
-
password,
|
|
995
|
-
...others
|
|
996
|
-
});
|
|
1042
|
+
password }, others));
|
|
997
1043
|
this.$pool.set(pool.connection());
|
|
998
1044
|
return this;
|
|
999
1045
|
}
|
|
@@ -1003,7 +1049,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1003
1049
|
* @return {this} this
|
|
1004
1050
|
*/
|
|
1005
1051
|
pool(pool) {
|
|
1006
|
-
if (!pool
|
|
1052
|
+
if (!(pool === null || pool === void 0 ? void 0 : pool.hasOwnProperty('query'))) {
|
|
1007
1053
|
throw new Error('pool must have a query property');
|
|
1008
1054
|
}
|
|
1009
1055
|
this.$pool.set(pool);
|
|
@@ -1015,7 +1061,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1015
1061
|
* @return {this} this
|
|
1016
1062
|
*/
|
|
1017
1063
|
bind(connection) {
|
|
1018
|
-
if (!connection
|
|
1064
|
+
if (!(connection === null || connection === void 0 ? void 0 : connection.hasOwnProperty('query'))) {
|
|
1019
1065
|
throw new Error('connection must have a query property');
|
|
1020
1066
|
}
|
|
1021
1067
|
if (typeof connection.query !== 'function') {
|
|
@@ -1028,25 +1074,29 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1028
1074
|
* exceptColumns for method except
|
|
1029
1075
|
* @return {promise<string>} string
|
|
1030
1076
|
*/
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1077
|
+
exceptColumns() {
|
|
1078
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1079
|
+
const sql = [
|
|
1080
|
+
`${this.$constants('SHOW')}`,
|
|
1081
|
+
`${this.$constants('COLUMNS')}`,
|
|
1082
|
+
`${this.$constants('FROM')}`,
|
|
1083
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
1084
|
+
].join(' ');
|
|
1085
|
+
const rawColumns = yield this.queryStatement(sql);
|
|
1086
|
+
const columns = rawColumns.map((column) => column.Field);
|
|
1087
|
+
const removeExcept = columns.filter((column) => !this.$state.get('EXCEPT').includes(column));
|
|
1088
|
+
return removeExcept.join(', ');
|
|
1089
|
+
});
|
|
1042
1090
|
}
|
|
1043
1091
|
/**
|
|
1044
1092
|
* execute sql statements with raw sql query
|
|
1045
1093
|
* @param {string} sql sql execute return data
|
|
1046
1094
|
* @return {promise<any>}
|
|
1047
1095
|
*/
|
|
1048
|
-
|
|
1049
|
-
return
|
|
1096
|
+
rawQuery(sql) {
|
|
1097
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1098
|
+
return yield this.queryStatement(sql);
|
|
1099
|
+
});
|
|
1050
1100
|
}
|
|
1051
1101
|
/**
|
|
1052
1102
|
*
|
|
@@ -1055,14 +1105,16 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1055
1105
|
* @param {number} value
|
|
1056
1106
|
* @return {promise<any>}
|
|
1057
1107
|
*/
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1108
|
+
increment(column = 'id', value = 1) {
|
|
1109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1110
|
+
const query = `${this.$constants('SET')} ${column} = ${column} + ${value}`;
|
|
1111
|
+
this.$state.set('UPDATE', [
|
|
1112
|
+
`${this.$constants('UPDATE')}`,
|
|
1113
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1114
|
+
`${query}`
|
|
1115
|
+
].join(' '));
|
|
1116
|
+
return yield this._update(true);
|
|
1117
|
+
});
|
|
1066
1118
|
}
|
|
1067
1119
|
/**
|
|
1068
1120
|
*
|
|
@@ -1071,26 +1123,30 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1071
1123
|
* @param {number} value
|
|
1072
1124
|
* @return {promise<any>}
|
|
1073
1125
|
*/
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1126
|
+
decrement(column = 'id', value = 1) {
|
|
1127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1128
|
+
const query = `${this.$constants('SET')} ${column} = ${column} - ${value}`;
|
|
1129
|
+
this.$state.set('UPDATE', [
|
|
1130
|
+
`${this.$constants('UPDATE')}`,
|
|
1131
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1132
|
+
`${query}`
|
|
1133
|
+
].join(' '));
|
|
1134
|
+
return yield this._update(true);
|
|
1135
|
+
});
|
|
1082
1136
|
}
|
|
1083
1137
|
/**
|
|
1084
1138
|
* execute data without condition
|
|
1085
1139
|
* @return {promise<any>}
|
|
1086
1140
|
*/
|
|
1087
|
-
|
|
1088
|
-
return
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1141
|
+
all() {
|
|
1142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1143
|
+
return yield this.queryStatement([
|
|
1144
|
+
`${this.$constants('SELECT')}`,
|
|
1145
|
+
`*`,
|
|
1146
|
+
`${this.$constants('FROM')}`,
|
|
1147
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
1148
|
+
].join(' '));
|
|
1149
|
+
});
|
|
1094
1150
|
}
|
|
1095
1151
|
/**
|
|
1096
1152
|
*
|
|
@@ -1098,86 +1154,89 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1098
1154
|
* @param {number} id
|
|
1099
1155
|
* @return {promise<any>}
|
|
1100
1156
|
*/
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1157
|
+
find(id) {
|
|
1158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1159
|
+
const result = yield this.queryStatement([
|
|
1160
|
+
`${this.$constants('SELECT')}`,
|
|
1161
|
+
`*`,
|
|
1162
|
+
`${this.$constants('FROM')}`,
|
|
1163
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1164
|
+
`${this.$constants('WHERE')} id = ${id}`
|
|
1165
|
+
].join(' '));
|
|
1166
|
+
return (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
1167
|
+
});
|
|
1110
1168
|
}
|
|
1111
1169
|
/**
|
|
1112
1170
|
*
|
|
1113
1171
|
* execute data page & limit
|
|
1114
1172
|
* @param {?object} paginationOptions
|
|
1115
|
-
* @param {number} paginationOptions.limit
|
|
1116
|
-
* @param {number} paginationOptions.page
|
|
1173
|
+
* @param {number} paginationOptions.limit default 15
|
|
1174
|
+
* @param {number} paginationOptions.page default 1
|
|
1117
1175
|
* @return {promise<Pagination>}
|
|
1118
1176
|
*/
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
sql =
|
|
1133
|
-
`${sql}`,
|
|
1134
|
-
`${this.$constants('LIMIT')}`,
|
|
1135
|
-
`${limit}`,
|
|
1136
|
-
`${this.$constants('OFFSET')} ${offset}`
|
|
1137
|
-
].join(' ');
|
|
1138
|
-
}
|
|
1139
|
-
else {
|
|
1177
|
+
pagination(paginationOptions) {
|
|
1178
|
+
var _a, _b;
|
|
1179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1180
|
+
let limit = 15;
|
|
1181
|
+
let page = 1;
|
|
1182
|
+
if (paginationOptions != null) {
|
|
1183
|
+
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
1184
|
+
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1185
|
+
}
|
|
1186
|
+
const currentPage = page;
|
|
1187
|
+
const nextPage = currentPage + 1;
|
|
1188
|
+
const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
|
|
1189
|
+
const offset = (page - 1) * limit;
|
|
1190
|
+
let sql = this._buildQuery();
|
|
1140
1191
|
sql = sql.replace(this.$state.get('LIMIT'), `${limit} ${this.$constants('OFFSET')} ${offset}`);
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1192
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
1193
|
+
sql = [
|
|
1194
|
+
`${sql}`,
|
|
1195
|
+
`${this.$constants('LIMIT')}`,
|
|
1196
|
+
`${limit}`,
|
|
1197
|
+
`${this.$constants('OFFSET')} ${offset}`
|
|
1198
|
+
].join(' ');
|
|
1199
|
+
}
|
|
1200
|
+
const result = yield this.queryStatement(sql);
|
|
1201
|
+
if ((_a = this.$state.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1202
|
+
this._hiddenColumn(result);
|
|
1203
|
+
if (!result.length)
|
|
1204
|
+
return {
|
|
1205
|
+
meta: {
|
|
1206
|
+
total: 0,
|
|
1207
|
+
limit,
|
|
1208
|
+
total_page: 0,
|
|
1209
|
+
current_page: currentPage,
|
|
1210
|
+
last_page: 0,
|
|
1211
|
+
next_page: 0,
|
|
1212
|
+
prev_page: 0
|
|
1213
|
+
},
|
|
1214
|
+
data: []
|
|
1215
|
+
};
|
|
1216
|
+
this.$state.set('SELECT', [
|
|
1217
|
+
`${this.$constants('SELECT')}`,
|
|
1218
|
+
`${this.$constants('COUNT')}(*)`,
|
|
1219
|
+
`${this.$constants('AS')} total`
|
|
1220
|
+
].join(' '));
|
|
1221
|
+
sql = this._buildQuery();
|
|
1222
|
+
const count = yield this.queryStatement(sql);
|
|
1223
|
+
const total = count.shift().total || 0;
|
|
1224
|
+
let lastPage = Math.ceil(total / limit) || 0;
|
|
1225
|
+
lastPage = lastPage > 1 ? lastPage : 1;
|
|
1226
|
+
const totalPage = (_b = result === null || result === void 0 ? void 0 : result.length) !== null && _b !== void 0 ? _b : 0;
|
|
1146
1227
|
return {
|
|
1147
1228
|
meta: {
|
|
1148
|
-
|
|
1229
|
+
total_page: totalPage,
|
|
1230
|
+
total,
|
|
1149
1231
|
limit,
|
|
1150
|
-
total_page: 0,
|
|
1151
1232
|
current_page: currentPage,
|
|
1152
|
-
last_page:
|
|
1153
|
-
next_page:
|
|
1154
|
-
prev_page:
|
|
1233
|
+
last_page: lastPage,
|
|
1234
|
+
next_page: nextPage,
|
|
1235
|
+
prev_page: prevPage
|
|
1155
1236
|
},
|
|
1156
|
-
data: []
|
|
1237
|
+
data: result !== null && result !== void 0 ? result : []
|
|
1157
1238
|
};
|
|
1158
|
-
|
|
1159
|
-
`${this.$constants('SELECT')}`,
|
|
1160
|
-
`${this.$constants('COUNT')}(*)`,
|
|
1161
|
-
`${this.$constants('AS')} total`
|
|
1162
|
-
].join(' '));
|
|
1163
|
-
sql = this._buildQuery();
|
|
1164
|
-
const count = await this.queryStatement(sql);
|
|
1165
|
-
const total = count.shift().total || 0;
|
|
1166
|
-
let lastPage = Math.ceil(total / limit) || 0;
|
|
1167
|
-
lastPage = lastPage > 1 ? lastPage : 1;
|
|
1168
|
-
const totalPage = result?.length ?? 0;
|
|
1169
|
-
return {
|
|
1170
|
-
meta: {
|
|
1171
|
-
total_page: totalPage,
|
|
1172
|
-
total,
|
|
1173
|
-
limit,
|
|
1174
|
-
current_page: currentPage,
|
|
1175
|
-
last_page: lastPage,
|
|
1176
|
-
next_page: nextPage,
|
|
1177
|
-
prev_page: prevPage
|
|
1178
|
-
},
|
|
1179
|
-
data: result ?? []
|
|
1180
|
-
};
|
|
1239
|
+
});
|
|
1181
1240
|
}
|
|
1182
1241
|
/**
|
|
1183
1242
|
*
|
|
@@ -1187,168 +1246,193 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1187
1246
|
* @param {number} paginationOptions.page
|
|
1188
1247
|
* @return {promise<Pagination>}
|
|
1189
1248
|
*/
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1249
|
+
paginate(paginationOptions) {
|
|
1250
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1251
|
+
let limit = 15;
|
|
1252
|
+
let page = 1;
|
|
1253
|
+
if (paginationOptions != null) {
|
|
1254
|
+
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
1255
|
+
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1256
|
+
}
|
|
1257
|
+
return yield this.pagination({ limit, page });
|
|
1258
|
+
});
|
|
1198
1259
|
}
|
|
1199
1260
|
/**
|
|
1200
1261
|
*
|
|
1201
1262
|
* execute data return object | null
|
|
1202
1263
|
* @return {promise<object | null>}
|
|
1203
1264
|
*/
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
sql =
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
this.
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1265
|
+
first() {
|
|
1266
|
+
var _a, _b;
|
|
1267
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1268
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1269
|
+
this.select(yield this.exceptColumns());
|
|
1270
|
+
let sql = this._buildQuery();
|
|
1271
|
+
if (!sql.includes(this.$constants('LIMIT')))
|
|
1272
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
1273
|
+
else
|
|
1274
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
1275
|
+
const result = yield this.queryStatement(sql);
|
|
1276
|
+
if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1277
|
+
this._hiddenColumn(result);
|
|
1278
|
+
if (this.$state.get('PLUCK')) {
|
|
1279
|
+
const pluck = this.$state.get('PLUCK');
|
|
1280
|
+
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
1281
|
+
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1282
|
+
if (!checkProperty)
|
|
1283
|
+
throw new Error(`can't find property '${pluck}' of result`);
|
|
1284
|
+
const r = newData[pluck] || null;
|
|
1285
|
+
const hook = this.$state.get('HOOK');
|
|
1286
|
+
if (hook)
|
|
1287
|
+
yield hook(r);
|
|
1288
|
+
return r;
|
|
1289
|
+
}
|
|
1290
|
+
const r = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
1291
|
+
const hook = this.$state.get('HOOK');
|
|
1292
|
+
if (hook)
|
|
1293
|
+
yield hook(r);
|
|
1294
|
+
return r;
|
|
1295
|
+
});
|
|
1224
1296
|
}
|
|
1225
1297
|
/**
|
|
1226
1298
|
*
|
|
1227
1299
|
* execute data return object | throw rror
|
|
1228
1300
|
* @return {promise<object | null>}
|
|
1229
1301
|
*/
|
|
1230
|
-
|
|
1231
|
-
return
|
|
1302
|
+
findOne() {
|
|
1303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1304
|
+
return yield this.first();
|
|
1305
|
+
});
|
|
1232
1306
|
}
|
|
1233
1307
|
/**
|
|
1234
1308
|
*
|
|
1235
1309
|
* execute data return object | throw Error
|
|
1236
1310
|
* @return {promise<object | Error>}
|
|
1237
1311
|
*/
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
sql =
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
this.
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1312
|
+
firstOrError(message, options) {
|
|
1313
|
+
var _a, _b;
|
|
1314
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1315
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1316
|
+
this.select(yield this.exceptColumns());
|
|
1317
|
+
let sql = this._buildQuery();
|
|
1318
|
+
if (!sql.includes(this.$constants('LIMIT')))
|
|
1319
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
1320
|
+
else
|
|
1321
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
1322
|
+
const result = yield this.queryStatement(sql);
|
|
1323
|
+
if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1324
|
+
this._hiddenColumn(result);
|
|
1325
|
+
if (this.$state.get('PLUCK')) {
|
|
1326
|
+
const pluck = this.$state.get('PLUCK');
|
|
1327
|
+
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
1328
|
+
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1329
|
+
if (!checkProperty)
|
|
1330
|
+
throw new Error(`can't find property '${pluck}' of result`);
|
|
1331
|
+
const data = newData[pluck] || null;
|
|
1332
|
+
if (data == null) {
|
|
1333
|
+
if (options == null) {
|
|
1334
|
+
throw { message, code: 400 };
|
|
1335
|
+
}
|
|
1336
|
+
throw Object.assign({ message }, options);
|
|
1337
|
+
}
|
|
1338
|
+
const hook = this.$state.get('HOOK');
|
|
1339
|
+
if (hook)
|
|
1340
|
+
yield hook(data);
|
|
1341
|
+
return data;
|
|
1342
|
+
}
|
|
1343
|
+
const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
1256
1344
|
if (data == null) {
|
|
1257
1345
|
if (options == null) {
|
|
1258
1346
|
throw { message, code: 400 };
|
|
1259
1347
|
}
|
|
1260
|
-
throw { message,
|
|
1348
|
+
throw Object.assign({ message }, options);
|
|
1261
1349
|
}
|
|
1350
|
+
const hook = this.$state.get('HOOK');
|
|
1351
|
+
if (hook)
|
|
1352
|
+
yield hook(data);
|
|
1262
1353
|
return data;
|
|
1263
|
-
}
|
|
1264
|
-
const data = result?.shift() || null;
|
|
1265
|
-
if (data == null) {
|
|
1266
|
-
if (options == null) {
|
|
1267
|
-
throw { message, code: 400 };
|
|
1268
|
-
}
|
|
1269
|
-
throw { message, ...options };
|
|
1270
|
-
}
|
|
1271
|
-
return data;
|
|
1354
|
+
});
|
|
1272
1355
|
}
|
|
1273
1356
|
/**
|
|
1274
1357
|
*
|
|
1275
1358
|
* execute data return object | null
|
|
1276
1359
|
* @return {promise<object | null>}
|
|
1277
1360
|
*/
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
this.
|
|
1281
|
-
|
|
1282
|
-
if (!sql.includes(this.$constants('LIMIT')))
|
|
1283
|
-
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
1284
|
-
else
|
|
1285
|
-
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
1286
|
-
const result = await this.queryStatement(sql);
|
|
1287
|
-
if (this.$state.get('HIDDEN')?.length)
|
|
1288
|
-
this._hiddenColumn(result);
|
|
1289
|
-
if (this.$state.get('PLUCK')) {
|
|
1290
|
-
const pluck = this.$state.get('PLUCK');
|
|
1291
|
-
const newData = result?.shift();
|
|
1292
|
-
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1293
|
-
if (!checkProperty)
|
|
1294
|
-
throw new Error(`can't find property '${pluck}' of result`);
|
|
1295
|
-
return newData[pluck] || null;
|
|
1296
|
-
}
|
|
1297
|
-
return result?.shift() || null;
|
|
1361
|
+
findOneOrError(message, options) {
|
|
1362
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1363
|
+
return this.firstOrError(message, options);
|
|
1364
|
+
});
|
|
1298
1365
|
}
|
|
1299
1366
|
/**
|
|
1300
1367
|
*
|
|
1301
1368
|
* execute data return Array
|
|
1302
1369
|
* @return {promise<Array<any>>}
|
|
1303
1370
|
*/
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
this.
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
throw new Error(`can't find property '${pluck}' of result`);
|
|
1371
|
+
get() {
|
|
1372
|
+
var _a, _b;
|
|
1373
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1374
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1375
|
+
this.select(yield this.exceptColumns());
|
|
1376
|
+
const sql = this._buildQuery();
|
|
1377
|
+
const result = yield this.queryStatement(sql);
|
|
1378
|
+
if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1379
|
+
this._hiddenColumn(result);
|
|
1380
|
+
if (this.$state.get('CHUNK')) {
|
|
1381
|
+
const data = result.reduce((resultArray, item, index) => {
|
|
1382
|
+
const chunkIndex = Math.floor(index / this.$state.get('CHUNK'));
|
|
1383
|
+
if (!resultArray[chunkIndex])
|
|
1384
|
+
resultArray[chunkIndex] = [];
|
|
1385
|
+
resultArray[chunkIndex].push(item);
|
|
1386
|
+
return resultArray;
|
|
1387
|
+
}, []);
|
|
1388
|
+
const hook = this.$state.get('HOOK');
|
|
1389
|
+
if (hook)
|
|
1390
|
+
yield hook(data || []);
|
|
1391
|
+
return data || [];
|
|
1326
1392
|
}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1393
|
+
if (this.$state.get('PLUCK')) {
|
|
1394
|
+
const pluck = this.$state.get('PLUCK');
|
|
1395
|
+
const newData = result.map((d) => d[pluck]);
|
|
1396
|
+
if (newData.every((d) => d == null)) {
|
|
1397
|
+
throw new Error(`can't find property '${pluck}' of result`);
|
|
1398
|
+
}
|
|
1399
|
+
const hook = this.$state.get('HOOK');
|
|
1400
|
+
if (hook)
|
|
1401
|
+
yield hook(newData || []);
|
|
1402
|
+
return newData || [];
|
|
1403
|
+
}
|
|
1404
|
+
const hook = this.$state.get('HOOK');
|
|
1405
|
+
if (hook)
|
|
1406
|
+
yield hook(result || []);
|
|
1407
|
+
return result || [];
|
|
1408
|
+
});
|
|
1330
1409
|
}
|
|
1331
1410
|
/**
|
|
1332
1411
|
*
|
|
1333
1412
|
* execute data return Array
|
|
1334
1413
|
* @return {promise<Array<any>>}
|
|
1335
1414
|
*/
|
|
1336
|
-
|
|
1337
|
-
return
|
|
1415
|
+
findMany() {
|
|
1416
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1417
|
+
return yield this.get();
|
|
1418
|
+
});
|
|
1338
1419
|
}
|
|
1339
1420
|
/**
|
|
1340
1421
|
*
|
|
1341
1422
|
* execute data return json of result
|
|
1342
1423
|
* @return {promise<string>}
|
|
1343
1424
|
*/
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
this.
|
|
1351
|
-
|
|
1425
|
+
toJSON() {
|
|
1426
|
+
var _a, _b;
|
|
1427
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1428
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1429
|
+
this.select(yield this.exceptColumns());
|
|
1430
|
+
const sql = this._buildQuery();
|
|
1431
|
+
const result = yield this.queryStatement(sql);
|
|
1432
|
+
if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1433
|
+
this._hiddenColumn(result);
|
|
1434
|
+
return JSON.stringify(result);
|
|
1435
|
+
});
|
|
1352
1436
|
}
|
|
1353
1437
|
/**
|
|
1354
1438
|
*
|
|
@@ -1356,12 +1440,14 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1356
1440
|
* @param {string=} column [column=id]
|
|
1357
1441
|
* @return {promise<Array>}
|
|
1358
1442
|
*/
|
|
1359
|
-
|
|
1360
|
-
this
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1443
|
+
toArray(column = 'id') {
|
|
1444
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1445
|
+
this.$state.set('SELECT', `${this.$constants('SELECT')} ${column}`);
|
|
1446
|
+
const sql = this._buildQuery();
|
|
1447
|
+
const result = yield this.queryStatement(sql);
|
|
1448
|
+
const toArray = result.map((data) => data[column]);
|
|
1449
|
+
return toArray;
|
|
1450
|
+
});
|
|
1365
1451
|
}
|
|
1366
1452
|
/**
|
|
1367
1453
|
*
|
|
@@ -1369,32 +1455,38 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1369
1455
|
* @param {string=} column [column=id]
|
|
1370
1456
|
* @return {promise<number>}
|
|
1371
1457
|
*/
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1458
|
+
count(column = 'id') {
|
|
1459
|
+
var _a;
|
|
1460
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1461
|
+
this.$state.set('SELECT', [
|
|
1462
|
+
`${this.$constants('SELECT')}`,
|
|
1463
|
+
`${this.$constants('COUNT')}(${column})`,
|
|
1464
|
+
`${this.$constants('AS')} total`
|
|
1465
|
+
].join(' '));
|
|
1466
|
+
const sql = this._buildQuery();
|
|
1467
|
+
const result = yield this.queryStatement(sql);
|
|
1468
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0;
|
|
1469
|
+
});
|
|
1381
1470
|
}
|
|
1382
1471
|
/**
|
|
1383
1472
|
*
|
|
1384
1473
|
* execute data return result is exists
|
|
1385
1474
|
* @return {promise<boolean>}
|
|
1386
1475
|
*/
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1476
|
+
exists() {
|
|
1477
|
+
var _a;
|
|
1478
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1479
|
+
const result = yield this.queryStatement([
|
|
1480
|
+
`${this.$constants('SELECT')}`,
|
|
1481
|
+
`${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
|
|
1482
|
+
`*`,
|
|
1483
|
+
`${this.$state.get('FROM')}`,
|
|
1484
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1485
|
+
`${this.$state.get('WHERE')}`,
|
|
1486
|
+
`${this.$constants('LIMIT')} 1) ${this.$constants('AS')} 'exists'`
|
|
1487
|
+
].join(' '));
|
|
1488
|
+
return !!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false;
|
|
1489
|
+
});
|
|
1398
1490
|
}
|
|
1399
1491
|
/**
|
|
1400
1492
|
*
|
|
@@ -1402,15 +1494,18 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1402
1494
|
* @param {string=} column [column=id]
|
|
1403
1495
|
* @return {promise<number>}
|
|
1404
1496
|
*/
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1497
|
+
avg(column = 'id') {
|
|
1498
|
+
var _a;
|
|
1499
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1500
|
+
this.$state.set('SELECT', [
|
|
1501
|
+
`${this.$constants('SELECT')}`,
|
|
1502
|
+
`${this.$constants('AVG')}(${column})`,
|
|
1503
|
+
`${this.$constants('AS')} avg`
|
|
1504
|
+
].join(' '));
|
|
1505
|
+
const sql = this._buildQuery();
|
|
1506
|
+
const result = yield this.queryStatement(sql);
|
|
1507
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0;
|
|
1508
|
+
});
|
|
1414
1509
|
}
|
|
1415
1510
|
/**
|
|
1416
1511
|
*
|
|
@@ -1418,11 +1513,14 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1418
1513
|
* @param {string=} column [column=id]
|
|
1419
1514
|
* @return {promise<number>}
|
|
1420
1515
|
*/
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1516
|
+
sum(column = 'id') {
|
|
1517
|
+
var _a;
|
|
1518
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1519
|
+
this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('SUM')}(${column}) ${this.$constants('AS')} sum`);
|
|
1520
|
+
const sql = this._buildQuery();
|
|
1521
|
+
const result = yield this.queryStatement(sql);
|
|
1522
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0;
|
|
1523
|
+
});
|
|
1426
1524
|
}
|
|
1427
1525
|
/**
|
|
1428
1526
|
*
|
|
@@ -1430,11 +1528,14 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1430
1528
|
* @param {string=} column [column=id]
|
|
1431
1529
|
* @return {promise<number>}
|
|
1432
1530
|
*/
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1531
|
+
max(column = 'id') {
|
|
1532
|
+
var _a;
|
|
1533
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1534
|
+
this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MAX')}(${column}) ${this.$constants('AS')} max`);
|
|
1535
|
+
const sql = this._buildQuery();
|
|
1536
|
+
const result = yield this.queryStatement(sql);
|
|
1537
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0;
|
|
1538
|
+
});
|
|
1438
1539
|
}
|
|
1439
1540
|
/**
|
|
1440
1541
|
*
|
|
@@ -1442,48 +1543,57 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1442
1543
|
* @param {string=} column [column=id]
|
|
1443
1544
|
* @return {promise<number>}
|
|
1444
1545
|
*/
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1546
|
+
min(column = 'id') {
|
|
1547
|
+
var _a;
|
|
1548
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1549
|
+
this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MIN')}(${column}) ${this.$constants('AS')} min`);
|
|
1550
|
+
const sql = this._buildQuery();
|
|
1551
|
+
const result = yield this.queryStatement(sql);
|
|
1552
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0;
|
|
1553
|
+
});
|
|
1450
1554
|
}
|
|
1451
1555
|
/**
|
|
1452
1556
|
*
|
|
1453
1557
|
* delete data from database
|
|
1454
1558
|
* @return {promise<boolean>}
|
|
1455
1559
|
*/
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1560
|
+
delete() {
|
|
1561
|
+
var _a;
|
|
1562
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1563
|
+
if (!this.$state.get('WHERE')) {
|
|
1564
|
+
throw new Error("can't delete without where condition");
|
|
1565
|
+
}
|
|
1566
|
+
this.$state.set('DELETE', [
|
|
1567
|
+
`${this.$constants('DELETE')}`,
|
|
1568
|
+
`${this.$state.get('FROM')}`,
|
|
1569
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1570
|
+
`${this.$state.get('WHERE')}`
|
|
1571
|
+
].join(' '));
|
|
1572
|
+
const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
|
|
1573
|
+
if (result)
|
|
1574
|
+
return (_a = !!result) !== null && _a !== void 0 ? _a : false;
|
|
1575
|
+
return false;
|
|
1576
|
+
});
|
|
1470
1577
|
}
|
|
1471
1578
|
/**
|
|
1472
1579
|
*
|
|
1473
1580
|
* delete data from database ignore where condition
|
|
1474
1581
|
* @return {promise<boolean>}
|
|
1475
1582
|
*/
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1583
|
+
forceDelete() {
|
|
1584
|
+
var _a;
|
|
1585
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1586
|
+
this.$state.set('DELETE', [
|
|
1587
|
+
`${this.$constants('DELETE')}`,
|
|
1588
|
+
`${this.$state.get('FROM')}`,
|
|
1589
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1590
|
+
`${this.$state.get('WHERE')}`
|
|
1591
|
+
].join(' '));
|
|
1592
|
+
const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
|
|
1593
|
+
if (result)
|
|
1594
|
+
return (_a = !!result) !== null && _a !== void 0 ? _a : false;
|
|
1595
|
+
return false;
|
|
1596
|
+
});
|
|
1487
1597
|
}
|
|
1488
1598
|
/**
|
|
1489
1599
|
*
|
|
@@ -1491,38 +1601,41 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1491
1601
|
* @param {string} column
|
|
1492
1602
|
* @return {promise<Array>}
|
|
1493
1603
|
*/
|
|
1494
|
-
|
|
1495
|
-
this
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
const
|
|
1519
|
-
const
|
|
1520
|
-
|
|
1521
|
-
[column]
|
|
1522
|
-
|
|
1604
|
+
getGroupBy(column) {
|
|
1605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1606
|
+
this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${column}`);
|
|
1607
|
+
this.$state.set('SELECT', [
|
|
1608
|
+
`${this.$state.get('SELECT')}`,
|
|
1609
|
+
`, ${this.$constants('GROUP_CONCAT')}(id)`,
|
|
1610
|
+
`${this.$constants('AS')} data`
|
|
1611
|
+
].join(' '));
|
|
1612
|
+
const sql = this._buildQuery();
|
|
1613
|
+
const results = yield this.queryStatement(sql);
|
|
1614
|
+
let data = [];
|
|
1615
|
+
results.forEach((result) => {
|
|
1616
|
+
var _a, _b;
|
|
1617
|
+
const splits = (_b = (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.split(',')) !== null && _b !== void 0 ? _b : '0';
|
|
1618
|
+
splits.forEach((split) => data = [...data, split]);
|
|
1619
|
+
});
|
|
1620
|
+
const sqlGroups = [
|
|
1621
|
+
`${this.$constants('SELECT')}`,
|
|
1622
|
+
`*`,
|
|
1623
|
+
`${this.$constants('FROM')}`,
|
|
1624
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1625
|
+
`${this.$constants('WHERE')} id ${this.$constants('IN')}`,
|
|
1626
|
+
`(${data.map((a) => `\'${a}\'`).join(',') || ['0']})`
|
|
1627
|
+
].join(' ');
|
|
1628
|
+
const groups = yield this.queryStatement(sqlGroups);
|
|
1629
|
+
const resultData = results.map((result) => {
|
|
1630
|
+
const id = result[column];
|
|
1631
|
+
const newData = groups.filter((data) => data[column] === id);
|
|
1632
|
+
return ({
|
|
1633
|
+
[column]: id,
|
|
1634
|
+
data: newData
|
|
1635
|
+
});
|
|
1523
1636
|
});
|
|
1637
|
+
return resultData;
|
|
1524
1638
|
});
|
|
1525
|
-
return resultData;
|
|
1526
1639
|
}
|
|
1527
1640
|
/**
|
|
1528
1641
|
*
|
|
@@ -1530,45 +1643,49 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1530
1643
|
* @param {string} column
|
|
1531
1644
|
* @return {promise<Array>}
|
|
1532
1645
|
*/
|
|
1533
|
-
|
|
1534
|
-
return
|
|
1646
|
+
findManyGroupBy(column) {
|
|
1647
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1648
|
+
return yield this.getGroupBy(column);
|
|
1649
|
+
});
|
|
1535
1650
|
}
|
|
1536
1651
|
/**
|
|
1537
1652
|
* execute data when save *action [insert , update]
|
|
1538
1653
|
* @return {Promise<any>} promise
|
|
1539
1654
|
*/
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1655
|
+
save() {
|
|
1656
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1657
|
+
const attributes = this.$attributes;
|
|
1658
|
+
if (attributes != null) {
|
|
1659
|
+
while (true) {
|
|
1660
|
+
if (this.$state.get('WHERE')) {
|
|
1661
|
+
const query = this._queryUpdate(attributes);
|
|
1662
|
+
this.$state.set('UPDATE', [
|
|
1663
|
+
`${this.$constants('UPDATE')}`,
|
|
1664
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1665
|
+
`${query}`
|
|
1666
|
+
].join(' '));
|
|
1667
|
+
this.$state.set('SAVE', 'UPDATE');
|
|
1668
|
+
break;
|
|
1669
|
+
}
|
|
1670
|
+
const query = this._queryInsert(attributes);
|
|
1671
|
+
this.$state.set('INSERT', [
|
|
1672
|
+
`${this.$constants('INSERT')}`,
|
|
1548
1673
|
`${this.$state.get('TABLE_NAME')}`,
|
|
1549
1674
|
`${query}`
|
|
1550
1675
|
].join(' '));
|
|
1551
|
-
this.$state.set('SAVE', '
|
|
1676
|
+
this.$state.set('SAVE', 'INSERT');
|
|
1552
1677
|
break;
|
|
1553
1678
|
}
|
|
1554
|
-
const query = this._queryInsert(attributes);
|
|
1555
|
-
this.$state.set('INSERT', [
|
|
1556
|
-
`${this.$constants('INSERT')}`,
|
|
1557
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1558
|
-
`${query}`
|
|
1559
|
-
].join(' '));
|
|
1560
|
-
this.$state.set('SAVE', 'INSERT');
|
|
1561
|
-
break;
|
|
1562
1679
|
}
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
}
|
|
1680
|
+
switch (this.$state.get('SAVE')) {
|
|
1681
|
+
case 'INSERT_MULTIPLE': return yield this._createMultiple();
|
|
1682
|
+
case 'INSERT': return yield this._create();
|
|
1683
|
+
case 'UPDATE': return yield this._update();
|
|
1684
|
+
case 'INSERT_NOT_EXISTS': return yield this._insertNotExists();
|
|
1685
|
+
case 'UPDATE_OR_INSERT': return yield this._updateOrInsert();
|
|
1686
|
+
default: throw new Error(`unknown this [${this.$state.get('SAVE')}]`);
|
|
1687
|
+
}
|
|
1688
|
+
});
|
|
1572
1689
|
}
|
|
1573
1690
|
/**
|
|
1574
1691
|
*
|
|
@@ -1576,16 +1693,18 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1576
1693
|
* @param {string=} table table name
|
|
1577
1694
|
* @return {Promise<Array>}
|
|
1578
1695
|
*/
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1696
|
+
showColumns(table = this.$state.get('TABLE_NAME')) {
|
|
1697
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1698
|
+
const sql = [
|
|
1699
|
+
`${this.$constants('SHOW')}`,
|
|
1700
|
+
`${this.$constants('COLUMNS')}`,
|
|
1701
|
+
`${this.$constants('FROM')}`,
|
|
1702
|
+
`\`${table.replace(/\`/g, '')}\``
|
|
1703
|
+
].join(' ');
|
|
1704
|
+
const rawColumns = yield this.queryStatement(sql);
|
|
1705
|
+
const columns = rawColumns.map((column) => column.Field);
|
|
1706
|
+
return columns;
|
|
1707
|
+
});
|
|
1589
1708
|
}
|
|
1590
1709
|
/**
|
|
1591
1710
|
*
|
|
@@ -1593,39 +1712,41 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1593
1712
|
* @param {string=} table [table= current table name]
|
|
1594
1713
|
* @return {Promise<Array>}
|
|
1595
1714
|
*/
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
const
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1715
|
+
showSchemas(table = this.$state.get('TABLE_NAME')) {
|
|
1716
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1717
|
+
const sql = [
|
|
1718
|
+
`${this.$constants('SHOW')}`,
|
|
1719
|
+
`${this.$constants('COLUMNS')}`,
|
|
1720
|
+
`${this.$constants('FROM')}`,
|
|
1721
|
+
`\`${table.replace(/\`/g, '')}\``
|
|
1722
|
+
].join(' ');
|
|
1723
|
+
const raw = yield this.queryStatement(sql);
|
|
1724
|
+
const schemas = raw.map((r) => {
|
|
1725
|
+
const schema = [];
|
|
1726
|
+
schema.push(`${r.Field}`);
|
|
1727
|
+
schema.push(`${r.Type}`);
|
|
1728
|
+
if (r.Null === 'YES') {
|
|
1729
|
+
schema.push(`NULL`);
|
|
1730
|
+
}
|
|
1731
|
+
if (r.Null === 'NO') {
|
|
1732
|
+
schema.push(`NOT NULL`);
|
|
1733
|
+
}
|
|
1734
|
+
if (r.Key === 'PRI') {
|
|
1735
|
+
schema.push(`PRIMARY KEY`);
|
|
1736
|
+
}
|
|
1737
|
+
if (r.Key === 'UNI') {
|
|
1738
|
+
schema.push(`UNIQUE`);
|
|
1739
|
+
}
|
|
1740
|
+
if (r.Default) {
|
|
1741
|
+
schema.push(`DEFAULT '${r.Default}'`);
|
|
1742
|
+
}
|
|
1743
|
+
if (r.Extra) {
|
|
1744
|
+
schema.push(`${r.Extra.toUpperCase()}`);
|
|
1745
|
+
}
|
|
1746
|
+
return schema.join(' ');
|
|
1747
|
+
});
|
|
1748
|
+
return schemas;
|
|
1627
1749
|
});
|
|
1628
|
-
return schemas;
|
|
1629
1750
|
}
|
|
1630
1751
|
/**
|
|
1631
1752
|
*
|
|
@@ -1633,20 +1754,22 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1633
1754
|
* @param {string=} table table name
|
|
1634
1755
|
* @return {Promise<Array>}
|
|
1635
1756
|
*/
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
return v
|
|
1647
|
-
|
|
1757
|
+
showValues(table = this.$state.get('TABLE_NAME')) {
|
|
1758
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1759
|
+
const sql = [
|
|
1760
|
+
`${this.$constants('SELECT')}`,
|
|
1761
|
+
'*',
|
|
1762
|
+
`${this.$constants('FROM')}`,
|
|
1763
|
+
`\`${table.replace(/\`/g, '')}\``
|
|
1764
|
+
].join(' ');
|
|
1765
|
+
const raw = yield this.queryStatement(sql);
|
|
1766
|
+
const values = raw.map((value) => {
|
|
1767
|
+
return `(${Object.values(value).map((v) => {
|
|
1768
|
+
return v == null ? 'NULL' : `'${v}'`;
|
|
1769
|
+
}).join(',')})`;
|
|
1770
|
+
});
|
|
1771
|
+
return values;
|
|
1648
1772
|
});
|
|
1649
|
-
return values;
|
|
1650
1773
|
}
|
|
1651
1774
|
/**
|
|
1652
1775
|
*
|
|
@@ -1662,47 +1785,49 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1662
1785
|
|
|
1663
1786
|
* @return {Promise<boolean>}
|
|
1664
1787
|
*/
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
const
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
`(${schemas.join(',')})`,
|
|
1675
|
-
`${this.$constants('ENGINE')}`,
|
|
1676
|
-
];
|
|
1677
|
-
const values = await this.showValues(table);
|
|
1678
|
-
let valueSQL = [];
|
|
1679
|
-
if (values.length) {
|
|
1680
|
-
const columns = await this.showColumns(table);
|
|
1681
|
-
valueSQL = [
|
|
1682
|
-
`${this.$constants('INSERT')}`,
|
|
1788
|
+
backup({ database, to }) {
|
|
1789
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1790
|
+
const tables = yield this.queryStatement('SHOW TABLES');
|
|
1791
|
+
let backup = [];
|
|
1792
|
+
for (const t of tables) {
|
|
1793
|
+
const table = String(Object.values(t).shift());
|
|
1794
|
+
const schemas = yield this.showSchemas(table);
|
|
1795
|
+
const createTableSQL = [
|
|
1796
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
1683
1797
|
`\`${database}.${table}\``,
|
|
1684
|
-
`(${
|
|
1685
|
-
`${this.$constants('
|
|
1798
|
+
`(${schemas.join(',')})`,
|
|
1799
|
+
`${this.$constants('ENGINE')}`,
|
|
1800
|
+
];
|
|
1801
|
+
const values = yield this.showValues(table);
|
|
1802
|
+
let valueSQL = [];
|
|
1803
|
+
if (values.length) {
|
|
1804
|
+
const columns = yield this.showColumns(table);
|
|
1805
|
+
valueSQL = [
|
|
1806
|
+
`${this.$constants('INSERT')}`,
|
|
1807
|
+
`\`${database}.${table}\``,
|
|
1808
|
+
`(${columns.map((column) => `\`${column}\``).join(',')})`,
|
|
1809
|
+
`${this.$constants('VALUES')} ${values.join(',')}`
|
|
1810
|
+
];
|
|
1811
|
+
}
|
|
1812
|
+
backup = [
|
|
1813
|
+
...backup,
|
|
1814
|
+
{
|
|
1815
|
+
table: createTableSQL.join(' '),
|
|
1816
|
+
values: valueSQL.join(' '),
|
|
1817
|
+
}
|
|
1686
1818
|
];
|
|
1687
1819
|
}
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1820
|
+
if (to != null && Object.keys(to).length)
|
|
1821
|
+
this.connection(to);
|
|
1822
|
+
yield this.queryStatement(`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\``);
|
|
1823
|
+
for (const b of backup) {
|
|
1824
|
+
yield this.queryStatement(b.table);
|
|
1825
|
+
if (b.values) {
|
|
1826
|
+
yield this.queryStatement(b.values);
|
|
1693
1827
|
}
|
|
1694
|
-
];
|
|
1695
|
-
}
|
|
1696
|
-
if (to != null && Object.keys(to).length)
|
|
1697
|
-
this.connection(to);
|
|
1698
|
-
await this.queryStatement(`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\``);
|
|
1699
|
-
for (const b of backup) {
|
|
1700
|
-
await this.queryStatement(b.table);
|
|
1701
|
-
if (b.values) {
|
|
1702
|
-
await this.queryStatement(b.values);
|
|
1703
1828
|
}
|
|
1704
|
-
|
|
1705
|
-
|
|
1829
|
+
return true;
|
|
1830
|
+
});
|
|
1706
1831
|
}
|
|
1707
1832
|
/**
|
|
1708
1833
|
*
|
|
@@ -1719,350 +1844,352 @@ class Database extends AbstractDatabase_1.default {
|
|
|
1719
1844
|
|
|
1720
1845
|
* @return {Promise<boolean>}
|
|
1721
1846
|
*/
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
const
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
`${this.$constants('ENGINE')};`,
|
|
1733
|
-
];
|
|
1734
|
-
const values = await this.showValues(table);
|
|
1735
|
-
let valueSQL = [];
|
|
1736
|
-
if (values.length) {
|
|
1737
|
-
const columns = await this.showColumns(table);
|
|
1738
|
-
valueSQL = [
|
|
1739
|
-
`${this.$constants('INSERT')}`,
|
|
1847
|
+
backupToFile({ filePath, database, connection }) {
|
|
1848
|
+
var _a, _b;
|
|
1849
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1850
|
+
const tables = yield this.queryStatement(this.$constants('SHOW_TABLES'));
|
|
1851
|
+
let backup = [];
|
|
1852
|
+
for (const t of tables) {
|
|
1853
|
+
const table = String((_a = Object.values(t)) === null || _a === void 0 ? void 0 : _a.shift());
|
|
1854
|
+
const schemas = yield this.showSchemas(table);
|
|
1855
|
+
const createTableSQL = [
|
|
1856
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
1740
1857
|
`\`${table}\``,
|
|
1741
|
-
`(${
|
|
1742
|
-
`${this.$constants('
|
|
1858
|
+
`(${schemas.join(',')})`,
|
|
1859
|
+
`${this.$constants('ENGINE')};`,
|
|
1743
1860
|
];
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1861
|
+
const values = yield this.showValues(table);
|
|
1862
|
+
let valueSQL = [];
|
|
1863
|
+
if (values.length) {
|
|
1864
|
+
const columns = yield this.showColumns(table);
|
|
1865
|
+
valueSQL = [
|
|
1866
|
+
`${this.$constants('INSERT')}`,
|
|
1867
|
+
`\`${table}\``,
|
|
1868
|
+
`(${columns.map((column) => `\`${column}\``).join(',')})`,
|
|
1869
|
+
`${this.$constants('VALUES')} ${values.join(',')};`
|
|
1870
|
+
];
|
|
1750
1871
|
}
|
|
1872
|
+
backup = [
|
|
1873
|
+
...backup,
|
|
1874
|
+
{
|
|
1875
|
+
table: createTableSQL.join(' '),
|
|
1876
|
+
values: valueSQL.join(' ')
|
|
1877
|
+
}
|
|
1878
|
+
];
|
|
1879
|
+
}
|
|
1880
|
+
if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
|
|
1881
|
+
this.connection(connection);
|
|
1882
|
+
let sql = [
|
|
1883
|
+
`SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";`,
|
|
1884
|
+
`START TRANSACTION;`,
|
|
1885
|
+
`SET time_zone = "+00:00";`,
|
|
1886
|
+
`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\`;`,
|
|
1887
|
+
`USE \`${database}\`;`
|
|
1751
1888
|
];
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
`START TRANSACTION;`,
|
|
1758
|
-
`SET time_zone = "+00:00";`,
|
|
1759
|
-
`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\`;`,
|
|
1760
|
-
`USE \`${database}\`;`
|
|
1761
|
-
];
|
|
1762
|
-
for (const b of backup) {
|
|
1763
|
-
sql = [...sql, b.table];
|
|
1764
|
-
if (b.values) {
|
|
1765
|
-
sql = [...sql, b.values];
|
|
1889
|
+
for (const b of backup) {
|
|
1890
|
+
sql = [...sql, b.table];
|
|
1891
|
+
if (b.values) {
|
|
1892
|
+
sql = [...sql, b.values];
|
|
1893
|
+
}
|
|
1766
1894
|
}
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1895
|
+
fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)([...sql, 'COMMIT;'].join('\n'), {
|
|
1896
|
+
language: 'spark',
|
|
1897
|
+
tabWidth: 2,
|
|
1898
|
+
keywordCase: 'upper',
|
|
1899
|
+
linesBetweenQueries: 1,
|
|
1900
|
+
}));
|
|
1901
|
+
return;
|
|
1902
|
+
});
|
|
1775
1903
|
}
|
|
1776
1904
|
/**
|
|
1777
1905
|
*
|
|
1778
1906
|
* fake data
|
|
1779
1907
|
* @param {number} rows number of rows
|
|
1780
|
-
* @return {promise<any}
|
|
1908
|
+
* @return {promise<any>}
|
|
1781
1909
|
*/
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
1786
|
-
throw new Error("unknow this table");
|
|
1787
|
-
}
|
|
1910
|
+
faker(rows = 1) {
|
|
1911
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1912
|
+
let data = [];
|
|
1788
1913
|
const sql = [
|
|
1789
1914
|
`${this.$constants('SHOW')}`,
|
|
1790
1915
|
`${this.$constants('FIELDS')}`,
|
|
1791
1916
|
`${this.$constants('FROM')}`,
|
|
1792
1917
|
`${this.$state.get('TABLE_NAME')}`
|
|
1793
1918
|
].join(' ');
|
|
1794
|
-
const fields =
|
|
1795
|
-
let
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1919
|
+
const fields = yield this.queryStatement(sql);
|
|
1920
|
+
for (let row = 0; row < rows; row++) {
|
|
1921
|
+
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
1922
|
+
throw new Error("unknow this table");
|
|
1923
|
+
}
|
|
1924
|
+
let columnAndValue = {};
|
|
1925
|
+
for (const { Field: field, Type: type } of fields) {
|
|
1926
|
+
const passed = field.toLowerCase() === 'id' ||
|
|
1927
|
+
field.toLowerCase() === '_id' ||
|
|
1928
|
+
field.toLowerCase() === 'uuid';
|
|
1929
|
+
if (passed)
|
|
1930
|
+
continue;
|
|
1931
|
+
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
|
|
1932
|
+
}
|
|
1933
|
+
data = [...data, columnAndValue];
|
|
1806
1934
|
}
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
return this.save();
|
|
1935
|
+
const query = this._queryInsertMultiple(data);
|
|
1936
|
+
this.$state.set('INSERT', [
|
|
1937
|
+
`${this.$constants('INSERT')}`,
|
|
1938
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1939
|
+
`${query}`
|
|
1940
|
+
].join(' '));
|
|
1941
|
+
this.$state.set('SAVE', 'INSERT_MULTIPLE');
|
|
1942
|
+
return this.save();
|
|
1943
|
+
});
|
|
1817
1944
|
}
|
|
1818
1945
|
/**
|
|
1819
1946
|
*
|
|
1820
1947
|
* truncate of table
|
|
1821
1948
|
* @return {promise<boolean>}
|
|
1822
1949
|
*/
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1950
|
+
truncate() {
|
|
1951
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1952
|
+
const sql = [
|
|
1953
|
+
`${this.$constants('TRUNCATE_TABLE')}`,
|
|
1954
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
1955
|
+
].join(' ');
|
|
1956
|
+
yield this.queryStatement(sql);
|
|
1957
|
+
return true;
|
|
1958
|
+
});
|
|
1830
1959
|
}
|
|
1831
1960
|
/**
|
|
1832
1961
|
*
|
|
1833
1962
|
* drop of table
|
|
1834
1963
|
* @return {promise<boolean>}
|
|
1835
1964
|
*/
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1965
|
+
drop() {
|
|
1966
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1967
|
+
const sql = [
|
|
1968
|
+
`${this.$constants('DROP_TABLE')}`,
|
|
1969
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
1970
|
+
].join(' ');
|
|
1971
|
+
yield this.queryStatement(sql);
|
|
1972
|
+
return true;
|
|
1973
|
+
});
|
|
1843
1974
|
}
|
|
1844
1975
|
_queryWhereIsExists() {
|
|
1845
|
-
|
|
1976
|
+
var _a;
|
|
1977
|
+
return ((_a = this.$state.get('WHERE')) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
|
|
1846
1978
|
}
|
|
1847
1979
|
_bindTableAndColumnInQueryWhere(column) {
|
|
1848
1980
|
return `${this.$state.get('TABLE_NAME')}.\`${column}\``;
|
|
1849
1981
|
}
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1982
|
+
_insertNotExists() {
|
|
1983
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1984
|
+
if (!this.$state.get('WHERE'))
|
|
1985
|
+
throw new Error("Can't insert not exists without where condition");
|
|
1986
|
+
let sql = [
|
|
1987
|
+
`${this.$constants('SELECT')}`,
|
|
1988
|
+
`${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
|
|
1989
|
+
`*`,
|
|
1990
|
+
`${this.$state.get('FROM')}`,
|
|
1991
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1992
|
+
`${this.$state.get('WHERE')}`,
|
|
1993
|
+
`${this.$constants('LIMIT')} 1)`,
|
|
1994
|
+
`${this.$constants('AS')} 'exists'`
|
|
1995
|
+
].join(' ');
|
|
1996
|
+
const [{ exists: result }] = yield this.queryStatement(sql);
|
|
1997
|
+
const check = !!Number.parseInt(result);
|
|
1998
|
+
switch (check) {
|
|
1999
|
+
case false: {
|
|
2000
|
+
const [result, id] = yield this.actionStatement({
|
|
2001
|
+
sql: this.$state.get('INSERT'),
|
|
2002
|
+
returnId: true
|
|
2003
|
+
});
|
|
2004
|
+
if (this.$state.get('VOID'))
|
|
2005
|
+
return null;
|
|
2006
|
+
if (result) {
|
|
2007
|
+
const sql = [
|
|
2008
|
+
`${this.$state.get('SELECT')}`,
|
|
2009
|
+
`${this.$state.get('FROM')}`,
|
|
2010
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2011
|
+
`${this.$constants('WHERE')} id = ${id}`
|
|
2012
|
+
].join(' ');
|
|
2013
|
+
const data = yield this.queryStatement(sql);
|
|
2014
|
+
return (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2015
|
+
}
|
|
2016
|
+
return null;
|
|
1882
2017
|
}
|
|
1883
|
-
return null;
|
|
2018
|
+
default: return null;
|
|
1884
2019
|
}
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
2020
|
+
});
|
|
2021
|
+
}
|
|
2022
|
+
queryStatement(sql) {
|
|
2023
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2024
|
+
if (this.$state.get('DEBUG'))
|
|
2025
|
+
this.$utils.consoleDebug(sql);
|
|
2026
|
+
const result = yield this.$pool.query(sql);
|
|
2027
|
+
return result;
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
actionStatement({ sql, returnId = false }) {
|
|
2031
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2032
|
+
if (this.$state.get('DEBUG'))
|
|
2033
|
+
this.$utils.consoleDebug(sql);
|
|
2034
|
+
if (returnId) {
|
|
2035
|
+
const result = yield this.$pool.query(sql);
|
|
2036
|
+
return [result.affectedRows, result.insertId];
|
|
1889
2037
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2038
|
+
const { affectedRows: result } = yield this.$pool.query(sql);
|
|
2039
|
+
return result;
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
_create() {
|
|
2043
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2044
|
+
const [result, id] = yield this.actionStatement({
|
|
2045
|
+
sql: this.$state.get('INSERT'),
|
|
2046
|
+
returnId: true
|
|
2047
|
+
});
|
|
2048
|
+
if (this.$state.get('VOID'))
|
|
1893
2049
|
return null;
|
|
2050
|
+
if (result) {
|
|
2051
|
+
const sql = [
|
|
2052
|
+
`${this.$state.get('SELECT')}`,
|
|
2053
|
+
`${this.$state.get('FROM')}`,
|
|
2054
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2055
|
+
`${this.$constants('WHERE')} id = ${id}`
|
|
2056
|
+
].join(' ');
|
|
2057
|
+
const data = yield this.queryStatement(sql);
|
|
2058
|
+
const result = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2059
|
+
this.$state.set('RESULT', result);
|
|
2060
|
+
return result;
|
|
1894
2061
|
}
|
|
1895
|
-
|
|
2062
|
+
return null;
|
|
2063
|
+
});
|
|
1896
2064
|
}
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
return;
|
|
2065
|
+
_createMultiple() {
|
|
2066
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2067
|
+
const [result, id] = yield this.actionStatement({
|
|
2068
|
+
sql: this.$state.get('INSERT'),
|
|
2069
|
+
returnId: true
|
|
2070
|
+
});
|
|
2071
|
+
if (this.$state.get('VOID'))
|
|
2072
|
+
return null;
|
|
2073
|
+
if (result) {
|
|
2074
|
+
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
2075
|
+
const sql = [
|
|
2076
|
+
`${this.$state.get('SELECT')}`,
|
|
2077
|
+
`${this.$state.get('FROM')}`,
|
|
2078
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2079
|
+
`${this.$constants('WHERE')} id`,
|
|
2080
|
+
`${this.$constants('IN')} (${arrayId})`
|
|
2081
|
+
].join(' ');
|
|
2082
|
+
const data = yield this.queryStatement(sql);
|
|
2083
|
+
const resultData = data || null;
|
|
2084
|
+
this.$state.set('RESULT', resultData);
|
|
2085
|
+
return resultData;
|
|
1905
2086
|
}
|
|
1906
|
-
|
|
1907
|
-
}
|
|
1908
|
-
async queryStatement(sql) {
|
|
1909
|
-
if (this.$state.get('DEBUG'))
|
|
1910
|
-
this.$utils.consoleDebug(sql);
|
|
1911
|
-
const result = await this.$pool.get(sql);
|
|
1912
|
-
return result;
|
|
1913
|
-
}
|
|
1914
|
-
async actionStatement({ sql, returnId = false }) {
|
|
1915
|
-
if (this.$state.get('DEBUG'))
|
|
1916
|
-
this.$utils.consoleDebug(sql);
|
|
1917
|
-
if (returnId) {
|
|
1918
|
-
const result = await this.$pool.get(sql);
|
|
1919
|
-
return [result.affectedRows, result.insertId];
|
|
1920
|
-
}
|
|
1921
|
-
const { affectedRows: result } = await this.$pool.get(sql);
|
|
1922
|
-
return result;
|
|
1923
|
-
}
|
|
1924
|
-
async _create() {
|
|
1925
|
-
const [result, id] = await this.actionStatement({
|
|
1926
|
-
sql: this.$state.get('INSERT'),
|
|
1927
|
-
returnId: true
|
|
2087
|
+
return null;
|
|
1928
2088
|
});
|
|
1929
|
-
if (this.$state.get('VOID'))
|
|
1930
|
-
return Promise.resolve();
|
|
1931
|
-
if (result) {
|
|
1932
|
-
const sql = [
|
|
1933
|
-
`${this.$state.get('SELECT')}`,
|
|
1934
|
-
`${this.$state.get('FROM')}`,
|
|
1935
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1936
|
-
`${this.$constants('WHERE')} id = ${id}`
|
|
1937
|
-
].join(' ');
|
|
1938
|
-
const data = await this.queryStatement(sql);
|
|
1939
|
-
const result = data?.shift() || null;
|
|
1940
|
-
this.$state.set('RESULT', result);
|
|
1941
|
-
return result;
|
|
1942
|
-
}
|
|
1943
|
-
return null;
|
|
1944
2089
|
}
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
const sql = [
|
|
1955
|
-
`${this.$state.get('SELECT')}`,
|
|
2090
|
+
_updateOrInsert() {
|
|
2091
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2092
|
+
if (!this.$state.get('WHERE')) {
|
|
2093
|
+
throw new Error("Can't update or insert without where condition");
|
|
2094
|
+
}
|
|
2095
|
+
let sql = [
|
|
2096
|
+
`${this.$constants('SELECT')}`,
|
|
2097
|
+
`${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
|
|
2098
|
+
`*`,
|
|
1956
2099
|
`${this.$state.get('FROM')}`,
|
|
1957
2100
|
`${this.$state.get('TABLE_NAME')}`,
|
|
1958
|
-
`${this.$
|
|
1959
|
-
`${this.$constants('
|
|
2101
|
+
`${this.$state.get('WHERE')}`,
|
|
2102
|
+
`${this.$constants('LIMIT')} 1)`,
|
|
2103
|
+
`${this.$constants('AS')} 'exists'`
|
|
1960
2104
|
].join(' ');
|
|
1961
|
-
|
|
1962
|
-
const
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
switch (check) {
|
|
1986
|
-
case false: {
|
|
1987
|
-
const [result, id] = await this.actionStatement({
|
|
1988
|
-
sql: this.$state.get('INSERT'),
|
|
1989
|
-
returnId: true
|
|
1990
|
-
});
|
|
1991
|
-
if (this.$state.get('VOID'))
|
|
1992
|
-
return Promise.resolve();
|
|
1993
|
-
if (result) {
|
|
1994
|
-
const sql = [
|
|
1995
|
-
`${this.$state.get('SELECT')}`,
|
|
1996
|
-
`${this.$state.get('FROM')}`,
|
|
1997
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1998
|
-
`${this.$constants('WHERE')} id = ${id}`
|
|
1999
|
-
].join(' ');
|
|
2000
|
-
const data = await this.queryStatement(sql);
|
|
2001
|
-
const resultData = { ...data?.shift(), action_status: 'insert' } || null;
|
|
2002
|
-
this.$state.set('RESULT', resultData);
|
|
2003
|
-
return resultData;
|
|
2105
|
+
let check = false;
|
|
2106
|
+
const [{ exists: result }] = yield this.queryStatement(sql);
|
|
2107
|
+
check = !!parseInt(result);
|
|
2108
|
+
switch (check) {
|
|
2109
|
+
case false: {
|
|
2110
|
+
const [result, id] = yield this.actionStatement({
|
|
2111
|
+
sql: this.$state.get('INSERT'),
|
|
2112
|
+
returnId: true
|
|
2113
|
+
});
|
|
2114
|
+
if (this.$state.get('VOID'))
|
|
2115
|
+
return null;
|
|
2116
|
+
if (result) {
|
|
2117
|
+
const sql = [
|
|
2118
|
+
`${this.$state.get('SELECT')}`,
|
|
2119
|
+
`${this.$state.get('FROM')}`,
|
|
2120
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2121
|
+
`${this.$constants('WHERE')} id = ${id}`
|
|
2122
|
+
].join(' ');
|
|
2123
|
+
const data = yield this.queryStatement(sql);
|
|
2124
|
+
const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'insert' }) || null;
|
|
2125
|
+
this.$state.set('RESULT', resultData);
|
|
2126
|
+
return resultData;
|
|
2127
|
+
}
|
|
2128
|
+
return null;
|
|
2004
2129
|
}
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2130
|
+
case true: {
|
|
2131
|
+
const result = yield this.actionStatement({
|
|
2132
|
+
sql: [
|
|
2133
|
+
`${this.$state.get('UPDATE')}`,
|
|
2134
|
+
`${this.$state.get('WHERE')}`
|
|
2135
|
+
].join(' ')
|
|
2136
|
+
});
|
|
2137
|
+
if (this.$state.get('VOID'))
|
|
2138
|
+
return null;
|
|
2139
|
+
if (result) {
|
|
2140
|
+
const data = yield this.queryStatement([
|
|
2141
|
+
`${this.$state.get('SELECT')}`,
|
|
2142
|
+
`${this.$state.get('FROM')}`,
|
|
2143
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2144
|
+
`${this.$state.get('WHERE')}`
|
|
2145
|
+
].join(' '));
|
|
2146
|
+
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2147
|
+
for (const val of data) {
|
|
2148
|
+
val.action_status = 'update';
|
|
2149
|
+
}
|
|
2150
|
+
return data || [];
|
|
2026
2151
|
}
|
|
2027
|
-
return data ||
|
|
2152
|
+
return Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'update' }) || null;
|
|
2028
2153
|
}
|
|
2029
|
-
return
|
|
2154
|
+
return null;
|
|
2155
|
+
}
|
|
2156
|
+
default: {
|
|
2157
|
+
return null;
|
|
2030
2158
|
}
|
|
2031
|
-
return null;
|
|
2032
|
-
}
|
|
2033
|
-
default: {
|
|
2034
|
-
return null;
|
|
2035
2159
|
}
|
|
2036
|
-
}
|
|
2160
|
+
});
|
|
2037
2161
|
}
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2162
|
+
_update(ignoreWhere = false) {
|
|
2163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2164
|
+
if (!this.$state.get('WHERE') && !ignoreWhere)
|
|
2165
|
+
throw new Error("can't update without where condition");
|
|
2166
|
+
const result = yield this.actionStatement({
|
|
2167
|
+
sql: [
|
|
2168
|
+
`${this.$state.get('UPDATE')}`, `${this.$state.get('WHERE')}`
|
|
2169
|
+
].join(' ')
|
|
2170
|
+
});
|
|
2171
|
+
if (this.$state.get('VOID'))
|
|
2172
|
+
return null;
|
|
2173
|
+
if (!result)
|
|
2174
|
+
return null;
|
|
2175
|
+
const sql = [
|
|
2176
|
+
`${this.$state.get('SELECT')}`,
|
|
2177
|
+
`${this.$state.get('FROM')}`,
|
|
2178
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2179
|
+
`${this.$state.get('WHERE')}`
|
|
2180
|
+
].join(' ');
|
|
2181
|
+
const data = yield this.queryStatement(sql);
|
|
2182
|
+
if ((data === null || data === void 0 ? void 0 : data.length) > 1)
|
|
2183
|
+
return data || [];
|
|
2184
|
+
const res = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2185
|
+
this.$state.set('RESULT', res);
|
|
2186
|
+
return res;
|
|
2045
2187
|
});
|
|
2046
|
-
if (this.$state.get('VOID'))
|
|
2047
|
-
return Promise.resolve();
|
|
2048
|
-
if (!result)
|
|
2049
|
-
return null;
|
|
2050
|
-
const sql = [
|
|
2051
|
-
`${this.$state.get('SELECT')}`,
|
|
2052
|
-
`${this.$state.get('FROM')}`,
|
|
2053
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
2054
|
-
`${this.$state.get('WHERE')}`
|
|
2055
|
-
].join(' ');
|
|
2056
|
-
const data = await this.queryStatement(sql);
|
|
2057
|
-
if (data?.length > 1)
|
|
2058
|
-
return data || [];
|
|
2059
|
-
const res = data?.shift() || null;
|
|
2060
|
-
this.$state.set('RESULT', res);
|
|
2061
|
-
return res;
|
|
2062
2188
|
}
|
|
2063
2189
|
_hiddenColumn(data) {
|
|
2190
|
+
var _a;
|
|
2064
2191
|
const hidden = this.$state.get('HIDDEN');
|
|
2065
|
-
if (Object.keys(data)
|
|
2192
|
+
if ((_a = Object.keys(data)) === null || _a === void 0 ? void 0 : _a.length) {
|
|
2066
2193
|
hidden.forEach((column) => {
|
|
2067
2194
|
data.forEach((objColumn) => {
|
|
2068
2195
|
delete objColumn[column];
|
|
@@ -2073,6 +2200,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
2073
2200
|
}
|
|
2074
2201
|
_queryUpdate(data) {
|
|
2075
2202
|
const values = Object.entries(data).map(([column, value]) => {
|
|
2203
|
+
if (typeof value === 'string')
|
|
2204
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
2076
2205
|
return `${column} = ${value == null || value === 'NULL'
|
|
2077
2206
|
? 'NULL'
|
|
2078
2207
|
: typeof value === 'string' && value.startsWith(this.$constants('RAW'))
|
|
@@ -2084,6 +2213,8 @@ class Database extends AbstractDatabase_1.default {
|
|
|
2084
2213
|
_queryInsert(data) {
|
|
2085
2214
|
const columns = Object.keys(data).map((column) => `\`${column}\``);
|
|
2086
2215
|
const values = Object.values(data).map((value) => {
|
|
2216
|
+
if (typeof value === 'string')
|
|
2217
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
2087
2218
|
return `${value == null || value === 'NULL'
|
|
2088
2219
|
? 'NULL'
|
|
2089
2220
|
: `'${this.$utils.covertBooleanToNumber(value)}'`}`;
|
|
@@ -2095,9 +2226,12 @@ class Database extends AbstractDatabase_1.default {
|
|
|
2095
2226
|
].join(' ');
|
|
2096
2227
|
}
|
|
2097
2228
|
_queryInsertMultiple(data) {
|
|
2229
|
+
var _a;
|
|
2098
2230
|
let values = [];
|
|
2099
2231
|
for (let objects of data) {
|
|
2100
2232
|
const vals = Object.values(objects).map((value) => {
|
|
2233
|
+
if (typeof value === 'string')
|
|
2234
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
2101
2235
|
return `${value == null || value === 'NULL'
|
|
2102
2236
|
? 'NULL'
|
|
2103
2237
|
: typeof value === 'string' && value.includes(this.$constants('RAW'))
|
|
@@ -2106,7 +2240,7 @@ class Database extends AbstractDatabase_1.default {
|
|
|
2106
2240
|
});
|
|
2107
2241
|
values.push(`(${vals.join(',')})`);
|
|
2108
2242
|
}
|
|
2109
|
-
const columns = Object.keys([...data]
|
|
2243
|
+
const columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => `\`${column}\``);
|
|
2110
2244
|
return [
|
|
2111
2245
|
`(${columns})`,
|
|
2112
2246
|
`${this.$constants('VALUES')}`,
|
|
@@ -2168,23 +2302,32 @@ class Database extends AbstractDatabase_1.default {
|
|
|
2168
2302
|
}
|
|
2169
2303
|
return sql.filter(s => s !== '' || s == null).join(' ');
|
|
2170
2304
|
}
|
|
2171
|
-
_setupLogger() {
|
|
2172
|
-
let logger = [];
|
|
2173
|
-
return {
|
|
2174
|
-
get: () => logger,
|
|
2175
|
-
set: (data) => {
|
|
2176
|
-
logger = [...logger, data];
|
|
2177
|
-
return;
|
|
2178
|
-
},
|
|
2179
|
-
check: (data) => logger.indexOf(data) != -1
|
|
2180
|
-
};
|
|
2181
|
-
}
|
|
2182
2305
|
_initialConnection() {
|
|
2183
|
-
this.$pool = this._setupPool();
|
|
2184
|
-
this.$logger = this._setupLogger();
|
|
2185
2306
|
this.$utils = utils_1.default;
|
|
2307
|
+
this.$pool = (() => {
|
|
2308
|
+
let pool = connection_1.Pool;
|
|
2309
|
+
return {
|
|
2310
|
+
query: (sql) => __awaiter(this, void 0, void 0, function* () { return yield pool.query(sql); }),
|
|
2311
|
+
get: () => pool,
|
|
2312
|
+
set: (newConnection) => {
|
|
2313
|
+
pool = newConnection;
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
2316
|
+
};
|
|
2317
|
+
})();
|
|
2318
|
+
this.$logger = (() => {
|
|
2319
|
+
let logger = [];
|
|
2320
|
+
return {
|
|
2321
|
+
get: () => logger,
|
|
2322
|
+
set: (data) => {
|
|
2323
|
+
logger = [...logger, data];
|
|
2324
|
+
return;
|
|
2325
|
+
},
|
|
2326
|
+
check: (data) => logger.indexOf(data) != -1
|
|
2327
|
+
};
|
|
2328
|
+
})();
|
|
2186
2329
|
this.$constants = (name) => {
|
|
2187
|
-
if (
|
|
2330
|
+
if (name == null)
|
|
2188
2331
|
return constants_1.CONSTANTS;
|
|
2189
2332
|
if (!constants_1.CONSTANTS.hasOwnProperty(name.toUpperCase()))
|
|
2190
2333
|
throw new Error(`not found constants : ${name}`);
|