tspace-mysql 1.4.7 → 1.4.8
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 +24 -7
- package/dist/lib/{tspace/Interface.d.ts → Interface.d.ts} +2 -1
- package/dist/lib/connection/index.d.ts +1 -1
- package/dist/lib/constants/index.js +3 -3
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.d.ts +2 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.js +2 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.d.ts +8 -2
- package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.d.ts +4 -2
- package/dist/lib/tspace/Blueprint.d.ts +11 -1
- package/dist/lib/tspace/Blueprint.js +11 -1
- package/dist/lib/tspace/Builder.d.ts +22 -7
- package/dist/lib/tspace/Builder.js +384 -310
- package/dist/lib/tspace/DB.d.ts +42 -2
- package/dist/lib/tspace/DB.js +61 -5
- package/dist/lib/tspace/{ProxyHandler.js → Handlers/Proxy.js} +1 -1
- package/dist/lib/tspace/{RelationHandler.d.ts → Handlers/Relation.d.ts} +4 -4
- package/dist/lib/tspace/{RelationHandler.js → Handlers/Relation.js} +95 -19
- package/dist/lib/tspace/Model.d.ts +71 -10
- package/dist/lib/tspace/Model.js +228 -64
- package/dist/lib/tspace/Schema.js +4 -5
- package/dist/lib/tspace/index.d.ts +2 -0
- package/dist/lib/tspace/index.js +3 -1
- package/dist/lib/utils/index.d.ts +1 -0
- package/dist/lib/utils/index.js +8 -0
- package/package.json +1 -1
- /package/dist/lib/{tspace/Interface.js → Interface.js} +0 -0
- /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.js +0 -0
- /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.js +0 -0
- /package/dist/lib/tspace/{ProxyHandler.d.ts → Handlers/Proxy.d.ts} +0 -0
- /package/dist/lib/tspace/{StateHandler.d.ts → Handlers/State.d.ts} +0 -0
- /package/dist/lib/tspace/{StateHandler.js → Handlers/State.js} +0 -0
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -16,10 +16,10 @@ exports.Model = void 0;
|
|
|
16
16
|
const pluralize_1 = __importDefault(require("pluralize"));
|
|
17
17
|
const DB_1 = require("./DB");
|
|
18
18
|
const Schema_1 = require("./Schema");
|
|
19
|
-
const AbstractModel_1 = require("./
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
19
|
+
const AbstractModel_1 = require("./Abstracts/AbstractModel");
|
|
20
|
+
const Proxy_1 = require("./Handlers/Proxy");
|
|
21
|
+
const State_1 = require("./Handlers/State");
|
|
22
|
+
const Relation_1 = require("./Handlers/Relation");
|
|
23
23
|
/**
|
|
24
24
|
*
|
|
25
25
|
* 'Model' class is a representation of a database table
|
|
@@ -41,7 +41,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
41
41
|
*/
|
|
42
42
|
this.define();
|
|
43
43
|
this.boot();
|
|
44
|
-
return new Proxy(this,
|
|
44
|
+
return new Proxy(this, Proxy_1.proxyHandler);
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
47
|
* The 'define' method is a special method that you can define within a model.
|
|
@@ -145,7 +145,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
145
145
|
* this.useRegistry()
|
|
146
146
|
* }
|
|
147
147
|
* }
|
|
148
|
-
|
|
149
148
|
*/
|
|
150
149
|
useRegistry() {
|
|
151
150
|
this._setState('REGISTRY', Object.assign(Object.assign({}, this._getState('REGISTRY')), { '$attach': this._attach, '$detach': this._detach }));
|
|
@@ -247,11 +246,31 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
247
246
|
this._makeTableName();
|
|
248
247
|
return this;
|
|
249
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* The "useCamelCase" method is used to assign pattern camelCase.
|
|
251
|
+
* @return {this} this
|
|
252
|
+
* @example
|
|
253
|
+
* class User extends Model {
|
|
254
|
+
* constructor() {
|
|
255
|
+
* this.useCamelCase()
|
|
256
|
+
* }
|
|
257
|
+
* }
|
|
258
|
+
*/
|
|
250
259
|
useCamelCase() {
|
|
251
260
|
this._setState('PATTERN', this.$constants('PATTERN').camelCase);
|
|
252
261
|
this._makeTableName();
|
|
253
262
|
return this;
|
|
254
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* The "SnakeCase" method is used to assign pattern snake_case.
|
|
266
|
+
* @return {this} this
|
|
267
|
+
* @example
|
|
268
|
+
* class User extends Model {
|
|
269
|
+
* constructor() {
|
|
270
|
+
* this.SnakeCase()
|
|
271
|
+
* }
|
|
272
|
+
* }
|
|
273
|
+
*/
|
|
255
274
|
useSnakeCase() {
|
|
256
275
|
this._setState('PATTERN', this.$constants('PATTERN').snake_case);
|
|
257
276
|
this._makeTableName();
|
|
@@ -447,7 +466,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
447
466
|
* @override
|
|
448
467
|
* @return {promise<string>} string
|
|
449
468
|
*/
|
|
450
|
-
|
|
469
|
+
exceptColumns() {
|
|
451
470
|
return __awaiter(this, void 0, void 0, function* () {
|
|
452
471
|
const excepts = this._getState('EXCEPTS');
|
|
453
472
|
const hasDot = excepts.some((except) => /\./.test(except));
|
|
@@ -459,7 +478,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
459
478
|
const tableNames = names.length ? [...new Set(names)] : [this._getState('TABLE_NAME')];
|
|
460
479
|
const removeExcepts = [];
|
|
461
480
|
for (const tableName of tableNames) {
|
|
462
|
-
|
|
481
|
+
const isHasSchema = [
|
|
482
|
+
this._getState('SCHEMA_TABLE') != null,
|
|
483
|
+
tableName.replace(/`/g, '') === this._getState('TABLE_NAME').replace(/`/g, '')
|
|
484
|
+
].every(d => d);
|
|
485
|
+
if (isHasSchema) {
|
|
463
486
|
const columns = Object.keys(this._getState('SCHEMA_TABLE'));
|
|
464
487
|
const removeExcept = columns.filter((column) => {
|
|
465
488
|
return excepts.every((except) => {
|
|
@@ -470,7 +493,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
470
493
|
return except !== column;
|
|
471
494
|
});
|
|
472
495
|
});
|
|
473
|
-
removeExcepts.push(hasDot ? removeExcept.map(r =>
|
|
496
|
+
removeExcepts.push(hasDot ? removeExcept.map(r => `${tableName}.${r}`) : removeExcept);
|
|
474
497
|
continue;
|
|
475
498
|
}
|
|
476
499
|
const sql = [
|
|
@@ -515,6 +538,135 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
515
538
|
relation.query = callback(new relation.model());
|
|
516
539
|
return this;
|
|
517
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
*
|
|
543
|
+
* The 'makeSelectStatement' method is used to make select statement.
|
|
544
|
+
* @return {Promise<string>} string
|
|
545
|
+
*/
|
|
546
|
+
makeSelectStatement() {
|
|
547
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
548
|
+
const schemaModel = this.getSchemaModel();
|
|
549
|
+
const makeStatement = (columns) => {
|
|
550
|
+
return [
|
|
551
|
+
`${this.$constants('SELECT')}`,
|
|
552
|
+
`${columns.join(', ')}`,
|
|
553
|
+
`${this.$constants('FROM')}`,
|
|
554
|
+
`\`${this.getTableName()}\``,
|
|
555
|
+
].join(' ');
|
|
556
|
+
};
|
|
557
|
+
if (schemaModel == null) {
|
|
558
|
+
const schemaTable = yield this.getSchema();
|
|
559
|
+
const columns = schemaTable.map(column => `\`${this.getTableName()}\`.\`${column.Field}\``);
|
|
560
|
+
return makeStatement(columns);
|
|
561
|
+
}
|
|
562
|
+
const columns = Object.keys(schemaModel).map((column) => `\`${this.getTableName()}\`.\`${column}\``);
|
|
563
|
+
return makeStatement(columns);
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
*
|
|
568
|
+
* The 'makeInsertStatement' method is used to make insert table statement.
|
|
569
|
+
* @return {Promise<string>} string
|
|
570
|
+
*/
|
|
571
|
+
makeInsertStatement() {
|
|
572
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
573
|
+
const schemaModel = this.getSchemaModel();
|
|
574
|
+
const makeStatement = (columns) => {
|
|
575
|
+
return [
|
|
576
|
+
`${this.$constants('INSERT')}`,
|
|
577
|
+
`\`${this.getTableName()}\``,
|
|
578
|
+
`(${columns.join(', ')})`,
|
|
579
|
+
`${this.$constants('VALUES')}`,
|
|
580
|
+
`(${Array(columns.length).fill('?').join(' , ')})`
|
|
581
|
+
].join(' ');
|
|
582
|
+
};
|
|
583
|
+
if (schemaModel == null) {
|
|
584
|
+
const schemaTable = yield this.getSchema();
|
|
585
|
+
const columns = schemaTable.map(column => `\`${column.Field}\``);
|
|
586
|
+
return makeStatement(columns);
|
|
587
|
+
}
|
|
588
|
+
const columns = Object.keys(schemaModel).map((column) => `\`${column}\``);
|
|
589
|
+
return makeStatement(columns);
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
*
|
|
594
|
+
* The 'makeUpdateStatement' method is used to make update table statement.
|
|
595
|
+
* @return {Promise<string>} string
|
|
596
|
+
*/
|
|
597
|
+
makeUpdateStatement() {
|
|
598
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
599
|
+
const schemaModel = this.getSchemaModel();
|
|
600
|
+
const makeStatement = (columns) => {
|
|
601
|
+
return [
|
|
602
|
+
`${this.$constants('UPDATE')}`,
|
|
603
|
+
`\`${this.getTableName()}\``,
|
|
604
|
+
`${this.$constants('SET')}`,
|
|
605
|
+
`(${columns.join(', ')})`
|
|
606
|
+
].join(' ');
|
|
607
|
+
};
|
|
608
|
+
if (schemaModel == null) {
|
|
609
|
+
const schemaTable = yield this.getSchema();
|
|
610
|
+
const columns = schemaTable.map(column => `\`${column.Field}\` = ?`);
|
|
611
|
+
return makeStatement(columns);
|
|
612
|
+
}
|
|
613
|
+
const columns = Object.keys(schemaModel).map((column) => `\`${column}\` = ?`);
|
|
614
|
+
return makeStatement(columns);
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
*
|
|
619
|
+
* The 'makeDeleteStatement' method is used to make delete statement.
|
|
620
|
+
* @return {Promise<string>} string
|
|
621
|
+
*/
|
|
622
|
+
makeDeleteStatement() {
|
|
623
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
624
|
+
const makeStatement = () => {
|
|
625
|
+
return [
|
|
626
|
+
`${this.$constants('DELETE')}`,
|
|
627
|
+
`${this.$constants('FROM')}`,
|
|
628
|
+
`\`${this.getTableName()}\``,
|
|
629
|
+
`${this.$constants('WHERE')}`,
|
|
630
|
+
`?where_expression`
|
|
631
|
+
].join(' ');
|
|
632
|
+
};
|
|
633
|
+
return makeStatement();
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
*
|
|
638
|
+
* The 'makeCreateStatement' method is used to make create table statement.
|
|
639
|
+
* @return {Promise<string>} string
|
|
640
|
+
*/
|
|
641
|
+
makeCreateStatement() {
|
|
642
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
+
const schemaModel = this.getSchemaModel();
|
|
644
|
+
const makeStatement = (columns) => {
|
|
645
|
+
return [
|
|
646
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
647
|
+
`\`${this.getTableName()}\``,
|
|
648
|
+
`(`,
|
|
649
|
+
`\n${columns === null || columns === void 0 ? void 0 : columns.join(',\n')}`,
|
|
650
|
+
`\n)`,
|
|
651
|
+
`${this.$constants('ENGINE')}`
|
|
652
|
+
].join(' ');
|
|
653
|
+
};
|
|
654
|
+
if (schemaModel == null) {
|
|
655
|
+
const columns = yield this.showSchema();
|
|
656
|
+
return makeStatement(columns);
|
|
657
|
+
}
|
|
658
|
+
let columns = [];
|
|
659
|
+
for (const key in schemaModel) {
|
|
660
|
+
const data = schemaModel[key];
|
|
661
|
+
const { type, attributes } = data;
|
|
662
|
+
columns = [
|
|
663
|
+
...columns,
|
|
664
|
+
`${key} ${type} ${attributes === null || attributes === void 0 ? void 0 : attributes.join(' ')}`
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
return makeStatement(columns);
|
|
668
|
+
});
|
|
669
|
+
}
|
|
518
670
|
/**
|
|
519
671
|
*
|
|
520
672
|
* Clone instance of model
|
|
@@ -547,7 +699,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
547
699
|
if ((options === null || options === void 0 ? void 0 : options.delete) == null)
|
|
548
700
|
newInstance.$state.set('DELETE', '');
|
|
549
701
|
if ((options === null || options === void 0 ? void 0 : options.where) == null)
|
|
550
|
-
newInstance.$state.set('WHERE',
|
|
702
|
+
newInstance.$state.set('WHERE', []);
|
|
551
703
|
if ((options === null || options === void 0 ? void 0 : options.limit) == null)
|
|
552
704
|
newInstance.$state.set('LIMIT', '');
|
|
553
705
|
if ((options === null || options === void 0 ? void 0 : options.offset) == null)
|
|
@@ -559,7 +711,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
559
711
|
if ((options === null || options === void 0 ? void 0 : options.select) == null)
|
|
560
712
|
newInstance.$state.set('SELECT', []);
|
|
561
713
|
if ((options === null || options === void 0 ? void 0 : options.join) == null)
|
|
562
|
-
newInstance.$state.set('JOIN',
|
|
714
|
+
newInstance.$state.set('JOIN', []);
|
|
563
715
|
return newInstance;
|
|
564
716
|
}
|
|
565
717
|
/**
|
|
@@ -710,6 +862,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
710
862
|
this._setState('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'all'));
|
|
711
863
|
return this;
|
|
712
864
|
}
|
|
865
|
+
/**
|
|
866
|
+
* The 'withAll' method is used to eager load related (relations) data when retrieving records from a database.
|
|
867
|
+
*
|
|
868
|
+
* Eager loading allows you to retrieve a primary model and its related models in a more efficient
|
|
869
|
+
* It's method ignore soft delete
|
|
870
|
+
* @param {...string} nameRelations if data exists return blank
|
|
871
|
+
* @return {this} this
|
|
872
|
+
*/
|
|
873
|
+
withCount(...nameRelations) {
|
|
874
|
+
var _a;
|
|
875
|
+
this._setState('RELATIONS', (_a = this.$relation) === null || _a === void 0 ? void 0 : _a.apply(nameRelations, 'count'));
|
|
876
|
+
return this;
|
|
877
|
+
}
|
|
713
878
|
/**
|
|
714
879
|
* The 'withTrashed' method is used to eager load related (relations) data when retrieving records from a database.
|
|
715
880
|
*
|
|
@@ -780,7 +945,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
780
945
|
* @return {this} this
|
|
781
946
|
*/
|
|
782
947
|
has(...nameRelations) {
|
|
783
|
-
return this.
|
|
948
|
+
return this.withExists(...nameRelations);
|
|
784
949
|
}
|
|
785
950
|
/**
|
|
786
951
|
*
|
|
@@ -832,6 +997,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
832
997
|
*/
|
|
833
998
|
withQuery(nameRelation, callback) {
|
|
834
999
|
var _a;
|
|
1000
|
+
this.with(nameRelation);
|
|
835
1001
|
(_a = this.$relation) === null || _a === void 0 ? void 0 : _a.callback(nameRelation, callback);
|
|
836
1002
|
return this;
|
|
837
1003
|
}
|
|
@@ -860,7 +1026,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
860
1026
|
* @return {this} this
|
|
861
1027
|
*/
|
|
862
1028
|
relations(...nameRelations) {
|
|
863
|
-
return this.
|
|
1029
|
+
return this.with(...nameRelations);
|
|
864
1030
|
}
|
|
865
1031
|
/**
|
|
866
1032
|
*
|
|
@@ -887,7 +1053,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
887
1053
|
* @return {this} this
|
|
888
1054
|
*/
|
|
889
1055
|
relationsExists(...nameRelations) {
|
|
890
|
-
return this.
|
|
1056
|
+
return this.withExists(...nameRelations);
|
|
891
1057
|
}
|
|
892
1058
|
/**
|
|
893
1059
|
*
|
|
@@ -945,7 +1111,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
945
1111
|
* @return {this} this
|
|
946
1112
|
*/
|
|
947
1113
|
relationsAll(...nameRelations) {
|
|
948
|
-
return this.
|
|
1114
|
+
return this.withAll(...nameRelations);
|
|
949
1115
|
}
|
|
950
1116
|
/**
|
|
951
1117
|
*
|
|
@@ -954,7 +1120,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
954
1120
|
* @return {this} this
|
|
955
1121
|
*/
|
|
956
1122
|
relationsTrashed(...nameRelations) {
|
|
957
|
-
return this.
|
|
1123
|
+
return this.withTrashed(...nameRelations);
|
|
958
1124
|
}
|
|
959
1125
|
/**
|
|
960
1126
|
* The 'hasOne' relationship defines a one-to-one relationship between two database tables.
|
|
@@ -1235,8 +1401,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1235
1401
|
delete() {
|
|
1236
1402
|
var _a, _b;
|
|
1237
1403
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1238
|
-
|
|
1239
|
-
throw new Error("Can't delete without where condition");
|
|
1404
|
+
this._assertError(!this._getState('where').length, "This method 'delete' is require to use 'where' conditions");
|
|
1240
1405
|
this.limit(1);
|
|
1241
1406
|
if (this._getState('SOFT_DELETE')) {
|
|
1242
1407
|
const deletedAt = this._valuePattern(this._getState('SOFT_DELETE_FORMAT'));
|
|
@@ -1270,14 +1435,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1270
1435
|
deleteMany() {
|
|
1271
1436
|
var _a, _b;
|
|
1272
1437
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1273
|
-
|
|
1274
|
-
throw new Error("Can't delete without where condition");
|
|
1438
|
+
this._assertError(!this._getState('where').length, "This method 'delete' is require to use 'where' conditions");
|
|
1275
1439
|
if (this._getState('SOFT_DELETE')) {
|
|
1276
1440
|
const deletedAt = this._valuePattern(this._getState('SOFT_DELETE_FORMAT'));
|
|
1277
1441
|
const sql = new Model()
|
|
1278
1442
|
.copyModel(this, { where: true, limit: true })
|
|
1279
1443
|
.bind(this.$pool.get())
|
|
1280
|
-
.
|
|
1444
|
+
.updateMany({
|
|
1281
1445
|
[deletedAt]: this.$utils.timestamp()
|
|
1282
1446
|
})
|
|
1283
1447
|
.toString();
|
|
@@ -1308,7 +1472,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1308
1472
|
if (this._getState('VOID'))
|
|
1309
1473
|
return this._resultHandler(undefined);
|
|
1310
1474
|
if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1311
|
-
this.select(...yield this.
|
|
1475
|
+
this.select(...yield this.exceptColumns());
|
|
1312
1476
|
this.limit(1);
|
|
1313
1477
|
if (this._getState('RELATIONS_EXISTS')) {
|
|
1314
1478
|
return yield this._execute({
|
|
@@ -1340,7 +1504,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1340
1504
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1341
1505
|
this._validateMethod('firstOrError');
|
|
1342
1506
|
if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1343
|
-
this.select(...yield this.
|
|
1507
|
+
this.select(...yield this.exceptColumns());
|
|
1344
1508
|
this.limit(1);
|
|
1345
1509
|
if (this._getState('RELATIONS_EXISTS')) {
|
|
1346
1510
|
return yield this._execute({
|
|
@@ -1378,7 +1542,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1378
1542
|
if (this._getState('VOID'))
|
|
1379
1543
|
return [];
|
|
1380
1544
|
if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1381
|
-
this.select(...yield this.
|
|
1545
|
+
this.select(...yield this.exceptColumns());
|
|
1382
1546
|
let sql = this._queryBuilder().select();
|
|
1383
1547
|
if (this._getState('RELATIONS_EXISTS'))
|
|
1384
1548
|
sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
|
|
@@ -1416,7 +1580,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1416
1580
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1417
1581
|
}
|
|
1418
1582
|
if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1419
|
-
this.select(...yield this.
|
|
1583
|
+
this.select(...yield this.exceptColumns());
|
|
1420
1584
|
const offset = (page - 1) * limit;
|
|
1421
1585
|
this._setState('PER_PAGE', limit);
|
|
1422
1586
|
this._setState('PAGE', page);
|
|
@@ -1453,7 +1617,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1453
1617
|
var _a;
|
|
1454
1618
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1455
1619
|
if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1456
|
-
this.select(...yield this.
|
|
1620
|
+
this.select(...yield this.exceptColumns());
|
|
1457
1621
|
this.selectRaw([
|
|
1458
1622
|
`\`${column}\`,`,
|
|
1459
1623
|
`${this.$constants('GROUP_CONCAT')}(id)`,
|
|
@@ -1721,25 +1885,22 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1721
1885
|
insertNotExists(data) {
|
|
1722
1886
|
return this.createNotExists(data);
|
|
1723
1887
|
}
|
|
1724
|
-
/**
|
|
1725
|
-
*
|
|
1726
|
-
* get schema from table
|
|
1727
|
-
* @return {this} this this
|
|
1728
|
-
*/
|
|
1729
|
-
getSchema() {
|
|
1730
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1731
|
-
const sql = [
|
|
1732
|
-
`${this.$constants('SHOW')}`,
|
|
1733
|
-
`${this.$constants('COLUMNS')}`,
|
|
1734
|
-
`${this.$constants('FROM')}`,
|
|
1735
|
-
`\`${this._getState('TABLE_NAME').replace(/\`/g, '')}\``
|
|
1736
|
-
].join(' ');
|
|
1737
|
-
return yield this._queryStatement(sql);
|
|
1738
|
-
});
|
|
1739
|
-
}
|
|
1740
1888
|
getSchemaModel() {
|
|
1741
1889
|
return this._getState('SCHEMA_TABLE');
|
|
1742
1890
|
}
|
|
1891
|
+
validation(schema) {
|
|
1892
|
+
this._setState('VALIDATE_SCHEMA', true);
|
|
1893
|
+
this._setState('VALIDATE_SCHEMA_DEFINED', schema);
|
|
1894
|
+
return this;
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* The 'bindPattern' method is used to covert column relate with pattern
|
|
1898
|
+
* @param {string} column
|
|
1899
|
+
* @return {string} return table.column
|
|
1900
|
+
*/
|
|
1901
|
+
bindPattern(column) {
|
|
1902
|
+
return this._valuePattern(column);
|
|
1903
|
+
}
|
|
1743
1904
|
/**
|
|
1744
1905
|
* @override Method
|
|
1745
1906
|
* @return {Promise<Record<string,any> | any[] | null | undefined>}
|
|
@@ -1749,11 +1910,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1749
1910
|
const attributes = this.$attributes;
|
|
1750
1911
|
if (attributes != null) {
|
|
1751
1912
|
while (true) {
|
|
1752
|
-
if (this._getState('
|
|
1753
|
-
this.
|
|
1913
|
+
if (!this._getState('where').length) {
|
|
1914
|
+
this.create(attributes);
|
|
1754
1915
|
break;
|
|
1755
1916
|
}
|
|
1756
|
-
this.
|
|
1917
|
+
this.update(attributes);
|
|
1757
1918
|
break;
|
|
1758
1919
|
}
|
|
1759
1920
|
}
|
|
@@ -1778,7 +1939,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1778
1939
|
faker(rows = 1) {
|
|
1779
1940
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1780
1941
|
let data = [];
|
|
1781
|
-
this.void();
|
|
1782
1942
|
const sql = [
|
|
1783
1943
|
`${this.$constants('SHOW')}`,
|
|
1784
1944
|
`${this.$constants('FIELDS')}`,
|
|
@@ -1790,21 +1950,15 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1790
1950
|
this._assertError(this._getState('TABLE_NAME') === '' || this._getState('TABLE_NAME') == null, "Unknow this table");
|
|
1791
1951
|
let columnAndValue = {};
|
|
1792
1952
|
for (const { Field: field, Type: type } of fields) {
|
|
1793
|
-
const
|
|
1953
|
+
const deletedAt = this._valuePattern(this._getState('SOFT_DELETE_FORMAT'));
|
|
1954
|
+
const passed = ['id', '_id', 'uuid', deletedAt].some(p => field === p);
|
|
1794
1955
|
if (passed)
|
|
1795
1956
|
continue;
|
|
1796
1957
|
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
|
|
1797
1958
|
}
|
|
1798
1959
|
data = [...data, columnAndValue];
|
|
1799
1960
|
}
|
|
1800
|
-
|
|
1801
|
-
this._setState('INSERT', [
|
|
1802
|
-
`${this.$constants('INSERT')}`,
|
|
1803
|
-
`${this._getState('TABLE_NAME')}`,
|
|
1804
|
-
`${query}`
|
|
1805
|
-
].join(' '));
|
|
1806
|
-
this._setState('SAVE', 'INSERT_MULTIPLE');
|
|
1807
|
-
return yield this.save();
|
|
1961
|
+
return yield this.createMultiple(data).save();
|
|
1808
1962
|
});
|
|
1809
1963
|
}
|
|
1810
1964
|
_valuePattern(value) {
|
|
@@ -1837,7 +1991,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1837
1991
|
_handleSoftDelete() {
|
|
1838
1992
|
if (this._getState('SOFT_DELETE')) {
|
|
1839
1993
|
const deletedAt = this._valuePattern(this._getState('SOFT_DELETE_FORMAT'));
|
|
1840
|
-
this.
|
|
1994
|
+
const wheres = this._getState('WHERE');
|
|
1995
|
+
const softDeleteIsNull = [
|
|
1996
|
+
this.bindColumn(`${this.getTableName()}.${deletedAt}`),
|
|
1997
|
+
this.$constants('IS_NULL')
|
|
1998
|
+
].join(' ');
|
|
1999
|
+
if (!wheres.some((where) => where.includes(softDeleteIsNull))) {
|
|
2000
|
+
this.whereNull(deletedAt);
|
|
2001
|
+
return this;
|
|
2002
|
+
}
|
|
2003
|
+
return this;
|
|
1841
2004
|
}
|
|
1842
2005
|
return this;
|
|
1843
2006
|
}
|
|
@@ -2339,12 +2502,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2339
2502
|
return [
|
|
2340
2503
|
`(${[...new Set(columns)].join(',')})`,
|
|
2341
2504
|
`${this.$constants('VALUES')}`,
|
|
2342
|
-
`${values.join(',')}`
|
|
2505
|
+
`${values.join(', ')}`
|
|
2343
2506
|
].join(' ');
|
|
2344
2507
|
}
|
|
2345
2508
|
_insertNotExistsModel() {
|
|
2346
2509
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2347
|
-
this._assertError(!this._getState('
|
|
2510
|
+
this._assertError(!this._getState('where').length, "This method 'createNotExists' is require to use 'where' conditions");
|
|
2348
2511
|
const check = (yield new Model()
|
|
2349
2512
|
.copyModel(this, { where: true, select: true, limit: true })
|
|
2350
2513
|
.bind(this.$pool.get())
|
|
@@ -2390,8 +2553,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2390
2553
|
});
|
|
2391
2554
|
}
|
|
2392
2555
|
_createMultipleModel() {
|
|
2556
|
+
var _a;
|
|
2393
2557
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2394
|
-
for (const data of this._getState('DATA')) {
|
|
2558
|
+
for (const data of (_a = this._getState('DATA')) !== null && _a !== void 0 ? _a : []) {
|
|
2395
2559
|
yield this._validateSchema(data, 'insert');
|
|
2396
2560
|
}
|
|
2397
2561
|
const [result, id] = yield this._actionStatement({
|
|
@@ -2416,7 +2580,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2416
2580
|
}
|
|
2417
2581
|
_updateOrInsertModel() {
|
|
2418
2582
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2419
|
-
this._assertError(!this._getState('
|
|
2583
|
+
this._assertError(!this._getState('where').length, "This method 'createOrUpdate' is require to use 'where' conditions");
|
|
2420
2584
|
const check = (yield new Model()
|
|
2421
2585
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
2422
2586
|
.bind(this.$pool.get())
|
|
@@ -2471,7 +2635,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2471
2635
|
}
|
|
2472
2636
|
_insertOrSelectModel() {
|
|
2473
2637
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2474
|
-
this._assertError(!this._getState('
|
|
2638
|
+
this._assertError(!this._getState('where').length, "This method 'createOrSelect' is require to use 'where' conditions");
|
|
2475
2639
|
const check = (yield new Model()
|
|
2476
2640
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
2477
2641
|
.bind(this.$pool.get())
|
|
@@ -2520,7 +2684,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2520
2684
|
}
|
|
2521
2685
|
_updateModel() {
|
|
2522
2686
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2523
|
-
this._assertError(!
|
|
2687
|
+
this._assertError(!this._getState('where').length, "This method 'update' is require to use 'where' conditions");
|
|
2524
2688
|
yield this._validateSchema(this._getState('DATA'), 'update');
|
|
2525
2689
|
const sql = this._queryBuilder().update();
|
|
2526
2690
|
const result = yield this._actionStatement({ sql });
|
|
@@ -2665,9 +2829,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2665
2829
|
return;
|
|
2666
2830
|
}
|
|
2667
2831
|
_initialModel() {
|
|
2668
|
-
this.$state = new
|
|
2832
|
+
this.$state = new State_1.StateHandler(this.$constants('MODEL'));
|
|
2669
2833
|
this._makeTableName();
|
|
2670
|
-
this.$relation = new
|
|
2834
|
+
this.$relation = new Relation_1.RelationHandler(this);
|
|
2671
2835
|
return this;
|
|
2672
2836
|
}
|
|
2673
2837
|
}
|
|
@@ -55,9 +55,9 @@ class Schema extends Builder_1.Builder {
|
|
|
55
55
|
];
|
|
56
56
|
}
|
|
57
57
|
const sql = [
|
|
58
|
-
|
|
58
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
59
59
|
`${table} (${columns === null || columns === void 0 ? void 0 : columns.join(',')})`,
|
|
60
|
-
|
|
60
|
+
`${this.$constants('ENGINE')}`
|
|
61
61
|
].join(' ');
|
|
62
62
|
yield this.query(sql);
|
|
63
63
|
console.log(`Migrats : '${table}' created successfully`);
|
|
@@ -78,9 +78,9 @@ class Schema extends Builder_1.Builder {
|
|
|
78
78
|
];
|
|
79
79
|
}
|
|
80
80
|
const sql = [
|
|
81
|
-
|
|
81
|
+
`${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
|
|
82
82
|
`${table} (${columns === null || columns === void 0 ? void 0 : columns.join(', ')})`,
|
|
83
|
-
|
|
83
|
+
`${this.$constants('ENGINE')}`
|
|
84
84
|
].join(' ');
|
|
85
85
|
return sql;
|
|
86
86
|
};
|
|
@@ -175,7 +175,6 @@ class Schema extends Builder_1.Builder {
|
|
|
175
175
|
return __awaiter(this, void 0, void 0, function* () {
|
|
176
176
|
const checkTables = yield new Builder_1.Builder().query('SHOW TABLES');
|
|
177
177
|
const existsTables = checkTables.map((c) => Object.values(c)[0]);
|
|
178
|
-
// console.log(checkTables)
|
|
179
178
|
for (const model of models) {
|
|
180
179
|
if (model == null)
|
|
181
180
|
continue;
|
|
@@ -2,10 +2,12 @@ import DB from './DB';
|
|
|
2
2
|
import Model from './Model';
|
|
3
3
|
import Schema from './Schema';
|
|
4
4
|
import Blueprint from './Blueprint';
|
|
5
|
+
import Pool from '../connection';
|
|
5
6
|
export { DB };
|
|
6
7
|
export { Model };
|
|
7
8
|
export { Schema };
|
|
8
9
|
export { Blueprint };
|
|
10
|
+
export { Pool };
|
|
9
11
|
declare const _default: {
|
|
10
12
|
DB: typeof DB;
|
|
11
13
|
Model: typeof Model;
|
package/dist/lib/tspace/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Blueprint = exports.Schema = exports.Model = exports.DB = void 0;
|
|
6
|
+
exports.Pool = exports.Blueprint = exports.Schema = exports.Model = exports.DB = void 0;
|
|
7
7
|
const DB_1 = __importDefault(require("./DB"));
|
|
8
8
|
exports.DB = DB_1.default;
|
|
9
9
|
const Model_1 = __importDefault(require("./Model"));
|
|
@@ -12,6 +12,8 @@ const Schema_1 = __importDefault(require("./Schema"));
|
|
|
12
12
|
exports.Schema = Schema_1.default;
|
|
13
13
|
const Blueprint_1 = __importDefault(require("./Blueprint"));
|
|
14
14
|
exports.Blueprint = Blueprint_1.default;
|
|
15
|
+
const connection_1 = __importDefault(require("../connection"));
|
|
16
|
+
exports.Pool = connection_1.default;
|
|
15
17
|
exports.default = {
|
|
16
18
|
DB: DB_1.default,
|
|
17
19
|
Model: Model_1.default,
|
|
@@ -7,6 +7,7 @@ declare const utils: {
|
|
|
7
7
|
timestamp: (dateString?: string) => string;
|
|
8
8
|
date: () => string;
|
|
9
9
|
escape: (str: any) => any;
|
|
10
|
+
escapeXSS: (str: any) => any;
|
|
10
11
|
isSubQuery: (subQuery: string) => boolean;
|
|
11
12
|
generateUUID: () => string;
|
|
12
13
|
covertBooleanToNumber: (data: any) => any;
|
package/dist/lib/utils/index.js
CHANGED
|
@@ -49,6 +49,13 @@ const escape = (str) => {
|
|
|
49
49
|
return str;
|
|
50
50
|
return str.replace(/[\0\b\t\n\r\x1a\'\\]/g, '');
|
|
51
51
|
};
|
|
52
|
+
const escapeXSS = (str) => {
|
|
53
|
+
if (typeof str !== 'string')
|
|
54
|
+
return str;
|
|
55
|
+
return str
|
|
56
|
+
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
|
|
57
|
+
.replace(/on\w+="[^"]+"/g, '');
|
|
58
|
+
};
|
|
52
59
|
const isSubQuery = (subQuery) => {
|
|
53
60
|
const checkIsSubQuery = (/\bSELECT\s+(?!\*)/i.test(subQuery));
|
|
54
61
|
if (!checkIsSubQuery)
|
|
@@ -196,6 +203,7 @@ const utils = {
|
|
|
196
203
|
timestamp,
|
|
197
204
|
date,
|
|
198
205
|
escape,
|
|
206
|
+
escapeXSS,
|
|
199
207
|
isSubQuery,
|
|
200
208
|
generateUUID,
|
|
201
209
|
covertBooleanToNumber,
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|