tspace-mysql 1.5.2 → 1.5.3
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 +8 -8
- package/build/lib/connection/options.js +1 -1
- package/build/lib/constants/index.d.ts +1 -3
- package/build/lib/constants/index.js +0 -96
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.js +1 -4
- package/build/lib/core/Builder.d.ts +1 -1
- package/build/lib/core/Builder.js +41 -18
- package/build/lib/core/DB.js +1 -1
- package/build/lib/core/Handlers/State.d.ts +2 -2
- package/build/lib/core/Handlers/State.js +126 -12
- package/build/lib/core/Model.d.ts +77 -71
- package/build/lib/core/Model.js +64 -18
- package/build/lib/core/Type.d.ts +55 -1
- package/package.json +1 -1
package/build/lib/core/Model.js
CHANGED
|
@@ -18,9 +18,9 @@ const DB_1 = require("./DB");
|
|
|
18
18
|
const Schema_1 = require("./Schema");
|
|
19
19
|
const AbstractModel_1 = require("./Abstracts/AbstractModel");
|
|
20
20
|
const Proxy_1 = require("./Handlers/Proxy");
|
|
21
|
-
const State_1 = require("./Handlers/State");
|
|
22
21
|
const Relation_1 = require("./Handlers/Relation");
|
|
23
22
|
const Blueprint_1 = require("./Blueprint");
|
|
23
|
+
const State_1 = require("./Handlers/State");
|
|
24
24
|
let globalSettings = {
|
|
25
25
|
softDelete: false,
|
|
26
26
|
uuid: false,
|
|
@@ -310,7 +310,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
310
310
|
this.$constants('PATTERN').snake_case,
|
|
311
311
|
this.$constants('PATTERN').camelCase
|
|
312
312
|
];
|
|
313
|
-
|
|
313
|
+
if (!allowPattern.includes(pattern)) {
|
|
314
|
+
this._assertError(`tspace-mysql support only pattern ['${this.$constants('PATTERN').snake_case}','${this.$constants('PATTERN').camelCase}']`);
|
|
315
|
+
}
|
|
314
316
|
this.$state.set('PATTERN', pattern);
|
|
315
317
|
this._makeTableName();
|
|
316
318
|
return this;
|
|
@@ -788,13 +790,18 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
788
790
|
groupBy(...columns) {
|
|
789
791
|
let groupBy = 'id';
|
|
790
792
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
791
|
-
groupBy = columns.map(
|
|
792
|
-
if (
|
|
793
|
-
return
|
|
794
|
-
|
|
793
|
+
groupBy = columns.map(c => {
|
|
794
|
+
if (/\./.test(c))
|
|
795
|
+
return this.bindColumn(c);
|
|
796
|
+
if (c.includes(this.$constants('RAW')))
|
|
797
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
798
|
+
return `\`${c}\``;
|
|
795
799
|
}).join(', ');
|
|
796
800
|
}
|
|
797
|
-
this.$state.set('GROUP_BY',
|
|
801
|
+
this.$state.set('GROUP_BY', [
|
|
802
|
+
...this.$state.get('GROUP_BY'),
|
|
803
|
+
`${groupBy}`
|
|
804
|
+
]);
|
|
798
805
|
return this;
|
|
799
806
|
}
|
|
800
807
|
/**
|
|
@@ -1839,13 +1846,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1839
1846
|
* @return {this} this
|
|
1840
1847
|
*/
|
|
1841
1848
|
where(column, operator, value) {
|
|
1842
|
-
if (typeof column === 'object'
|
|
1849
|
+
if (typeof column === 'object') {
|
|
1843
1850
|
return this.whereObject(column);
|
|
1844
1851
|
}
|
|
1845
1852
|
const c = this._columnPattern(String(column));
|
|
1846
1853
|
[value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
|
|
1847
1854
|
value = this.$utils.escape(value);
|
|
1848
1855
|
value = this._valueTrueFalse(value);
|
|
1856
|
+
if (value === null) {
|
|
1857
|
+
return this.whereNull(column);
|
|
1858
|
+
}
|
|
1859
|
+
if (Array.isArray(value)) {
|
|
1860
|
+
return this.whereIn(column, value);
|
|
1861
|
+
}
|
|
1849
1862
|
this.$state.set('WHERE', [
|
|
1850
1863
|
...this.$state.get('WHERE'),
|
|
1851
1864
|
[
|
|
@@ -1869,6 +1882,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1869
1882
|
const c = this._columnPattern(String(column));
|
|
1870
1883
|
value = this.$utils.escape(value);
|
|
1871
1884
|
value = this._valueTrueFalse(value);
|
|
1885
|
+
if (value === null) {
|
|
1886
|
+
return this.orWhereNull(column);
|
|
1887
|
+
}
|
|
1888
|
+
if (Array.isArray(value)) {
|
|
1889
|
+
return this.orWhereIn(column, value);
|
|
1890
|
+
}
|
|
1872
1891
|
this.$state.set('WHERE', [
|
|
1873
1892
|
...this.$state.get('WHERE'),
|
|
1874
1893
|
[
|
|
@@ -1887,14 +1906,18 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1887
1906
|
*/
|
|
1888
1907
|
whereObject(columns) {
|
|
1889
1908
|
for (let column in columns) {
|
|
1890
|
-
column = this._columnPattern(String(column));
|
|
1891
1909
|
const operator = '=';
|
|
1892
1910
|
const value = this.$utils.escape(columns[column]);
|
|
1911
|
+
if (value == null) {
|
|
1912
|
+
this.whereNull(column);
|
|
1913
|
+
continue;
|
|
1914
|
+
}
|
|
1915
|
+
const c = this._columnPattern(String(column));
|
|
1893
1916
|
this.$state.set('WHERE', [
|
|
1894
1917
|
...this.$state.get('WHERE'),
|
|
1895
1918
|
[
|
|
1896
1919
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
1897
|
-
`${this.bindColumn(String(
|
|
1920
|
+
`${this.bindColumn(String(c))}`,
|
|
1898
1921
|
`${operator}`,
|
|
1899
1922
|
`${this._checkValueHasRaw(value)}`
|
|
1900
1923
|
].join(' ')
|
|
@@ -2497,7 +2520,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2497
2520
|
delete() {
|
|
2498
2521
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2499
2522
|
var _a, _b;
|
|
2500
|
-
this._assertError(!this.$state.get('
|
|
2523
|
+
this._assertError(!this.$state.get('WHERE').length, "The 'delete' method requires the use of 'where' conditions.");
|
|
2501
2524
|
this.limit(1);
|
|
2502
2525
|
if (this.$state.get('SOFT_DELETE')) {
|
|
2503
2526
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
@@ -2581,6 +2604,22 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2581
2604
|
return Boolean(this._resultHandler((_b = !!result) !== null && _b !== void 0 ? _b : false));
|
|
2582
2605
|
});
|
|
2583
2606
|
}
|
|
2607
|
+
/**
|
|
2608
|
+
* @override
|
|
2609
|
+
* @param {string=} column [column=id]
|
|
2610
|
+
* @return {promise<Array>}
|
|
2611
|
+
*/
|
|
2612
|
+
toArray(column) {
|
|
2613
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2614
|
+
if (column == null)
|
|
2615
|
+
column = 'id';
|
|
2616
|
+
this.selectRaw(`${this.bindColumn(column)}`);
|
|
2617
|
+
const sql = this._queryBuilder().select();
|
|
2618
|
+
const result = yield this._queryStatement(sql);
|
|
2619
|
+
const toArray = result.map((data) => data[column]);
|
|
2620
|
+
return this._resultHandler(toArray);
|
|
2621
|
+
});
|
|
2622
|
+
}
|
|
2584
2623
|
/**
|
|
2585
2624
|
*
|
|
2586
2625
|
* @override
|
|
@@ -3172,7 +3211,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3172
3211
|
}
|
|
3173
3212
|
data.push(columnAndValue);
|
|
3174
3213
|
}
|
|
3175
|
-
|
|
3214
|
+
yield this.createMultiple(data).save();
|
|
3215
|
+
return;
|
|
3176
3216
|
});
|
|
3177
3217
|
}
|
|
3178
3218
|
/**
|
|
@@ -3459,7 +3499,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3459
3499
|
acc[key] = schemaTable[key].valueType;
|
|
3460
3500
|
return acc;
|
|
3461
3501
|
}, {});
|
|
3462
|
-
if (schema == null)
|
|
3502
|
+
if (schema == null || !Object.keys(schema).length)
|
|
3463
3503
|
return;
|
|
3464
3504
|
const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
|
|
3465
3505
|
const regexDateTime = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]/;
|
|
@@ -3973,7 +4013,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3973
4013
|
}
|
|
3974
4014
|
_insertNotExistsModel() {
|
|
3975
4015
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3976
|
-
this._assertError(!this.$state.get('
|
|
4016
|
+
this._assertError(!this.$state.get('WHERE').length, "The 'createNotExists' method requires the use of 'where' conditions.");
|
|
3977
4017
|
const check = (yield new Model()
|
|
3978
4018
|
.copyModel(this, { where: true, select: true, limit: true })
|
|
3979
4019
|
.bind(this.$pool.get())
|
|
@@ -4046,7 +4086,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4046
4086
|
}
|
|
4047
4087
|
_updateOrInsertModel() {
|
|
4048
4088
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4049
|
-
|
|
4089
|
+
if (!this.$state.get('WHERE').length) {
|
|
4090
|
+
throw this._assertError("The 'createOrUpdate' method requires the use of 'where' conditions.");
|
|
4091
|
+
}
|
|
4050
4092
|
const check = (yield new Model()
|
|
4051
4093
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
4052
4094
|
.bind(this.$pool.get())
|
|
@@ -4101,7 +4143,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4101
4143
|
}
|
|
4102
4144
|
_insertOrSelectModel() {
|
|
4103
4145
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4104
|
-
|
|
4146
|
+
if (!this.$state.get('WHERE').length) {
|
|
4147
|
+
throw this._assertError("The 'createOrSelect' method requires the use of 'where' conditions.");
|
|
4148
|
+
}
|
|
4105
4149
|
const check = (yield new Model()
|
|
4106
4150
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
4107
4151
|
.bind(this.$pool.get())
|
|
@@ -4150,7 +4194,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4150
4194
|
}
|
|
4151
4195
|
_updateModel() {
|
|
4152
4196
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4153
|
-
|
|
4197
|
+
if (!this.$state.get('WHERE').length) {
|
|
4198
|
+
throw this._assertError("The 'update' method requires the use of 'where' conditions.");
|
|
4199
|
+
}
|
|
4154
4200
|
yield this._validateSchema(this.$state.get('DATA'), 'update');
|
|
4155
4201
|
const sql = this._queryBuilder().update();
|
|
4156
4202
|
const result = yield this._actionStatement({ sql });
|
|
@@ -4320,7 +4366,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4320
4366
|
return this;
|
|
4321
4367
|
}
|
|
4322
4368
|
_initialModel() {
|
|
4323
|
-
this.$state = new State_1.StateHandler(
|
|
4369
|
+
this.$state = new State_1.StateHandler('model');
|
|
4324
4370
|
if (this.$pattern != null)
|
|
4325
4371
|
this.usePattern(this.$pattern);
|
|
4326
4372
|
this._makeTableName();
|
package/build/lib/core/Type.d.ts
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param {type} TSchema typeof the schema
|
|
4
|
+
* @param {type} TSpecific override of the schema
|
|
5
|
+
* @example
|
|
6
|
+
* import { Blueprint , SchemaType , Model } from 'tspace-mysql'
|
|
7
|
+
* const schemaUser = {
|
|
8
|
+
* id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
9
|
+
* uuid :new Blueprint().varchar(50).null(),
|
|
10
|
+
* email :new Blueprint().varchar(50).null(),
|
|
11
|
+
* name :new Blueprint().varchar(255).null(),
|
|
12
|
+
* username : new Blueprint().varchar(255).null(),
|
|
13
|
+
* password : new Blueprint().varchar(255).null(),
|
|
14
|
+
* createdAt :new Blueprint().timestamp().null(),
|
|
15
|
+
* updatedAt :new Blueprint().timestamp().null()
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* type SchemaUserType = SchemaType<typeof schemaUser , {
|
|
19
|
+
* id : number,
|
|
20
|
+
* uuid : string,
|
|
21
|
+
* ........
|
|
22
|
+
* }>
|
|
23
|
+
*
|
|
24
|
+
* class User<SchemaUserType> {}
|
|
25
|
+
*/
|
|
1
26
|
export type SchemaType<TSchema, TSpecific = any> = {
|
|
2
27
|
[K in keyof TSchema]: K extends keyof TSpecific ? TSpecific[K] : any;
|
|
3
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param {type} T relationships type
|
|
32
|
+
* @example
|
|
33
|
+
* import { Blueprint , RelationType , SchemaUserType , Model } from 'tspace-mysql'
|
|
34
|
+
* const schemaUser = {
|
|
35
|
+
* id :new Blueprint().int().notNull().primary().autoIncrement(),
|
|
36
|
+
* uuid :new Blueprint().varchar(50).null(),
|
|
37
|
+
* email :new Blueprint().varchar(50).null(),
|
|
38
|
+
* name :new Blueprint().varchar(255).null(),
|
|
39
|
+
* username : new Blueprint().varchar(255).null(),
|
|
40
|
+
* password : new Blueprint().varchar(255).null(),
|
|
41
|
+
* createdAt :new Blueprint().timestamp().null(),
|
|
42
|
+
* updatedAt :new Blueprint().timestamp().null()
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* type SchemaUserType = SchemaType<typeof schemaUser , {
|
|
46
|
+
* id : number,
|
|
47
|
+
* uuid : string,
|
|
48
|
+
* ........
|
|
49
|
+
* }>
|
|
50
|
+
*
|
|
51
|
+
* type RelationUserType = RelationType<{
|
|
52
|
+
* phones : SchemaPhoneType[]
|
|
53
|
+
* phone : SchemaPhoneType
|
|
54
|
+
* }>
|
|
55
|
+
*
|
|
56
|
+
* class User<SchemaUserType,RelationUserType> {}
|
|
57
|
+
*/
|
|
4
58
|
export type RelationType<T> = {
|
|
5
|
-
[K in keyof T]
|
|
59
|
+
[K in keyof T]+?: T[K];
|
|
6
60
|
};
|