tspace-mysql 1.9.4 → 1.9.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.
Files changed (121) hide show
  1. package/README.md +376 -72
  2. package/dist/cli/dump/db.js +3 -15
  3. package/dist/cli/dump/db.js.map +1 -1
  4. package/dist/cli/index.js +0 -3
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/migrations/make-db.js +1 -1
  7. package/dist/cli/migrations/make-db.js.map +1 -1
  8. package/dist/cli/migrations/make-model.js +1 -6
  9. package/dist/cli/migrations/make-model.js.map +1 -1
  10. package/dist/lib/config/index.d.ts +6 -5
  11. package/dist/lib/config/index.js +18 -8
  12. package/dist/lib/config/index.js.map +1 -1
  13. package/dist/lib/constants/index.d.ts +1 -1
  14. package/dist/lib/constants/index.js +1 -1
  15. package/dist/lib/constants/index.js.map +1 -1
  16. package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +2 -1
  17. package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -3
  18. package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
  19. package/dist/lib/core/Abstracts/AbstractModel.d.ts +1 -2
  20. package/dist/lib/core/Abstracts/AbstractModel.js +0 -1
  21. package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
  22. package/dist/lib/core/Abstracts/AbstractView.d.ts +2 -1
  23. package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
  24. package/dist/lib/core/Blueprint.d.ts +13 -3
  25. package/dist/lib/core/Blueprint.js +16 -1
  26. package/dist/lib/core/Blueprint.js.map +1 -1
  27. package/dist/lib/core/Builder.d.ts +172 -19
  28. package/dist/lib/core/Builder.js +564 -197
  29. package/dist/lib/core/Builder.js.map +1 -1
  30. package/dist/lib/core/Cache/DBCache.js +6 -5
  31. package/dist/lib/core/Cache/DBCache.js.map +1 -1
  32. package/dist/lib/core/Cache/index.js +1 -1
  33. package/dist/lib/core/Cache/index.js.map +1 -1
  34. package/dist/lib/core/Contracts/Audit.d.ts +5 -3
  35. package/dist/lib/core/Contracts/Audit.js +18 -2
  36. package/dist/lib/core/Contracts/Audit.js.map +1 -1
  37. package/dist/lib/core/DB.d.ts +148 -190
  38. package/dist/lib/core/DB.js +459 -525
  39. package/dist/lib/core/DB.js.map +1 -1
  40. package/dist/lib/core/Driver/index.d.ts +40 -15
  41. package/dist/lib/core/Driver/index.js +3 -0
  42. package/dist/lib/core/Driver/index.js.map +1 -1
  43. package/dist/lib/core/Driver/mariadb/MariadbDriver.d.ts +3 -2
  44. package/dist/lib/core/Driver/mariadb/MariadbDriver.js +7 -5
  45. package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
  46. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +42 -17
  47. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +281 -154
  48. package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
  49. package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +4 -3
  50. package/dist/lib/core/Driver/mongodb/MongodbDriver.js +7 -5
  51. package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -1
  52. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +40 -15
  53. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +205 -157
  54. package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -1
  55. package/dist/lib/core/Driver/mysql/MysqlDriver.d.ts +3 -2
  56. package/dist/lib/core/Driver/mysql/MysqlDriver.js +6 -14
  57. package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
  58. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +42 -17
  59. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +251 -144
  60. package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
  61. package/dist/lib/core/Driver/postgres/PostgresDriver.d.ts +3 -2
  62. package/dist/lib/core/Driver/postgres/PostgresDriver.js +7 -5
  63. package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
  64. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +45 -19
  65. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +471 -287
  66. package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
  67. package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +3 -2
  68. package/dist/lib/core/Driver/sqlite/SqliteDriver.js +8 -6
  69. package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -1
  70. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +47 -19
  71. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +331 -203
  72. package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -1
  73. package/dist/lib/core/Model.d.ts +255 -52
  74. package/dist/lib/core/Model.js +459 -117
  75. package/dist/lib/core/Model.js.map +1 -1
  76. package/dist/lib/core/Package.d.ts +5 -0
  77. package/dist/lib/core/Package.js +7 -2
  78. package/dist/lib/core/Package.js.map +1 -1
  79. package/dist/lib/core/Pool.d.ts +1 -1
  80. package/dist/lib/core/Pool.js +53 -75
  81. package/dist/lib/core/Pool.js.map +1 -1
  82. package/dist/lib/core/Queue/index.d.ts +2 -11
  83. package/dist/lib/core/Queue/index.js +3 -3
  84. package/dist/lib/core/Queue/index.js.map +1 -1
  85. package/dist/lib/core/Queue/types.d.ts +32 -3
  86. package/dist/lib/core/Queue/worker-lock.d.ts +17 -0
  87. package/dist/lib/core/Queue/worker-lock.js +28 -0
  88. package/dist/lib/core/Queue/worker-lock.js.map +1 -0
  89. package/dist/lib/core/Queue/worker.d.ts +37 -23
  90. package/dist/lib/core/Queue/worker.js +385 -171
  91. package/dist/lib/core/Queue/worker.js.map +1 -1
  92. package/dist/lib/core/RelationManager.js +1 -1
  93. package/dist/lib/core/RelationManager.js.map +1 -1
  94. package/dist/lib/core/Repository.d.ts +164 -29
  95. package/dist/lib/core/Repository.js +181 -40
  96. package/dist/lib/core/Repository.js.map +1 -1
  97. package/dist/lib/core/Schema.d.ts +11 -7
  98. package/dist/lib/core/Schema.js +249 -80
  99. package/dist/lib/core/Schema.js.map +1 -1
  100. package/dist/lib/core/SqlLike.js +4 -4
  101. package/dist/lib/core/SqlLike.js.map +1 -1
  102. package/dist/lib/core/StateManager.d.ts +19 -0
  103. package/dist/lib/core/StateManager.js +9 -4
  104. package/dist/lib/core/StateManager.js.map +1 -1
  105. package/dist/lib/core/UtilityTypes.d.ts +17 -3
  106. package/dist/lib/core/UtilityTypes.js.map +1 -1
  107. package/dist/lib/core/View.d.ts +3 -1
  108. package/dist/lib/core/View.js +18 -25
  109. package/dist/lib/core/View.js.map +1 -1
  110. package/dist/lib/core/index.d.ts +2 -0
  111. package/dist/lib/core/index.js +3 -1
  112. package/dist/lib/core/index.js.map +1 -1
  113. package/dist/lib/types/index.d.ts +77 -90
  114. package/dist/lib/types/repository/index.d.ts +18 -26
  115. package/dist/lib/utils/index.d.ts +7 -3
  116. package/dist/lib/utils/index.js +99 -37
  117. package/dist/lib/utils/index.js.map +1 -1
  118. package/package.json +1 -2
  119. package/dist/cli/dump/table.d.ts +0 -4
  120. package/dist/cli/dump/table.js +0 -41
  121. package/dist/cli/dump/table.js.map +0 -1
@@ -86,46 +86,52 @@ export type TPagination<K = any> = {
86
86
  };
87
87
  data: K[];
88
88
  };
89
+ export type TCursorPagination<T = any> = {
90
+ meta: {
91
+ limit: number;
92
+ count: number;
93
+ next: {
94
+ cursor: string | null;
95
+ has: boolean;
96
+ };
97
+ };
98
+ data: T[];
99
+ };
89
100
  export type TSave = "INSERT" | "INSERT_MULTIPLE" | "INSERT_NOT_EXISTS" | "INSERT_OR_SELECT" | "UPDATE" | "UPDATE_OR_INSERT";
90
- export type TBackup = {
101
+ export type TClone = {
91
102
  database: string;
92
- to?: {
93
- driver?: TDriver;
94
- host: string;
95
- port: number;
96
- username: string;
97
- password: string;
98
- };
103
+ excludes?: string[];
104
+ only?: never;
105
+ value?: boolean;
106
+ to?: TConnectionOptions;
107
+ } | {
108
+ database: string;
109
+ only?: string[];
110
+ excludes?: never;
111
+ value?: boolean;
112
+ to?: TConnectionOptions;
99
113
  };
100
- export type TBackupToFile = {
101
- database?: string;
114
+ export type TDump = ({
102
115
  filePath: string;
103
- table?: string;
104
- connection?: {
105
- host: string;
106
- port: number;
107
- database: string;
108
- username: string;
109
- password: string;
110
- };
111
- };
112
- export type TBackupTableToFile = {
116
+ only?: string[];
117
+ excludes?: never;
118
+ database?: string;
119
+ value?: boolean;
120
+ } | {
113
121
  filePath: string;
114
- table: string;
115
- connection?: {
116
- host: string;
117
- port: number;
118
- database: string;
119
- username: string;
120
- password: string;
121
- };
122
- };
122
+ only?: never;
123
+ excludes?: string[];
124
+ database?: string;
125
+ value?: boolean;
126
+ });
123
127
  export type TConnectionOptions = {
124
- [key: string]: any;
125
- driver?: TDriver;
128
+ driver: AssertDriver<'sqlite'>;
129
+ database: string;
130
+ } | {
131
+ driver: Exclude<TDriver, AssertDriver<'sqlite'>>;
126
132
  host: string;
127
133
  port: number;
128
- database?: string;
134
+ database: string;
129
135
  username: string;
130
136
  password: string;
131
137
  };
@@ -133,35 +139,9 @@ export type TPoolCallback = {
133
139
  query: (sql: string, callback: (err: any, result: any) => void) => void;
134
140
  release: () => void;
135
141
  };
136
- export type TConnectionTransaction = {
137
- query: (sql: string) => Promise<any[]>;
138
- /**
139
- * The 'startTransaction' method is used when need to started the transaction
140
- * @returns {Promise<void>}
141
- */
142
- startTransaction: () => Promise<void>;
143
- /**
144
- * The 'commit' method is used to when need to commit the transactions is successfully
145
- * @returns {Promise<void>}
146
- */
147
- commit: () => Promise<void>;
148
- /**
149
- * The 'rollback ' method is used to when the transaction is failed
150
- * @returns {Promise<void>}
151
- */
152
- rollback: () => Promise<void>;
153
- /**
154
- * The 'end ' method is used to end the transction
155
- * @returns {Promise<void>}
156
- */
157
- end: () => Promise<void>;
158
- /**
159
- * The 'release' method is used to release connection
160
- * @returns {Promise<void>}
161
- */
162
- release: () => Promise<void>;
163
- };
164
142
  export type TConnection = {
143
+ driver: () => TDriver;
144
+ database: () => string;
165
145
  on: (event: TPoolEvent, data: any) => void;
166
146
  query: (sql: string) => Promise<any[]>;
167
147
  queryBuilder: typeof QueryBuilder;
@@ -172,6 +152,7 @@ export type TConnection = {
172
152
  release: () => Promise<void>;
173
153
  };
174
154
  export type TPoolConnected = {
155
+ driver: () => TDriver;
175
156
  database: () => string;
176
157
  on: (event: TPoolEvent, data: (r: any) => any) => void;
177
158
  queryBuilder: typeof QueryBuilder;
@@ -189,7 +170,7 @@ export type TModelOrObject = {
189
170
  export type TCreateNewConnection = {
190
171
  on: (event: TPoolEvent, data: (r: any) => any) => void;
191
172
  query: (sql: string) => Promise<any>;
192
- connection: () => Promise<TConnectionTransaction>;
173
+ connection: () => Promise<TConnection>;
193
174
  };
194
175
  export type TOptions = {
195
176
  [key: string]: any;
@@ -321,21 +302,7 @@ export type TLiteralStringKeys<T> = {
321
302
  [K in keyof T]: T[K] extends string ? (string extends T[K] ? never : K) : never;
322
303
  }[keyof T];
323
304
  export type TLiteralEnumKeys<T> = TLiteralStringKeys<TRemoveIndexSignature<T>>;
324
- export type TDriver = 'pg' | 'postgres' | 'mysql' | 'mysql2' | 'mariadb' | 'mssql' | 'sqlite' | 'mongodb';
325
- export type TPoolCusterOptions = {
326
- writer: {
327
- host: string;
328
- user: string;
329
- password: string;
330
- database: string;
331
- };
332
- readers: {
333
- host: string;
334
- user: string;
335
- password: string;
336
- database: string;
337
- }[];
338
- };
305
+ export type TDriver = 'postgres' | 'mysql' | 'mariadb' | 'sqlite' | 'mongodb';
339
306
  type TClusterPool = {
340
307
  pool: {
341
308
  type: string;
@@ -345,6 +312,8 @@ type TClusterPool = {
345
312
  username: string;
346
313
  };
347
314
  on: (event: TPoolEvent, data: (r: any) => any) => void;
315
+ database: () => string;
316
+ driver: () => TDriver;
348
317
  queryBuilder: typeof QueryBuilder;
349
318
  query: (sql: string) => Promise<any[]>;
350
319
  connection: () => Promise<TConnection>;
@@ -354,18 +323,18 @@ export type TPoolCusterConnected = {
354
323
  database?: () => string;
355
324
  query?: Function | null;
356
325
  queryBuilder?: QueryBuilder | null;
357
- masters: TClusterPool[];
358
- slaves: TClusterPool[];
326
+ primary: TClusterPool;
327
+ replicas: TClusterPool[];
359
328
  };
360
329
  export type TIsEnum<T> = T extends {
361
330
  __isDefault: true;
362
- } ? false : string extends T ? false : number extends T ? false : boolean extends T ? false : [
331
+ } ? TIsEnum<TRemoveDefault<T>> : string extends T ? false : number extends T ? false : boolean extends T ? false : [
363
332
  T
364
333
  ] extends [string | number] ? true : false;
365
334
  export type TLifecycle = "beforeInsert" | "afterInsert" | "beforeUpdate" | "afterUpdate" | "beforeRemove" | "afterRemove";
366
- export type TDeepOmit<T, Filter> = T extends infer ObjectType ? {
367
- [K in keyof ObjectType as K extends keyof Filter ? Filter[K] extends true ? never : K : NonNullable<ObjectType[K]> extends object ? (NonNullable<ObjectType[K]> extends Date ? K : never) : K]: K extends keyof Filter ? Filter[K] extends object ? TDeepOmit<NonNullable<ObjectType[K]>, Filter[K]> : ObjectType[K] : ObjectType[K];
368
- } : never;
335
+ export type TDeepOmit<T, Filter> = T extends Date | Function ? T : T extends (infer U)[] ? TDeepOmit<U, Filter>[] : T extends object ? {
336
+ [K in keyof T as K extends keyof Filter ? Filter[K] extends true ? never : K : K]: K extends keyof Filter ? Filter[K] extends object ? TDeepOmit<NonNullable<T[K]>, Filter[K]> : T[K] : T[K];
337
+ } : T;
369
338
  export type TDeepExpand<T> = T extends TDefault<infer D> ? D : T extends Date ? T : T extends Function ? T : T extends Model ? T : T extends (infer U)[] ? TDeepExpand<U>[] : T extends object ? {
370
339
  [K in keyof T]: TDeepExpand<T[K]>;
371
340
  } : T;
@@ -386,7 +355,7 @@ type IsStrict<T> = T extends true ? false : T extends object ? (T extends {
386
355
  select: any;
387
356
  } ? true : T extends {
388
357
  except: any;
389
- } ? false : (keyof Omit<T, 'relations' | 'except'> extends never ? false : true)) : false;
358
+ } ? false : false) : false;
390
359
  type RemoveUndefined<T> = T extends undefined ? never : T;
391
360
  type NonObjectKeys<T> = {
392
361
  [K in keyof T]: RemoveUndefined<T[K]> extends Date ? K : RemoveUndefined<T[K]> extends object ? never : K;
@@ -394,11 +363,14 @@ type NonObjectKeys<T> = {
394
363
  type GetExceptKeys<T> = T extends {
395
364
  _except: infer E;
396
365
  } ? keyof E : never;
397
- export type TSelectionMerger<IncomingResult, S1, S2> = IncomingResult extends Date | Function ? IncomingResult : IncomingResult extends (infer U)[] ? TSelectionMerger<U, S1, S2>[] : IncomingResult extends object ? (Normalize<S1> extends infer F1 ? Normalize<S2> extends infer F2 ? (F1 & F2) extends infer Merged ? {
398
- [K in keyof Omit<Merged, '_except'> & keyof IncomingResult]-?: TSelectionMerger<RemoveUndefined<IncomingResult[K]>, K extends keyof F1 ? F1[K] : unknown, K extends keyof F2 ? F2[K] : unknown>;
366
+ export type TSelectionMerger<IncomingResult, S1, S2> = IncomingResult extends Date | Function ? IncomingResult : IncomingResult extends (infer U)[] ? TSelectionMerger<U, S1, S2>[] : IncomingResult extends object ? Normalize<S1> extends infer F1 ? Normalize<S2> extends infer F2 ? (F1 & F2) extends infer Merged ? {
367
+ [K in keyof Omit<Merged, '_except'> & keyof IncomingResult as Merged[K] extends false ? never : K]-?: TSelectionMerger<RemoveUndefined<IncomingResult[K]>, K extends keyof F1 ? F1[K] : unknown, K extends keyof F2 ? F2[K] : unknown>;
399
368
  } & ((IsStrict<S1> extends true ? true : IsStrict<S2> extends true ? true : false) extends true ? {} : {
400
- [K in Exclude<NonObjectKeys<IncomingResult>, GetExceptKeys<F1> | GetExceptKeys<F2>>]-?: IncomingResult[K];
401
- }) : never : never : never) : IncomingResult;
369
+ [K in Exclude<NonObjectKeys<IncomingResult>, GetExceptKeys<F1> | GetExceptKeys<F2>> as string extends K ? never : K]: IncomingResult[K];
370
+ }) : never : never : never : IncomingResult;
371
+ export type TSelection<IncomingResult, S1> = IncomingResult extends Date | Function ? IncomingResult : IncomingResult extends (infer U)[] ? TSelection<U, S1>[] : IncomingResult extends object ? Normalize<S1> extends infer F1 ? {
372
+ [K in keyof F1 & keyof IncomingResult as F1[K] extends false ? never : K]-?: F1[K] extends true ? IncomingResult[K] : TSelection<RemoveUndefined<IncomingResult[K]>, F1[K]>;
373
+ } : never : IncomingResult;
402
374
  export type TStateWhereCondition = {
403
375
  column?: string;
404
376
  operator?: string;
@@ -406,7 +378,7 @@ export type TStateWhereCondition = {
406
378
  condition?: 'AND' | 'OR';
407
379
  nested?: TStateWhereCondition[];
408
380
  };
409
- export type TDefault<T = any> = (string extends T ? string : boolean extends T ? boolean | number : number extends T ? number : Date extends T ? Date : `${T & string}` | null) & {
381
+ export type TDefault<T = any> = (string extends T ? string : boolean extends T ? Boolean | number : number extends T ? number : Date extends T ? Date : `${T & string}` | null) & {
410
382
  __isDefault: true;
411
383
  };
412
384
  export type TRemoveDefault<T> = T extends TDefault<infer U> ? U : T;
@@ -437,10 +409,25 @@ export type TInsertOrUpdateInput<K, C> = {
437
409
  [Q in Exclude<Extract<K, keyof C>, P>]?: TCleanDefaultType<C[Q]>;
438
410
  };
439
411
  }[Extract<K, keyof C>];
440
- export type TAction = "create" | "createMany" | "createNotExists" | "createOrSelect" | "createOrUpdate" | "update" | "updateMany" | null;
412
+ export type TAction = "create" | "createMany" | "createNotExists" | "createOrSelect" | "createOrUpdate" | "update" | "updateMany" | void | null;
441
413
  export type TSaveBuilderResult<A extends TAction> = A extends "create" ? Record<string, any> : A extends "createMany" ? Record<string, any>[] : A extends "createNotExists" ? Record<string, any> | null : A extends "createOrUpdate" ? Record<string, any> : A extends "createOrSelect" ? Record<string, any> : A extends "update" ? Record<string, any> | null : A extends "updateMany" ? Record<string, any>[] : any;
442
- export type TSaveModelResult<M extends Model<any, any, any>, A extends TAction> = A extends "create" ? T.InsertResult<M> : A extends "createMany" ? T.InsertManyResult<M> : A extends "createNotExists" ? T.InsertResult<M> | null : A extends "createOrUpdate" ? T.Result<M> : A extends "createOrSelect" ? T.Result<M> : A extends "update" ? T.UpdateResult<M> : A extends "updateMany" ? T.UpdateManyResult<M> : T.InsertResult<M> | T.InsertManyResult<M> | T.UpdateResult<M> | T.UpdateManyResult<M> | T.Result<M>;
414
+ export type TSaveModelResult<M extends Model<any, any, any>, A extends TAction> = A extends "create" ? T.InsertResult<M> : A extends "createMany" ? T.InsertManyResult<M> : A extends "createNotExists" ? T.InsertResult<M> | null : A extends "createOrUpdate" ? T.Result<M> : A extends "createOrSelect" ? T.Result<M> : A extends "update" ? T.UpdateResult<M> : A extends "updateMany" ? T.UpdateManyResult<M> : A extends void ? void : T.InsertResult<M> | T.InsertManyResult<M> | T.UpdateResult<M> | T.UpdateManyResult<M> | T.Result<M>;
443
415
  export type TAnyKeys = {
444
416
  [customKey: string]: unknown;
445
417
  };
418
+ export type TExplainIssue = {
419
+ type: 'FULL_SCAN' | 'LARGE_SCAN' | 'NO_INDEX' | 'FILE_SORT' | 'FILTER_AFTER_SCAN' | 'TEMPORARY_TABLE';
420
+ severity: 'info' | 'warning' | 'critical';
421
+ message: string;
422
+ };
423
+ export type TExplain<K = any> = {
424
+ sql: string;
425
+ plan: K[];
426
+ analysis: {
427
+ status: 'ok' | 'warning' | 'critical';
428
+ issues: TExplainIssue[];
429
+ recommendations: string[];
430
+ };
431
+ };
432
+ type AssertDriver<T extends TDriver> = T;
446
433
  export {};
@@ -1,11 +1,11 @@
1
1
  import { Model } from "../../core/Model";
2
2
  import type { T } from "../../core";
3
- import type { TConnection, TConnectionTransaction, TFreezeStringQuery, TNestedBoolean, TOperatorQuery, TRawStringQuery, TRelationKeys, TSchemaKeys } from "..";
3
+ import type { TConnection, TFreezeStringQuery, TInsertInput, TNestedBoolean, TOperatorQuery, TRawStringQuery, TRelationKeys, TSchemaKeys, TUpdateInput } from "..";
4
4
  import type { TColumnsDecorator, TRelationsDecorator } from "../decorator";
5
5
  export type TRepositorySelect<T = unknown, R = unknown, M extends Model = Model> = [unknown, unknown] extends [T, R] ? keyof TColumnsDecorator<M> extends never ? TNestedBoolean : Partial<{
6
- [K in keyof TColumnsDecorator<M> | keyof TRelationsDecorator<M>]: K extends `${string}.${string}` | TRawStringQuery ? boolean : K extends keyof TRelationsDecorator<M> ? M[K] extends (infer U)[] ? U extends Model ? "*" | TRepositorySelect<any, unknown, U> : boolean : M[K] extends Model ? "*" | TRepositorySelect<any, unknown, M[K]> : boolean : K extends keyof TColumnsDecorator<M> ? boolean : boolean;
6
+ [K in keyof TColumnsDecorator<M> | keyof TRelationsDecorator<M>]: K extends `${string}.${string}` | TRawStringQuery ? boolean : K extends keyof TRelationsDecorator<M> ? M[K] extends (infer U)[] ? U extends Model ? "*" | TRepositorySelect<any, unknown, U> : boolean : M[K] extends Model ? "*" | TRepositorySelect<any, unknown, M[K]> : boolean : boolean;
7
7
  }> : Partial<{
8
- [K in TSchemaKeys<T> | TRelationKeys<R>]: K extends TRelationKeys<R> ? K extends TRawStringQuery | `${string}.${string}` ? boolean : K extends `$${string & K}` ? R extends Record<string, any> ? boolean : never : R extends Record<string, any> ? R[`$${string & K}`] extends (infer U)[] ? U extends Model ? "*" | TRepositorySelect<T.SchemaModel<U>, T.RelationModel<U>> : never : R[`$${K & string}`] extends Model ? "*" | TRepositorySelect<T.SchemaModel<R[`$${K & string}`]>, T.RelationModel<R[`$${K & string}`]>> : never : never : K extends keyof T ? boolean : never;
8
+ [K in TSchemaKeys<T> | (unknown extends R ? never : TRelationKeys<R>)]: unknown extends R ? K extends TSchemaKeys<T> ? boolean : never : K extends TRelationKeys<R> ? K extends TRawStringQuery | `${string}.${string}` ? boolean : K extends `$${string & K}` ? R extends Record<string, any> ? boolean : never : R extends Record<string, any> ? R[`$${string & K}`] extends (infer U)[] ? U extends Model ? "*" | TRepositorySelect<T.SchemaModel<U>, T.RelationModel<U>> : never : R[`$${K & string}`] extends Model ? "*" | TRepositorySelect<T.SchemaModel<R[`$${K & string}`]>, T.RelationModel<R[`$${K & string}`]>> : never : never : K extends keyof T ? boolean : never;
9
9
  }>;
10
10
  export type TRepositoryExcept<T = unknown, R = unknown, M extends Model = Model> = [unknown, unknown] extends [T, R] ? keyof TColumnsDecorator<M> extends never ? TNestedBoolean : Partial<{
11
11
  [K in keyof TColumnsDecorator<M> | keyof TRelationsDecorator<M>]: K extends `${string}.${string}` | TRawStringQuery ? boolean : K extends keyof TRelationsDecorator<M> ? M[K] extends (infer U)[] ? U extends Model ? TRepositoryExcept<any, unknown, U> : boolean : M[K] extends Model ? TRepositoryExcept<any, unknown, M[K]> : boolean : K extends keyof TColumnsDecorator<M> ? boolean : boolean;
@@ -106,41 +106,33 @@ export type TRepositoryRequestPagination<T extends Record<string, any> = any, R
106
106
  page?: number;
107
107
  };
108
108
  export type TRepositoryRequestAggregate<T extends Record<string, any> = any, R = any, M extends Model<any, any> = Model<any, any>> = Partial<Omit<TRepositoryRequest<T, R, M>, "relations" | "relationQuery">>;
109
- export type TRepositoryCreate<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.ColumnOptions<M> = T.ColumnOptions<M>> = {
110
- data: {
111
- [P in Exclude<K & keyof C, "id" | "uuid"> as null extends C[P] ? any : P]: Extract<C[P], Date> extends never ? Extract<C[P], Record<string, unknown>> extends never ? C[P] : string : any;
112
- };
109
+ export type TRepositoryCreate<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.InputOptions<M> = T.InputOptions<M>> = {
110
+ data: TInsertInput<K, C>;
113
111
  debug?: boolean;
114
- transaction?: TConnection | TConnectionTransaction;
112
+ transaction?: TConnection | TConnection;
115
113
  noReturn?: NR;
116
114
  };
117
- export type TRepositoryCreateMultiple<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.ColumnOptions<M> = T.ColumnOptions<M>> = {
118
- data: {
119
- [P in Exclude<K & keyof C, "id" | "uuid"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? Extract<C[P], Record<string, unknown>> extends never ? C[P] : string : any;
120
- }[];
115
+ export type TRepositoryCreateMultiple<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.InputOptions<M> = T.InputOptions<M>> = {
116
+ data: TInsertInput<K, C>[];
121
117
  debug?: boolean;
122
- transaction?: TConnection | TConnectionTransaction;
118
+ transaction?: TConnection | TConnection;
123
119
  noReturn?: NR;
124
120
  };
125
- export type TRepositoryCreateOrThings<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.ColumnOptions<M> = T.ColumnOptions<M>> = {
126
- data: {
127
- [P in Exclude<K & keyof C, "id" | "uuid"> as null extends C[P] ? any : undefined extends C[P] ? never : P]: Extract<C[P], Date> extends never ? Extract<C[P], Record<string, unknown>> extends never ? C[P] : string : any;
128
- };
121
+ export type TRepositoryCreateOrThings<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.InputOptions<M> = T.InputOptions<M>> = {
122
+ data: TInsertInput<K, C>[];
129
123
  where: T.WhereOptions<M>;
130
124
  debug?: boolean;
131
- transaction?: TConnection | TConnectionTransaction;
125
+ transaction?: TConnection | TConnection;
132
126
  noReturn?: NR;
133
127
  };
134
- export type TRepositoryUpdate<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.ColumnOptions<M> = T.ColumnOptions<M>> = {
135
- data: Partial<{
136
- [P in K & keyof C]: Extract<C[P], Date> extends never ? Extract<C[P], Record<string, unknown>> extends never ? C[P] : string : any;
137
- }>;
128
+ export type TRepositoryUpdate<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.InputOptions<M> = T.InputOptions<M>> = {
129
+ data: TUpdateInput<K, C>;
138
130
  where: T.WhereOptions<M>;
139
131
  debug?: boolean;
140
- transaction?: TConnection | TConnectionTransaction;
132
+ transaction?: TConnection | TConnection;
141
133
  noReturn?: NR;
142
134
  };
143
- export type TRepositoryUpdateMultiple<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.ColumnOptions<M> = T.ColumnOptions<M>> = {
135
+ export type TRepositoryUpdateMultiple<M extends Model<any, any> = Model<any, any>, NR extends boolean | undefined = false, K extends T.ColumnKeys<M> = T.ColumnKeys<M>, C extends T.InputOptions<M> = T.InputOptions<M>> = {
144
136
  cases: {
145
137
  condition: ((query: M) => M) | Partial<{
146
138
  [P in K & keyof C]: Extract<C[P], Date> extends never ? Extract<C[P], Record<string, unknown>> extends never ? C[P] : string : any;
@@ -150,13 +142,13 @@ export type TRepositoryUpdateMultiple<M extends Model<any, any> = Model<any, any
150
142
  }>;
151
143
  }[];
152
144
  debug?: boolean;
153
- transaction?: TConnection | TConnectionTransaction;
145
+ transaction?: TConnection | TConnection;
154
146
  noReturn?: NR;
155
147
  };
156
148
  export type TRepositoryDelete<M extends Model<any, any> = Model<any, any>> = {
157
149
  where: T.WhereOptions<M>;
158
150
  debug?: boolean;
159
- transaction?: TConnection | TConnectionTransaction;
151
+ transaction?: TConnection | TConnection;
160
152
  };
161
153
  export type TRepositoryPrimitiveExtendType<T> = T extends NumberConstructor ? number : T extends StringConstructor ? string : T extends BooleanConstructor ? boolean : T extends DateConstructor ? Date : T extends readonly (infer U)[] ? TRepositoryPrimitiveExtendType<U>[] : T extends Record<string, any> ? {
162
154
  [K in keyof T]: TRepositoryPrimitiveExtendType<T[K]>;
@@ -1,9 +1,12 @@
1
1
  import { T } from "..";
2
- import { TStateWhereCondition } from "../types";
2
+ import type { TStateWhereCondition } from "../types";
3
3
  declare const utils: {
4
4
  typeOf: (data: any) => string;
5
5
  isDate: (data: any) => boolean;
6
- consoleDebug: (sql?: string, retry?: boolean) => void;
6
+ consoleDebug: (sql?: string, retry?: boolean, node?: {
7
+ type: "primary" | "replica";
8
+ node?: number;
9
+ } | null) => void;
7
10
  consoleExec: (startTime: number, endTime: number) => void;
8
11
  consoleCache: (provider: Record<string, any>) => void;
9
12
  faker: (value: string) => string | number | boolean | Date;
@@ -15,7 +18,7 @@ declare const utils: {
15
18
  }) => any;
16
19
  escapePayload: (v: any) => any;
17
20
  escapeXSS: (str: any) => any;
18
- generateUUID: () => string;
21
+ generateUUID: (version?: 4 | 7) => string;
19
22
  transfromBooleanToNumber: (data: any) => any;
20
23
  transfromDateToDateString: (data: any) => any;
21
24
  snakeCase: (data: any) => any;
@@ -67,6 +70,7 @@ declare const utils: {
67
70
  raw: boolean;
68
71
  }) => string;
69
72
  shallowClone: (value: any) => any;
73
+ sqlFormatted: (sql: string) => string;
70
74
  };
71
75
  export type TUtils = typeof utils;
72
76
  export { utils };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.utils = void 0;
4
4
  const __1 = require("..");
5
5
  const constants_1 = require("../constants");
6
+ const config_1 = require("../config");
6
7
  const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
7
8
  const isDate = (data) => {
8
9
  if (typeOf(data) === 'date')
@@ -11,31 +12,22 @@ const isDate = (data) => {
11
12
  };
12
13
  const timestamp = (dateString) => {
13
14
  const d = dateString == null ? new Date() : new Date(dateString);
14
- const year = d.getFullYear();
15
- const month = `0${(d.getMonth() + 1)}`.slice(-2);
16
- const date = `0${(d.getDate())}`.slice(-2);
17
- const hours = `0${(d.getHours())}`.slice(-2);
18
- const minutes = `0${(d.getMinutes())}`.slice(-2);
19
- const seconds = `0${(d.getSeconds())}`.slice(-2);
20
- const ymd = `${[
21
- year,
22
- month,
23
- date
24
- ].join('-')}`;
25
- const his = `${[
26
- hours,
27
- minutes,
28
- seconds
29
- ].join(':')}`;
30
- return `${ymd} ${his}`;
15
+ const isLocal = config_1.Config.TIMEZONE === 'local';
16
+ const year = isLocal ? d.getFullYear() : d.getUTCFullYear();
17
+ const month = `0${(isLocal ? d.getMonth() : d.getUTCMonth()) + 1}`.slice(-2);
18
+ const date = `0${isLocal ? d.getDate() : d.getUTCDate()}`.slice(-2);
19
+ const hours = `0${isLocal ? d.getHours() : d.getUTCHours()}`.slice(-2);
20
+ const minutes = `0${isLocal ? d.getMinutes() : d.getUTCMinutes()}`.slice(-2);
21
+ const seconds = `0${isLocal ? d.getSeconds() : d.getUTCSeconds()}`.slice(-2);
22
+ return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
31
23
  };
32
24
  const date = (value) => {
33
25
  const d = value == null ? new Date() : new Date(value);
34
- const year = d.getFullYear();
35
- const month = `0${(d.getMonth() + 1)}`.slice(-2);
36
- const date = `0${(d.getDate())}`.slice(-2);
37
- const now = `${year}-${month}-${date}`;
38
- return now;
26
+ const isLocal = config_1.Config.TIMEZONE === 'local';
27
+ const year = isLocal ? d.getFullYear() : d.getUTCFullYear();
28
+ const month = `0${(isLocal ? d.getMonth() : d.getUTCMonth()) + 1}`.slice(-2);
29
+ const date = `0${isLocal ? d.getDate() : d.getUTCDate()}`.slice(-2);
30
+ return `${year}-${month}-${date}`;
39
31
  };
40
32
  const escape = (v, opts = { hard: false }) => {
41
33
  if (typeof v !== 'string') {
@@ -83,13 +75,31 @@ const columnRelation = (name) => {
83
75
  return `${name.toLocaleLowerCase()}`;
84
76
  return name.replace(matches[0], `_${matches[0].toUpperCase()}`);
85
77
  };
86
- const generateUUID = () => {
87
- const date = +new Date();
88
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
89
- let r = Math.random() * 16;
90
- r = (date + r) % 16 | 0;
91
- return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
92
- });
78
+ const generateUUID = (version = 7) => {
79
+ const randomHex = (length) => {
80
+ let result = '';
81
+ for (let i = 0; i < length; i++) {
82
+ result += Math.floor(Math.random() * 16).toString(16);
83
+ }
84
+ return result;
85
+ };
86
+ if (version === 4) {
87
+ return [
88
+ randomHex(8),
89
+ randomHex(4),
90
+ `4${randomHex(3)}`,
91
+ `${(8 + Math.floor(Math.random() * 4)).toString(16)}${randomHex(3)}`,
92
+ randomHex(12),
93
+ ].join('-');
94
+ }
95
+ const timestamp = Date.now().toString(16).padStart(12, '0');
96
+ return [
97
+ timestamp.slice(0, 8),
98
+ timestamp.slice(8, 12),
99
+ `7${randomHex(3)}`,
100
+ `${(8 + Math.floor(Math.random() * 4)).toString(16)}${randomHex(3)}`,
101
+ randomHex(12),
102
+ ].join('-');
93
103
  };
94
104
  const transfromBooleanToNumber = (data) => {
95
105
  if (typeOf(data) === 'boolean')
@@ -181,7 +191,7 @@ const camelCase = (data) => {
181
191
  return data;
182
192
  }
183
193
  };
184
- const consoleDebug = (sql, retry = false) => {
194
+ const consoleDebug = (sql, retry = false, node) => {
185
195
  if (typeof sql !== "string" || sql == null)
186
196
  return;
187
197
  const colors = {
@@ -206,11 +216,12 @@ const consoleDebug = (sql, retry = false) => {
206
216
  .replace(/([`"])(?:\\.|(?!\1).)*\1/g, m => `${colors.special}${m}${colors.reset}`)
207
217
  .replace(new RegExp(`\\b(${sqlKeywords.join('|').replace(/ /g, '\\s+')})\\b`, 'g'), (m) => `${colors.keyword}${m}${colors.reset}`);
208
218
  };
209
- if (retry) {
210
- console.log(`\n\x1b[31mRETRY QUERY:\x1b[0m ${colorSQL(sql.trim())};`);
211
- return;
212
- }
213
- console.log(`\n\x1b[34mQUERY:\x1b[0m ${colorSQL(sql.trim())};`);
219
+ const prefix = retry ? 'RETRY QUERY' : 'QUERY';
220
+ const color = retry ? 31 : 34;
221
+ const target = (node
222
+ ? `(${node.type}${node.type === 'replica' ? `:${node.node}` : ''})`
223
+ : '');
224
+ console.log(`\n\x1b[${color}m${prefix}${target}:\x1b[0m ${colorSQL(sql.trim())};`);
214
225
  };
215
226
  const consoleExec = (startTime, endTime) => {
216
227
  const diffInMilliseconds = endTime - startTime;
@@ -314,9 +325,11 @@ const softNumber = (n) => {
314
325
  if (number === -1) {
315
326
  return 2 ** 31 - 1; // int 32 bit
316
327
  }
328
+ ;
317
329
  if (Number.isNaN(number)) {
318
- return -1;
330
+ return 0;
319
331
  }
332
+ ;
320
333
  return Number.parseInt(`${number}`);
321
334
  };
322
335
  const checkValueHasRaw = (value) => {
@@ -521,6 +534,54 @@ const shallowClone = (value) => {
521
534
  }
522
535
  return value;
523
536
  };
537
+ const sqlFormatted = (sql) => {
538
+ const statements = sql
539
+ .split(";")
540
+ .map((s) => s.trim())
541
+ .filter(Boolean);
542
+ const formattedStatements = statements.map((stmt) => {
543
+ if (stmt.includes("(")) {
544
+ const firstParen = stmt.indexOf("(");
545
+ const lastParen = stmt.lastIndexOf(")");
546
+ if (firstParen === -1 || lastParen === -1)
547
+ return stmt + ";";
548
+ const prefix = stmt.slice(0, firstParen).trim();
549
+ let columnsPart = stmt.slice(firstParen + 1, lastParen).trim();
550
+ const colArray = [];
551
+ let parenCount = 0;
552
+ let start = 0;
553
+ for (let i = 0; i < columnsPart.length; i++) {
554
+ const char = columnsPart[i];
555
+ if (char === "(")
556
+ parenCount++;
557
+ if (char === ")")
558
+ parenCount--;
559
+ if (char === "," && parenCount === 0) {
560
+ colArray.push(columnsPart.slice(start, i).trim());
561
+ start = i + 1;
562
+ }
563
+ }
564
+ colArray.push(columnsPart.slice(start).trim());
565
+ const seen = new Set();
566
+ const uniqueColumns = colArray.filter((col) => {
567
+ const colNameMatch = col.match(/"([\w]+)"/) || col.match(/([\w]+)/);
568
+ const colName = colNameMatch ? colNameMatch[1] : col;
569
+ if (seen.has(colName))
570
+ return false;
571
+ seen.add(colName);
572
+ return true;
573
+ });
574
+ const formattedColumns = uniqueColumns
575
+ .map((c) => " " + c)
576
+ .join(",\n");
577
+ return `${prefix} (\n${formattedColumns}\n)`;
578
+ }
579
+ else {
580
+ return stmt;
581
+ }
582
+ });
583
+ return formattedStatements.join(";\n\n");
584
+ };
524
585
  const utils = {
525
586
  typeOf,
526
587
  isDate,
@@ -555,7 +616,8 @@ const utils = {
555
616
  nestConditions,
556
617
  formatQueryValue,
557
618
  bindingParameters,
558
- shallowClone
619
+ shallowClone,
620
+ sqlFormatted
559
621
  };
560
622
  exports.utils = utils;
561
623
  exports.default = utils;