tspace-mysql 1.4.4 → 1.4.6
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 +112 -90
- package/dist/lib/connection/index.d.ts +1 -1
- package/dist/lib/connection/index.js +9 -10
- package/dist/lib/connection/options.js +1 -1
- package/dist/lib/constants/index.js +3 -2
- package/dist/lib/tspace/Abstract/AbstractBuilder.d.ts +41 -42
- package/dist/lib/tspace/Abstract/AbstractDB.d.ts +2 -4
- package/dist/lib/tspace/Abstract/AbstractModel.d.ts +5 -7
- package/dist/lib/tspace/Blueprint.d.ts +9 -3
- package/dist/lib/tspace/Blueprint.js +15 -8
- package/dist/lib/tspace/Builder.d.ts +92 -96
- package/dist/lib/tspace/Builder.js +220 -142
- package/dist/lib/tspace/Model.d.ts +45 -139
- package/dist/lib/tspace/Model.js +78 -180
- package/dist/lib/tspace/Schema.js +7 -5
- package/package.json +1 -1
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -206,7 +206,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
206
206
|
* @example
|
|
207
207
|
* class User extends Model {
|
|
208
208
|
* constructor() {
|
|
209
|
-
* this.useSoftDelete('
|
|
209
|
+
* this.useSoftDelete('deletedAt')
|
|
210
210
|
* }
|
|
211
211
|
* }
|
|
212
212
|
* @return {this} this
|
|
@@ -540,7 +540,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
540
540
|
}
|
|
541
541
|
/**
|
|
542
542
|
* Assign build in function to result of data
|
|
543
|
-
* @param {
|
|
543
|
+
* @param {Record} func
|
|
544
544
|
* @return {this} this
|
|
545
545
|
*/
|
|
546
546
|
registry(func) {
|
|
@@ -1133,7 +1133,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1133
1133
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1134
1134
|
this.disableSoftDelete();
|
|
1135
1135
|
this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
|
|
1136
|
-
const sql = this.
|
|
1136
|
+
const sql = this._buildQueryStatement();
|
|
1137
1137
|
return yield this._execute({ sql, type: 'GET' });
|
|
1138
1138
|
});
|
|
1139
1139
|
}
|
|
@@ -1145,7 +1145,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1145
1145
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1146
1146
|
this.disableSoftDelete();
|
|
1147
1147
|
this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
|
|
1148
|
-
const sql = this.
|
|
1148
|
+
const sql = this._buildQueryStatement();
|
|
1149
1149
|
return yield this._execute({ sql, type: 'GET' });
|
|
1150
1150
|
});
|
|
1151
1151
|
}
|
|
@@ -1176,162 +1176,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1176
1176
|
toTableNameAndColumn(column) {
|
|
1177
1177
|
return `\`${this._tableName()}\`.\`${this._valuePattern(column)}\``;
|
|
1178
1178
|
}
|
|
1179
|
-
/**
|
|
1180
|
-
*
|
|
1181
|
-
* @override Method
|
|
1182
|
-
* @return {string}
|
|
1183
|
-
*/
|
|
1184
|
-
toString() {
|
|
1185
|
-
const sql = this._buildQueryModel();
|
|
1186
|
-
if (this.$state.get('DEBUG'))
|
|
1187
|
-
this.$utils.consoleDebug(sql);
|
|
1188
|
-
return this.resultHandler(sql);
|
|
1189
|
-
}
|
|
1190
|
-
/**
|
|
1191
|
-
*
|
|
1192
|
-
* @override Method
|
|
1193
|
-
* @return {string}
|
|
1194
|
-
*/
|
|
1195
|
-
toSQL() {
|
|
1196
|
-
const sql = this._buildQueryModel();
|
|
1197
|
-
if (this.$state.get('DEBUG'))
|
|
1198
|
-
this.$utils.consoleDebug(sql);
|
|
1199
|
-
return this.resultHandler(sql);
|
|
1200
|
-
}
|
|
1201
|
-
/**
|
|
1202
|
-
*
|
|
1203
|
-
* @override Method
|
|
1204
|
-
* @return {string}
|
|
1205
|
-
*/
|
|
1206
|
-
toRawSQL() {
|
|
1207
|
-
const sql = this._buildQueryModel();
|
|
1208
|
-
if (this.$state.get('DEBUG'))
|
|
1209
|
-
this.$utils.consoleDebug(sql);
|
|
1210
|
-
return this.resultHandler(sql);
|
|
1211
|
-
}
|
|
1212
|
-
/**
|
|
1213
|
-
*
|
|
1214
|
-
* @override Method
|
|
1215
|
-
* @return {promise<string>}
|
|
1216
|
-
*/
|
|
1217
|
-
toJSON() {
|
|
1218
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1219
|
-
const sql = this._buildQueryModel();
|
|
1220
|
-
const result = yield this.queryStatement(sql);
|
|
1221
|
-
if (this.$state.get('HIDDEN').length)
|
|
1222
|
-
this._hiddenColumnModel(result);
|
|
1223
|
-
return this.resultHandler(JSON.stringify(result));
|
|
1224
|
-
});
|
|
1225
|
-
}
|
|
1226
|
-
/**
|
|
1227
|
-
*
|
|
1228
|
-
* @override Method
|
|
1229
|
-
* @param {string=} column [column=id]
|
|
1230
|
-
* @return {promise<Array>}
|
|
1231
|
-
*/
|
|
1232
|
-
toArray(column = 'id') {
|
|
1233
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1234
|
-
this.selectRaw(column);
|
|
1235
|
-
const sql = this._buildQueryModel();
|
|
1236
|
-
const result = yield this.queryStatement(sql);
|
|
1237
|
-
const toArray = result.map((data) => data[column]);
|
|
1238
|
-
return this.resultHandler(toArray);
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
/**
|
|
1242
|
-
*
|
|
1243
|
-
* @override Method
|
|
1244
|
-
* @param {string=} column [column=id]
|
|
1245
|
-
* @return {promise<number>}
|
|
1246
|
-
*/
|
|
1247
|
-
avg(column = 'id') {
|
|
1248
|
-
var _a;
|
|
1249
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1250
|
-
this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} \`avg\``);
|
|
1251
|
-
const sql = this._buildQueryModel();
|
|
1252
|
-
const result = yield this.queryStatement(sql);
|
|
1253
|
-
return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0));
|
|
1254
|
-
});
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
*
|
|
1258
|
-
* @override Method
|
|
1259
|
-
* @param {string} column [column=id]
|
|
1260
|
-
* @return {promise<number>}
|
|
1261
|
-
*/
|
|
1262
|
-
sum(column = 'id') {
|
|
1263
|
-
var _a;
|
|
1264
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1265
|
-
this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`sum\``);
|
|
1266
|
-
const sql = this._buildQueryModel();
|
|
1267
|
-
const result = yield this.queryStatement(sql);
|
|
1268
|
-
return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0));
|
|
1269
|
-
});
|
|
1270
|
-
}
|
|
1271
|
-
/**
|
|
1272
|
-
*
|
|
1273
|
-
* @override Method
|
|
1274
|
-
* @param {string=} column [column=id]
|
|
1275
|
-
* @return {promise<number>}
|
|
1276
|
-
*/
|
|
1277
|
-
max(column = 'id') {
|
|
1278
|
-
var _a;
|
|
1279
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1280
|
-
this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`max\``);
|
|
1281
|
-
const sql = this._buildQueryModel();
|
|
1282
|
-
const result = yield this.queryStatement(sql);
|
|
1283
|
-
return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0));
|
|
1284
|
-
});
|
|
1285
|
-
}
|
|
1286
|
-
/**
|
|
1287
|
-
*
|
|
1288
|
-
* @override Method
|
|
1289
|
-
* @param {string=} column [column=id]
|
|
1290
|
-
* @return {promise<number>}
|
|
1291
|
-
*/
|
|
1292
|
-
min(column = 'id') {
|
|
1293
|
-
var _a;
|
|
1294
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1295
|
-
this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`min\``);
|
|
1296
|
-
const sql = this._buildQueryModel();
|
|
1297
|
-
const result = yield this.queryStatement(sql);
|
|
1298
|
-
return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0));
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
/**
|
|
1302
|
-
*
|
|
1303
|
-
* @override Method
|
|
1304
|
-
* @param {string=} column [column=id]
|
|
1305
|
-
* @return {promise<number>}
|
|
1306
|
-
*/
|
|
1307
|
-
count(column = 'id') {
|
|
1308
|
-
var _a;
|
|
1309
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1310
|
-
this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`total\``);
|
|
1311
|
-
const sql = this._buildQueryModel();
|
|
1312
|
-
const result = yield this.queryStatement(sql);
|
|
1313
|
-
return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0));
|
|
1314
|
-
});
|
|
1315
|
-
}
|
|
1316
|
-
/**
|
|
1317
|
-
*
|
|
1318
|
-
* execute data return result is exists
|
|
1319
|
-
* @return {promise<boolean>}
|
|
1320
|
-
*/
|
|
1321
|
-
exists() {
|
|
1322
|
-
var _a;
|
|
1323
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1324
|
-
this.limit(1);
|
|
1325
|
-
const sql = this._buildQueryModel();
|
|
1326
|
-
const result = yield this.queryStatement([
|
|
1327
|
-
`${this.$constants('SELECT')}`,
|
|
1328
|
-
`${this.$constants('EXISTS')}`,
|
|
1329
|
-
`(${sql})`,
|
|
1330
|
-
`${this.$constants('AS')} \`exists\``
|
|
1331
|
-
].join(' '));
|
|
1332
|
-
return Boolean(this.resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false));
|
|
1333
|
-
});
|
|
1334
|
-
}
|
|
1335
1179
|
/**
|
|
1336
1180
|
* delete data from the database
|
|
1337
1181
|
* @override Method
|
|
@@ -1372,14 +1216,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1372
1216
|
/**
|
|
1373
1217
|
*
|
|
1374
1218
|
* @override Method
|
|
1375
|
-
* @return {promise<
|
|
1219
|
+
* @return {promise<Record<string,any> | null>}
|
|
1376
1220
|
*/
|
|
1377
1221
|
first() {
|
|
1378
1222
|
var _a;
|
|
1379
1223
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1380
1224
|
this._validateMethod('first');
|
|
1381
1225
|
if (this.$state.get('VOID'))
|
|
1382
|
-
return this.resultHandler(
|
|
1226
|
+
return this.resultHandler(undefined);
|
|
1383
1227
|
if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1384
1228
|
this.select(...yield this.exceptColumns());
|
|
1385
1229
|
this.limit(1);
|
|
@@ -1390,7 +1234,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1390
1234
|
});
|
|
1391
1235
|
}
|
|
1392
1236
|
return yield this._execute({
|
|
1393
|
-
sql: this.
|
|
1237
|
+
sql: this._buildQueryStatement(),
|
|
1394
1238
|
type: 'FIRST'
|
|
1395
1239
|
});
|
|
1396
1240
|
});
|
|
@@ -1398,7 +1242,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1398
1242
|
/**
|
|
1399
1243
|
*
|
|
1400
1244
|
* @override Method
|
|
1401
|
-
* @return {promise<
|
|
1245
|
+
* @return {promise<Record<string,any> | null>}
|
|
1402
1246
|
*/
|
|
1403
1247
|
findOne() {
|
|
1404
1248
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1424,7 +1268,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1424
1268
|
});
|
|
1425
1269
|
}
|
|
1426
1270
|
return yield this._execute({
|
|
1427
|
-
sql: this.
|
|
1271
|
+
sql: this._buildQueryStatement(),
|
|
1428
1272
|
type: 'FIRST_OR_ERROR',
|
|
1429
1273
|
message,
|
|
1430
1274
|
options
|
|
@@ -1454,7 +1298,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1454
1298
|
return [];
|
|
1455
1299
|
if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1456
1300
|
this.select(...yield this.exceptColumns());
|
|
1457
|
-
let sql = this.
|
|
1301
|
+
let sql = this._buildQueryStatement();
|
|
1458
1302
|
if (this.$state.get('RELATIONS_EXISTS'))
|
|
1459
1303
|
sql = this._queryRelationsExists();
|
|
1460
1304
|
return yield this._execute({
|
|
@@ -1498,7 +1342,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1498
1342
|
this.$state.set('PAGE', page);
|
|
1499
1343
|
this.limit(limit);
|
|
1500
1344
|
this.offset(offset);
|
|
1501
|
-
let sql = this.
|
|
1345
|
+
let sql = this._buildQueryStatement();
|
|
1502
1346
|
if (this.$state.get('RELATIONS_EXISTS'))
|
|
1503
1347
|
sql = this._queryRelationsExists();
|
|
1504
1348
|
return yield this._execute({
|
|
@@ -1537,7 +1381,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1537
1381
|
`${this.$constants('GROUP_CONCAT')}(id)`,
|
|
1538
1382
|
`${this.$constants('AS')} data`
|
|
1539
1383
|
].join(' '));
|
|
1540
|
-
const sql = this.
|
|
1384
|
+
const sql = this._buildQueryStatement();
|
|
1541
1385
|
const results = yield this.queryStatement(sql);
|
|
1542
1386
|
let data = [];
|
|
1543
1387
|
results.forEach((result) => {
|
|
@@ -1570,11 +1414,46 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1570
1414
|
/**
|
|
1571
1415
|
*
|
|
1572
1416
|
* update data in the database
|
|
1417
|
+
* @param {object} data
|
|
1418
|
+
* @param {array?} updateNotExists options for except update some records in your ${data}
|
|
1419
|
+
* @return {this} this
|
|
1420
|
+
*/
|
|
1421
|
+
update(data, updateNotExists = []) {
|
|
1422
|
+
if (!Object.keys(data).length)
|
|
1423
|
+
throw new Error('This method must be required');
|
|
1424
|
+
if (updateNotExists.length) {
|
|
1425
|
+
for (const c of updateNotExists) {
|
|
1426
|
+
for (const column in data) {
|
|
1427
|
+
if (c !== column)
|
|
1428
|
+
continue;
|
|
1429
|
+
const value = data[column];
|
|
1430
|
+
data[column] = this._updateHandler(column, value);
|
|
1431
|
+
break;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
const query = this._queryUpdateModel(data);
|
|
1436
|
+
this.$state.set('UPDATE', [
|
|
1437
|
+
`${this.$constants('UPDATE')}`,
|
|
1438
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1439
|
+
`${query}`
|
|
1440
|
+
].join(' '));
|
|
1441
|
+
this.$state.set('SAVE', 'UPDATE');
|
|
1442
|
+
return this;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
*
|
|
1573
1446
|
* @override Method
|
|
1574
1447
|
* @param {object} data
|
|
1575
1448
|
* @return {this} this
|
|
1576
1449
|
*/
|
|
1577
|
-
|
|
1450
|
+
updateNotExists(data) {
|
|
1451
|
+
if (!Object.keys(data).length)
|
|
1452
|
+
throw new Error('This method must be required');
|
|
1453
|
+
for (const column in data) {
|
|
1454
|
+
const value = data[column];
|
|
1455
|
+
data[column] = this._updateHandler(column, value);
|
|
1456
|
+
}
|
|
1578
1457
|
const query = this._queryUpdateModel(data);
|
|
1579
1458
|
this.$state.set('UPDATE', [
|
|
1580
1459
|
`${this.$constants('UPDATE')}`,
|
|
@@ -1591,6 +1470,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1591
1470
|
* @return {this} this
|
|
1592
1471
|
*/
|
|
1593
1472
|
insert(data) {
|
|
1473
|
+
if (!Object.keys(data).length)
|
|
1474
|
+
throw new Error('This method must be required');
|
|
1594
1475
|
const query = this._queryInsertModel(data);
|
|
1595
1476
|
this.$state.set('INSERT', [
|
|
1596
1477
|
`${this.$constants('INSERT')}`,
|
|
@@ -1607,6 +1488,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1607
1488
|
* @return {this} this
|
|
1608
1489
|
*/
|
|
1609
1490
|
create(data) {
|
|
1491
|
+
if (!Object.keys(data).length)
|
|
1492
|
+
throw new Error('This method must be required');
|
|
1610
1493
|
const query = this._queryInsertModel(data);
|
|
1611
1494
|
this.$state.set('INSERT', [
|
|
1612
1495
|
`${this.$constants('INSERT')}`,
|
|
@@ -1623,6 +1506,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1623
1506
|
* @return {this} this
|
|
1624
1507
|
*/
|
|
1625
1508
|
updateOrCreate(data) {
|
|
1509
|
+
if (!Object.keys(data).length)
|
|
1510
|
+
throw new Error('This method must be required');
|
|
1626
1511
|
const queryUpdate = this._queryUpdateModel(data);
|
|
1627
1512
|
const queryInsert = this._queryInsertModel(data);
|
|
1628
1513
|
this.$state.set('INSERT', [
|
|
@@ -1672,6 +1557,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1672
1557
|
* @return {this} this
|
|
1673
1558
|
*/
|
|
1674
1559
|
createOrSelect(data) {
|
|
1560
|
+
if (!Object.keys(data).length)
|
|
1561
|
+
throw new Error('This method must be required');
|
|
1675
1562
|
const queryInsert = this._queryInsertModel(data);
|
|
1676
1563
|
this.$state.set('INSERT', [
|
|
1677
1564
|
`${this.$constants('INSERT')}`,
|
|
@@ -1771,7 +1658,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1771
1658
|
/**
|
|
1772
1659
|
*
|
|
1773
1660
|
* @override Method
|
|
1774
|
-
* @return {Promise<Record<string,any> |
|
|
1661
|
+
* @return {Promise<Record<string,any> | any[] | null | undefined>}
|
|
1775
1662
|
*/
|
|
1776
1663
|
save() {
|
|
1777
1664
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1813,12 +1700,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1813
1700
|
/**
|
|
1814
1701
|
*
|
|
1815
1702
|
* fake data into to this table
|
|
1703
|
+
* @override Method
|
|
1816
1704
|
* @param {number} rows number of rows
|
|
1817
1705
|
* @return {promise<any>}
|
|
1818
1706
|
*/
|
|
1819
1707
|
faker(rows = 1) {
|
|
1820
1708
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1821
1709
|
let data = [];
|
|
1710
|
+
this.void();
|
|
1822
1711
|
const sql = [
|
|
1823
1712
|
`${this.$constants('SHOW')}`,
|
|
1824
1713
|
`${this.$constants('FIELDS')}`,
|
|
@@ -1950,7 +1839,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1950
1839
|
}
|
|
1951
1840
|
return this;
|
|
1952
1841
|
}
|
|
1953
|
-
|
|
1842
|
+
/**
|
|
1843
|
+
*
|
|
1844
|
+
* generate sql statements
|
|
1845
|
+
* @override
|
|
1846
|
+
* @return {string} string generated query string
|
|
1847
|
+
*/
|
|
1848
|
+
_buildQueryStatement() {
|
|
1954
1849
|
let sql = [];
|
|
1955
1850
|
while (true) {
|
|
1956
1851
|
this._handleSoftDelete();
|
|
@@ -2152,6 +2047,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2152
2047
|
}
|
|
2153
2048
|
const sql = clone
|
|
2154
2049
|
.bind(this.$pool.get())
|
|
2050
|
+
.select(this.$state.get('PRIMARY_KEY'))
|
|
2155
2051
|
.whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
|
|
2156
2052
|
.toString();
|
|
2157
2053
|
return sql;
|
|
@@ -2194,11 +2090,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2194
2090
|
}
|
|
2195
2091
|
const sql = clone
|
|
2196
2092
|
.bind(this.$pool.get())
|
|
2093
|
+
.select(this.$state.get('PRIMARY_KEY'))
|
|
2197
2094
|
.whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
|
|
2198
2095
|
.toString();
|
|
2199
2096
|
this.whereExists(sql);
|
|
2200
2097
|
}
|
|
2201
|
-
const sql = this.
|
|
2098
|
+
const sql = this._buildQueryStatement();
|
|
2202
2099
|
return sql;
|
|
2203
2100
|
}
|
|
2204
2101
|
_relation(parents, relation) {
|
|
@@ -2682,7 +2579,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2682
2579
|
returnId: true
|
|
2683
2580
|
});
|
|
2684
2581
|
if (this.$state.get('VOID'))
|
|
2685
|
-
return this.resultHandler(
|
|
2582
|
+
return this.resultHandler(undefined);
|
|
2686
2583
|
if (!result)
|
|
2687
2584
|
return this.resultHandler(null);
|
|
2688
2585
|
const resultData = yield new Model().copyModel(this)
|
|
@@ -2699,7 +2596,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2699
2596
|
returnId: true
|
|
2700
2597
|
});
|
|
2701
2598
|
if (this.$state.get('VOID'))
|
|
2702
|
-
return this.resultHandler(
|
|
2599
|
+
return this.resultHandler(undefined);
|
|
2703
2600
|
if (!result)
|
|
2704
2601
|
return this.resultHandler(null);
|
|
2705
2602
|
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
@@ -2720,7 +2617,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2720
2617
|
returnId: true
|
|
2721
2618
|
});
|
|
2722
2619
|
if (this.$state.get('VOID') || !result)
|
|
2723
|
-
return this.resultHandler(
|
|
2620
|
+
return this.resultHandler(undefined);
|
|
2724
2621
|
const data = yield new Model().copyModel(this).bind(this.$pool.get()).where('id', id).first();
|
|
2725
2622
|
const resultData = data == null
|
|
2726
2623
|
? null
|
|
@@ -2732,7 +2629,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2732
2629
|
sql: new Model().copyModel(this, { update: true, where: true }).toString()
|
|
2733
2630
|
});
|
|
2734
2631
|
if (this.$state.get('VOID') || !result)
|
|
2735
|
-
return this.resultHandler(
|
|
2632
|
+
return this.resultHandler(undefined);
|
|
2736
2633
|
const data = yield new Model().copyModel(this, { where: true }).bind(this.$pool.get()).get();
|
|
2737
2634
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2738
2635
|
for (const v of data)
|
|
@@ -2757,7 +2654,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2757
2654
|
returnId: true
|
|
2758
2655
|
});
|
|
2759
2656
|
if (this.$state.get('VOID') || !result)
|
|
2760
|
-
return this.resultHandler(
|
|
2657
|
+
return this.resultHandler(undefined);
|
|
2761
2658
|
const data = yield new Model().copyModel(this).bind(this.$pool.get()).where('id', id).first();
|
|
2762
2659
|
const resultData = data == null
|
|
2763
2660
|
? null
|
|
@@ -2766,7 +2663,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2766
2663
|
}
|
|
2767
2664
|
case true: {
|
|
2768
2665
|
if (this.$state.get('VOID'))
|
|
2769
|
-
return this.resultHandler(
|
|
2666
|
+
return this.resultHandler(undefined);
|
|
2770
2667
|
const data = yield new Model().copyModel(this, { where: true }).bind(this.$pool.get()).get();
|
|
2771
2668
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2772
2669
|
for (const v of data)
|
|
@@ -2782,10 +2679,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2782
2679
|
_updateModel() {
|
|
2783
2680
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2784
2681
|
this._assertError(!String(this.$state.get('WHERE')), "can not update [ update ] without where condition");
|
|
2785
|
-
const sql = this.
|
|
2682
|
+
const sql = this._buildQueryStatement();
|
|
2786
2683
|
const result = yield this.actionStatement({ sql });
|
|
2787
2684
|
if (this.$state.get('VOID') || !result || result == null)
|
|
2788
|
-
return this.resultHandler(
|
|
2685
|
+
return this.resultHandler(undefined);
|
|
2789
2686
|
const data = yield new Model().copyModel(this, { where: true }).bind(this.$pool.get()).get();
|
|
2790
2687
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2791
2688
|
return this.resultHandler(data || []);
|
|
@@ -2832,6 +2729,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2832
2729
|
'insert',
|
|
2833
2730
|
'create',
|
|
2834
2731
|
'update',
|
|
2732
|
+
'updateNotExists',
|
|
2835
2733
|
'delete',
|
|
2836
2734
|
'forceDelete',
|
|
2837
2735
|
'restore',
|
|
@@ -143,9 +143,11 @@ class Schema extends Builder_1.Builder {
|
|
|
143
143
|
const directories = fs_1.default.readdirSync(pathFolders, { withFileTypes: true });
|
|
144
144
|
const files = yield Promise.all(directories.map((directory) => {
|
|
145
145
|
const newDir = path_1.default.resolve(String(pathFolders), directory.name);
|
|
146
|
-
|
|
146
|
+
if (directory.isDirectory() && directory.name.toLocaleLowerCase().includes('migrations'))
|
|
147
|
+
return null;
|
|
148
|
+
return directory.isDirectory() ? Schema.sync(newDir, { force, log, delay }) : newDir;
|
|
147
149
|
}));
|
|
148
|
-
const pathModels = [].concat(...files);
|
|
150
|
+
const pathModels = [].concat(...files).filter(d => d != null || d === '');
|
|
149
151
|
yield new Promise(r => setTimeout(r, delay));
|
|
150
152
|
const promises = pathModels.map((pathModel) => Schema._syncExecute({ pathModel, force, log }));
|
|
151
153
|
yield Promise.all(promises);
|
|
@@ -165,7 +167,7 @@ class Schema extends Builder_1.Builder {
|
|
|
165
167
|
const checkTableIsExists = Boolean((yield new Builder_1.Builder().rawQuery(`SHOW TABLES LIKE '${model.getTableName()}';`)).length);
|
|
166
168
|
if (!checkTableIsExists) {
|
|
167
169
|
const sql = new Schema().createTable(model.getTableName(), schemaModel);
|
|
168
|
-
yield new Builder_1.Builder().
|
|
170
|
+
yield new Builder_1.Builder().debug(log).rawQuery(sql);
|
|
169
171
|
return;
|
|
170
172
|
}
|
|
171
173
|
if (!force)
|
|
@@ -183,7 +185,7 @@ class Schema extends Builder_1.Builder {
|
|
|
183
185
|
const type = (_b = (_a = schemaModel[column]) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : null;
|
|
184
186
|
const attributes = (_d = (_c = schemaModel[column]) === null || _c === void 0 ? void 0 : _c.attributes) !== null && _d !== void 0 ? _d : null;
|
|
185
187
|
if (findAfterIndex == null || type == null || attributes == null)
|
|
186
|
-
|
|
188
|
+
continue;
|
|
187
189
|
const sql = [
|
|
188
190
|
'ALTER TABLE',
|
|
189
191
|
`\`${model.getTableName()}\``,
|
|
@@ -192,7 +194,7 @@ class Schema extends Builder_1.Builder {
|
|
|
192
194
|
'AFTER',
|
|
193
195
|
`\`${findAfterIndex}\``
|
|
194
196
|
].join(' ');
|
|
195
|
-
yield new Builder_1.Builder().
|
|
197
|
+
yield new Builder_1.Builder().debug(log).rawQuery(sql);
|
|
196
198
|
}
|
|
197
199
|
return;
|
|
198
200
|
});
|