tspace-mysql 1.3.4 → 1.3.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.
@@ -1,5 +1,9 @@
1
1
  import { AbstractDB } from './AbstractDB';
2
2
  import { Connection, ConnectionOptions, ConnectionTransaction } from './Interface';
3
+ /**
4
+ * Assign table name
5
+ * @param {string?} table table name
6
+ */
3
7
  declare class DB extends AbstractDB {
4
8
  constructor(table?: string);
5
9
  /**
@@ -19,11 +19,19 @@ var __rest = (this && this.__rest) || function (s, e) {
19
19
  }
20
20
  return t;
21
21
  };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
22
25
  Object.defineProperty(exports, "__esModule", { value: true });
23
26
  exports.DB = void 0;
24
27
  const AbstractDB_1 = require("./AbstractDB");
25
28
  const ProxyHandler_1 = require("./ProxyHandler");
26
29
  const connection_1 = require("../connection");
30
+ const StateHandler_1 = __importDefault(require("./StateHandler"));
31
+ /**
32
+ * Assign table name
33
+ * @param {string?} table table name
34
+ */
27
35
  class DB extends AbstractDB_1.AbstractDB {
28
36
  constructor(table) {
29
37
  super();
@@ -333,30 +341,7 @@ class DB extends AbstractDB_1.AbstractDB {
333
341
  return Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
334
342
  }
335
343
  _initialDB() {
336
- this.$state = (() => {
337
- let db = new Map(Object.entries(Object.assign({}, this.$constants('DB'))));
338
- let original = new Map(Object.entries(Object.assign({}, this.$constants('DB'))));
339
- return {
340
- original: () => original,
341
- get: (key) => {
342
- if (key == null)
343
- return db;
344
- if (!db.has(key))
345
- throw new Error(`can't get this [${key}]`);
346
- return db.get(key);
347
- },
348
- set: (key, value) => {
349
- if (!db.has(key))
350
- throw new Error(`can't set this [${key}]`);
351
- db.set(key, value);
352
- return;
353
- },
354
- clone: (data) => {
355
- db = new Map(Object.entries(Object.assign({}, data)));
356
- return;
357
- }
358
- };
359
- })();
344
+ this.$state = new StateHandler_1.default(this.$constants('DB'));
360
345
  return this;
361
346
  }
362
347
  }
@@ -5,42 +5,112 @@ declare class Model extends AbstractModel {
5
5
  /**
6
6
  *
7
7
  * define for initialize of models
8
+ * @example
9
+ * class User extends Model {
10
+ * define() {
11
+ * this.useUUID()
12
+ * this.usePrimaryKey('id')
13
+ * this.useTimestamp()
14
+ * this.useSoftDelete()
15
+ * }
16
+ * }
8
17
  * @return {void} void
9
18
  */
10
19
  protected define(): void;
11
20
  /**
12
21
  *
13
- * boot for initialize of models
22
+ * boot for initialize of models like constructor()
23
+ * @example
24
+ * class User extends Model {
25
+ * boot() {
26
+ * this.useUUID()
27
+ * this.usePrimaryKey('id')
28
+ * this.useTimestamp()
29
+ * this.useSoftDelete()
30
+ * }
31
+ * }
14
32
  * @return {void} void
15
33
  */
16
34
  protected boot(): void;
17
35
  /**
18
36
  *
19
- * Assign function callback in model
37
+ * Assign auto create table when not exists table
38
+ * @param {object} schema using Blueprint for schema
39
+ * @example
40
+ * import { Blueprint } from 'tspace-mysql'
41
+ * class User extends Model {
42
+ * constructor() {
43
+ * this.useCreateTableIfNotExists ({
44
+ * id : new Blueprint().int().notNull().primary().autoIncrement(),
45
+ * uuid : new Blueprint().varchar(50).null(),
46
+ * email : new Blueprint().varchar(50).null(),
47
+ * name : new Blueprint().varchar(255).null(),
48
+ * created_at : new Blueprint().timestamp().null(),
49
+ * updated_at : new Blueprint().timestamp().null()
50
+ * })
51
+ * }
52
+ * }
53
+ * @return {this} this
54
+ */
55
+ protected useCreateTableIfNotExists(schema: Record<string, any>): this;
56
+ /**
57
+ *
58
+ * Assign function callback in model like constructor()
59
+ * @example
60
+ * class User extends Model {
61
+ * constructor() {
62
+ * this.useRegistry()
63
+ * }
64
+ * }
20
65
  * @return {this} this
21
66
  */
22
67
  protected useRegistry(): this;
23
68
  /**
24
69
  *
25
70
  * Assign model calling all relationships in model
71
+ * @example
72
+ * class User extends Model {
73
+ * constructor() {
74
+ * this.useLoadRelationInRegistry()
75
+ * }
76
+ * }
26
77
  * @return {this} this
27
78
  */
28
- protected useLoadRelationInRegistry(): this;
79
+ protected useLoadRelationsInRegistry(): this;
29
80
  /**
30
81
  *
31
82
  * Assign model built-in relation functions to a results
83
+ * @example
84
+ * class User extends Model {
85
+ * constructor() {
86
+ * this.useBuiltInRelationsFunction()
87
+ * }
88
+ * }
32
89
  * @return {this} this
33
90
  */
34
91
  protected useBuiltInRelationFunctions(): this;
35
92
  /**
36
93
  *
37
94
  * Assign primary column in model
95
+ * @param {string} primary
96
+ * @example
97
+ * class User extends Model {
98
+ * constructor() {
99
+ * this.usePrimaryKey()
100
+ * }
101
+ * }
38
102
  * @return {this} this
39
103
  */
40
104
  protected usePrimaryKey(primary: string): this;
41
105
  /**
42
106
  * Assign generate uuid when creating in model
43
107
  * @param {string?} column [column=uuid] make new name column for custom column replace uuid with this
108
+ * @example
109
+ * class User extends Model {
110
+ * constructor() {
111
+ * this.useUUID()
112
+ * }
113
+ * }
44
114
  * @return {this} this
45
115
  */
46
116
  protected useUUID(column?: string): this;
@@ -53,14 +123,26 @@ declare class Model extends AbstractModel {
53
123
  *
54
124
  * Assign in model use pattern [snake_case , camelCase]
55
125
  * @param {string} pattern
126
+ * @example
127
+ * class User extends Model {
128
+ * constructor() {
129
+ * this.usePattern('camelCase')
130
+ * }
131
+ * }
56
132
  * @return {this} this
57
133
  */
58
- protected usePattern(pattern: string): this;
134
+ protected usePattern(pattern: "snake_case" | "camelCase"): this;
59
135
  /**
60
136
  *
61
137
  * Assign in model show data not be deleted
62
138
  * Relations has reference this method
63
- * @param {string?} column
139
+ * @param {string?} column default deleted_at
140
+ * @example
141
+ * class User extends Model {
142
+ * constructor() {
143
+ * this.useSoftDelete('delete_at')
144
+ * }
145
+ * }
64
146
  * @return {this} this
65
147
  */
66
148
  protected useSoftDelete(column?: string): this;
@@ -70,6 +152,15 @@ declare class Model extends AbstractModel {
70
152
  * @param {object} timestampFormat
71
153
  * @property {string} timestampFormat.createdAt - change new name column replace by default [created at]
72
154
  * @property {string} timestampFormat.updatedAt - change new name column replace by default updated at
155
+ * @example
156
+ * class User extends Model {
157
+ * constructor() {
158
+ * this.useTimestamp({
159
+ * createdAt : 'createdAt',
160
+ * updatedAt : 'updatedAt'
161
+ * })
162
+ * }
163
+ * }
73
164
  * @return {this} this
74
165
  */
75
166
  protected useTimestamp(timestampFormat?: {
@@ -80,39 +171,74 @@ declare class Model extends AbstractModel {
80
171
  *
81
172
  * Assign table name in model
82
173
  * @param {string} table table name in database
174
+ * @example
175
+ * class User extends Model {
176
+ * constructor() {
177
+ * this.useTable('setTableNameIsUser') // => 'setTableNameIsUser'
178
+ * }
179
+ * }
83
180
  * @return {this} this
84
181
  */
85
182
  protected useTable(table: string): this;
86
183
  /**
87
184
  *
88
185
  * Assign table name in model with signgular pattern
186
+ * @example
187
+ * class User extends Model {
188
+ * constructor() {
189
+ * this.useTableSingular() // => 'user'
190
+ * }
191
+ * }
89
192
  * @return {this} this
90
193
  */
91
194
  protected useTableSingular(): this;
92
195
  /**
93
196
  *
94
197
  * Assign table name in model with pluarl pattern
198
+ * @example
199
+ * class User extends Model {
200
+ * constructor() {
201
+ * this.useTablePlural() // => 'users'
202
+ * }
203
+ * }
95
204
  * @return {this} this
96
205
  */
97
206
  protected useTablePlural(): this;
98
207
  /**
99
208
  *
100
209
  * Assign schema column in model for validation data types
101
- * @param {Object<Function>} schema types (String Number and Date)
102
- * @return {this} this
103
- */
104
- protected useSchema(schema: Record<string, Function>): this;
210
+ * @param {Object<NumberConstructor | StringConstructor | DateConstructor>} schema types (String Number and Date)
211
+ * @example
212
+ * class User extends Model {
213
+ * constructor() {
214
+ * this.useSchema({
215
+ * id : Number,
216
+ * email : String,
217
+ * name : String,
218
+ * date : Date
219
+ * })
220
+ * }
221
+ * }
222
+ * @return {this} this
223
+ */
224
+ protected useSchema(schema: Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
105
225
  /**
106
226
  * Assign hook function when execute returned results to callback function
107
- * @param {Function} arrayFunction function for callback result
227
+ * @param {Array<Function>} arrayFunctions functions for callback result
228
+ * @example
229
+ * class User extends Model {
230
+ * constructor() {
231
+ * this.useHook([(results) => console.log(results)])
232
+ * }
233
+ * }
108
234
  * @return {this}
109
235
  */
110
- protected useHook(functions: Array<Function>): this;
236
+ protected useHook(arrayFunctions: Array<Function>): this;
111
237
  /**
112
238
  * exceptColumns for method except
113
239
  * @return {promise<string>} string
114
240
  */
115
- protected exceptColumns(): Promise<any>;
241
+ protected exceptColumns(): Promise<string[]>;
116
242
  /**
117
243
  * Build method for relation in model
118
244
  * @param {string} name name relation registry in your model
@@ -139,13 +265,30 @@ declare class Model extends AbstractModel {
139
265
  insert?: boolean;
140
266
  delete?: boolean;
141
267
  where?: boolean;
268
+ limit?: boolean;
269
+ offset?: boolean;
142
270
  }): Model;
143
271
  /**
144
- * Assign ignore delete_at in model
145
- * @param {boolean} condition
272
+ *
273
+ * execute the query using raw sql syntax
274
+ * @override method
275
+ * @param {string} sql
146
276
  * @return {this} this
147
277
  */
148
- ignoreSoftDelete(condition?: boolean): this;
278
+ protected queryStatement(sql: string): Promise<Array<any>>;
279
+ /**
280
+ *
281
+ * execute the query using raw sql syntax actions for insert update and delete
282
+ * @override method
283
+ * @param {Object} actions
284
+ * @property {Function} actions.sql
285
+ * @property {Function} actions.returnId
286
+ * @return {this} this
287
+ */
288
+ protected actionStatement({ sql, returnId }: {
289
+ sql: string;
290
+ returnId?: boolean;
291
+ }): Promise<any>;
149
292
  /**
150
293
  * Assign table name
151
294
  * @param {string} table table name
@@ -158,6 +301,12 @@ declare class Model extends AbstractModel {
158
301
  * @return {this} this
159
302
  */
160
303
  disableSoftDelete(condition?: boolean): this;
304
+ /**
305
+ * Assign ignore delete_at in model
306
+ * @param {boolean} condition
307
+ * @return {this} this
308
+ */
309
+ ignoreSoftDelete(condition?: boolean): this;
161
310
  /**
162
311
  * Assign build in function to result of data
163
312
  * @param {object} func
@@ -170,6 +319,24 @@ declare class Model extends AbstractModel {
170
319
  *
171
320
  * Use relations in registry of model return result of relation query
172
321
  * @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
322
+ * @example
323
+ * import { Model } from 'tspace-mysql'
324
+ * class User extends Model {
325
+ * constructor(){
326
+ * super()
327
+ * this.hasMany({ name : 'posts' , model : Post })
328
+ * }
329
+ * }
330
+ *
331
+ * class Post extends Model {
332
+ * constructor(){
333
+ * super()
334
+ * this.hasMany({ name : 'comments' , model : Comment })
335
+ * this.belongsTo({ name : 'user' , model : User })
336
+ * }
337
+ * }
338
+ * // use with for results of relationship
339
+ * await new User().with('posts').findMany()
173
340
  * @return {this} this
174
341
  */
175
342
  with(...nameRelations: Array<string>): this;
@@ -184,6 +351,24 @@ declare class Model extends AbstractModel {
184
351
  *
185
352
  * Use relations in registry of model return only exists result of relation query
186
353
  * @param {...string} nameRelations if data exists return blank
354
+ * @example
355
+ * import { Model } from 'tspace-mysql'
356
+ * class User extends Model {
357
+ * constructor(){
358
+ * super()
359
+ * this.hasMany({ name : 'posts' , model : Post })
360
+ * }
361
+ * }
362
+ *
363
+ * class Post extends Model {
364
+ * constructor(){
365
+ * super()
366
+ * this.hasMany({ name : 'comments' , model : Comment })
367
+ * this.belongsTo({ name : 'user' , model : User })
368
+ * }
369
+ * }
370
+ * // use with for results of relationship if relations is exists
371
+ * await new User().withExists('posts').findMany()
187
372
  * @return {this} this
188
373
  */
189
374
  withExists(...nameRelations: Array<string>): this;
@@ -191,6 +376,24 @@ declare class Model extends AbstractModel {
191
376
  *
192
377
  * Use relations in registry of model return only exists result of relation query
193
378
  * @param {...string} nameRelations if data exists return blank
379
+ * @example
380
+ * import { Model } from 'tspace-mysql'
381
+ * class User extends Model {
382
+ * constructor(){
383
+ * super()
384
+ * this.hasMany({ name : 'posts' , model : Post })
385
+ * }
386
+ * }
387
+ *
388
+ * class Post extends Model {
389
+ * constructor(){
390
+ * super()
391
+ * this.hasMany({ name : 'comments' , model : Comment })
392
+ * this.belongsTo({ name : 'user' , model : User })
393
+ * }
394
+ * }
395
+ * // use with for results of relationship if relations is exists
396
+ * await new User().has('posts').findMany()
194
397
  * @return {this} this
195
398
  */
196
399
  has(...nameRelations: Array<string>): this;
@@ -199,13 +402,70 @@ declare class Model extends AbstractModel {
199
402
  * Use relation '${name}' registry of model return callback this query model
200
403
  * @param {string} nameRelation name relation in registry in your model
201
404
  * @param {function} callback query callback
405
+ * @example
406
+ * import { Model } from 'tspace-mysql'
407
+ * class User extends Model {
408
+ * constructor(){
409
+ * super()
410
+ * this.hasMany({ name : 'posts' , model : Post })
411
+ * }
412
+ * }
413
+ *
414
+ * class Post extends Model {
415
+ * constructor(){
416
+ * super()
417
+ * this.hasMany({ name : 'comments' , model : Comment })
418
+ * this.belongsTo({ name : 'user' , model : User })
419
+ * }
420
+ * }
421
+ *
422
+ * class Comment extends Model {
423
+ * constructor(){
424
+ * super()
425
+ * this.hasMany({ name : 'users' , model : User })
426
+ * this.belongsTo({ name : 'post' , model : Post })
427
+ * }
428
+ * }
429
+ *
430
+ * await new User().with('posts')
431
+ * .withQuery('posts', (query : Post) => {
432
+ * return query.with('comments','user')
433
+ * .withQuery('comments', (query : Comment) => {
434
+ * return query.with('user','post')
435
+ * })
436
+ * .withQuery('user', (query : User) => {
437
+ * return query.with('posts').withQuery('posts',(query : Post)=> {
438
+ * return query.with('comments','user')
439
+ * // relation n, n, ...n
440
+ * })
441
+ * })
442
+ * })
443
+ * .findMany()
202
444
  * @return {this} this
203
445
  */
204
446
  withQuery(nameRelation: string, callback: Function): this;
205
447
  /**
206
448
  *
207
- * Use relations in registry of model retrun result of relation query
449
+ * Use relations in registry of model return result of relation query
208
450
  * @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
451
+ * @example
452
+ * import { Model } from 'tspace-mysql'
453
+ * class User extends Model {
454
+ * constructor(){
455
+ * super()
456
+ * this.hasMany({ name : 'posts' , model : Post })
457
+ * }
458
+ * }
459
+ *
460
+ * class Post extends Model {
461
+ * constructor(){
462
+ * super()
463
+ * this.hasMany({ name : 'comments' , model : Comment })
464
+ * this.belongsTo({ name : 'user' , model : User })
465
+ * }
466
+ * }
467
+ * // use with for results of relationship
468
+ * await new User().relations('posts').findMany()
209
469
  * @return {this} this
210
470
  */
211
471
  relations(...nameRelations: Array<string>): this;
@@ -213,7 +473,25 @@ declare class Model extends AbstractModel {
213
473
  *
214
474
  * Use relations in registry of model return only exists result of relation query
215
475
  * @param {...string} nameRelations if data exists return blank
216
- * @return {this}
476
+ * @example
477
+ * import { Model } from 'tspace-mysql'
478
+ * class User extends Model {
479
+ * constructor(){
480
+ * super()
481
+ * this.hasMany({ name : 'posts' , model : Post })
482
+ * }
483
+ * }
484
+ *
485
+ * class Post extends Model {
486
+ * constructor(){
487
+ * super()
488
+ * this.hasMany({ name : 'comments' , model : Comment })
489
+ * this.belongsTo({ name : 'user' , model : User })
490
+ * }
491
+ * }
492
+ * // use with for results of relationship if relations is exists
493
+ * await new User().relationsExists('posts').findMany()
494
+ * @return {this} this
217
495
  */
218
496
  relationsExists(...nameRelations: Array<string>): this;
219
497
  /**
@@ -221,6 +499,45 @@ declare class Model extends AbstractModel {
221
499
  * Use relation '${name}' registry of model return callback this query model
222
500
  * @param {string} nameRelation name relation in registry in your model
223
501
  * @param {function} callback query callback
502
+ * @example
503
+ * import { Model } from 'tspace-mysql'
504
+ * class User extends Model {
505
+ * constructor(){
506
+ * super()
507
+ * this.hasMany({ name : 'posts' , model : Post })
508
+ * }
509
+ * }
510
+ *
511
+ * class Post extends Model {
512
+ * constructor(){
513
+ * super()
514
+ * this.hasMany({ name : 'comments' , model : Comment })
515
+ * this.belongsTo({ name : 'user' , model : User })
516
+ * }
517
+ * }
518
+ *
519
+ * class Comment extends Model {
520
+ * constructor(){
521
+ * super()
522
+ * this.hasMany({ name : 'users' , model : User })
523
+ * this.belongsTo({ name : 'post' , model : Post })
524
+ * }
525
+ * }
526
+ *
527
+ * await new User().with('posts')
528
+ * .relationQuery('posts', (query : Post) => {
529
+ * return query.with('comments','user')
530
+ * .relationQuery('comments', (query : Comment) => {
531
+ * return query.with('user','post')
532
+ * })
533
+ * .relationQuery('user', (query : User) => {
534
+ * return query.with('posts').relationQuery('posts',(query : Post)=> {
535
+ * return query.with('comments','user')
536
+ * // relation n, n, ...n
537
+ * })
538
+ * })
539
+ * })
540
+ * .findMany()
224
541
  * @return {this} this
225
542
  */
226
543
  relationQuery(nameRelation: string, callback: Function): this;
@@ -426,12 +743,6 @@ declare class Model extends AbstractModel {
426
743
  * @return {promise<boolean>}
427
744
  */
428
745
  delete(): Promise<boolean>;
429
- /**
430
- *
431
- * force delete data from the database
432
- * @return {promise<boolean>}
433
- */
434
- forceDelete(): Promise<boolean>;
435
746
  /**
436
747
  *
437
748
  * @override Method
@@ -473,18 +784,6 @@ declare class Model extends AbstractModel {
473
784
  * @override Method
474
785
  * @return {promise<array>}
475
786
  */
476
- all(): Promise<Array<any>>;
477
- /**
478
- *
479
- * @override Method
480
- * @return {promise<object | null>}
481
- */
482
- find(id: number): Promise<Record<string, any> | null>;
483
- /**
484
- *
485
- * @override Method
486
- * @return {promise<array>}
487
- */
488
787
  get(): Promise<Array<any>>;
489
788
  /**
490
789
  *
@@ -612,9 +911,7 @@ declare class Model extends AbstractModel {
612
911
  * @param {array<object>} data create multiple data
613
912
  * @return {this} this this
614
913
  */
615
- createMultiple(data: Array<{
616
- [key: string]: any;
617
- }>): this;
914
+ createMultiple(data: Array<Record<string, any>>): this;
618
915
  /**
619
916
  *
620
917
  * insert muliple data into the database
@@ -622,9 +919,7 @@ declare class Model extends AbstractModel {
622
919
  * @param {array<object>} data create multiple data
623
920
  * @return {this} this this
624
921
  */
625
- insertMultiple(data: Array<{
626
- [key: string]: any;
627
- }>): this;
922
+ insertMultiple(data: Array<Record<string, any>>): this;
628
923
  /**
629
924
  *
630
925
  * @param {object} data create not exists data
@@ -647,7 +942,7 @@ declare class Model extends AbstractModel {
647
942
  }): this;
648
943
  /**
649
944
  *
650
- * get schema of table
945
+ * get schema from table
651
946
  * @return {this} this this
652
947
  */
653
948
  getSchema(): Promise<any>;
@@ -682,7 +977,6 @@ declare class Model extends AbstractModel {
682
977
  private _relation;
683
978
  private _belongsToMany;
684
979
  private _pagination;
685
- private _result;
686
980
  private _returnEmpty;
687
981
  private _returnResult;
688
982
  private _hiddenColumnModel;
@@ -702,6 +996,7 @@ declare class Model extends AbstractModel {
702
996
  private _handleRelations;
703
997
  private _handleRelationsQuery;
704
998
  private _validateMethod;
999
+ private _tryToCreateTable;
705
1000
  private _initialModel;
706
1001
  }
707
1002
  export { Model };