tspace-mysql 1.2.3 → 1.2.5
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
CHANGED
|
@@ -374,7 +374,7 @@ class User extends Model {
|
|
|
374
374
|
* this.useTable('users')
|
|
375
375
|
* this.useTableSingular() // 'user'
|
|
376
376
|
* this.useTablePlural() // 'users'
|
|
377
|
-
* this.usePattern('snake_case')
|
|
377
|
+
* this.usePattern('snake_case') // by defalut snake_case
|
|
378
378
|
* this.useUUID('uuid') // => runing a uuid (universally unique identifier) when insert new data
|
|
379
379
|
* this.useRegistry()
|
|
380
380
|
* this.useSchema({
|
|
@@ -65,6 +65,14 @@ declare class Database extends AbstractDatabase {
|
|
|
65
65
|
* @return {this}
|
|
66
66
|
*/
|
|
67
67
|
where(column: string, operator?: any, value?: any): this;
|
|
68
|
+
/**
|
|
69
|
+
* where using object operator only '='
|
|
70
|
+
* @param {Object} columns
|
|
71
|
+
* @return {this}
|
|
72
|
+
*/
|
|
73
|
+
whereObject(columns: {
|
|
74
|
+
[key: string]: any;
|
|
75
|
+
}): this;
|
|
68
76
|
/**
|
|
69
77
|
* if has 2 arguments default operator '='
|
|
70
78
|
* @param {string} column
|
|
@@ -151,6 +151,30 @@ class Database extends AbstractDatabase_1.default {
|
|
|
151
151
|
].join(' '));
|
|
152
152
|
return this;
|
|
153
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* where using object operator only '='
|
|
156
|
+
* @param {Object} columns
|
|
157
|
+
* @return {this}
|
|
158
|
+
*/
|
|
159
|
+
whereObject(columns) {
|
|
160
|
+
for (const column in columns) {
|
|
161
|
+
const operator = '=';
|
|
162
|
+
const value = columns[column];
|
|
163
|
+
if (!this._queryWhereIsExists()) {
|
|
164
|
+
this.$state.set('WHERE', [
|
|
165
|
+
`${this.$constants('WHERE')}`,
|
|
166
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${operator} '${value}'`
|
|
167
|
+
].join(' '));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
this.$state.set('WHERE', [
|
|
171
|
+
`${this.$state.get('WHERE')}`,
|
|
172
|
+
`${this.$constants('AND')}`,
|
|
173
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${operator} '${value}'`
|
|
174
|
+
].join(' '));
|
|
175
|
+
}
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
154
178
|
/**
|
|
155
179
|
* if has 2 arguments default operator '='
|
|
156
180
|
* @param {string} column
|
|
@@ -148,11 +148,11 @@ declare class Model extends AbstractModel {
|
|
|
148
148
|
*/
|
|
149
149
|
withExists(...nameRelations: Array<string>): this;
|
|
150
150
|
/**
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
151
|
+
*
|
|
152
|
+
* Use relations in registry of model return only exists result of relation query
|
|
153
|
+
* @param {...string} nameRelations if data exists return blank
|
|
154
|
+
* @return {this} this
|
|
155
|
+
*/
|
|
156
156
|
has(...nameRelations: Array<string>): this;
|
|
157
157
|
/**
|
|
158
158
|
*
|
|
@@ -231,7 +231,7 @@ declare class Model extends AbstractModel {
|
|
|
231
231
|
* @property {string} relation.freezeTable
|
|
232
232
|
* @return {this} this
|
|
233
233
|
*/
|
|
234
|
-
protected belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot }: Relation): this;
|
|
234
|
+
protected belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion }: Relation): this;
|
|
235
235
|
/**
|
|
236
236
|
* Assign the relation in model Objects
|
|
237
237
|
* @param {object} relation registry relation in your model
|
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -170,12 +170,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
170
170
|
* @return {this} this
|
|
171
171
|
*/
|
|
172
172
|
buildMethodRelation(name, callback) {
|
|
173
|
-
var _a;
|
|
173
|
+
var _a, _b;
|
|
174
174
|
this.with(name);
|
|
175
175
|
const r = this.$state.get('WITH').find((data) => data.name === name);
|
|
176
|
-
this._assertError(r == null, `
|
|
177
|
-
const relationHasExists = (
|
|
178
|
-
this._assertError(!relationHasExists, `
|
|
176
|
+
this._assertError(r == null, `This Relation ${name} not be register in Model ${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}`);
|
|
177
|
+
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(r.relation);
|
|
178
|
+
this._assertError(!relationHasExists, `Unknown Relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
179
179
|
if (callback == null) {
|
|
180
180
|
r.query = new r.model();
|
|
181
181
|
return this;
|
|
@@ -202,7 +202,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
202
202
|
* @return {Model} Model
|
|
203
203
|
*/
|
|
204
204
|
copyModel(instance, options) {
|
|
205
|
-
this._assertError(!(instance instanceof Model), '
|
|
205
|
+
this._assertError(!(instance instanceof Model), 'Value is not a instanceof Model');
|
|
206
206
|
const copy = Object.fromEntries(instance.$state.get());
|
|
207
207
|
const newInstance = new Model();
|
|
208
208
|
newInstance.$state.clone(copy);
|
|
@@ -251,11 +251,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
251
251
|
*/
|
|
252
252
|
with(...nameRelations) {
|
|
253
253
|
const relations = nameRelations.map((name) => {
|
|
254
|
-
var _a, _b;
|
|
254
|
+
var _a, _b, _c;
|
|
255
255
|
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
256
|
-
this._assertError(relation == null, `
|
|
257
|
-
const relationHasExists = (
|
|
258
|
-
this._assertError(!relationHasExists, `
|
|
256
|
+
this._assertError(relation == null, `This Relation ${name} not be register in Model ${(_b = this.constructor) === null || _b === void 0 ? void 0 : _b.name}`);
|
|
257
|
+
const relationHasExists = (_c = Object.values(this.$constants('RELATIONSHIP'))) === null || _c === void 0 ? void 0 : _c.includes(relation.relation);
|
|
258
|
+
this._assertError(!relationHasExists, `Unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
259
259
|
if (relation.query == null)
|
|
260
260
|
relation.query = new relation.model();
|
|
261
261
|
return relation;
|
|
@@ -280,20 +280,39 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
280
280
|
* @return {this} this
|
|
281
281
|
*/
|
|
282
282
|
withExists(...nameRelations) {
|
|
283
|
-
this.with(...nameRelations);
|
|
284
283
|
this.$state.set('WITH_EXISTS', true);
|
|
284
|
+
const relations = nameRelations.map((name) => {
|
|
285
|
+
var _a, _b, _c;
|
|
286
|
+
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
287
|
+
this._assertError(relation == null, `This Relation ${name} not be register in Model ${(_b = this.constructor) === null || _b === void 0 ? void 0 : _b.name}`);
|
|
288
|
+
const relationHasExists = (_c = Object.values(this.$constants('RELATIONSHIP'))) === null || _c === void 0 ? void 0 : _c.includes(relation.relation);
|
|
289
|
+
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
290
|
+
if (relation.query == null)
|
|
291
|
+
relation.query = new relation.model();
|
|
292
|
+
relation.exists = true;
|
|
293
|
+
return relation;
|
|
294
|
+
});
|
|
295
|
+
relations.sort((cur, prev) => cur.relation.length - prev.relation.length);
|
|
296
|
+
const setRelations = this.$state.get('WITH').length
|
|
297
|
+
? [...relations.map((w) => {
|
|
298
|
+
const exists = this.$state.get('WITH').find((r) => r.name === w.name);
|
|
299
|
+
if (exists)
|
|
300
|
+
return null;
|
|
301
|
+
return w;
|
|
302
|
+
}).filter((d) => d != null),
|
|
303
|
+
...this.$state.get('WITH')]
|
|
304
|
+
: relations;
|
|
305
|
+
this.$state.set('WITH', setRelations);
|
|
285
306
|
return this;
|
|
286
307
|
}
|
|
287
308
|
/**
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
309
|
+
*
|
|
310
|
+
* Use relations in registry of model return only exists result of relation query
|
|
311
|
+
* @param {...string} nameRelations if data exists return blank
|
|
312
|
+
* @return {this} this
|
|
313
|
+
*/
|
|
293
314
|
has(...nameRelations) {
|
|
294
|
-
this.
|
|
295
|
-
this.$state.set('WITH_EXISTS', true);
|
|
296
|
-
return this;
|
|
315
|
+
return this.withExists(...nameRelations);
|
|
297
316
|
}
|
|
298
317
|
/**
|
|
299
318
|
*
|
|
@@ -303,10 +322,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
303
322
|
* @return {this} this
|
|
304
323
|
*/
|
|
305
324
|
withQuery(nameRelation, callback) {
|
|
306
|
-
var _a;
|
|
325
|
+
var _a, _b;
|
|
307
326
|
const relation = this.$state.get('WITH').find((data) => data.name === nameRelation);
|
|
308
|
-
this._assertError(relation == null, `
|
|
309
|
-
const relationHasExists = (
|
|
327
|
+
this._assertError(relation == null, `This Relation ${nameRelation} not be register in Model ${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}`);
|
|
328
|
+
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
310
329
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
311
330
|
relation.query = callback(new relation.model());
|
|
312
331
|
return this;
|
|
@@ -318,27 +337,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
318
337
|
* @return {this} this
|
|
319
338
|
*/
|
|
320
339
|
relations(...nameRelations) {
|
|
321
|
-
|
|
322
|
-
var _a, _b;
|
|
323
|
-
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
324
|
-
this._assertError(relation == null, `relation ${name} not be register !`);
|
|
325
|
-
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
326
|
-
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
327
|
-
if (relation.query == null)
|
|
328
|
-
relation.query = new relation.model();
|
|
329
|
-
return relation;
|
|
330
|
-
});
|
|
331
|
-
relations.sort((cur, prev) => cur.relation.length - prev.relation.length);
|
|
332
|
-
const setRelations = this.$state.get('WITH').length
|
|
333
|
-
? [...relations.map((w) => {
|
|
334
|
-
const exists = this.$state.get('WITH').find((r) => r.name === w.name);
|
|
335
|
-
if (exists)
|
|
336
|
-
return null;
|
|
337
|
-
return w;
|
|
338
|
-
}).filter((d) => d != null), ...this.$state.get('WITH')]
|
|
339
|
-
: relations;
|
|
340
|
-
this.$state.set('WITH', setRelations);
|
|
341
|
-
return this;
|
|
340
|
+
return this.with(...nameRelations);
|
|
342
341
|
}
|
|
343
342
|
/**
|
|
344
343
|
*
|
|
@@ -347,9 +346,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
347
346
|
* @return {this}
|
|
348
347
|
*/
|
|
349
348
|
relationsExists(...nameRelations) {
|
|
350
|
-
this.
|
|
351
|
-
this.$state.set('WITH_EXISTS', true);
|
|
352
|
-
return this;
|
|
349
|
+
return this.withExists(...nameRelations);
|
|
353
350
|
}
|
|
354
351
|
/**
|
|
355
352
|
*
|
|
@@ -359,13 +356,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
359
356
|
* @return {this} this
|
|
360
357
|
*/
|
|
361
358
|
relationQuery(nameRelation, callback) {
|
|
362
|
-
|
|
363
|
-
const relation = this.$state.get('WITH').find((data) => data.name === nameRelation);
|
|
364
|
-
this._assertError(relation == null, `relation ${nameRelation} not be register !`);
|
|
365
|
-
const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(relation.relation);
|
|
366
|
-
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
367
|
-
relation.query = callback(new relation.model());
|
|
368
|
-
return this;
|
|
359
|
+
return this.withQuery(nameRelation, callback);
|
|
369
360
|
}
|
|
370
361
|
/**
|
|
371
362
|
* Assign the relation in model Objects
|
|
@@ -453,7 +444,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
453
444
|
* @property {string} relation.freezeTable
|
|
454
445
|
* @return {this} this
|
|
455
446
|
*/
|
|
456
|
-
belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot }) {
|
|
447
|
+
belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion }) {
|
|
457
448
|
const relation = {
|
|
458
449
|
name,
|
|
459
450
|
model,
|
|
@@ -463,6 +454,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
463
454
|
foreignKey,
|
|
464
455
|
freezeTable,
|
|
465
456
|
pivot,
|
|
457
|
+
oldVersion,
|
|
466
458
|
query: null
|
|
467
459
|
};
|
|
468
460
|
this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
|
|
@@ -1481,6 +1473,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1481
1473
|
var _a, _b, _c, _d;
|
|
1482
1474
|
const relation = relationModel.relation;
|
|
1483
1475
|
const model = (_a = relationModel.model) === null || _a === void 0 ? void 0 : _a.name;
|
|
1476
|
+
const oldVersion = relationModel.oldVersion;
|
|
1484
1477
|
const table = relationModel.freezeTable
|
|
1485
1478
|
? relationModel.freezeTable
|
|
1486
1479
|
: (_b = relationModel.query) === null || _b === void 0 ? void 0 : _b._tableName();
|
|
@@ -1525,7 +1518,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1525
1518
|
pluralize_1.default.singular((_d = relationModel.query) === null || _d === void 0 ? void 0 : _d._tableName())
|
|
1526
1519
|
].sort().join('_'));
|
|
1527
1520
|
}
|
|
1528
|
-
return { name, as, relation, table, localKey, foreignKey, model, pivot };
|
|
1521
|
+
return { name, as, relation, table, localKey, foreignKey, model, pivot, oldVersion };
|
|
1529
1522
|
}
|
|
1530
1523
|
_handleSoftDelete() {
|
|
1531
1524
|
if (this.$state.get('SOFT_DELETE')) {
|
|
@@ -1600,23 +1593,27 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1600
1593
|
return;
|
|
1601
1594
|
const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
|
|
1602
1595
|
for (const result of results) {
|
|
1603
|
-
for (const
|
|
1604
|
-
const s = schema[
|
|
1596
|
+
for (const column in result) {
|
|
1597
|
+
const s = schema[column];
|
|
1598
|
+
if (s == null && this.$state.get('SELECT').includes('*'))
|
|
1599
|
+
this._assertError(`Not found this column [${column}] in schema`);
|
|
1605
1600
|
if (s == null)
|
|
1606
|
-
|
|
1601
|
+
continue;
|
|
1607
1602
|
const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
|
|
1608
1603
|
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]/;
|
|
1609
|
-
if (regexDate.test(result[
|
|
1610
|
-
if (typeOf(new Date(result[
|
|
1604
|
+
if (regexDate.test(result[column]) || regexDateTime.test(result[column])) {
|
|
1605
|
+
if (typeOf(new Date(result[column])) === typeOf(new s()))
|
|
1611
1606
|
continue;
|
|
1612
|
-
this._assertError(`
|
|
1607
|
+
this._assertError(`This column [${column}] is invalid schema field type`);
|
|
1613
1608
|
}
|
|
1614
|
-
if (
|
|
1609
|
+
if (result[column] === null)
|
|
1610
|
+
continue;
|
|
1611
|
+
if (typeOf(result[column]) === typeOf(new s()))
|
|
1615
1612
|
continue;
|
|
1616
|
-
this._assertError(`
|
|
1613
|
+
this._assertError(`This column [${column}] is invalid schema field type`);
|
|
1617
1614
|
}
|
|
1618
1615
|
}
|
|
1619
|
-
return
|
|
1616
|
+
return;
|
|
1620
1617
|
}
|
|
1621
1618
|
_execute({ sql, type, message, options }) {
|
|
1622
1619
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1721,6 +1718,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1721
1718
|
const relation = relations[index];
|
|
1722
1719
|
if (!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length))
|
|
1723
1720
|
continue;
|
|
1721
|
+
if (relation.exists == null)
|
|
1722
|
+
continue;
|
|
1724
1723
|
const { localKey, foreignKey, pivot } = this._valueInRelation(relation);
|
|
1725
1724
|
const query = relation.query;
|
|
1726
1725
|
this._assertError(query == null, `unknown callback query in [relation : '${relation.name}']`);
|
|
@@ -1734,13 +1733,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1734
1733
|
}
|
|
1735
1734
|
}
|
|
1736
1735
|
if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
|
|
1736
|
+
const thisPivot = new Model();
|
|
1737
|
+
thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
|
|
1737
1738
|
const sql = clone
|
|
1738
1739
|
.bind(this.$pool.get())
|
|
1739
|
-
.whereReference(`\`${
|
|
1740
|
+
.whereReference(`\`${query._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${localKey}\``)
|
|
1740
1741
|
.toString();
|
|
1741
|
-
|
|
1742
|
-
const thisPivot = new Model();
|
|
1743
|
-
thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
|
|
1742
|
+
thisPivot.whereExists(sql);
|
|
1744
1743
|
const sqlPivot = thisPivot
|
|
1745
1744
|
.bind(this.$pool.get())
|
|
1746
1745
|
.whereReference(`\`${this._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${this._valuePattern([pluralize_1.default.singular(this._tableName()), foreignKey].join("_"))}\``)
|
|
@@ -1786,7 +1785,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1786
1785
|
_belongsToMany(dataFromParent, relation) {
|
|
1787
1786
|
var _a;
|
|
1788
1787
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1789
|
-
const { name, foreignKey, pivot } = this._valueInRelation(relation);
|
|
1788
|
+
const { name, foreignKey, localKey, pivot, oldVersion } = this._valueInRelation(relation);
|
|
1790
1789
|
const pivotTable = String(((_a = relation.pivot) !== null && _a !== void 0 ? _a : pivot));
|
|
1791
1790
|
const localKeyId = dataFromParent.map((parent) => {
|
|
1792
1791
|
const data = parent[foreignKey];
|
|
@@ -1795,7 +1794,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1795
1794
|
}
|
|
1796
1795
|
return data;
|
|
1797
1796
|
}).filter((data) => data != null);
|
|
1798
|
-
const dataPerentId = Array.from(new Set(localKeyId))
|
|
1797
|
+
const dataPerentId = Array.from(new Set(localKeyId));
|
|
1799
1798
|
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1800
1799
|
return [];
|
|
1801
1800
|
const modelOther = new relation.model();
|
|
@@ -1803,35 +1802,53 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1803
1802
|
const otherlocalKey = 'id';
|
|
1804
1803
|
const otherforeignKey = this._valuePattern(`${other}Id`);
|
|
1805
1804
|
const localKeyPivotTable = this._valuePattern([pluralize_1.default.singular(this._tableName()), foreignKey].join("_"));
|
|
1806
|
-
const
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1805
|
+
const sql = new Model().copyModel(modelOther)
|
|
1806
|
+
.whereReference(`\`${modelOther._tableName()}\`.\`${foreignKey}\``, `\`${pivotTable}\`.\`${localKey}\``)
|
|
1807
|
+
.toString();
|
|
1808
|
+
const queryChildModel = new Model();
|
|
1809
|
+
queryChildModel.$state.set('TABLE_NAME', `\`${pivotTable}\``);
|
|
1810
|
+
const sqldataChilds = queryChildModel
|
|
1811
|
+
.whereIn(localKeyPivotTable, dataPerentId)
|
|
1812
|
+
.when(relation.exists, (query) => query.whereExists(sql))
|
|
1813
|
+
.toString();
|
|
1814
|
+
const dataChilds = yield this.queryStatement(sqldataChilds);
|
|
1815
1815
|
const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
|
|
1816
1816
|
const otherArrId = Array.from(new Set(otherId)) || [];
|
|
1817
1817
|
const otherdataChilds = yield this.queryStatement(modelOther
|
|
1818
|
-
.bind(this.$pool.get())
|
|
1819
1818
|
.whereIn(otherlocalKey, otherArrId)
|
|
1820
1819
|
.debug(this.$state.get('DEBUG'))
|
|
1821
1820
|
.toString());
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
sub[
|
|
1826
|
-
|
|
1821
|
+
if (oldVersion) {
|
|
1822
|
+
dataChilds.forEach((sub) => {
|
|
1823
|
+
otherdataChilds.forEach((otherSub) => {
|
|
1824
|
+
if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
|
|
1825
|
+
sub[other] = otherSub;
|
|
1826
|
+
}
|
|
1827
|
+
});
|
|
1827
1828
|
});
|
|
1828
|
-
|
|
1829
|
+
dataFromParent.forEach((dataPerent) => {
|
|
1830
|
+
if (dataPerent[name] == null)
|
|
1831
|
+
dataPerent[name] = [];
|
|
1832
|
+
dataChilds.forEach((sub) => {
|
|
1833
|
+
if (sub[localKeyPivotTable] === dataPerent[foreignKey]) {
|
|
1834
|
+
dataPerent[name].push(sub);
|
|
1835
|
+
}
|
|
1836
|
+
});
|
|
1837
|
+
});
|
|
1838
|
+
if (this.$state.get('HIDDEN').length)
|
|
1839
|
+
this._hiddenColumnModel(dataFromParent);
|
|
1840
|
+
return dataFromParent;
|
|
1841
|
+
}
|
|
1829
1842
|
dataFromParent.forEach((dataPerent) => {
|
|
1830
1843
|
if (dataPerent[name] == null)
|
|
1831
1844
|
dataPerent[name] = [];
|
|
1832
1845
|
dataChilds.forEach((sub) => {
|
|
1833
1846
|
if (sub[localKeyPivotTable] === dataPerent[foreignKey]) {
|
|
1834
|
-
|
|
1847
|
+
const data = otherdataChilds.find(u => u[foreignKey] === sub[localKey]);
|
|
1848
|
+
if (data != null) {
|
|
1849
|
+
data.pivot = Object.assign({}, sub);
|
|
1850
|
+
dataPerent[name].push(data);
|
|
1851
|
+
}
|
|
1835
1852
|
}
|
|
1836
1853
|
});
|
|
1837
1854
|
});
|
|
@@ -2379,10 +2396,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2379
2396
|
return functionName.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
|
|
2380
2397
|
}
|
|
2381
2398
|
_handleRelationsQuery(nameRelation, relation) {
|
|
2399
|
+
var _a;
|
|
2382
2400
|
this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
|
|
2383
2401
|
this.with(nameRelation);
|
|
2384
2402
|
const r = this.$state.get('WITH').find((data) => data.name === nameRelation);
|
|
2385
|
-
this._assertError(
|
|
2403
|
+
this._assertError(relation == null, `This Relation ${nameRelation} not be register in Model ${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}`);
|
|
2386
2404
|
this._assertError(!Object.values(this.$constants('RELATIONSHIP')).includes(r.relation), `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
2387
2405
|
return r;
|
|
2388
2406
|
}
|