tspace-mysql 1.3.6 → 1.3.8

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
@@ -189,7 +189,8 @@ const users = await new DB('users')
189
189
  name :'tspace6',
190
190
  email : 'tspace6@gmail.com'
191
191
  },
192
- ]).save()
192
+ ])
193
+ .save()
193
194
 
194
195
  const users = await new DB('users')
195
196
  .where('name','tspace4')
@@ -275,7 +276,8 @@ try {
275
276
  .bind(connection)
276
277
  .save()
277
278
 
278
- const posts = await new Post().createMultiple([
279
+ const posts = await new Post()
280
+ .createMultiple([
279
281
  {
280
282
  user_id : user.id,
281
283
  title : `tspace-post1`
@@ -412,7 +414,7 @@ class User extends Model {
412
414
  * this.useRegistry() // => build-in functions registry
413
415
  * this.useLoadRelationsInRegistry() // => auto generated results from relationship to results
414
416
  * this.useBuiltInRelationFunctions() // => build-in functions relationships to results
415
- * this.useSchema({
417
+ * this.useValidationSchema({
416
418
  * id : Number,
417
419
  * username : String
418
420
  * created_at : Date,
@@ -853,23 +855,32 @@ hook((result) => ...) // callback result to function
853
855
  /**
854
856
  * registry relation in your models
855
857
  * @relationship
856
- */
857
- hasOne({ name , model , localKey , foreignKey , freezeTable , as })
858
- hasMany({ name , model , localKey , foreignKey , freezeTable , as })
859
- belongsTo({ name , model , localKey , foreignKey , freezeTable , as })
860
- belongsToMany({ name , model , localKey , foreignKey , freezeTable , as , pivot })
858
+ */
859
+ hasOne({ name, model, localKey, foreignKey, freezeTable , as })
860
+ hasMany({ name, model, localKey, foreignKey, freezeTable , as })
861
+ belongsTo({ name, model, localKey, foreignKey, freezeTable , as })
862
+ belongsToMany({ name, model, localKey, foreignKey, freezeTable, as, pivot })
861
863
  /**
862
864
  * @relation using registry in your models
863
- */
865
+ */
864
866
  relations(name1 , name2,...nameN)
867
+ /**
868
+ * @relation using registry in your models ignore soft delete
869
+ */
870
+ relationsAll(name1 , name2,...nameN)
865
871
  /**
866
872
  * @relation using registry in your models. if exists child data remove this data
867
- */
873
+ */
868
874
  relationsExists(name1 , name2,...nameN)
869
875
  /**
870
876
  * @relation call a name of relation in registry, callback query of data
871
- */
877
+ */
872
878
  relationQuery(name, (callback) )
879
+ /**
880
+ * @relation using registry in your models return only in trash (soft delete)
881
+ */
882
+ relationsTrashed(name1 , name2,...nameN)
883
+
873
884
 
874
885
  /**
875
886
  * queries statements
@@ -9,8 +9,8 @@ class ${model} extends Model {
9
9
  *
10
10
  * Assign setting global in your model
11
11
  * @useMethod
12
- *
13
- * this.useDebug() // => runing a uuid (universally unique identifier) when insert new data
12
+ * this.useUUID() // => runing a uuid (universally unique identifier) when insert new data
13
+ * this.useDebug() // debug raw query
14
14
  * this.usePrimaryKey('id')
15
15
  * this.useTimestamp({ createdAt : 'created_at' , updatedAt : 'updated_at' }) // runing a timestamp when insert or update
16
16
  * this.useSoftDelete()
@@ -11,6 +11,7 @@ const CONSTANTS = Object.freeze({
11
11
  BETWEEN: 'BETWEEN',
12
12
  NOT_BETWEEN: 'NOT BETWEEN',
13
13
  AND: 'AND',
14
+ NULL: 'NULL',
14
15
  IS_NULL: 'IS NULL',
15
16
  IS_NOT_NULL: 'IS NOT NULL',
16
17
  OR: 'OR',
@@ -1,5 +1,5 @@
1
- import { Pagination } from './Interface';
2
- import { StateHandler } from './StateHandler';
1
+ import { Pagination } from '../Interface';
2
+ import { StateHandler } from '../StateHandler';
3
3
  declare abstract class AbstractBuilder {
4
4
  protected $setters: string[];
5
5
  protected $utils: {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractBuilder = void 0;
4
- const StateHandler_1 = require("./StateHandler");
4
+ const StateHandler_1 = require("../StateHandler");
5
5
  class AbstractBuilder {
6
6
  constructor() {
7
7
  this.$setters = [
@@ -1,5 +1,5 @@
1
- import Builder from './Builder';
2
- import { Connection, ConnectionOptions } from './Interface';
1
+ import Builder from '../Builder';
2
+ import { Connection, ConnectionOptions } from '../Interface';
3
3
  declare abstract class AbstractDB extends Builder {
4
4
  abstract table(tableName: string): void;
5
5
  abstract beginTransaction(): Promise<any>;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.AbstractDB = void 0;
7
- const Builder_1 = __importDefault(require("./Builder"));
7
+ const Builder_1 = __importDefault(require("../Builder"));
8
8
  class AbstractDB extends Builder_1.default {
9
9
  }
10
10
  exports.AbstractDB = AbstractDB;
@@ -1,5 +1,5 @@
1
- import { Relation, RelationQuery } from './Interface';
2
- import Builder from './Builder';
1
+ import { Relation, RelationQuery } from '../Interface';
2
+ import Builder from '../Builder';
3
3
  declare abstract class AbstractModel extends Builder {
4
4
  protected abstract useUUID(): this;
5
5
  protected abstract usePrimaryKey(primaryKey: string): this;
@@ -34,12 +34,14 @@ declare abstract class AbstractModel extends Builder {
34
34
  abstract with(...nameRelations: string[]): this;
35
35
  abstract withQuery(nameRelations: string, callback: Function): this;
36
36
  abstract withExists(...nameRelations: string[]): this;
37
- abstract withAndTrashed(...nameRelations: string[]): this;
37
+ abstract withTrashed(...nameRelations: string[]): this;
38
+ abstract withAll(...nameRelations: string[]): this;
38
39
  abstract has(...nameRelations: string[]): this;
39
40
  abstract relations(...nameRelations: string[]): this;
40
41
  abstract relationQuery(nameRelations: string, callback: Function): this;
41
42
  abstract relationsExists(...nameRelations: string[]): this;
42
- abstract relationsAndTrashed(...nameRelations: string[]): this;
43
+ abstract relationsAll(...nameRelations: string[]): this;
44
+ abstract relationsTrashed(...nameRelations: string[]): this;
43
45
  }
44
46
  export { AbstractModel };
45
47
  export default AbstractModel;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.AbstractModel = void 0;
7
- const Builder_1 = __importDefault(require("./Builder"));
7
+ const Builder_1 = __importDefault(require("../Builder"));
8
8
  class AbstractModel extends Builder_1.default {
9
9
  }
10
10
  exports.AbstractModel = AbstractModel;
@@ -1,3 +1,17 @@
1
+ /**
2
+ * make schema for table with Blueprint
3
+ * @example
4
+ * import { Schema , Blueprint } from 'tspace-mysql'
5
+ * await new Schema().table('persos1',{
6
+ * id : new Blueprint().int().notNull().primary().autoIncrement(),
7
+ * name : new Blueprint().varchar(255).default('my name ...'),
8
+ * email : new Blueprint().varchar(255).unique(),
9
+ * verify : new Blueprint().tinyInt(1).notNull(),
10
+ * created_at : new Blueprint().timestamp().null(),
11
+ * updated_at : new Blueprint().timestamp().null(),
12
+ * deleted_at : new Blueprint().timestamp().null()
13
+ * })
14
+ */
1
15
  declare class Blueprint {
2
16
  protected type: string;
3
17
  protected attrbuites: Array<string>;
@@ -115,10 +129,10 @@ declare class Blueprint {
115
129
  primary(): this;
116
130
  /**
117
131
  * Assign attrbuites 'default' in table
118
- * @param {string | number} n default value
132
+ * @param {string | number} value default value
119
133
  * @return {this} this
120
134
  */
121
- default(n: string | number): this;
135
+ default(value: string | number): this;
122
136
  /**
123
137
  * Assign attrbuites 'default currentTimestamp' in table
124
138
  * @return {this} this
@@ -1,6 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Blueprint = void 0;
4
+ /**
5
+ * make schema for table with Blueprint
6
+ * @example
7
+ * import { Schema , Blueprint } from 'tspace-mysql'
8
+ * await new Schema().table('persos1',{
9
+ * id : new Blueprint().int().notNull().primary().autoIncrement(),
10
+ * name : new Blueprint().varchar(255).default('my name ...'),
11
+ * email : new Blueprint().varchar(255).unique(),
12
+ * verify : new Blueprint().tinyInt(1).notNull(),
13
+ * created_at : new Blueprint().timestamp().null(),
14
+ * updated_at : new Blueprint().timestamp().null(),
15
+ * deleted_at : new Blueprint().timestamp().null()
16
+ * })
17
+ */
4
18
  class Blueprint {
5
19
  constructor() {
6
20
  this.type = '';
@@ -190,11 +204,11 @@ class Blueprint {
190
204
  }
191
205
  /**
192
206
  * Assign attrbuites 'default' in table
193
- * @param {string | number} n default value
207
+ * @param {string | number} value default value
194
208
  * @return {this} this
195
209
  */
196
- default(n) {
197
- this._addAssignAttrbuite(`DEFAULT '${n}'`);
210
+ default(value) {
211
+ this._addAssignAttrbuite(`DEFAULT '${value}'`);
198
212
  return this;
199
213
  }
200
214
  /**
@@ -1,4 +1,4 @@
1
- import { AbstractBuilder } from './AbstractBuilder';
1
+ import { AbstractBuilder } from './Abstract/AbstractBuilder';
2
2
  import { Pagination, Backup, ConnectionOptions, BackupToFile, Connection, ConnectionTransaction } from './Interface';
3
3
  declare class Builder extends AbstractBuilder {
4
4
  constructor();
@@ -189,6 +189,13 @@ declare class Builder extends AbstractBuilder {
189
189
  * @return {this}
190
190
  */
191
191
  whereNotBetween(column: string, array: Array<any>): this;
192
+ /**
193
+ * where not between using [value1, value2]
194
+ * @param {string} column
195
+ * @param {array} array
196
+ * @return {this}
197
+ */
198
+ orWhereNotBetween(column: string, array: Array<any>): this;
192
199
  /**
193
200
  * where null using NULL
194
201
  * @param {string} column
@@ -26,7 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Builder = void 0;
27
27
  const fs_1 = __importDefault(require("fs"));
28
28
  const sql_formatter_1 = require("sql-formatter");
29
- const AbstractBuilder_1 = require("./AbstractBuilder");
29
+ const AbstractBuilder_1 = require("./Abstract/AbstractBuilder");
30
30
  const utils_1 = require("../utils");
31
31
  const constants_1 = require("../constants");
32
32
  const DB_1 = require("./DB");
@@ -52,7 +52,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
52
52
  * @return {this} this
53
53
  */
54
54
  except(...columns) {
55
- this.$state.set('EXCEPT', columns.length ? columns : ['id']);
55
+ this.$state.set('EXCEPT', columns.length ? columns : []);
56
56
  return this;
57
57
  }
58
58
  /**
@@ -300,9 +300,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
300
300
  whereIn(column, array) {
301
301
  if (!Array.isArray(array))
302
302
  throw new Error(`[${array}] is't array`);
303
- if (!array.length)
304
- return this;
305
- const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
303
+ const values = array.length
304
+ ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
305
+ : this.$constants('NULL');
306
306
  this.$state.set('WHERE', [
307
307
  this._queryWhereIsExists()
308
308
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
@@ -322,9 +322,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
322
322
  orWhereIn(column, array) {
323
323
  if (!Array.isArray(array))
324
324
  throw new Error(`[${array}] is't array`);
325
- if (!array.length)
326
- return this;
327
- const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
325
+ const values = array.length
326
+ ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
327
+ : this.$constants('NULL');
328
328
  this.$state.set('WHERE', [
329
329
  this._queryWhereIsExists()
330
330
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
@@ -345,9 +345,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
345
345
  const sql = this.$state.get('WHERE');
346
346
  if (!Array.isArray(array))
347
347
  throw new Error(`[${array}] is't array`);
348
- if (!array.length)
349
- return this;
350
- const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
348
+ const values = array.length
349
+ ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
350
+ : this.$constants('NULL');
351
351
  this.$state.set('WHERE', [
352
352
  this._queryWhereIsExists()
353
353
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
@@ -367,9 +367,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
367
367
  orWhereNotIn(column, array) {
368
368
  if (!Array.isArray(array))
369
369
  throw new Error(`[${array}] is't array`);
370
- if (!array.length)
371
- return this;
372
- const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
370
+ const values = array.length
371
+ ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
372
+ : this.$constants('NULL');
373
373
  this.$state.set('WHERE', [
374
374
  this._queryWhereIsExists()
375
375
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
@@ -387,6 +387,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
387
387
  * @return {this}
388
388
  */
389
389
  whereSubQuery(column, subQuery) {
390
+ if (!this.$utils.isQuery(subQuery))
391
+ throw new Error(`This "${subQuery}" is invalid sub query`);
390
392
  this.$state.set('WHERE', [
391
393
  this._queryWhereIsExists()
392
394
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
@@ -404,6 +406,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
404
406
  * @return {this}
405
407
  */
406
408
  whereNotSubQuery(column, subQuery) {
409
+ if (!this.$utils.isQuery(subQuery))
410
+ throw new Error(`This "${subQuery}" is invalid sub query`);
407
411
  this.$state.set('WHERE', [
408
412
  this._queryWhereIsExists()
409
413
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
@@ -421,6 +425,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
421
425
  * @return {this}
422
426
  */
423
427
  orWhereSubQuery(column, subQuery) {
428
+ if (!this.$utils.isQuery(subQuery))
429
+ throw new Error(`This "${subQuery}" is invalid sub query`);
424
430
  this.$state.set('WHERE', [
425
431
  this._queryWhereIsExists()
426
432
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
@@ -438,6 +444,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
438
444
  * @return {this}
439
445
  */
440
446
  orWhereNotSubQuery(column, subQuery) {
447
+ if (!this.$utils.isQuery(subQuery))
448
+ throw new Error(`This "${subQuery}" is invalid sub query`);
441
449
  this.$state.set('WHERE', [
442
450
  this._queryWhereIsExists()
443
451
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
@@ -457,8 +465,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
457
465
  whereBetween(column, array) {
458
466
  if (!Array.isArray(array))
459
467
  throw new Error("Value is't array");
460
- if (!array.length)
468
+ if (!array.length) {
469
+ this.$state.set('WHERE', [
470
+ this._queryWhereIsExists()
471
+ ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
472
+ : `${this.$constants('WHERE')}`,
473
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
474
+ `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
475
+ ].join(' '));
461
476
  return this;
477
+ }
462
478
  const [value1, value2] = array;
463
479
  this.$state.set('WHERE', [
464
480
  this._queryWhereIsExists()
@@ -478,8 +494,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
478
494
  orWhereBetween(column, array) {
479
495
  if (!Array.isArray(array))
480
496
  throw new Error("Value is't array");
481
- if (!array.length)
497
+ if (!array.length) {
498
+ this.$state.set('WHERE', [
499
+ this._queryWhereIsExists()
500
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
501
+ : `${this.$constants('WHERE')}`,
502
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
503
+ `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
504
+ ].join(' '));
482
505
  return this;
506
+ }
483
507
  const [value1, value2] = array;
484
508
  this.$state.set('WHERE', [
485
509
  this._queryWhereIsExists()
@@ -499,8 +523,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
499
523
  whereNotBetween(column, array) {
500
524
  if (!Array.isArray(array))
501
525
  throw new Error("Value is't array");
502
- if (!array.length)
526
+ if (!array.length) {
527
+ this.$state.set('WHERE', [
528
+ this._queryWhereIsExists()
529
+ ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
530
+ : `${this.$constants('WHERE')}`,
531
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
532
+ `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
533
+ ].join(' '));
503
534
  return this;
535
+ }
504
536
  const [value1, value2] = array;
505
537
  this.$state.set('WHERE', [
506
538
  this._queryWhereIsExists()
@@ -511,6 +543,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
511
543
  ].join(' '));
512
544
  return this;
513
545
  }
546
+ /**
547
+ * where not between using [value1, value2]
548
+ * @param {string} column
549
+ * @param {array} array
550
+ * @return {this}
551
+ */
552
+ orWhereNotBetween(column, array) {
553
+ if (!Array.isArray(array))
554
+ throw new Error("Value is't array");
555
+ if (!array.length) {
556
+ this.$state.set('WHERE', [
557
+ this._queryWhereIsExists()
558
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
559
+ : `${this.$constants('WHERE')}`,
560
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
561
+ `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
562
+ ].join(' '));
563
+ return this;
564
+ }
565
+ const [value1, value2] = array;
566
+ this.$state.set('WHERE', [
567
+ this._queryWhereIsExists()
568
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
569
+ : `${this.$constants('WHERE')}`,
570
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
571
+ `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
572
+ ].join(' '));
573
+ return this;
574
+ }
514
575
  /**
515
576
  * where null using NULL
516
577
  * @param {string} column
@@ -1343,7 +1404,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1343
1404
  ].join(' ');
1344
1405
  const rawColumns = yield this.queryStatement(sql);
1345
1406
  const columns = rawColumns.map((column) => column.Field);
1346
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
1407
+ const removeExcept = columns.filter((column) => {
1408
+ const excepts = this.$state.get('EXCEPT');
1409
+ return excepts.every((except) => except !== column);
1410
+ });
1347
1411
  return removeExcept;
1348
1412
  });
1349
1413
  }
@@ -2162,6 +2226,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2162
2226
  tabWidth: 2,
2163
2227
  linesBetweenQueries: 1,
2164
2228
  }));
2229
+ // fs.writeFileSync(filePath, [...sql,'COMMIT;'].join('\n'))
2230
+ console.log(sql);
2165
2231
  return;
2166
2232
  });
2167
2233
  }
@@ -1,4 +1,4 @@
1
- import { AbstractDB } from './AbstractDB';
1
+ import { AbstractDB } from './Abstract/AbstractDB';
2
2
  import { Connection, ConnectionOptions, ConnectionTransaction } from './Interface';
3
3
  /**
4
4
  * Assign table name
@@ -24,7 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.DB = void 0;
27
- const AbstractDB_1 = require("./AbstractDB");
27
+ const AbstractDB_1 = require("./Abstract/AbstractDB");
28
28
  const ProxyHandler_1 = require("./ProxyHandler");
29
29
  const connection_1 = require("../connection");
30
30
  const StateHandler_1 = __importDefault(require("./StateHandler"));
@@ -9,6 +9,7 @@ export interface Relation {
9
9
  query?: any | undefined;
10
10
  relation?: Object | undefined;
11
11
  exists?: boolean | undefined;
12
+ all?: boolean | undefined;
12
13
  trashed?: boolean | undefined;
13
14
  oldVersion?: boolean | undefined;
14
15
  }
@@ -1,4 +1,4 @@
1
- import { AbstractModel } from './AbstractModel';
1
+ import { AbstractModel } from './Abstract/AbstractModel';
2
2
  import { Relation, Pagination, RelationQuery } from './Interface';
3
3
  declare class Model extends AbstractModel {
4
4
  constructor();
@@ -211,7 +211,7 @@ declare class Model extends AbstractModel {
211
211
  * @example
212
212
  * class User extends Model {
213
213
  * constructor() {
214
- * this.useSchema({
214
+ * this.useValidationSchema({
215
215
  * id : Number,
216
216
  * email : String,
217
217
  * name : String,
@@ -221,7 +221,7 @@ declare class Model extends AbstractModel {
221
221
  * }
222
222
  * @return {this} this
223
223
  */
224
- protected useSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
224
+ protected useValidationSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
225
225
  /**
226
226
  * Assign hook function when execute returned results to callback function
227
227
  * @param {Array<Function>} arrayFunctions functions for callback result
@@ -342,11 +342,18 @@ declare class Model extends AbstractModel {
342
342
  with(...nameRelations: Array<string>): this;
343
343
  /**
344
344
  *
345
- * Use relations in registry of model return normal and in trash
345
+ * Use relations in registry of model return ignore soft delete
346
346
  * @param {...string} nameRelations if data exists return blank
347
347
  * @return {this} this
348
348
  */
349
- withAndTrashed(...nameRelations: Array<string>): this;
349
+ withAll(...nameRelations: Array<string>): this;
350
+ /**
351
+ *
352
+ * Use relations in registry of model return only in trash (soft delete)
353
+ * @param {...string} nameRelations if data exists return blank
354
+ * @return {this} this
355
+ */
356
+ withTrashed(...nameRelations: Array<string>): this;
350
357
  /**
351
358
  *
352
359
  * Use relations in registry of model return only exists result of relation query
@@ -547,7 +554,14 @@ declare class Model extends AbstractModel {
547
554
  * @param {...string} nameRelations if data exists return blank
548
555
  * @return {this} this
549
556
  */
550
- relationsAndTrashed(...nameRelations: Array<string>): this;
557
+ relationsAll(...nameRelations: Array<string>): this;
558
+ /**
559
+ *
560
+ * Use relations in registry of model return only in trash (soft delete)
561
+ * @param {...string} nameRelations if data exists return blank
562
+ * @return {this} this
563
+ */
564
+ relationsTrashed(...nameRelations: Array<string>): this;
551
565
  /**
552
566
  * Assign the relation in model Objects
553
567
  * @param {object} relations registry relation in your model
@@ -654,6 +668,12 @@ declare class Model extends AbstractModel {
654
668
  * @return {this} this
655
669
  */
656
670
  protected belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot }: RelationQuery, callback?: Function): this;
671
+ /**
672
+ * where not null using NULL
673
+ * @override
674
+ * @return {this}
675
+ */
676
+ whereTrashed(): this;
657
677
  /**
658
678
  * return only in trashed (data has been remove)
659
679
  * @return {promise}
@@ -16,7 +16,7 @@ exports.Model = void 0;
16
16
  const pluralize_1 = __importDefault(require("pluralize"));
17
17
  const DB_1 = require("./DB");
18
18
  const Schema_1 = require("./Schema");
19
- const AbstractModel_1 = require("./AbstractModel");
19
+ const AbstractModel_1 = require("./Abstract/AbstractModel");
20
20
  const ProxyHandler_1 = require("./ProxyHandler");
21
21
  const StateHandler_1 = require("./StateHandler");
22
22
  class Model extends AbstractModel_1.AbstractModel {
@@ -194,7 +194,7 @@ class Model extends AbstractModel_1.AbstractModel {
194
194
  this.$constants('PATTERN').snake_case,
195
195
  this.$constants('PATTERN').camelCase
196
196
  ];
197
- this._assertError(!allowPattern.includes(pattern), `tspace-mysql support only pattern [${allowPattern}]`);
197
+ this._assertError(!allowPattern.includes(pattern), `tspace-mysql support only pattern ["${this.$constants('PATTERN').snake_case}","${this.$constants('PATTERN').camelCase}"]`);
198
198
  this.$state.set('PATTERN', pattern);
199
199
  return this;
200
200
  }
@@ -301,7 +301,7 @@ class Model extends AbstractModel_1.AbstractModel {
301
301
  * @example
302
302
  * class User extends Model {
303
303
  * constructor() {
304
- * this.useSchema({
304
+ * this.useValidationSchema({
305
305
  * id : Number,
306
306
  * email : String,
307
307
  * name : String,
@@ -311,7 +311,7 @@ class Model extends AbstractModel_1.AbstractModel {
311
311
  * }
312
312
  * @return {this} this
313
313
  */
314
- useSchema(schema) {
314
+ useValidationSchema(schema) {
315
315
  this.$state.set('SCHEMA', schema);
316
316
  return this;
317
317
  }
@@ -342,7 +342,10 @@ class Model extends AbstractModel_1.AbstractModel {
342
342
  return __awaiter(this, void 0, void 0, function* () {
343
343
  if (this.$state.get('SCHEMA')) {
344
344
  const columns = Object.keys(this.$state.get('SCHEMA'));
345
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
345
+ const removeExcept = columns.filter((column) => {
346
+ const excepts = this.$state.get('EXCEPT');
347
+ return excepts.every((except) => except !== column);
348
+ });
346
349
  return removeExcept;
347
350
  }
348
351
  const rawColumns = yield this.queryStatement([
@@ -352,7 +355,10 @@ class Model extends AbstractModel_1.AbstractModel {
352
355
  `${this.$state.get('TABLE_NAME')}`
353
356
  ].join(' '));
354
357
  const columns = rawColumns.map((column) => column.Field);
355
- const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
358
+ const removeExcept = columns.filter((column) => {
359
+ const excepts = this.$state.get('EXCEPT');
360
+ return excepts.every((except) => except !== column);
361
+ });
356
362
  return removeExcept;
357
363
  });
358
364
  }
@@ -543,11 +549,32 @@ class Model extends AbstractModel_1.AbstractModel {
543
549
  }
544
550
  /**
545
551
  *
546
- * Use relations in registry of model return normal and in trash
552
+ * Use relations in registry of model return ignore soft delete
547
553
  * @param {...string} nameRelations if data exists return blank
548
554
  * @return {this} this
549
555
  */
550
- withAndTrashed(...nameRelations) {
556
+ withAll(...nameRelations) {
557
+ const relations = this._handleRelations(nameRelations);
558
+ relations.forEach(relation => relation.all = true);
559
+ const setRelations = this.$state.get('RELATIONS').length
560
+ ? [...relations.map((w) => {
561
+ const exists = this.$state.get('RELATIONS').find((r) => r.name === w.name);
562
+ if (exists)
563
+ return null;
564
+ return w;
565
+ }).filter((d) => d != null),
566
+ ...this.$state.get('RELATIONS')]
567
+ : relations;
568
+ this.$state.set('RELATIONS', setRelations);
569
+ return this;
570
+ }
571
+ /**
572
+ *
573
+ * Use relations in registry of model return only in trash (soft delete)
574
+ * @param {...string} nameRelations if data exists return blank
575
+ * @return {this} this
576
+ */
577
+ withTrashed(...nameRelations) {
551
578
  const relations = this._handleRelations(nameRelations);
552
579
  relations.forEach(relation => relation.trashed = true);
553
580
  const setRelations = this.$state.get('RELATIONS').length
@@ -793,8 +820,17 @@ class Model extends AbstractModel_1.AbstractModel {
793
820
  * @param {...string} nameRelations if data exists return blank
794
821
  * @return {this} this
795
822
  */
796
- relationsAndTrashed(...nameRelations) {
797
- return this.withAndTrashed(...nameRelations);
823
+ relationsAll(...nameRelations) {
824
+ return this.withAll(...nameRelations);
825
+ }
826
+ /**
827
+ *
828
+ * Use relations in registry of model return only in trash (soft delete)
829
+ * @param {...string} nameRelations if data exists return blank
830
+ * @return {this} this
831
+ */
832
+ relationsTrashed(...nameRelations) {
833
+ return this.withTrashed(...nameRelations);
798
834
  }
799
835
  /**
800
836
  * Assign the relation in model Objects
@@ -1041,6 +1077,16 @@ class Model extends AbstractModel_1.AbstractModel {
1041
1077
  r.query = callback(new r.model());
1042
1078
  return this;
1043
1079
  }
1080
+ /**
1081
+ * where not null using NULL
1082
+ * @override
1083
+ * @return {this}
1084
+ */
1085
+ whereTrashed() {
1086
+ this.disableSoftDelete();
1087
+ this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
1088
+ return this;
1089
+ }
1044
1090
  /**
1045
1091
  * return only in trashed (data has been remove)
1046
1092
  * @return {promise}
@@ -2146,7 +2192,8 @@ class Model extends AbstractModel_1.AbstractModel {
2146
2192
  .bind(this.$pool.get())
2147
2193
  .whereIn(foreignKey, dataPerentId)
2148
2194
  .debug(this.$state.get('DEBUG'))
2149
- .when(relation.trashed, (query) => query.disableSoftDelete())
2195
+ .when(relation.trashed, (query) => query.whereTrashed())
2196
+ .when(relation.all, (query) => query.disableSoftDelete())
2150
2197
  .get();
2151
2198
  return dataFromRelation;
2152
2199
  });
@@ -2179,7 +2226,8 @@ class Model extends AbstractModel_1.AbstractModel {
2179
2226
  const sqldataChilds = queryChildModel
2180
2227
  .whereIn(localKeyPivotTable, dataPerentId)
2181
2228
  .when(relation.exists, (query) => query.whereExists(sql))
2182
- .when(relation.trashed, (query) => query.disableSoftDelete())
2229
+ .when(relation.trashed, (query) => query.whereTrashed())
2230
+ .when(relation.all, (query) => query.disableSoftDelete())
2183
2231
  .toString();
2184
2232
  const dataChilds = yield this.queryStatement(sqldataChilds);
2185
2233
  const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
@@ -1,13 +1,7 @@
1
1
  import { Builder } from "./Builder";
2
2
  declare class Schema extends Builder {
3
- table: (table: string, schemas: Record<string, {
4
- type: string;
5
- attrbuites: string[];
6
- }>) => Promise<void>;
7
- createTable: (table: string, schemas: Record<string, {
8
- type: string;
9
- attrbuites: string[];
10
- }>) => Promise<any>;
3
+ table: (table: string, schemas: Record<string, any>) => Promise<void>;
4
+ createTable: (table: string, schemas: Record<string, any>) => Promise<any>;
11
5
  }
12
6
  export { Schema };
13
7
  export default Schema;
@@ -5,6 +5,7 @@ declare const utils: {
5
5
  timestamp: () => string;
6
6
  date: () => string;
7
7
  escape: (str: any) => any;
8
+ isQuery: (str: string) => boolean;
8
9
  generateUUID: () => string;
9
10
  covertBooleanToNumber: (data: any) => any;
10
11
  snakeCase: (data: any) => any;
@@ -34,6 +34,7 @@ const escape = (str) => {
34
34
  return str;
35
35
  return str.replace(/[\0\b\t\n\r\x1a\'\\]/g, '');
36
36
  };
37
+ const isQuery = (str) => /\b(?:SELECT|FROM|WHERE|JOIN|INNER JOIN|LEFT JOIN|RIGHT JOIN|OUTER JOIN|AND|OR|ORDER BY|GROUP BY|LIMIT)\b/i.test(str);
37
38
  const columnRelation = (name) => {
38
39
  var _a;
39
40
  const matches = (_a = name === null || name === void 0 ? void 0 : name.match(/[A-Z]/g)) !== null && _a !== void 0 ? _a : [];
@@ -145,6 +146,7 @@ const utils = {
145
146
  timestamp,
146
147
  date,
147
148
  escape,
149
+ isQuery,
148
150
  generateUUID,
149
151
  covertBooleanToNumber,
150
152
  snakeCase,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",