tspace-mysql 1.6.2 → 1.6.4
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 +130 -103
- package/build/lib/connection/index.d.ts +1 -0
- package/build/lib/connection/index.js +50 -19
- package/build/lib/connection/index.js.map +1 -1
- package/build/lib/core/Blueprint.d.ts +1 -1
- package/build/lib/core/Blueprint.js +1 -1
- package/build/lib/core/Builder.js +1 -9
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/DB.d.ts +1 -1
- package/build/lib/core/Handlers/Relation.d.ts +4 -3
- package/build/lib/core/Handlers/Relation.js +88 -43
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Model.d.ts +130 -108
- package/build/lib/core/Model.js +50 -27
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Repository.d.ts +2 -2
- package/build/lib/core/Schema.js +3 -3
- package/build/lib/core/Schema.js.map +1 -1
- package/build/lib/core/index.js.map +1 -1
- package/build/lib/types.d.ts +6 -0
- package/build/lib/utils/index.d.ts +0 -1
- package/build/lib/utils/index.js +0 -13
- package/build/lib/utils/index.js.map +1 -1
- package/build/tests/01-Pool.test.js +1 -2
- package/build/tests/01-Pool.test.js.map +1 -1
- package/build/tests/02-DB.test.js +33 -34
- package/build/tests/02-DB.test.js.map +1 -1
- package/build/tests/03-Model.test.js +209 -23
- package/build/tests/03-Model.test.js.map +1 -1
- package/build/tests/mock-data-spec.d.ts +44 -0
- package/build/tests/mock-data-spec.js +53 -0
- package/build/tests/mock-data-spec.js.map +1 -0
- package/build/tests/schema-spec.d.ts +192 -0
- package/build/tests/schema-spec.js +107 -0
- package/build/tests/schema-spec.js.map +1 -0
- package/package.json +3 -2
package/build/lib/core/Model.js
CHANGED
|
@@ -36,10 +36,25 @@ let globalSettings = {
|
|
|
36
36
|
/**
|
|
37
37
|
*
|
|
38
38
|
* 'Model' class is a representation of a database table
|
|
39
|
+
* @generic {Type} TS
|
|
40
|
+
* @generic {Type} TR
|
|
39
41
|
* @example
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
+
* import { Model, Blueprint , TSchema , TRelation } from 'tspace-mysql'
|
|
43
|
+
*
|
|
44
|
+
* const schema = {
|
|
45
|
+
* id : new Blueprint().int().primary().autoIncrement(),
|
|
46
|
+
* uuid : new Blueprint().varchar(50).null(),
|
|
47
|
+
* email : new Blueprint().varchar(50).null(),
|
|
48
|
+
* name : new Blueprint().varchar(255).null(),
|
|
42
49
|
* }
|
|
50
|
+
*
|
|
51
|
+
* type TS = TSchema<typeof schema>
|
|
52
|
+
* type TR = TRelation<{}>
|
|
53
|
+
*
|
|
54
|
+
* class User extends Model<TS,TR> {
|
|
55
|
+
* ...........configration
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
43
58
|
* const users = await new User().findMany()
|
|
44
59
|
* console.log(users)
|
|
45
60
|
*/
|
|
@@ -208,7 +223,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
208
223
|
* It's automatically create, called when not exists table or columns.
|
|
209
224
|
* @param {object} schema using Blueprint for schema
|
|
210
225
|
* @example
|
|
211
|
-
* import { Blueprint,
|
|
226
|
+
* import { Blueprint, TR } from 'tspace-mysql';
|
|
212
227
|
* class User extends Model {
|
|
213
228
|
* constructor() {
|
|
214
229
|
* super()
|
|
@@ -243,7 +258,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
243
258
|
* }
|
|
244
259
|
*/
|
|
245
260
|
useRegistry() {
|
|
246
|
-
this.$state.set('REGISTRY', Object.assign(Object.assign({}, this.$state.get('REGISTRY')), { '$attach': this._attach, '$detach': this._detach }));
|
|
261
|
+
this.$state.set('REGISTRY', Object.assign(Object.assign({}, this.$state.get('REGISTRY')), { '$save': this._save.bind(this), '$attach': this._attach.bind(this), '$detach': this._detach.bind(this) }));
|
|
247
262
|
return this;
|
|
248
263
|
}
|
|
249
264
|
/**
|
|
@@ -674,14 +689,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
674
689
|
}
|
|
675
690
|
/**
|
|
676
691
|
* The 'typeOfSchema' method is used get type of schema.
|
|
677
|
-
* @returns {
|
|
692
|
+
* @returns {TS} type of schema
|
|
678
693
|
*/
|
|
679
694
|
typeOfSchema() {
|
|
680
695
|
return {};
|
|
681
696
|
}
|
|
682
697
|
/**
|
|
683
698
|
* The 'typeOfRelation' method is used get type of relation.
|
|
684
|
-
* @returns {
|
|
699
|
+
* @returns {TR} type of Relation
|
|
685
700
|
*/
|
|
686
701
|
typeOfRelation() {
|
|
687
702
|
return {};
|
|
@@ -1284,7 +1299,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1284
1299
|
* @returns {this} this
|
|
1285
1300
|
*/
|
|
1286
1301
|
registry(func) {
|
|
1287
|
-
this.$state.set('REGISTRY', Object.assign(Object.assign({}, func), { attach: this._attach, detach: this._detach }));
|
|
1302
|
+
this.$state.set('REGISTRY', Object.assign(Object.assign({}, func), { '$save': this._save.bind(this), '$attach': this._attach.bind(this), '$detach': this._detach.bind(this) }));
|
|
1288
1303
|
return this;
|
|
1289
1304
|
}
|
|
1290
1305
|
/**
|
|
@@ -1295,7 +1310,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1295
1310
|
* @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
|
|
1296
1311
|
* @returns {this} this
|
|
1297
1312
|
* @example
|
|
1298
|
-
* import { Model ,
|
|
1313
|
+
* import { Model , TR } from 'tspace-mysql'
|
|
1299
1314
|
*
|
|
1300
1315
|
* class User extends Model {
|
|
1301
1316
|
* constructor(){
|
|
@@ -1330,7 +1345,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1330
1345
|
* @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
|
|
1331
1346
|
* @returns {this} this
|
|
1332
1347
|
* @example
|
|
1333
|
-
* import { Model ,
|
|
1348
|
+
* import { Model , TR } from 'tspace-mysql'
|
|
1334
1349
|
*
|
|
1335
1350
|
* class User extends Model {
|
|
1336
1351
|
* constructor(){
|
|
@@ -1536,6 +1551,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1536
1551
|
* Use relation '${name}' registry models then return callback queries
|
|
1537
1552
|
* @param {string} nameRelation name relation in registry in your model
|
|
1538
1553
|
* @param {function} callback query callback
|
|
1554
|
+
* @param {object} options pivot the query
|
|
1539
1555
|
* @example
|
|
1540
1556
|
* import { Model } from 'tspace-mysql'
|
|
1541
1557
|
* class User extends Model {
|
|
@@ -1577,10 +1593,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1577
1593
|
* .findMany()
|
|
1578
1594
|
* @returns {this} this
|
|
1579
1595
|
*/
|
|
1580
|
-
withQuery(nameRelation, callback) {
|
|
1581
|
-
var _a;
|
|
1596
|
+
withQuery(nameRelation, callback, options = { pivot: false }) {
|
|
1597
|
+
var _a, _b;
|
|
1582
1598
|
this.with(nameRelation);
|
|
1583
|
-
(
|
|
1599
|
+
if (options.pivot) {
|
|
1600
|
+
(_a = this.$relation) === null || _a === void 0 ? void 0 : _a.callbackPivot(String(nameRelation), callback);
|
|
1601
|
+
return this;
|
|
1602
|
+
}
|
|
1603
|
+
(_b = this.$relation) === null || _b === void 0 ? void 0 : _b.callback(String(nameRelation), callback);
|
|
1584
1604
|
return this;
|
|
1585
1605
|
}
|
|
1586
1606
|
/**
|
|
@@ -1590,6 +1610,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1590
1610
|
* Use relation '${name}' registry models then return callback queries
|
|
1591
1611
|
* @param {string} nameRelation name relation in registry in your model
|
|
1592
1612
|
* @param {function} callback query callback
|
|
1613
|
+
* @param {object} options pivot the query
|
|
1593
1614
|
* @example
|
|
1594
1615
|
* import { Model } from 'tspace-mysql'
|
|
1595
1616
|
* class User extends Model {
|
|
@@ -1631,8 +1652,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1631
1652
|
* .findMany()
|
|
1632
1653
|
* @returns {this} this
|
|
1633
1654
|
*/
|
|
1634
|
-
relationQuery(nameRelation, callback) {
|
|
1635
|
-
return this.withQuery(nameRelation, callback);
|
|
1655
|
+
relationQuery(nameRelation, callback, options = { pivot: false }) {
|
|
1656
|
+
return this.withQuery(nameRelation, callback, options);
|
|
1636
1657
|
}
|
|
1637
1658
|
/**
|
|
1638
1659
|
*
|
|
@@ -2218,9 +2239,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2218
2239
|
* @returns {this}
|
|
2219
2240
|
*/
|
|
2220
2241
|
whereSubQuery(column, subQuery) {
|
|
2221
|
-
if (!this.$utils.isSubQuery(subQuery)) {
|
|
2222
|
-
throw this._assertError(`This "subQuery" is invalid. Sub query is should contain 1 column(s)`);
|
|
2223
|
-
}
|
|
2224
2242
|
const c = this._columnPattern(String(column));
|
|
2225
2243
|
this.$state.set('WHERE', [
|
|
2226
2244
|
...this.$state.get('WHERE'),
|
|
@@ -2240,9 +2258,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2240
2258
|
* @returns {this}
|
|
2241
2259
|
*/
|
|
2242
2260
|
whereNotSubQuery(column, subQuery) {
|
|
2243
|
-
if (!this.$utils.isSubQuery(subQuery)) {
|
|
2244
|
-
throw this._assertError(`This "subQuery" is invalid. Sub query is should contain 1 column(s)`);
|
|
2245
|
-
}
|
|
2246
2261
|
const c = this._columnPattern(String(column));
|
|
2247
2262
|
this.$state.set('WHERE', [
|
|
2248
2263
|
...this.$state.get('WHERE'),
|
|
@@ -2262,9 +2277,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2262
2277
|
* @returns {this}
|
|
2263
2278
|
*/
|
|
2264
2279
|
orWhereSubQuery(column, subQuery) {
|
|
2265
|
-
if (!this.$utils.isSubQuery(subQuery)) {
|
|
2266
|
-
throw this._assertError(`This "subQuery" is invalid. Sub query is should contain 1 column(s)`);
|
|
2267
|
-
}
|
|
2268
2280
|
const c = this._columnPattern(String(column));
|
|
2269
2281
|
this.$state.set('WHERE', [
|
|
2270
2282
|
...this.$state.get('WHERE'),
|
|
@@ -2286,9 +2298,6 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2286
2298
|
* @returns {this}
|
|
2287
2299
|
*/
|
|
2288
2300
|
orWhereNotSubQuery(column, subQuery) {
|
|
2289
|
-
if (!this.$utils.isSubQuery(subQuery)) {
|
|
2290
|
-
throw this._assertError(`This "subQuery" is invalid. Sub query is should contain 1 column(s)`);
|
|
2291
|
-
}
|
|
2292
2301
|
const c = this._columnPattern(String(column));
|
|
2293
2302
|
this.$state.set('WHERE', [
|
|
2294
2303
|
...this.$state.get('WHERE'),
|
|
@@ -3431,7 +3440,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3431
3440
|
const existsTables = checkTables.map((c) => Object.values(c)[0]);
|
|
3432
3441
|
const schemaModel = this.getSchemaModel();
|
|
3433
3442
|
if (schemaModel == null)
|
|
3434
|
-
throw this._assertError(
|
|
3443
|
+
throw this._assertError('The schema model does not exist.');
|
|
3435
3444
|
const checkTableIsExists = existsTables.some((table) => table === this.getTableName());
|
|
3436
3445
|
const syncForeignKey = (_j) => __awaiter(this, [_j], void 0, function* ({ schemaModel, model }) {
|
|
3437
3446
|
var _k;
|
|
@@ -4061,6 +4070,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4061
4070
|
}
|
|
4062
4071
|
return data;
|
|
4063
4072
|
}
|
|
4073
|
+
_save() {
|
|
4074
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
4075
|
+
const result = this.$state.get('RESULT');
|
|
4076
|
+
if (result.id == null) {
|
|
4077
|
+
throw this._assertError(`This '$save' must be required the 'id' for the function`);
|
|
4078
|
+
}
|
|
4079
|
+
const update = JSON.parse(JSON.stringify(Object.assign({}, result)));
|
|
4080
|
+
return yield this
|
|
4081
|
+
.where('id', result.id)
|
|
4082
|
+
.update(update)
|
|
4083
|
+
.dd()
|
|
4084
|
+
.save();
|
|
4085
|
+
});
|
|
4086
|
+
}
|
|
4064
4087
|
_attach(name, dataId, fields) {
|
|
4065
4088
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4066
4089
|
var _a;
|