tspace-mysql 1.2.2 → 1.2.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 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({
@@ -640,7 +640,7 @@ hook((result) => ...) // callback result to function
640
640
  hasOne({ name , model , localKey , foreignKey , freezeTable , as })
641
641
  hasMany({ name , model , localKey , foreignKey , freezeTable , as })
642
642
  belongsTo({ name , model , localKey , foreignKey , freezeTable , as })
643
- belongsToMany({ name , model , localKey , foreignKey , freezeTable , as })
643
+ belongsToMany({ name , model , localKey , foreignKey , freezeTable , as , pivot })
644
644
  /**
645
645
  * @relation using registry in your models
646
646
  */
@@ -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
@@ -8,6 +8,7 @@ export interface Relation {
8
8
  pivot?: string | undefined;
9
9
  query?: any | undefined;
10
10
  relation?: Object | undefined;
11
+ exists?: boolean | undefined;
11
12
  }
12
13
  export interface RelationQuery {
13
14
  name?: string;
@@ -148,11 +148,11 @@ declare class Model extends AbstractModel {
148
148
  */
149
149
  withExists(...nameRelations: Array<string>): this;
150
150
  /**
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
- */
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 }: Relation): this;
234
+ protected belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot }: Relation): this;
235
235
  /**
236
236
  * Assign the relation in model Objects
237
237
  * @param {object} relation registry relation in your model
@@ -287,7 +287,7 @@ declare class Model extends AbstractModel {
287
287
  * @param {function?} callback callback of query
288
288
  * @return {this} this
289
289
  */
290
- protected belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable }: RelationQuery, callback?: Function): this;
290
+ protected belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot }: RelationQuery, callback?: Function): this;
291
291
  /**
292
292
  * return only in trashed (data has been remove)
293
293
  * @return {promise}
@@ -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, `relation ${name} not be register !`);
177
- const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(r.relation);
178
- this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
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), 'value is not a 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, `relation ${name} not be register !`);
257
- const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
258
- this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
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
- * Use relations in registry of model return only exists result of relation query
290
- * @param {...string} nameRelations if data exists return blank
291
- * @return {this} this
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.with(...nameRelations);
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, `relation ${nameRelation} not be register !`);
309
- const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(relation.relation);
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
- const relations = nameRelations.map((name) => {
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.with(...nameRelations);
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
- var _a;
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 }) {
447
+ belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot }) {
457
448
  const relation = {
458
449
  name,
459
450
  model,
@@ -462,6 +453,7 @@ class Model extends AbstractModel_1.AbstractModel {
462
453
  localKey,
463
454
  foreignKey,
464
455
  freezeTable,
456
+ pivot,
465
457
  query: null
466
458
  };
467
459
  this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
@@ -585,7 +577,7 @@ class Model extends AbstractModel_1.AbstractModel {
585
577
  * @param {function?} callback callback of query
586
578
  * @return {this} this
587
579
  */
588
- belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
580
+ belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot }, callback) {
589
581
  const nameRelation = name == null
590
582
  ? this._functionRelationName()
591
583
  : String(name);
@@ -597,6 +589,7 @@ class Model extends AbstractModel_1.AbstractModel {
597
589
  localKey,
598
590
  foreignKey,
599
591
  freezeTable,
592
+ pivot,
600
593
  query: null
601
594
  };
602
595
  const r = this._handleRelationsQuery(nameRelation, relation);
@@ -1598,23 +1591,27 @@ class Model extends AbstractModel_1.AbstractModel {
1598
1591
  return;
1599
1592
  const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
1600
1593
  for (const result of results) {
1601
- for (const key in result) {
1602
- const s = schema[key];
1594
+ for (const column in result) {
1595
+ const s = schema[column];
1596
+ if (s == null && this.$state.get('SELECT').includes('*'))
1597
+ this._assertError(`Not found this column [${column}] in schema`);
1603
1598
  if (s == null)
1604
- this._assertError(`not found this column [${key}] in schema`);
1599
+ continue;
1605
1600
  const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
1606
1601
  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]/;
1607
- if (regexDate.test(result[key]) || regexDateTime.test(result[key])) {
1608
- if (typeOf(new Date(result[key])) === typeOf(new s()))
1602
+ if (regexDate.test(result[column]) || regexDateTime.test(result[column])) {
1603
+ if (typeOf(new Date(result[column])) === typeOf(new s()))
1609
1604
  continue;
1610
- this._assertError(`this column [${key}] is invalid schema field type`);
1605
+ this._assertError(`This column [${column}] is invalid schema field type`);
1611
1606
  }
1612
- if (typeOf(result[key]) === typeOf(new s()))
1607
+ if (result[column] === null)
1608
+ continue;
1609
+ if (typeOf(result[column]) === typeOf(new s()))
1613
1610
  continue;
1614
- this._assertError(`this column [${key}] is invalid schema field type`);
1611
+ this._assertError(`This column [${column}] is invalid schema field type`);
1615
1612
  }
1616
1613
  }
1617
- return this;
1614
+ return;
1618
1615
  }
1619
1616
  _execute({ sql, type, message, options }) {
1620
1617
  return __awaiter(this, void 0, void 0, function* () {
@@ -1719,6 +1716,8 @@ class Model extends AbstractModel_1.AbstractModel {
1719
1716
  const relation = relations[index];
1720
1717
  if (!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length))
1721
1718
  continue;
1719
+ if (relation.exists == null)
1720
+ continue;
1722
1721
  const { localKey, foreignKey, pivot } = this._valueInRelation(relation);
1723
1722
  const query = relation.query;
1724
1723
  this._assertError(query == null, `unknown callback query in [relation : '${relation.name}']`);
@@ -1732,13 +1731,13 @@ class Model extends AbstractModel_1.AbstractModel {
1732
1731
  }
1733
1732
  }
1734
1733
  if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
1734
+ const thisPivot = new Model();
1735
+ thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
1735
1736
  const sql = clone
1736
1737
  .bind(this.$pool.get())
1737
- .whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
1738
+ .whereReference(`\`${query._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${localKey}\``)
1738
1739
  .toString();
1739
- this.whereExists(sql);
1740
- const thisPivot = new Model();
1741
- thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
1740
+ thisPivot.whereExists(sql);
1742
1741
  const sqlPivot = thisPivot
1743
1742
  .bind(this.$pool.get())
1744
1743
  .whereReference(`\`${this._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${this._valuePattern([pluralize_1.default.singular(this._tableName()), foreignKey].join("_"))}\``)
@@ -1784,7 +1783,7 @@ class Model extends AbstractModel_1.AbstractModel {
1784
1783
  _belongsToMany(dataFromParent, relation) {
1785
1784
  var _a;
1786
1785
  return __awaiter(this, void 0, void 0, function* () {
1787
- const { name, localKey, foreignKey, pivot } = this._valueInRelation(relation);
1786
+ const { name, foreignKey, pivot } = this._valueInRelation(relation);
1788
1787
  const pivotTable = String(((_a = relation.pivot) !== null && _a !== void 0 ? _a : pivot));
1789
1788
  const localKeyId = dataFromParent.map((parent) => {
1790
1789
  const data = parent[foreignKey];
@@ -2377,10 +2376,11 @@ class Model extends AbstractModel_1.AbstractModel {
2377
2376
  return functionName.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
2378
2377
  }
2379
2378
  _handleRelationsQuery(nameRelation, relation) {
2379
+ var _a;
2380
2380
  this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
2381
2381
  this.with(nameRelation);
2382
2382
  const r = this.$state.get('WITH').find((data) => data.name === nameRelation);
2383
- this._assertError(r == null, `relation ${nameRelation} not be register !`);
2383
+ this._assertError(relation == null, `This Relation ${nameRelation} not be register in Model ${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}`);
2384
2384
  this._assertError(!Object.values(this.$constants('RELATIONSHIP')).includes(r.relation), `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
2385
2385
  return r;
2386
2386
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",