tspace-mysql 1.6.2 → 1.6.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 +130 -103
- package/build/lib/connection/index.d.ts +1 -0
- package/build/lib/connection/index.js +43 -12
- package/build/lib/connection/index.js.map +1 -1
- package/build/lib/core/Builder.js +1 -1
- package/build/lib/core/Builder.js.map +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 +49 -14
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/types.d.ts +6 -0
- 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(),
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* type TS = TSchema<typeof schema>
|
|
52
|
+
* type TR = TRelation<{}>
|
|
53
|
+
*
|
|
54
|
+
* class User extends Model<TS,TR> {
|
|
55
|
+
* ...........configration
|
|
42
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
|
*
|
|
@@ -4061,6 +4082,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4061
4082
|
}
|
|
4062
4083
|
return data;
|
|
4063
4084
|
}
|
|
4085
|
+
_save() {
|
|
4086
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
4087
|
+
const result = this.$state.get('RESULT');
|
|
4088
|
+
if (result.id == null) {
|
|
4089
|
+
throw this._assertError(`This '$save' must be required the 'id' for the function`);
|
|
4090
|
+
}
|
|
4091
|
+
const update = JSON.parse(JSON.stringify(Object.assign({}, result)));
|
|
4092
|
+
return yield this
|
|
4093
|
+
.where('id', result.id)
|
|
4094
|
+
.update(update)
|
|
4095
|
+
.dd()
|
|
4096
|
+
.save();
|
|
4097
|
+
});
|
|
4098
|
+
}
|
|
4064
4099
|
_attach(name, dataId, fields) {
|
|
4065
4100
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4066
4101
|
var _a;
|