tspace-mysql 1.2.4 → 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.
@@ -9,6 +9,7 @@ export interface Relation {
9
9
  query?: any | undefined;
10
10
  relation?: Object | undefined;
11
11
  exists?: boolean | undefined;
12
+ oldVersion?: boolean | undefined;
12
13
  }
13
14
  export interface RelationQuery {
14
15
  name?: string;
@@ -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
@@ -444,7 +444,7 @@ class Model extends AbstractModel_1.AbstractModel {
444
444
  * @property {string} relation.freezeTable
445
445
  * @return {this} this
446
446
  */
447
- belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot }) {
447
+ belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion }) {
448
448
  const relation = {
449
449
  name,
450
450
  model,
@@ -454,6 +454,7 @@ class Model extends AbstractModel_1.AbstractModel {
454
454
  foreignKey,
455
455
  freezeTable,
456
456
  pivot,
457
+ oldVersion,
457
458
  query: null
458
459
  };
459
460
  this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
@@ -1472,6 +1473,7 @@ class Model extends AbstractModel_1.AbstractModel {
1472
1473
  var _a, _b, _c, _d;
1473
1474
  const relation = relationModel.relation;
1474
1475
  const model = (_a = relationModel.model) === null || _a === void 0 ? void 0 : _a.name;
1476
+ const oldVersion = relationModel.oldVersion;
1475
1477
  const table = relationModel.freezeTable
1476
1478
  ? relationModel.freezeTable
1477
1479
  : (_b = relationModel.query) === null || _b === void 0 ? void 0 : _b._tableName();
@@ -1516,7 +1518,7 @@ class Model extends AbstractModel_1.AbstractModel {
1516
1518
  pluralize_1.default.singular((_d = relationModel.query) === null || _d === void 0 ? void 0 : _d._tableName())
1517
1519
  ].sort().join('_'));
1518
1520
  }
1519
- return { name, as, relation, table, localKey, foreignKey, model, pivot };
1521
+ return { name, as, relation, table, localKey, foreignKey, model, pivot, oldVersion };
1520
1522
  }
1521
1523
  _handleSoftDelete() {
1522
1524
  if (this.$state.get('SOFT_DELETE')) {
@@ -1783,7 +1785,7 @@ class Model extends AbstractModel_1.AbstractModel {
1783
1785
  _belongsToMany(dataFromParent, relation) {
1784
1786
  var _a;
1785
1787
  return __awaiter(this, void 0, void 0, function* () {
1786
- const { name, foreignKey, pivot } = this._valueInRelation(relation);
1788
+ const { name, foreignKey, localKey, pivot, oldVersion } = this._valueInRelation(relation);
1787
1789
  const pivotTable = String(((_a = relation.pivot) !== null && _a !== void 0 ? _a : pivot));
1788
1790
  const localKeyId = dataFromParent.map((parent) => {
1789
1791
  const data = parent[foreignKey];
@@ -1792,7 +1794,7 @@ class Model extends AbstractModel_1.AbstractModel {
1792
1794
  }
1793
1795
  return data;
1794
1796
  }).filter((data) => data != null);
1795
- const dataPerentId = Array.from(new Set(localKeyId)).join(',') || [];
1797
+ const dataPerentId = Array.from(new Set(localKeyId));
1796
1798
  if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
1797
1799
  return [];
1798
1800
  const modelOther = new relation.model();
@@ -1800,35 +1802,53 @@ class Model extends AbstractModel_1.AbstractModel {
1800
1802
  const otherlocalKey = 'id';
1801
1803
  const otherforeignKey = this._valuePattern(`${other}Id`);
1802
1804
  const localKeyPivotTable = this._valuePattern([pluralize_1.default.singular(this._tableName()), foreignKey].join("_"));
1803
- const sqldataChilds = [
1804
- `${this.$constants('SELECT')}`,
1805
- `*`,
1806
- `${this.$constants('FROM')}`,
1807
- `${pivotTable}`,
1808
- `${this.$constants('WHERE')}`,
1809
- `${localKeyPivotTable} ${this.$constants('IN')} (${dataPerentId})`
1810
- ].join(' ');
1811
- let dataChilds = yield this.queryStatement(sqldataChilds);
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);
1812
1815
  const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
1813
1816
  const otherArrId = Array.from(new Set(otherId)) || [];
1814
1817
  const otherdataChilds = yield this.queryStatement(modelOther
1815
- .bind(this.$pool.get())
1816
1818
  .whereIn(otherlocalKey, otherArrId)
1817
1819
  .debug(this.$state.get('DEBUG'))
1818
1820
  .toString());
1819
- dataChilds.forEach((sub) => {
1820
- otherdataChilds.forEach((otherSub) => {
1821
- if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
1822
- sub[other] = otherSub;
1823
- }
1821
+ if (oldVersion) {
1822
+ dataChilds.forEach((sub) => {
1823
+ otherdataChilds.forEach((otherSub) => {
1824
+ if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
1825
+ sub[other] = otherSub;
1826
+ }
1827
+ });
1824
1828
  });
1825
- });
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
+ }
1826
1842
  dataFromParent.forEach((dataPerent) => {
1827
1843
  if (dataPerent[name] == null)
1828
1844
  dataPerent[name] = [];
1829
1845
  dataChilds.forEach((sub) => {
1830
1846
  if (sub[localKeyPivotTable] === dataPerent[foreignKey]) {
1831
- dataPerent[name].push(sub);
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
+ }
1832
1852
  }
1833
1853
  });
1834
1854
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",