tspace-mysql 1.5.1 → 1.5.2

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
@@ -2039,6 +2039,7 @@ import { User } from './User.ts'
2039
2039
  const schemaPhone = {
2040
2040
  id :new Blueprint().int().notNull().primary().autoIncrement(),
2041
2041
  uuid :new Blueprint().varchar(50).null(),
2042
+ userId : new Blueprint().int().notNull(),
2042
2043
  number :new Blueprint().varchar(50).notNull(),
2043
2044
  createdAt :new Blueprint().timestamp().null(),
2044
2045
  updatedAt :new Blueprint().timestamp().null()
@@ -2179,11 +2180,8 @@ const users = await new User().where('idx',1).delete() ❌
2179
2180
  ### Safety Relationships
2180
2181
 
2181
2182
  ```js
2182
-
2183
- import { User , schemaUserType } from './User.ts'
2184
- import { Phone, schemaPhoneType } from './Phone.ts'
2185
-
2186
- +--------------------------------------------------------------------------+
2183
+ import { User } from './User.ts'
2184
+ import { Phone } from './Phone.ts'
2187
2185
  // Case #1 : Relationship with 2 relations 'phone' and 'phones'
2188
2186
  const users = await new User()
2189
2187
  .relations('phone','phones')
@@ -2225,7 +2223,7 @@ for(const user of users) {
2225
2223
 
2226
2224
  +--------------------------------------------------------------------------+
2227
2225
 
2228
- // Case #2 : Relationship with 2 relations 'phone' and 'phones' and nested relations to 'user'
2226
+ // Case #2 : There is a relationship between two entities, 'phone' and 'phones', both of which are related to the 'user' entity through nested relations
2229
2227
  const users = await new User()
2230
2228
  .relations('phone','phones')
2231
2229
  .relationQuery('phone' , (query : Phone) => query.relations('user'))
@@ -2272,6 +2270,110 @@ for(const user of users) {
2272
2270
  }
2273
2271
 
2274
2272
  +--------------------------------------------------------------------------+
2273
+ // If you don't want to set types for every returning method such as 'findOne', 'findMany', and so on...
2274
+
2275
+ import { Model , Blueprint , SchemaType , RelationType } from 'tspace-mysql'
2276
+ import { Phone , SchemaPhoneType } from '../Phone'
2277
+
2278
+ const schemaUser = {
2279
+ id :new Blueprint().int().notNull().primary().autoIncrement(),
2280
+ uuid :new Blueprint().varchar(50).null(),
2281
+ email :new Blueprint().varchar(50).null(),
2282
+ name :new Blueprint().varchar(255).null(),
2283
+ username : new Blueprint().varchar(255).null(),
2284
+ password : new Blueprint().varchar(255).null(),
2285
+ createdAt :new Blueprint().timestamp().null(),
2286
+ updatedAt :new Blueprint().timestamp().null()
2287
+ }
2288
+
2289
+ type SchemaUserType = SchemaType<typeof schemaUser>
2290
+
2291
+ type RelationUserType = RelationType<{
2292
+ phones : SchemaPhoneType[]
2293
+ phone : SchemaPhoneType
2294
+ }>
2295
+
2296
+ class User extends Model<SchemaUserType, RelationUserType> {
2297
+ constructor() {
2298
+ super()
2299
+ this.useSchema(schemaUser)
2300
+ this.hasOne({ model : Phone, name : 'phonex' }) ❌
2301
+ this.hasMany({ model : Phone, name : 'phonesx' }) ❌
2302
+ this.hasOne({ model : Phone, name : 'phone' }) ✅
2303
+ this.hasMany({ model : Phone, name : 'phones' }) ✅
2304
+ }
2305
+ }
2306
+
2307
+ export { User , SchemaUserType }
2308
+ export default User
2309
+
2310
+ +--------------------------------------------------------------------------+
2311
+
2312
+ // in file Phone.ts
2313
+ import { Model , Blueprint , SchemaType , RelationType} from 'tspace-mysql'
2314
+ import { User } from './User.ts'
2315
+ const schemaPhone = {
2316
+ id :new Blueprint().int().notNull().primary().autoIncrement(),
2317
+ uuid :new Blueprint().varchar(50).null(),
2318
+ userId : new Blueprint().int().notNull(),
2319
+ number :new Blueprint().varchar(50).notNull(),
2320
+ createdAt :new Blueprint().timestamp().null(),
2321
+ updatedAt :new Blueprint().timestamp().null()
2322
+ }
2323
+
2324
+ type SchemaPhoneType = SchemaType<typeof schemaPhone>
2325
+
2326
+ type RelationPhoneType = RelationType<{
2327
+ user : SchemaPhoneType[]
2328
+ }>
2329
+
2330
+ class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
2331
+ constructor() {
2332
+ super()
2333
+ this.useSchema(schemaPhone)
2334
+ this.useBelongsTo({ model : User, name : 'userx'}) ❌
2335
+ this.useBelongsTo({ model : User, name : 'user'}) ✅
2336
+ }
2337
+ }
2338
+
2339
+ export { Phone , SchemaPhoneType }
2340
+ export default Phone
2341
+
2342
+ +--------------------------------------------------------------------------+
2343
+
2344
+ const users = await new User()
2345
+ .relations('phonex','phonesx') ❌
2346
+ .relationQuery('phonex' ❌ , (query : Phone) => query.relations('user')) ✅
2347
+ .relationQuery('phonesx' ❌ , (query : Phone) => query.relations('user')) ✅
2348
+ .findMany()
2349
+
2350
+ const users = await new User()
2351
+ .relations('phone','phones') ✅
2352
+ .relationQuery('phonex' ❌ , (query : Phone) => query.relations('user')) ✅
2353
+ .relationQuery('phonesx' ❌ , (query : Phone) => query.relations('user')) ✅
2354
+ .findMany()
2355
+
2356
+ const users = await new User()
2357
+ .relations('phone','phones')
2358
+ .relationQuery('phone' , (query : Phone) => query.relations('userx')) ❌
2359
+ .relationQuery('phones' , (query : Phone) => query.relations('userx')) ❌
2360
+ .findMany()
2361
+
2362
+ const users = await new User()
2363
+ .relations('phone','phones') ✅
2364
+ .relationQuery('phone' ✅ , (query : Phone) => query.relations('user')) ✅
2365
+ .relationQuery('phones'✅ , (query : Phone) => query.relations('user')) ✅
2366
+ .findMany()
2367
+
2368
+ for(const user of users) {
2369
+ user.phone.user ✅
2370
+ user.phone.user.id ✅
2371
+ user.phone.userx ❌
2372
+ user.phone.user.idx ❌
2373
+ user.phones.map(phone =>phone.user.id) ✅
2374
+ user.phones.map(phone =>phone.user.idx) ❌
2375
+ }
2376
+
2275
2377
  ```
2276
2378
 
2277
2379
  ## Blueprint
@@ -1,6 +1,6 @@
1
1
  import Model from "./core/Model";
2
- export interface Relation {
3
- name: string;
2
+ export interface Relation<K = any> {
3
+ name: K extends void ? never : K;
4
4
  model: new () => Model;
5
5
  as?: string | undefined;
6
6
  localKey?: string | undefined;
@@ -16,8 +16,8 @@ export interface Relation {
16
16
  oldVersion?: boolean | undefined;
17
17
  modelPivot?: new () => Model | undefined;
18
18
  }
19
- export interface RelationQuery {
20
- name?: string;
19
+ export interface RelationQuery<K = any> {
20
+ name?: K extends void ? never : K;
21
21
  model: new () => Model;
22
22
  as?: string | undefined;
23
23
  localKey?: string | undefined;
@@ -2,7 +2,8 @@ import { Pattern, Relation, RelationQuery, ValidateSchema } from '../../Interfac
2
2
  import { Blueprint } from '../Blueprint';
3
3
  import { Builder } from '../Builder';
4
4
  import { RelationHandler } from '../Handlers/Relation';
5
- declare abstract class AbstractModel<T> extends Builder {
5
+ import { Model } from '../Model';
6
+ declare abstract class AbstractModel<T, R> extends Builder {
6
7
  protected $relation: RelationHandler | undefined;
7
8
  protected $schema: Record<string, Blueprint> | undefined;
8
9
  protected $validateSchema: ValidateSchema | undefined;
@@ -48,7 +49,7 @@ declare abstract class AbstractModel<T> extends Builder {
48
49
  protected abstract hasMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
49
50
  protected abstract belongsTo({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
50
51
  protected abstract belongsToMany({ name, model, localKey, foreignKey, freezeTable, as }: Relation): this;
51
- protected abstract buildMethodRelation(name: string, callback?: Function): this;
52
+ protected abstract buildMethodRelation<K extends keyof R>(name: K, callback?: Function): this;
52
53
  protected abstract hasOneBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
53
54
  protected abstract hasManyBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
54
55
  protected abstract belongsToBuilder({ name, model, localKey, foreignKey, freezeTable, as }: RelationQuery, callback: Function): this;
@@ -59,17 +60,17 @@ declare abstract class AbstractModel<T> extends Builder {
59
60
  abstract onlyTrashed(): this;
60
61
  abstract trashed(): this;
61
62
  abstract restore(): Promise<any[]>;
62
- abstract with(...nameRelations: string[]): this;
63
- abstract withQuery(nameRelations: string, callback: Function): this;
64
- abstract withExists(...nameRelations: string[]): this;
65
- abstract withTrashed(...nameRelations: string[]): this;
66
- abstract withAll(...nameRelations: string[]): this;
67
- abstract has(...nameRelations: string[]): this;
68
- abstract relations(...nameRelations: string[]): this;
69
- abstract relationQuery(nameRelations: string, callback: Function): this;
70
- abstract relationsExists(...nameRelations: string[]): this;
71
- abstract relationsAll(...nameRelations: string[]): this;
72
- abstract relationsTrashed(...nameRelations: string[]): this;
63
+ abstract with<K extends keyof R>(...nameRelations: K[]): this;
64
+ abstract withQuery<K extends keyof R, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
65
+ abstract withExists<K extends keyof R>(...nameRelations: K[]): this;
66
+ abstract withTrashed<K extends keyof R>(...nameRelations: K[]): this;
67
+ abstract withAll<K extends keyof R>(...nameRelations: K[]): this;
68
+ abstract has<K extends keyof R>(...nameRelations: K[]): this;
69
+ abstract relations<K extends keyof R>(...nameRelations: K[]): this;
70
+ abstract relationQuery<K extends keyof R, TModel extends Model>(nameRelations: K, callback: (query: TModel) => TModel): this;
71
+ abstract relationsExists<K extends keyof R>(...nameRelations: K[]): this;
72
+ abstract relationsAll<K extends keyof R>(...nameRelations: K[]): this;
73
+ abstract relationsTrashed<K extends keyof R>(...nameRelations: K[]): this;
73
74
  }
74
75
  export { AbstractModel };
75
76
  export default AbstractModel;
@@ -2156,8 +2156,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2156
2156
  * @param {number} value
2157
2157
  * @return {promise<any>}
2158
2158
  */
2159
- increment(column = 'id', value = 1) {
2160
- return __awaiter(this, void 0, void 0, function* () {
2159
+ increment() {
2160
+ return __awaiter(this, arguments, void 0, function* (column = 'id', value = 1) {
2161
2161
  const query = `${this.$constants('SET')} ${column} = ${column} + ${value}`;
2162
2162
  this.$state.set('UPDATE', [
2163
2163
  `${this.$constants('UPDATE')}`,
@@ -2174,8 +2174,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2174
2174
  * @param {number} value
2175
2175
  * @return {promise<any>}
2176
2176
  */
2177
- decrement(column = 'id', value = 1) {
2178
- return __awaiter(this, void 0, void 0, function* () {
2177
+ decrement() {
2178
+ return __awaiter(this, arguments, void 0, function* (column = 'id', value = 1) {
2179
2179
  const query = `${this.$constants('SET')} ${column} = ${column} - ${value}`;
2180
2180
  this.$state.set('UPDATE', [
2181
2181
  `${this.$constants('UPDATE')}`,
@@ -2237,8 +2237,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2237
2237
  * @return {promise<Pagination>}
2238
2238
  */
2239
2239
  pagination(paginationOptions) {
2240
- var _a, _b, _c;
2241
2240
  return __awaiter(this, void 0, void 0, function* () {
2241
+ var _a, _b, _c;
2242
2242
  let limit = 15;
2243
2243
  let page = 1;
2244
2244
  if (paginationOptions != null) {
@@ -2328,8 +2328,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2328
2328
  * @return {promise<object | null>}
2329
2329
  */
2330
2330
  first(cb) {
2331
- var _a, _b;
2332
2331
  return __awaiter(this, void 0, void 0, function* () {
2332
+ var _a, _b;
2333
2333
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2334
2334
  this.select(...yield this.exceptColumns());
2335
2335
  this.limit(1);
@@ -2388,8 +2388,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2388
2388
  * @return {promise<object | Error>}
2389
2389
  */
2390
2390
  firstOrError(message, options) {
2391
- var _a, _b;
2392
2391
  return __awaiter(this, void 0, void 0, function* () {
2392
+ var _a, _b;
2393
2393
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2394
2394
  this.select(...yield this.exceptColumns());
2395
2395
  let sql = this._queryBuilder().select();
@@ -2448,8 +2448,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2448
2448
  * @return {promise<any[]>}
2449
2449
  */
2450
2450
  get(cb) {
2451
- var _a, _b;
2452
2451
  return __awaiter(this, void 0, void 0, function* () {
2452
+ var _a, _b;
2453
2453
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2454
2454
  this.select(...yield this.exceptColumns());
2455
2455
  let sql = this._queryBuilder().select();
@@ -2533,8 +2533,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2533
2533
  * @param {string=} column [column=id]
2534
2534
  * @return {promise<Array>}
2535
2535
  */
2536
- toArray(column = 'id') {
2537
- return __awaiter(this, void 0, void 0, function* () {
2536
+ toArray() {
2537
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2538
2538
  this.selectRaw(`${this.bindColumn(column)}`);
2539
2539
  const sql = this._queryBuilder().select();
2540
2540
  const result = yield this._queryStatement(sql);
@@ -2549,8 +2549,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2549
2549
  * @return {promise<boolean>}
2550
2550
  */
2551
2551
  exists() {
2552
- var _a;
2553
2552
  return __awaiter(this, void 0, void 0, function* () {
2553
+ var _a;
2554
2554
  this.limit(1);
2555
2555
  this.selectRaw('1');
2556
2556
  const sql = this._queryBuilder().select();
@@ -2570,8 +2570,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2570
2570
  * @param {string=} column [column=id]
2571
2571
  * @return {promise<number>}
2572
2572
  */
2573
- count(column = 'id') {
2574
- return __awaiter(this, void 0, void 0, function* () {
2573
+ count() {
2574
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2575
2575
  const distinct = this.$state.get('DISTINCT');
2576
2576
  column = distinct
2577
2577
  ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}`
@@ -2589,8 +2589,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2589
2589
  * @param {string=} column [column=id]
2590
2590
  * @return {promise<number>}
2591
2591
  */
2592
- avg(column = 'id') {
2593
- return __awaiter(this, void 0, void 0, function* () {
2592
+ avg() {
2593
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2594
2594
  const distinct = this.$state.get('DISTINCT');
2595
2595
  column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2596
2596
  this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} \`aggregate\``);
@@ -2606,8 +2606,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2606
2606
  * @param {string=} column [column=id]
2607
2607
  * @return {promise<number>}
2608
2608
  */
2609
- sum(column = 'id') {
2610
- return __awaiter(this, void 0, void 0, function* () {
2609
+ sum() {
2610
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2611
2611
  const distinct = this.$state.get('DISTINCT');
2612
2612
  column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2613
2613
  this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`aggregate\``);
@@ -2623,9 +2623,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2623
2623
  * @param {string=} column [column=id]
2624
2624
  * @return {promise<number>}
2625
2625
  */
2626
- max(column = 'id') {
2627
- var _a;
2628
- return __awaiter(this, void 0, void 0, function* () {
2626
+ max() {
2627
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2628
+ var _a;
2629
2629
  const distinct = this.$state.get('DISTINCT');
2630
2630
  column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2631
2631
  this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`aggregate\``);
@@ -2641,9 +2641,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2641
2641
  * @param {string=} column [column=id]
2642
2642
  * @return {promise<number>}
2643
2643
  */
2644
- min(column = 'id') {
2645
- var _a;
2646
- return __awaiter(this, void 0, void 0, function* () {
2644
+ min() {
2645
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2646
+ var _a;
2647
2647
  const distinct = this.$state.get('DISTINCT');
2648
2648
  column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2649
2649
  this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`aggregate\``);
@@ -2659,8 +2659,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2659
2659
  * @return {promise<boolean>}
2660
2660
  */
2661
2661
  delete() {
2662
- var _a;
2663
2662
  return __awaiter(this, void 0, void 0, function* () {
2663
+ var _a;
2664
2664
  if (!this.$state.get('where').length) {
2665
2665
  throw new Error("can't delete without where condition");
2666
2666
  }
@@ -2683,8 +2683,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2683
2683
  * @return {promise<boolean>}
2684
2684
  */
2685
2685
  deleteMany() {
2686
- var _a;
2687
2686
  return __awaiter(this, void 0, void 0, function* () {
2687
+ var _a;
2688
2688
  if (!this.$state.get('where').length) {
2689
2689
  throw new Error("can't delete without where condition");
2690
2690
  }
@@ -2709,8 +2709,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2709
2709
  * @return {promise<boolean>}
2710
2710
  */
2711
2711
  forceDelete() {
2712
- var _a, _b;
2713
2712
  return __awaiter(this, void 0, void 0, function* () {
2713
+ var _a, _b;
2714
2714
  this.$state.set('DELETE', [
2715
2715
  `${this.$constants('DELETE')}`,
2716
2716
  `${this.$state.get('FROM')}`,
@@ -2929,8 +2929,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2929
2929
  * @param {string=} table table name
2930
2930
  * @return {Promise<Array>}
2931
2931
  */
2932
- showColumns(table = this.$state.get('TABLE_NAME')) {
2933
- return __awaiter(this, void 0, void 0, function* () {
2932
+ showColumns() {
2933
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2934
2934
  const sql = [
2935
2935
  `${this.$constants('SHOW')}`,
2936
2936
  `${this.$constants('COLUMNS')}`,
@@ -2948,8 +2948,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2948
2948
  * @param {string=} table [table= current table name]
2949
2949
  * @return {Promise<Array>}
2950
2950
  */
2951
- showSchema(table = this.$state.get('TABLE_NAME')) {
2952
- return __awaiter(this, void 0, void 0, function* () {
2951
+ showSchema() {
2952
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2953
2953
  const sql = [
2954
2954
  `${this.$constants('SHOW')}`,
2955
2955
  `${this.$constants('COLUMNS')}`,
@@ -2989,8 +2989,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2989
2989
  * @param {string=} table [table= current table name]
2990
2990
  * @return {Promise<Array>}
2991
2991
  */
2992
- showSchemas(table = this.$state.get('TABLE_NAME')) {
2993
- return __awaiter(this, void 0, void 0, function* () {
2992
+ showSchemas() {
2993
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2994
2994
  return this.showSchema(table);
2995
2995
  });
2996
2996
  }
@@ -3001,8 +3001,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3001
3001
  * @param {string=} table table name
3002
3002
  * @return {Promise<Array>}
3003
3003
  */
3004
- showValues(table = this.$state.get('TABLE_NAME')) {
3005
- return __awaiter(this, void 0, void 0, function* () {
3004
+ showValues() {
3005
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
3006
3006
  const sql = [
3007
3007
  `${this.$constants('SELECT')}`,
3008
3008
  '*',
@@ -3266,8 +3266,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3266
3266
  return result;
3267
3267
  });
3268
3268
  }
3269
- _actionStatement({ sql, returnId = false }) {
3270
- return __awaiter(this, void 0, void 0, function* () {
3269
+ _actionStatement(_a) {
3270
+ return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false }) {
3271
3271
  if (this.$state.get('DEBUG'))
3272
3272
  this.$utils.consoleDebug(sql);
3273
3273
  if (returnId) {
@@ -3464,8 +3464,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3464
3464
  }
3465
3465
  });
3466
3466
  }
3467
- _update(ignoreWhere = false) {
3468
- return __awaiter(this, void 0, void 0, function* () {
3467
+ _update() {
3468
+ return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
3469
3469
  if (!this.$state.get('where').length && !ignoreWhere)
3470
3470
  throw new Error("can't update without where condition");
3471
3471
  const result = yield this._actionStatement({
@@ -443,15 +443,15 @@ class DB extends AbstractDB_1.AbstractDB {
443
443
  * @return {Promise<boolean>}
444
444
  */
445
445
  cloneDB(database) {
446
- var _a;
447
446
  return __awaiter(this, void 0, void 0, function* () {
447
+ var _a;
448
448
  const db = yield this._queryStatement(`${this.$constants('SHOW_DATABASES')} ${this.$constants('LIKE')} '${database}'`);
449
449
  if (Object.values((_a = db[0]) !== null && _a !== void 0 ? _a : []).length)
450
450
  throw new Error(`This database : '${database}' is already exists`);
451
451
  const tables = yield this.showTables();
452
452
  const backup = yield this._backup({ tables, database });
453
453
  yield this._queryStatement(`${this.$constants('CREATE_DATABASE_NOT_EXISTS')} \`${database}\``);
454
- const creating = ({ table, values }) => __awaiter(this, void 0, void 0, function* () {
454
+ const creating = (_b) => __awaiter(this, [_b], void 0, function* ({ table, values }) {
455
455
  try {
456
456
  yield this._queryStatement(table);
457
457
  if (values != null && values !== '')
@@ -486,8 +486,8 @@ class DB extends AbstractDB_1.AbstractDB {
486
486
  * @property {string} backup.to.password
487
487
  * @return {Promise<void>}
488
488
  */
489
- backup({ database, to }) {
490
- return __awaiter(this, void 0, void 0, function* () {
489
+ backup(_a) {
490
+ return __awaiter(this, arguments, void 0, function* ({ database, to }) {
491
491
  if (to != null && Object.keys(to).length)
492
492
  this.connection(Object.assign(Object.assign({}, to), { database }));
493
493
  return this.cloneDB(database);
@@ -505,8 +505,8 @@ class DB extends AbstractDB_1.AbstractDB {
505
505
  * @property {string} backup.to.password
506
506
  * @return {Promise<void>}
507
507
  */
508
- static backup({ database, to }) {
509
- return __awaiter(this, void 0, void 0, function* () {
508
+ static backup(_a) {
509
+ return __awaiter(this, arguments, void 0, function* ({ database, to }) {
510
510
  return new this().backup({ database, to });
511
511
  });
512
512
  }
@@ -524,9 +524,9 @@ class DB extends AbstractDB_1.AbstractDB {
524
524
  * @property {string} backup.connection.password
525
525
  * @return {Promise<void>}
526
526
  */
527
- backupToFile({ filePath, database, connection }) {
528
- var _a;
529
- return __awaiter(this, void 0, void 0, function* () {
527
+ backupToFile(_a) {
528
+ return __awaiter(this, arguments, void 0, function* ({ filePath, database, connection }) {
529
+ var _b;
530
530
  const tables = yield this.showTables();
531
531
  const backup = (yield this._backup({ tables, database }))
532
532
  .map(b => {
@@ -539,7 +539,7 @@ class DB extends AbstractDB_1.AbstractDB {
539
539
  values: b.values !== '' ? b.values + "\n" : ""
540
540
  };
541
541
  });
542
- if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
542
+ if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
543
543
  this.connection(connection);
544
544
  let sql = [
545
545
  `SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";`,
@@ -572,8 +572,8 @@ class DB extends AbstractDB_1.AbstractDB {
572
572
  * @property {string} backup.connection.password
573
573
  * @return {Promise<void>}
574
574
  */
575
- static backupToFile({ filePath, database, connection }) {
576
- return __awaiter(this, void 0, void 0, function* () {
575
+ static backupToFile(_a) {
576
+ return __awaiter(this, arguments, void 0, function* ({ filePath, database, connection }) {
577
577
  return new this().backupToFile({ filePath, database, connection });
578
578
  });
579
579
  }
@@ -591,10 +591,10 @@ class DB extends AbstractDB_1.AbstractDB {
591
591
  * @property {string} backup.connection.password
592
592
  * @return {Promise<void>}
593
593
  */
594
- backupSchemaToFile({ filePath, database, connection }) {
595
- var _a;
596
- return __awaiter(this, void 0, void 0, function* () {
597
- if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
594
+ backupSchemaToFile(_a) {
595
+ return __awaiter(this, arguments, void 0, function* ({ filePath, database, connection }) {
596
+ var _b;
597
+ if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
598
598
  this.connection(connection);
599
599
  const tables = yield this.showTables();
600
600
  const backup = (yield this._backup({ tables, database }))
@@ -634,8 +634,8 @@ class DB extends AbstractDB_1.AbstractDB {
634
634
  * @property {string} backup.connection.password
635
635
  * @return {Promise<void>}
636
636
  */
637
- static backupSchemaToFile({ filePath, database, connection }) {
638
- return __awaiter(this, void 0, void 0, function* () {
637
+ static backupSchemaToFile(_a) {
638
+ return __awaiter(this, arguments, void 0, function* ({ filePath, database, connection }) {
639
639
  return new this().backupSchemaToFile({ filePath, database, connection });
640
640
  });
641
641
  }
@@ -653,10 +653,10 @@ class DB extends AbstractDB_1.AbstractDB {
653
653
  * @property {string} backup.connection.password
654
654
  * @return {Promise<void>}
655
655
  */
656
- backupTableToFile({ filePath, table, connection }) {
657
- var _a;
658
- return __awaiter(this, void 0, void 0, function* () {
659
- if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
656
+ backupTableToFile(_a) {
657
+ return __awaiter(this, arguments, void 0, function* ({ filePath, table, connection }) {
658
+ var _b;
659
+ if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
660
660
  this.connection(connection);
661
661
  const schemas = yield this.showSchema(table);
662
662
  const createTableSQL = [
@@ -702,8 +702,8 @@ class DB extends AbstractDB_1.AbstractDB {
702
702
  * @property {string} backup.connection.password
703
703
  * @return {Promise<void>}
704
704
  */
705
- static backupTableToFile({ filePath, table, connection }) {
706
- return __awaiter(this, void 0, void 0, function* () {
705
+ static backupTableToFile(_a) {
706
+ return __awaiter(this, arguments, void 0, function* ({ filePath, table, connection }) {
707
707
  return new this().backupTableToFile({ filePath, table, connection });
708
708
  });
709
709
  }
@@ -721,9 +721,9 @@ class DB extends AbstractDB_1.AbstractDB {
721
721
  * @property {string} backup.connection.password
722
722
  * @return {Promise<void>}
723
723
  */
724
- backupTableSchemaToFile({ filePath, table, connection }) {
725
- var _a;
726
- return __awaiter(this, void 0, void 0, function* () {
724
+ backupTableSchemaToFile(_a) {
725
+ return __awaiter(this, arguments, void 0, function* ({ filePath, table, connection }) {
726
+ var _b;
727
727
  const schemas = yield this.showSchema(table);
728
728
  const createTableSQL = [
729
729
  `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
@@ -732,7 +732,7 @@ class DB extends AbstractDB_1.AbstractDB {
732
732
  `${this.$constants('ENGINE')};`,
733
733
  ];
734
734
  const sql = [createTableSQL.join(' ')];
735
- if (connection != null && ((_a = Object.keys(connection)) === null || _a === void 0 ? void 0 : _a.length))
735
+ if (connection != null && ((_b = Object.keys(connection)) === null || _b === void 0 ? void 0 : _b.length))
736
736
  this.connection(connection);
737
737
  fs_1.default.writeFileSync(filePath, (0, sql_formatter_1.format)(sql.join('\n'), {
738
738
  language: 'spark',
@@ -756,13 +756,13 @@ class DB extends AbstractDB_1.AbstractDB {
756
756
  * @property {string} backup.connection.password
757
757
  * @return {Promise<void>}
758
758
  */
759
- static backupTableSchemaToFile({ filePath, table, connection }) {
760
- return __awaiter(this, void 0, void 0, function* () {
759
+ static backupTableSchemaToFile(_a) {
760
+ return __awaiter(this, arguments, void 0, function* ({ filePath, table, connection }) {
761
761
  return new this().backupTableSchemaToFile({ filePath, table, connection });
762
762
  });
763
763
  }
764
- _backup({ tables, database }) {
765
- return __awaiter(this, void 0, void 0, function* () {
764
+ _backup(_a) {
765
+ return __awaiter(this, arguments, void 0, function* ({ tables, database }) {
766
766
  const backup = [];
767
767
  for (const table of tables) {
768
768
  const schemas = yield this.showSchema(table);
@@ -7,8 +7,8 @@ declare class RelationHandler {
7
7
  constructor(model: Model);
8
8
  load(parents: Record<string, any>[], relation: Relation): Promise<any[]>;
9
9
  loadExists(): string;
10
- apply(nameRelations: string[], type: 'all' | 'exists' | 'trashed' | 'count' | 'default'): Relation[];
11
- callback(nameRelation: string, cb: Function): void;
10
+ apply(nameRelations: any[], type: 'all' | 'exists' | 'trashed' | 'count' | 'default'): Relation[];
11
+ callback(nameRelation: any, cb: Function): void;
12
12
  hasOne({ name, as, model, localKey, foreignKey, freezeTable }: Relation): void;
13
13
  hasMany({ name, as, model, localKey, foreignKey, freezeTable }: Relation): void;
14
14
  belongsTo({ name, as, model, localKey, foreignKey, freezeTable }: Relation): void;
@@ -22,8 +22,8 @@ class RelationHandler {
22
22
  this.$logger = this.MODEL["$logger"];
23
23
  }
24
24
  load(parents, relation) {
25
- var _a;
26
25
  return __awaiter(this, void 0, void 0, function* () {
26
+ var _a;
27
27
  const relationIsBelongsToMany = relation.relation === this.$constants('RELATIONSHIP').belongsToMany;
28
28
  if (relationIsBelongsToMany) {
29
29
  return yield this._belongsToMany(parents, relation);
@@ -409,8 +409,8 @@ class RelationHandler {
409
409
  return dataParents;
410
410
  }
411
411
  _belongsToMany(parents, relation) {
412
- var _a, _b;
413
412
  return __awaiter(this, void 0, void 0, function* () {
413
+ var _a, _b;
414
414
  const { name, foreignKey, localKey, pivot, oldVersion, modelPivot } = this._valueInRelation(relation);
415
415
  const localKeyId = parents.map((parent) => {
416
416
  const data = parent[foreignKey];
@@ -11,7 +11,7 @@ import { Relation, Pagination, RelationQuery, ValidateSchema, GlobalSetting } fr
11
11
  * const users = await new User().findMany()
12
12
  * console.log(users)
13
13
  */
14
- declare class Model<TSchemaModel extends Record<string, Blueprint | string | number | Date> = any> extends AbstractModel<TSchemaModel> {
14
+ declare class Model<TSchemaModel extends Record<string, Blueprint | string | number | Date> = any, TRelationModel = any> extends AbstractModel<TSchemaModel, TRelationModel> {
15
15
  constructor();
16
16
  /**
17
17
  * The 'global' method is used setting global variables in models.
@@ -444,7 +444,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
444
444
  * @param {Function} callback query callback
445
445
  * @return {this} this
446
446
  */
447
- protected buildMethodRelation(name: string, callback?: Function): this;
447
+ protected buildMethodRelation<K extends keyof TRelationModel>(name: K, callback?: Function): this;
448
448
  /**
449
449
  *
450
450
  * @override
@@ -639,7 +639,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
639
639
  * await new User().relations('posts').findMany()
640
640
  *
641
641
  */
642
- with(...nameRelations: string[]): this;
642
+ with<K extends keyof TRelationModel>(...nameRelations: K[]): this;
643
643
  /**
644
644
  * The 'withAll' method is used to eager load related (relations) data when retrieving records from a database.
645
645
  *
@@ -648,7 +648,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
648
648
  * @param {...string} nameRelations if data exists return blank
649
649
  * @return {this} this
650
650
  */
651
- withAll(...nameRelations: string[]): this;
651
+ withAll<K extends keyof TRelationModel>(...nameRelations: K[]): this;
652
652
  /**
653
653
  * The 'withAll' method is used to eager load related (relations) data when retrieving records from a database.
654
654
  *
@@ -657,7 +657,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
657
657
  * @param {...string} nameRelations if data exists return blank
658
658
  * @return {this} this
659
659
  */
660
- withCount(...nameRelations: string[]): this;
660
+ withCount<K extends keyof TRelationModel>(...nameRelations: K[]): this;
661
661
  /**
662
662
  * The 'withTrashed' method is used to eager load related (relations) data when retrieving records from a database.
663
663
  *
@@ -666,7 +666,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
666
666
  * @param {...string} nameRelations if data exists return blank
667
667
  * @return {this} this
668
668
  */
669
- withTrashed(...nameRelations: string[]): this;
669
+ withTrashed<K extends keyof TRelationModel>(...nameRelations: K[]): this;
670
670
  /**
671
671
  * The 'withExists' method is used to eager load related (relations) data when retrieving records from a database.
672
672
  *
@@ -693,7 +693,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
693
693
  * // use with for results of relationship if relations is exists
694
694
  * await new User().relationsExists('posts').findMany()
695
695
  */
696
- withExists(...nameRelations: string[]): this;
696
+ withExists<K extends keyof TRelationModel>(...nameRelations: K[]): this;
697
697
  /**
698
698
  *
699
699
  * Use relations in registry of model return only exists result of relation query
@@ -718,7 +718,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
718
718
  * await new User().has('posts').findMany()
719
719
  * @return {this} this
720
720
  */
721
- has(...nameRelations: string[]): this;
721
+ has<K extends keyof TRelationModel>(...nameRelations: K[]): this;
722
722
  /**
723
723
  *
724
724
  * The 'withQuery' method is particularly useful when you want to filter or add conditions records based on related data.
@@ -767,7 +767,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
767
767
  * .findMany()
768
768
  * @return {this} this
769
769
  */
770
- withQuery<T extends Model>(nameRelation: string, callback: (query: T) => T): this;
770
+ withQuery<K extends keyof TRelationModel, TModel extends Model>(nameRelation: K, callback: (query: TModel) => TModel): this;
771
771
  /**
772
772
  *
773
773
  * Use relations in registry of model return result of relation query
@@ -792,7 +792,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
792
792
  * await new User().relations('posts').findMany()
793
793
  * @return {this} this
794
794
  */
795
- relations(...nameRelations: string[]): this;
795
+ relations(...nameRelations: any[]): this;
796
796
  /**
797
797
  *
798
798
  * Use relations in registry of model return only exists result of relation query
@@ -817,7 +817,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
817
817
  * await new User().relationsExists('posts').findMany()
818
818
  * @return {this} this
819
819
  */
820
- relationsExists(...nameRelations: string[]): this;
820
+ relationsExists<K extends keyof TRelationModel>(...nameRelations: K[]): this;
821
821
  /**
822
822
  *
823
823
  * Use relation '${name}' registry of model return callback this query model
@@ -864,21 +864,21 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
864
864
  * .findMany()
865
865
  * @return {this} this
866
866
  */
867
- relationQuery<T extends Model>(nameRelation: string, callback: (query: T) => T): this;
867
+ relationQuery<K extends keyof TRelationModel, T extends Model>(nameRelation: K, callback: (query: T) => T): this;
868
868
  /**
869
869
  *
870
870
  * Use relations in registry of model return ignore soft deleted
871
871
  * @param {...string} nameRelations if data exists return blank
872
872
  * @return {this} this
873
873
  */
874
- relationsAll(...nameRelations: string[]): this;
874
+ relationsAll<K extends keyof TRelationModel>(...nameRelations: K[]): this;
875
875
  /**
876
876
  *
877
877
  * Use relations in registry of model return only in trash (soft delete)
878
878
  * @param {...string} nameRelations if data exists return blank
879
879
  * @return {this} this
880
880
  */
881
- relationsTrashed(...nameRelations: string[]): this;
881
+ relationsTrashed<K extends keyof TRelationModel>(...nameRelations: K[]): this;
882
882
  /**
883
883
  * The 'hasOne' relationship defines a one-to-one relationship between two database tables.
884
884
  *
@@ -895,7 +895,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
895
895
  * @property {string} relation.freezeTable
896
896
  * @return {this} this
897
897
  */
898
- protected hasOne({ name, as, model, localKey, foreignKey, freezeTable }: Relation): this;
898
+ protected hasOne<K extends keyof TRelationModel>({ name, as, model, localKey, foreignKey, freezeTable }: Relation<K>): this;
899
899
  /**
900
900
  * The 'hasMany' relationship defines a one-to-many relationship between two database tables.
901
901
  *
@@ -912,7 +912,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
912
912
  * @property {string} relation.freezeTable
913
913
  * @return {this} this
914
914
  */
915
- protected hasMany({ name, as, model, localKey, foreignKey, freezeTable }: Relation): this;
915
+ protected hasMany<K extends keyof TRelationModel>({ name, as, model, localKey, foreignKey, freezeTable }: Relation<K>): this;
916
916
  /**
917
917
  * The 'belongsTo' relationship defines a one-to-one or many-to-one relationship between two database tables.
918
918
  *
@@ -929,7 +929,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
929
929
  * @property {string} relation.freezeTable
930
930
  * @return {this} this
931
931
  */
932
- protected belongsTo({ name, as, model, localKey, foreignKey, freezeTable }: Relation): this;
932
+ protected belongsTo<K extends keyof TRelationModel>({ name, as, model, localKey, foreignKey, freezeTable }: Relation<K>): this;
933
933
  /**
934
934
  * The 'belongsToMany' relationship defines a many-to-many relationship between two database tables.
935
935
  *
@@ -949,7 +949,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
949
949
  * @property {class?} relation.modelPivot model for pivot
950
950
  * @return {this} this
951
951
  */
952
- protected belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }: Relation): this;
952
+ protected belongsToMany<K extends keyof TRelationModel>({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }: Relation<K>): this;
953
953
  /**
954
954
  * The 'hasOneBuilder' method is useful for creating 'hasOne' relationship to function
955
955
  *
@@ -1267,7 +1267,7 @@ declare class Model<TSchemaModel extends Record<string, Blueprint | string | num
1267
1267
  * @param {Function?} cb callback function return query sql
1268
1268
  * @return {promise<Record<string,any> | null>} Record | null
1269
1269
  */
1270
- first<K>(cb?: Function): Promise<Partial<TSchemaModel> & K | null>;
1270
+ first<K>(cb?: Function): Promise<Partial<TSchemaModel> & K & TRelationModel | null>;
1271
1271
  /**
1272
1272
  * @override
1273
1273
  * @param {Function?} cb callback function return query sql
@@ -631,7 +631,7 @@ class Model extends AbstractModel_1.AbstractModel {
631
631
  var _a, _b;
632
632
  this.relations(name);
633
633
  const relation = this.$state.get('RELATIONS').find((data) => data.name === name);
634
- this._assertError(relation == null, `This Relation "${name}" not be register in Model "${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}"`);
634
+ this._assertError(relation == null, `This Relation "${String(name)}" not be register in Model "${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}"`);
635
635
  const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
636
636
  this._assertError(!relationHasExists, `Unknown Relationship in [${this.$constants('RELATIONSHIP')}] !`);
637
637
  if (callback == null) {
@@ -1005,8 +1005,8 @@ class Model extends AbstractModel_1.AbstractModel {
1005
1005
  * @return {this} this
1006
1006
  */
1007
1007
  _queryStatement(sql) {
1008
- var _a;
1009
1008
  return __awaiter(this, void 0, void 0, function* () {
1009
+ var _a;
1010
1010
  try {
1011
1011
  if (this.$state.get('DEBUG'))
1012
1012
  this.$utils.consoleDebug(sql);
@@ -1057,9 +1057,9 @@ class Model extends AbstractModel_1.AbstractModel {
1057
1057
  * @property {Function} actions.returnId
1058
1058
  * @return {this} this
1059
1059
  */
1060
- _actionStatement({ sql, returnId = false }) {
1061
- var _a;
1062
- return __awaiter(this, void 0, void 0, function* () {
1060
+ _actionStatement(_a) {
1061
+ return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false }) {
1062
+ var _b;
1063
1063
  try {
1064
1064
  if (this.$state.get('DEBUG'))
1065
1065
  this.$utils.consoleDebug(sql);
@@ -1179,7 +1179,7 @@ class Model extends AbstractModel_1.AbstractModel {
1179
1179
  return result.affectedRows;
1180
1180
  }
1181
1181
  catch (error) {
1182
- if ((_a = this.$state.get('JOIN')) === null || _a === void 0 ? void 0 : _a.length)
1182
+ if ((_b = this.$state.get('JOIN')) === null || _b === void 0 ? void 0 : _b.length)
1183
1183
  throw error;
1184
1184
  yield this._checkSchemaOrNextError(error, Number(this.$state.get('RETRY')));
1185
1185
  this.$state.set('RETRY', Number(this.$state.get('RETRY')) + 1);
@@ -2495,8 +2495,8 @@ class Model extends AbstractModel_1.AbstractModel {
2495
2495
  * @return {promise<boolean>} promise boolean
2496
2496
  */
2497
2497
  delete() {
2498
- var _a, _b;
2499
2498
  return __awaiter(this, void 0, void 0, function* () {
2499
+ var _a, _b;
2500
2500
  this._assertError(!this.$state.get('where').length, "The 'delete' method requires the use of 'where' conditions.");
2501
2501
  this.limit(1);
2502
2502
  if (this.$state.get('SOFT_DELETE')) {
@@ -2529,8 +2529,8 @@ class Model extends AbstractModel_1.AbstractModel {
2529
2529
  * @return {promise<boolean>} promise boolean
2530
2530
  */
2531
2531
  deleteMany() {
2532
- var _a, _b;
2533
2532
  return __awaiter(this, void 0, void 0, function* () {
2533
+ var _a, _b;
2534
2534
  this._assertError(!this.$state.get('WHERE').length, "The 'deleteMany' method requires the use of 'where' conditions.");
2535
2535
  if (this.$state.get('SOFT_DELETE')) {
2536
2536
  const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
@@ -2567,8 +2567,8 @@ class Model extends AbstractModel_1.AbstractModel {
2567
2567
  * @return {promise<boolean>}
2568
2568
  */
2569
2569
  forceDelete() {
2570
- var _a, _b;
2571
2570
  return __awaiter(this, void 0, void 0, function* () {
2571
+ var _a, _b;
2572
2572
  this.disableSoftDelete();
2573
2573
  this.$state.set('DELETE', [
2574
2574
  `${this.$constants('DELETE')}`,
@@ -2588,8 +2588,8 @@ class Model extends AbstractModel_1.AbstractModel {
2588
2588
  * @return {promise<Record<string,any> | null>} Record | null
2589
2589
  */
2590
2590
  first(cb) {
2591
- var _a, _b;
2592
2591
  return __awaiter(this, void 0, void 0, function* () {
2592
+ var _a, _b;
2593
2593
  this._validateMethod('first');
2594
2594
  if (this.$state.get('VOID'))
2595
2595
  return this._resultHandler(undefined);
@@ -2625,8 +2625,8 @@ class Model extends AbstractModel_1.AbstractModel {
2625
2625
  * @return {promise<object | Error>} Record | throw error
2626
2626
  */
2627
2627
  firstOrError(message, options) {
2628
- var _a, _b;
2629
2628
  return __awaiter(this, void 0, void 0, function* () {
2629
+ var _a, _b;
2630
2630
  this._validateMethod('firstOrError');
2631
2631
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2632
2632
  this.select(...yield this.exceptColumns());
@@ -2659,8 +2659,8 @@ class Model extends AbstractModel_1.AbstractModel {
2659
2659
  * @return {promise<array>} Array
2660
2660
  */
2661
2661
  get(cb) {
2662
- var _a, _b;
2663
2662
  return __awaiter(this, void 0, void 0, function* () {
2663
+ var _a, _b;
2664
2664
  this._validateMethod('get');
2665
2665
  if (this.$state.get('VOID'))
2666
2666
  return [];
@@ -2699,8 +2699,8 @@ class Model extends AbstractModel_1.AbstractModel {
2699
2699
  * @return {promise<Pagination>} Pagination
2700
2700
  */
2701
2701
  pagination(paginationOptions) {
2702
- var _a, _b;
2703
2702
  return __awaiter(this, void 0, void 0, function* () {
2703
+ var _a, _b;
2704
2704
  this._validateMethod('pagination');
2705
2705
  let limit = 15;
2706
2706
  let page = 1;
@@ -2743,8 +2743,8 @@ class Model extends AbstractModel_1.AbstractModel {
2743
2743
  * @return {Promise<array>} Array
2744
2744
  */
2745
2745
  getGroupBy(column) {
2746
- var _a;
2747
2746
  return __awaiter(this, void 0, void 0, function* () {
2747
+ var _a;
2748
2748
  if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2749
2749
  this.select(...yield this.exceptColumns());
2750
2750
  this.selectRaw([
@@ -3184,19 +3184,19 @@ class Model extends AbstractModel_1.AbstractModel {
3184
3184
  * @property {boolean} options.changed - check when column is changed attribute will be change attribute
3185
3185
  * @return {Promise<void>}
3186
3186
  */
3187
- sync({ force = false, foreign = false, changed = false } = {}) {
3188
- var _a, _b, _c, _d, _e, _f, _g, _h;
3189
- return __awaiter(this, void 0, void 0, function* () {
3187
+ sync() {
3188
+ return __awaiter(this, arguments, void 0, function* ({ force = false, foreign = false, changed = false } = {}) {
3189
+ var _a, _b, _c, _d, _e, _f, _g, _h;
3190
3190
  const checkTables = yield this._queryStatement(`${this.$constants('SHOW_TABLES')} ${this.$constants('LIKE')} '${this.getTableName()}'`);
3191
3191
  const existsTables = checkTables.map((c) => Object.values(c)[0]);
3192
3192
  const schemaModel = this.getSchemaModel();
3193
3193
  if (schemaModel == null)
3194
3194
  return this._assertError(schemaModel == null, 'Schema model not found');
3195
3195
  const checkTableIsExists = existsTables.some((table) => table === this.getTableName());
3196
- const syncForeignKey = ({ schemaModel, model }) => __awaiter(this, void 0, void 0, function* () {
3197
- var _j;
3196
+ const syncForeignKey = (_j) => __awaiter(this, [_j], void 0, function* ({ schemaModel, model }) {
3197
+ var _k;
3198
3198
  for (const key in schemaModel) {
3199
- if (((_j = schemaModel[key]) === null || _j === void 0 ? void 0 : _j.foreignKey) == null)
3199
+ if (((_k = schemaModel[key]) === null || _k === void 0 ? void 0 : _k.foreignKey) == null)
3200
3200
  continue;
3201
3201
  const foreign = schemaModel[key].foreignKey;
3202
3202
  const table = typeof foreign.on === "string" ? foreign.on : foreign.on.getTableName();
@@ -3528,24 +3528,24 @@ class Model extends AbstractModel_1.AbstractModel {
3528
3528
  return;
3529
3529
  });
3530
3530
  }
3531
- _execute({ sql, type, message, options }) {
3532
- var _a, _b;
3533
- return __awaiter(this, void 0, void 0, function* () {
3531
+ _execute(_a) {
3532
+ return __awaiter(this, arguments, void 0, function* ({ sql, type, message, options }) {
3533
+ var _b, _c;
3534
3534
  let result = yield this._queryStatement(sql);
3535
3535
  if (!result.length)
3536
3536
  return this._returnEmpty(type, result, message, options);
3537
3537
  const relations = this.$state.get('RELATIONS');
3538
3538
  for (const relation of relations) {
3539
- result = (_b = yield ((_a = this.$relation) === null || _a === void 0 ? void 0 : _a.load(result, relation))) !== null && _b !== void 0 ? _b : [];
3539
+ result = (_c = yield ((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.load(result, relation))) !== null && _c !== void 0 ? _c : [];
3540
3540
  }
3541
3541
  if (this.$state.get('HIDDEN').length)
3542
3542
  this._hiddenColumnModel(result);
3543
3543
  return (yield this._returnResult(type, result)) || this._returnEmpty(type, result, message, options);
3544
3544
  });
3545
3545
  }
3546
- _executeGroup(dataParents, type = 'GET') {
3547
- var _a, _b, _c;
3548
- return __awaiter(this, void 0, void 0, function* () {
3546
+ _executeGroup(dataParents_1) {
3547
+ return __awaiter(this, arguments, void 0, function* (dataParents, type = 'GET') {
3548
+ var _a, _b, _c;
3549
3549
  if (!dataParents.length)
3550
3550
  return this._returnEmpty(type, dataParents);
3551
3551
  const relations = this.$state.get('RELATIONS');
@@ -3561,8 +3561,8 @@ class Model extends AbstractModel_1.AbstractModel {
3561
3561
  });
3562
3562
  }
3563
3563
  _pagination(data) {
3564
- var _a;
3565
3564
  return __awaiter(this, void 0, void 0, function* () {
3565
+ var _a;
3566
3566
  const currentPage = +(this.$state.get('PAGE'));
3567
3567
  const limit = Number(this.$state.get('PER_PAGE'));
3568
3568
  this._assertError(limit < 1, "This pagination needed limit minimun less 1 for limit");
@@ -3665,8 +3665,8 @@ class Model extends AbstractModel_1.AbstractModel {
3665
3665
  });
3666
3666
  }
3667
3667
  _returnResult(type, data) {
3668
- var _a, _b, _c, _d, _e;
3669
3668
  return __awaiter(this, void 0, void 0, function* () {
3669
+ var _a, _b, _c, _d, _e;
3670
3670
  const registry = this.$state.get('REGISTRY');
3671
3671
  if (((_a = Object.keys(registry)) === null || _a === void 0 ? void 0 : _a.length) && registry != null) {
3672
3672
  for (const d of data) {
@@ -3807,8 +3807,8 @@ class Model extends AbstractModel_1.AbstractModel {
3807
3807
  return data;
3808
3808
  }
3809
3809
  _attach(name, dataId, fields) {
3810
- var _a;
3811
3810
  return __awaiter(this, void 0, void 0, function* () {
3811
+ var _a;
3812
3812
  this._assertError(!Array.isArray(dataId), `this ${dataId} is not an array`);
3813
3813
  const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
3814
3814
  this._assertError(!relation, `unknown name relation ['${name}'] in model`);
@@ -4019,8 +4019,8 @@ class Model extends AbstractModel_1.AbstractModel {
4019
4019
  });
4020
4020
  }
4021
4021
  _createMultipleModel() {
4022
- var _a;
4023
4022
  return __awaiter(this, void 0, void 0, function* () {
4023
+ var _a;
4024
4024
  for (const data of (_a = this.$state.get('DATA')) !== null && _a !== void 0 ? _a : []) {
4025
4025
  yield this._validateSchema(data, 'insert');
4026
4026
  }
@@ -4227,9 +4227,9 @@ class Model extends AbstractModel_1.AbstractModel {
4227
4227
  }
4228
4228
  }
4229
4229
  }
4230
- _checkSchemaOrNextError(e, retry = 1) {
4231
- var _a, _b, _c, _d, _e;
4232
- return __awaiter(this, void 0, void 0, function* () {
4230
+ _checkSchemaOrNextError(e_1) {
4231
+ return __awaiter(this, arguments, void 0, function* (e, retry = 1) {
4232
+ var _a, _b, _c, _d, _e;
4233
4233
  try {
4234
4234
  if (retry > 2 || this.$state.get('RETRY') > 2)
4235
4235
  throw e;
@@ -4299,22 +4299,22 @@ class Model extends AbstractModel_1.AbstractModel {
4299
4299
  _makeRelations() {
4300
4300
  if (this.$hasOne != null) {
4301
4301
  for (const hasOne of this.$hasOne) {
4302
- this.hasOne(Object.assign(Object.assign({}, hasOne), { name: String(hasOne.name) }));
4302
+ this.hasOne(Object.assign(Object.assign({}, hasOne), { name: hasOne.name }));
4303
4303
  }
4304
4304
  }
4305
4305
  if (this.$hasMany != null) {
4306
4306
  for (const hasMany of this.$hasMany) {
4307
- this.hasMany(Object.assign(Object.assign({}, hasMany), { name: String(hasMany.name) }));
4307
+ this.hasMany(Object.assign(Object.assign({}, hasMany), { name: hasMany.name }));
4308
4308
  }
4309
4309
  }
4310
4310
  if (this.$belongsTo != null) {
4311
4311
  for (const belongsTo of this.$belongsTo) {
4312
- this.belongsTo(Object.assign(Object.assign({}, belongsTo), { name: String(belongsTo.name) }));
4312
+ this.belongsTo(Object.assign(Object.assign({}, belongsTo), { name: belongsTo.name }));
4313
4313
  }
4314
4314
  }
4315
4315
  if (this.$belongsToMany != null) {
4316
4316
  for (const belongsToMany of this.$belongsToMany) {
4317
- this.belongsToMany(Object.assign(Object.assign({}, belongsToMany), { name: String(belongsToMany.name) }));
4317
+ this.belongsToMany(Object.assign(Object.assign({}, belongsToMany), { name: belongsToMany.name }));
4318
4318
  }
4319
4319
  }
4320
4320
  return this;
@@ -133,8 +133,5 @@ declare class Schema extends Builder {
133
133
  private _syncExecute;
134
134
  private _syncForeignKey;
135
135
  }
136
- type SchemaType<TSchema, TSpecific = any> = {
137
- [K in keyof TSchema]: K extends keyof TSpecific ? TSpecific[K] : any;
138
- };
139
- export { Schema, SchemaType };
136
+ export { Schema };
140
137
  export default Schema;
@@ -141,8 +141,8 @@ class Schema extends Builder_1.Builder {
141
141
  *
142
142
  * await new Schema().sync(`app/Models` , { force : true , log = true, foreign = true , changed = true })
143
143
  */
144
- sync(pathFolders, { force = false, log = false, foreign = false, changed = false } = {}) {
145
- return __awaiter(this, void 0, void 0, function* () {
144
+ sync(pathFolders_1) {
145
+ return __awaiter(this, arguments, void 0, function* (pathFolders, { force = false, log = false, foreign = false, changed = false } = {}) {
146
146
  const directories = fs_1.default.readdirSync(pathFolders, { withFileTypes: true });
147
147
  const files = (yield Promise.all(directories.map((directory) => {
148
148
  const newDir = path_1.default.resolve(String(pathFolders), directory.name);
@@ -217,8 +217,8 @@ class Schema extends Builder_1.Builder {
217
217
  *
218
218
  * await Schema.sync(`app/Models` , { force : true })
219
219
  */
220
- static sync(pathFolders, { force = false, log = false, foreign = false, changed = false } = {}) {
221
- return __awaiter(this, void 0, void 0, function* () {
220
+ static sync(pathFolders_1) {
221
+ return __awaiter(this, arguments, void 0, function* (pathFolders, { force = false, log = false, foreign = false, changed = false } = {}) {
222
222
  return new this().sync(pathFolders, { force, log, foreign, changed });
223
223
  });
224
224
  }
@@ -235,9 +235,9 @@ class Schema extends Builder_1.Builder {
235
235
  }
236
236
  });
237
237
  }
238
- _syncExecute({ models, force, log, foreign, changed }) {
239
- var _a, _b, _c, _d, _e, _f, _g, _h;
240
- return __awaiter(this, void 0, void 0, function* () {
238
+ _syncExecute(_a) {
239
+ return __awaiter(this, arguments, void 0, function* ({ models, force, log, foreign, changed }) {
240
+ var _b, _c, _d, _e, _f, _g, _h, _j;
241
241
  const checkTables = yield this.query(this.$constants('SHOW_TABLES'));
242
242
  const existsTables = checkTables.map((c) => Object.values(c)[0]);
243
243
  for (const model of models) {
@@ -283,8 +283,8 @@ class Schema extends Builder_1.Builder {
283
283
  for (const column of wasChangedColumns) {
284
284
  if (column == null)
285
285
  continue;
286
- const type = (_b = (_a = schemaModel[column]) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : null;
287
- const attributes = (_d = (_c = schemaModel[column]) === null || _c === void 0 ? void 0 : _c.attributes) !== null && _d !== void 0 ? _d : null;
286
+ const type = (_c = (_b = schemaModel[column]) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : null;
287
+ const attributes = (_e = (_d = schemaModel[column]) === null || _d === void 0 ? void 0 : _d.attributes) !== null && _e !== void 0 ? _e : null;
288
288
  const sql = [
289
289
  this.$constants('ALTER_TABLE'),
290
290
  `\`${model.getTableName()}\``,
@@ -302,8 +302,8 @@ class Schema extends Builder_1.Builder {
302
302
  for (const column of missingColumns) {
303
303
  const indexWithColumn = entries.findIndex(([key]) => key === column);
304
304
  const findAfterIndex = indexWithColumn ? entries[indexWithColumn - 1][0] : null;
305
- const type = (_f = (_e = schemaModel[column]) === null || _e === void 0 ? void 0 : _e.type) !== null && _f !== void 0 ? _f : null;
306
- const attributes = (_h = (_g = schemaModel[column]) === null || _g === void 0 ? void 0 : _g.attributes) !== null && _h !== void 0 ? _h : null;
305
+ const type = (_g = (_f = schemaModel[column]) === null || _f === void 0 ? void 0 : _f.type) !== null && _g !== void 0 ? _g : null;
306
+ const attributes = (_j = (_h = schemaModel[column]) === null || _h === void 0 ? void 0 : _h.attributes) !== null && _j !== void 0 ? _j : null;
307
307
  if (findAfterIndex == null || type == null || attributes == null)
308
308
  continue;
309
309
  const sql = [
@@ -325,11 +325,11 @@ class Schema extends Builder_1.Builder {
325
325
  return;
326
326
  });
327
327
  }
328
- _syncForeignKey({ schemaModel, model, log }) {
329
- var _a;
330
- return __awaiter(this, void 0, void 0, function* () {
328
+ _syncForeignKey(_a) {
329
+ return __awaiter(this, arguments, void 0, function* ({ schemaModel, model, log }) {
330
+ var _b;
331
331
  for (const key in schemaModel) {
332
- if (((_a = schemaModel[key]) === null || _a === void 0 ? void 0 : _a.foreignKey) == null)
332
+ if (((_b = schemaModel[key]) === null || _b === void 0 ? void 0 : _b.foreignKey) == null)
333
333
  continue;
334
334
  const foreign = schemaModel[key].foreignKey;
335
335
  if (foreign.on == null)
@@ -0,0 +1,6 @@
1
+ export type SchemaType<TSchema, TSpecific = any> = {
2
+ [K in keyof TSchema]: K extends keyof TSpecific ? TSpecific[K] : any;
3
+ };
4
+ export type RelationType<T> = {
5
+ [K in keyof T]: T[K];
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -9,6 +9,7 @@ export { Blueprint };
9
9
  export { Pool };
10
10
  export * from './Decorator';
11
11
  export * from './Schema';
12
+ export * from './Type';
12
13
  declare const _default: {
13
14
  DB: typeof DB;
14
15
  Model: typeof Model;
@@ -29,6 +29,7 @@ const connection_1 = __importDefault(require("../connection"));
29
29
  exports.Pool = connection_1.default;
30
30
  __exportStar(require("./Decorator"), exports);
31
31
  __exportStar(require("./Schema"), exports);
32
+ __exportStar(require("./Type"), exports);
32
33
  exports.default = {
33
34
  DB: DB_1.default,
34
35
  Model: Model_1.default,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "mysql query builder object relational mapping (ORM)",
5
5
  "main": "build/lib/index.js",
6
6
  "types": "build/lib/index.d.ts",
@@ -15,6 +15,7 @@
15
15
  "url": "https://github.com/thanathip41/tspace-mysql.git"
16
16
  },
17
17
  "keywords": [
18
+ "tspace",
18
19
  "tspace-mysql",
19
20
  "mysql",
20
21
  "orm",