tspace-mysql 1.2.3 → 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({
@@ -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
  *
@@ -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
@@ -1600,23 +1591,27 @@ class Model extends AbstractModel_1.AbstractModel {
1600
1591
  return;
1601
1592
  const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
1602
1593
  for (const result of results) {
1603
- for (const key in result) {
1604
- 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`);
1605
1598
  if (s == null)
1606
- this._assertError(`not found this column [${key}] in schema`);
1599
+ continue;
1607
1600
  const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
1608
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]/;
1609
- if (regexDate.test(result[key]) || regexDateTime.test(result[key])) {
1610
- 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()))
1611
1604
  continue;
1612
- this._assertError(`this column [${key}] is invalid schema field type`);
1605
+ this._assertError(`This column [${column}] is invalid schema field type`);
1613
1606
  }
1614
- if (typeOf(result[key]) === typeOf(new s()))
1607
+ if (result[column] === null)
1608
+ continue;
1609
+ if (typeOf(result[column]) === typeOf(new s()))
1615
1610
  continue;
1616
- this._assertError(`this column [${key}] is invalid schema field type`);
1611
+ this._assertError(`This column [${column}] is invalid schema field type`);
1617
1612
  }
1618
1613
  }
1619
- return this;
1614
+ return;
1620
1615
  }
1621
1616
  _execute({ sql, type, message, options }) {
1622
1617
  return __awaiter(this, void 0, void 0, function* () {
@@ -1721,6 +1716,8 @@ class Model extends AbstractModel_1.AbstractModel {
1721
1716
  const relation = relations[index];
1722
1717
  if (!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length))
1723
1718
  continue;
1719
+ if (relation.exists == null)
1720
+ continue;
1724
1721
  const { localKey, foreignKey, pivot } = this._valueInRelation(relation);
1725
1722
  const query = relation.query;
1726
1723
  this._assertError(query == null, `unknown callback query in [relation : '${relation.name}']`);
@@ -1734,13 +1731,13 @@ class Model extends AbstractModel_1.AbstractModel {
1734
1731
  }
1735
1732
  }
1736
1733
  if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
1734
+ const thisPivot = new Model();
1735
+ thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
1737
1736
  const sql = clone
1738
1737
  .bind(this.$pool.get())
1739
- .whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
1738
+ .whereReference(`\`${query._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${localKey}\``)
1740
1739
  .toString();
1741
- this.whereExists(sql);
1742
- const thisPivot = new Model();
1743
- thisPivot.$state.set('TABLE_NAME', `\`${pivot}\``);
1740
+ thisPivot.whereExists(sql);
1744
1741
  const sqlPivot = thisPivot
1745
1742
  .bind(this.$pool.get())
1746
1743
  .whereReference(`\`${this._tableName()}\`.\`${foreignKey}\``, `\`${pivot}\`.\`${this._valuePattern([pluralize_1.default.singular(this._tableName()), foreignKey].join("_"))}\``)
@@ -2379,10 +2376,11 @@ class Model extends AbstractModel_1.AbstractModel {
2379
2376
  return functionName.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
2380
2377
  }
2381
2378
  _handleRelationsQuery(nameRelation, relation) {
2379
+ var _a;
2382
2380
  this.$state.set('RELATION', [...this.$state.get('RELATION'), relation]);
2383
2381
  this.with(nameRelation);
2384
2382
  const r = this.$state.get('WITH').find((data) => data.name === nameRelation);
2385
- 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}`);
2386
2384
  this._assertError(!Object.values(this.$constants('RELATIONSHIP')).includes(r.relation), `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
2387
2385
  return r;
2388
2386
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.2.3",
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",