tspace-mysql 1.3.3 → 1.3.5

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,10 @@ 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
- /**
144
- * Assign ignore delete_at in model
145
- * @param {boolean} condition
146
- * @return {this} this
147
- */
148
- ignoreSoftDelete(condition?: boolean): this;
271
+ protected queryStatement(sql: string): Promise<Array<any>>;
149
272
  /**
150
273
  * Assign table name
151
274
  * @param {string} table table name
@@ -158,6 +281,12 @@ declare class Model extends AbstractModel {
158
281
  * @return {this} this
159
282
  */
160
283
  disableSoftDelete(condition?: boolean): this;
284
+ /**
285
+ * Assign ignore delete_at in model
286
+ * @param {boolean} condition
287
+ * @return {this} this
288
+ */
289
+ ignoreSoftDelete(condition?: boolean): this;
161
290
  /**
162
291
  * Assign build in function to result of data
163
292
  * @param {object} func
@@ -170,6 +299,24 @@ declare class Model extends AbstractModel {
170
299
  *
171
300
  * Use relations in registry of model return result of relation query
172
301
  * @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
302
+ * @example
303
+ * import { Model } from 'tspace-mysql'
304
+ * class User extends Model {
305
+ * constructor(){
306
+ * super()
307
+ * this.hasMany({ name : 'posts' , model : Post })
308
+ * }
309
+ * }
310
+ *
311
+ * class Post extends Model {
312
+ * constructor(){
313
+ * super()
314
+ * this.hasMany({ name : 'comments' , model : Comment })
315
+ * this.belongsTo({ name : 'user' , model : User })
316
+ * }
317
+ * }
318
+ * // use with for results of relationship
319
+ * await new User().with('posts').findMany()
173
320
  * @return {this} this
174
321
  */
175
322
  with(...nameRelations: Array<string>): this;
@@ -184,6 +331,24 @@ declare class Model extends AbstractModel {
184
331
  *
185
332
  * Use relations in registry of model return only exists result of relation query
186
333
  * @param {...string} nameRelations if data exists return blank
334
+ * @example
335
+ * import { Model } from 'tspace-mysql'
336
+ * class User extends Model {
337
+ * constructor(){
338
+ * super()
339
+ * this.hasMany({ name : 'posts' , model : Post })
340
+ * }
341
+ * }
342
+ *
343
+ * class Post extends Model {
344
+ * constructor(){
345
+ * super()
346
+ * this.hasMany({ name : 'comments' , model : Comment })
347
+ * this.belongsTo({ name : 'user' , model : User })
348
+ * }
349
+ * }
350
+ * // use with for results of relationship if relations is exists
351
+ * await new User().withExists('posts').findMany()
187
352
  * @return {this} this
188
353
  */
189
354
  withExists(...nameRelations: Array<string>): this;
@@ -191,6 +356,24 @@ declare class Model extends AbstractModel {
191
356
  *
192
357
  * Use relations in registry of model return only exists result of relation query
193
358
  * @param {...string} nameRelations if data exists return blank
359
+ * @example
360
+ * import { Model } from 'tspace-mysql'
361
+ * class User extends Model {
362
+ * constructor(){
363
+ * super()
364
+ * this.hasMany({ name : 'posts' , model : Post })
365
+ * }
366
+ * }
367
+ *
368
+ * class Post extends Model {
369
+ * constructor(){
370
+ * super()
371
+ * this.hasMany({ name : 'comments' , model : Comment })
372
+ * this.belongsTo({ name : 'user' , model : User })
373
+ * }
374
+ * }
375
+ * // use with for results of relationship if relations is exists
376
+ * await new User().has('posts').findMany()
194
377
  * @return {this} this
195
378
  */
196
379
  has(...nameRelations: Array<string>): this;
@@ -199,13 +382,70 @@ declare class Model extends AbstractModel {
199
382
  * Use relation '${name}' registry of model return callback this query model
200
383
  * @param {string} nameRelation name relation in registry in your model
201
384
  * @param {function} callback query callback
385
+ * @example
386
+ * import { Model } from 'tspace-mysql'
387
+ * class User extends Model {
388
+ * constructor(){
389
+ * super()
390
+ * this.hasMany({ name : 'posts' , model : Post })
391
+ * }
392
+ * }
393
+ *
394
+ * class Post extends Model {
395
+ * constructor(){
396
+ * super()
397
+ * this.hasMany({ name : 'comments' , model : Comment })
398
+ * this.belongsTo({ name : 'user' , model : User })
399
+ * }
400
+ * }
401
+ *
402
+ * class Comment extends Model {
403
+ * constructor(){
404
+ * super()
405
+ * this.hasMany({ name : 'users' , model : User })
406
+ * this.belongsTo({ name : 'post' , model : Post })
407
+ * }
408
+ * }
409
+ *
410
+ * await new User().with('posts')
411
+ * .withQuery('posts', (query : Post) => {
412
+ * return query.with('comments','user')
413
+ * .withQuery('comments', (query : Comment) => {
414
+ * return query.with('user','post')
415
+ * })
416
+ * .withQuery('user', (query : User) => {
417
+ * return query.with('posts').withQuery('posts',(query : Post)=> {
418
+ * return query.with('comments','user')
419
+ * // relation n, n, ...n
420
+ * })
421
+ * })
422
+ * })
423
+ * .findMany()
202
424
  * @return {this} this
203
425
  */
204
426
  withQuery(nameRelation: string, callback: Function): this;
205
427
  /**
206
428
  *
207
- * Use relations in registry of model retrun result of relation query
429
+ * Use relations in registry of model return result of relation query
208
430
  * @param {...string} nameRelations ...name registry in models using (hasOne , hasMany , belongsTo , belongsToMany)
431
+ * @example
432
+ * import { Model } from 'tspace-mysql'
433
+ * class User extends Model {
434
+ * constructor(){
435
+ * super()
436
+ * this.hasMany({ name : 'posts' , model : Post })
437
+ * }
438
+ * }
439
+ *
440
+ * class Post extends Model {
441
+ * constructor(){
442
+ * super()
443
+ * this.hasMany({ name : 'comments' , model : Comment })
444
+ * this.belongsTo({ name : 'user' , model : User })
445
+ * }
446
+ * }
447
+ * // use with for results of relationship
448
+ * await new User().relations('posts').findMany()
209
449
  * @return {this} this
210
450
  */
211
451
  relations(...nameRelations: Array<string>): this;
@@ -213,7 +453,25 @@ declare class Model extends AbstractModel {
213
453
  *
214
454
  * Use relations in registry of model return only exists result of relation query
215
455
  * @param {...string} nameRelations if data exists return blank
216
- * @return {this}
456
+ * @example
457
+ * import { Model } from 'tspace-mysql'
458
+ * class User extends Model {
459
+ * constructor(){
460
+ * super()
461
+ * this.hasMany({ name : 'posts' , model : Post })
462
+ * }
463
+ * }
464
+ *
465
+ * class Post extends Model {
466
+ * constructor(){
467
+ * super()
468
+ * this.hasMany({ name : 'comments' , model : Comment })
469
+ * this.belongsTo({ name : 'user' , model : User })
470
+ * }
471
+ * }
472
+ * // use with for results of relationship if relations is exists
473
+ * await new User().relationsExists('posts').findMany()
474
+ * @return {this} this
217
475
  */
218
476
  relationsExists(...nameRelations: Array<string>): this;
219
477
  /**
@@ -221,6 +479,45 @@ declare class Model extends AbstractModel {
221
479
  * Use relation '${name}' registry of model return callback this query model
222
480
  * @param {string} nameRelation name relation in registry in your model
223
481
  * @param {function} callback query callback
482
+ * @example
483
+ * import { Model } from 'tspace-mysql'
484
+ * class User extends Model {
485
+ * constructor(){
486
+ * super()
487
+ * this.hasMany({ name : 'posts' , model : Post })
488
+ * }
489
+ * }
490
+ *
491
+ * class Post extends Model {
492
+ * constructor(){
493
+ * super()
494
+ * this.hasMany({ name : 'comments' , model : Comment })
495
+ * this.belongsTo({ name : 'user' , model : User })
496
+ * }
497
+ * }
498
+ *
499
+ * class Comment extends Model {
500
+ * constructor(){
501
+ * super()
502
+ * this.hasMany({ name : 'users' , model : User })
503
+ * this.belongsTo({ name : 'post' , model : Post })
504
+ * }
505
+ * }
506
+ *
507
+ * await new User().with('posts')
508
+ * .relationQuery('posts', (query : Post) => {
509
+ * return query.with('comments','user')
510
+ * .relationQuery('comments', (query : Comment) => {
511
+ * return query.with('user','post')
512
+ * })
513
+ * .relationQuery('user', (query : User) => {
514
+ * return query.with('posts').relationQuery('posts',(query : Post)=> {
515
+ * return query.with('comments','user')
516
+ * // relation n, n, ...n
517
+ * })
518
+ * })
519
+ * })
520
+ * .findMany()
224
521
  * @return {this} this
225
522
  */
226
523
  relationQuery(nameRelation: string, callback: Function): this;
@@ -426,12 +723,6 @@ declare class Model extends AbstractModel {
426
723
  * @return {promise<boolean>}
427
724
  */
428
725
  delete(): Promise<boolean>;
429
- /**
430
- *
431
- * force delete data from the database
432
- * @return {promise<boolean>}
433
- */
434
- forceDelete(): Promise<boolean>;
435
726
  /**
436
727
  *
437
728
  * @override Method
@@ -473,18 +764,6 @@ declare class Model extends AbstractModel {
473
764
  * @override Method
474
765
  * @return {promise<array>}
475
766
  */
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
767
  get(): Promise<Array<any>>;
489
768
  /**
490
769
  *
@@ -614,7 +893,9 @@ declare class Model extends AbstractModel {
614
893
  */
615
894
  createMultiple(data: Array<{
616
895
  [key: string]: any;
617
- }>): this;
896
+ }> & {
897
+ length?: never;
898
+ }): this;
618
899
  /**
619
900
  *
620
901
  * insert muliple data into the database
@@ -624,7 +905,9 @@ declare class Model extends AbstractModel {
624
905
  */
625
906
  insertMultiple(data: Array<{
626
907
  [key: string]: any;
627
- }>): this;
908
+ }> & {
909
+ length?: never;
910
+ }): this;
628
911
  /**
629
912
  *
630
913
  * @param {object} data create not exists data
@@ -647,7 +930,7 @@ declare class Model extends AbstractModel {
647
930
  }): this;
648
931
  /**
649
932
  *
650
- * get schema of table
933
+ * get schema from table
651
934
  * @return {this} this this
652
935
  */
653
936
  getSchema(): Promise<any>;
@@ -682,7 +965,6 @@ declare class Model extends AbstractModel {
682
965
  private _relation;
683
966
  private _belongsToMany;
684
967
  private _pagination;
685
- private _result;
686
968
  private _returnEmpty;
687
969
  private _returnResult;
688
970
  private _hiddenColumnModel;