tspace-mysql 1.2.4 → 1.2.6

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.
@@ -0,0 +1,4 @@
1
+ declare const _default: (cmd: {
2
+ [x: string]: any;
3
+ }) => void;
4
+ export default _default;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lib_1 = require("../../lib");
4
+ exports.default = (cmd) => {
5
+ const { dir, cwd, fs, db, type } = cmd;
6
+ if (dir) {
7
+ try {
8
+ fs.accessSync(cwd + `/${dir}`, fs.F_OK, {
9
+ recursive: true
10
+ });
11
+ }
12
+ catch (e) {
13
+ fs.mkdirSync(cwd + `/${dir}`, {
14
+ recursive: true
15
+ });
16
+ }
17
+ }
18
+ const directory = `${cwd}/${dir}/dump_${+new Date()}.sql`;
19
+ new lib_1.DB().backupToFile({
20
+ filePath: directory,
21
+ database: db
22
+ })
23
+ .then(r => console.log('dump file successfully'))
24
+ .catch(err => console.log(err));
25
+ };
package/dist/cli/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
6
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  const make_1 = __importDefault(require("./models/make"));
@@ -11,13 +11,15 @@ const make_2 = __importDefault(require("./tables/make"));
11
11
  const make_3 = __importDefault(require("./migrate/make"));
12
12
  const make_4 = __importDefault(require("./generate/make"));
13
13
  const query_1 = __importDefault(require("./query"));
14
+ const db_1 = __importDefault(require("./dump/db"));
14
15
  const commands = {
15
16
  'query': query_1.default,
16
17
  'make:model': make_1.default,
17
18
  'make:table': make_2.default,
18
19
  'make:migration': make_2.default,
19
20
  'migrate': make_3.default,
20
- 'generate:models': make_4.default
21
+ 'generate:models': make_4.default,
22
+ 'dump:db': db_1.default,
21
23
  };
22
24
  try {
23
25
  const name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(data => {
@@ -28,11 +30,14 @@ try {
28
30
  const dir = (_j = (_h = (_g = process.argv.slice(2)) === null || _g === void 0 ? void 0 : _g.find(data => {
29
31
  return data === null || data === void 0 ? void 0 : data.includes('--dir=');
30
32
  })) === null || _h === void 0 ? void 0 : _h.replace('--dir=', '/')) !== null && _j !== void 0 ? _j : null;
31
- let type = (_m = (_l = (_k = process.argv.slice(2)) === null || _k === void 0 ? void 0 : _k.find(data => {
33
+ const db = (_m = (_l = (_k = process.argv.slice(2)) === null || _k === void 0 ? void 0 : _k.find(data => {
34
+ return data === null || data === void 0 ? void 0 : data.includes('--db=');
35
+ })) === null || _l === void 0 ? void 0 : _l.replace('--db=', '')) !== null && _m !== void 0 ? _m : null;
36
+ let type = (_q = (_p = (_o = process.argv.slice(2)) === null || _o === void 0 ? void 0 : _o.find(data => {
32
37
  return data === null || data === void 0 ? void 0 : data.includes('--type=');
33
- })) === null || _l === void 0 ? void 0 : _l.replace('--type=', '.')) !== null && _m !== void 0 ? _m : '.ts';
38
+ })) === null || _p === void 0 ? void 0 : _p.replace('--type=', '.')) !== null && _q !== void 0 ? _q : '.ts';
34
39
  type = ['.js', '.ts'].includes(type) ? type : '.ts';
35
- const file = (_o = process.argv.slice(3)[0]) !== null && _o !== void 0 ? _o : '';
40
+ const file = (_r = process.argv.slice(3)[0]) !== null && _r !== void 0 ? _r : '';
36
41
  const cmd = {
37
42
  name,
38
43
  file,
@@ -42,6 +47,7 @@ try {
42
47
  cwd: process.cwd(),
43
48
  fs: fs_1.default,
44
49
  sql,
50
+ db,
45
51
  npm: 'tspace-mysql'
46
52
  };
47
53
  commands[process.argv[2]](cmd);
@@ -53,6 +59,7 @@ catch (err) {
53
59
  tspace-mysql migrate --dir=App/Models/Migrations --type=js
54
60
  tspace-mysql query "SELECT * FROM users"
55
61
  tspace-mysql generate:models --dir=app/Models
62
+ tspace-mysql dump:db --dir=app/Models
56
63
  `);
57
64
  console.log(`Read more https://www.npmjs.com/package/tspace-mysql`);
58
65
  }
@@ -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
  });
@@ -2396,13 +2416,17 @@ class Model extends AbstractModel_1.AbstractModel {
2396
2416
  case 'get': {
2397
2417
  const methodCallings = this.$logger.get();
2398
2418
  const methodsNotAllowed = [
2419
+ "insert",
2399
2420
  'create',
2400
2421
  'createNotExists',
2401
2422
  'updateOrCreate',
2402
2423
  'updateOrInsert',
2403
2424
  'insertOrUpdate',
2425
+ "createMultiple",
2426
+ "insertMultiple",
2404
2427
  'update',
2405
- 'delete'
2428
+ 'delete',
2429
+ 'restore'
2406
2430
  ];
2407
2431
  const findMethodNotAllowed = methodCallings.find((methodCalling) => methodsNotAllowed.includes(methodCalling));
2408
2432
  this._assertError(methodCallings.some((methodCalling) => methodsNotAllowed.includes(methodCalling)), `this method ${method} can't using method : [ ${findMethodNotAllowed} ]`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",