metal-orm 1.1.8 → 1.1.10

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 (75) hide show
  1. package/README.md +769 -764
  2. package/dist/index.cjs +2352 -226
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +605 -40
  5. package/dist/index.d.ts +605 -40
  6. package/dist/index.js +2324 -226
  7. package/dist/index.js.map +1 -1
  8. package/package.json +22 -17
  9. package/src/bulk/bulk-context.ts +83 -0
  10. package/src/bulk/bulk-delete-executor.ts +89 -0
  11. package/src/bulk/bulk-executor.base.ts +73 -0
  12. package/src/bulk/bulk-insert-executor.ts +74 -0
  13. package/src/bulk/bulk-types.ts +70 -0
  14. package/src/bulk/bulk-update-executor.ts +192 -0
  15. package/src/bulk/bulk-upsert-executor.ts +95 -0
  16. package/src/bulk/bulk-utils.ts +91 -0
  17. package/src/bulk/index.ts +18 -0
  18. package/src/codegen/typescript.ts +30 -21
  19. package/src/core/ast/expression-builders.ts +107 -10
  20. package/src/core/ast/expression-nodes.ts +52 -22
  21. package/src/core/ast/expression-visitor.ts +23 -13
  22. package/src/core/dialect/abstract.ts +30 -17
  23. package/src/core/dialect/mysql/index.ts +20 -5
  24. package/src/core/execution/db-executor.ts +96 -64
  25. package/src/core/execution/executors/better-sqlite3-executor.ts +94 -0
  26. package/src/core/execution/executors/mssql-executor.ts +66 -34
  27. package/src/core/execution/executors/mysql-executor.ts +98 -66
  28. package/src/core/execution/executors/postgres-executor.ts +33 -11
  29. package/src/core/execution/executors/sqlite-executor.ts +86 -30
  30. package/src/decorators/bootstrap.ts +482 -398
  31. package/src/decorators/column-decorator.ts +87 -96
  32. package/src/decorators/decorator-metadata.ts +100 -24
  33. package/src/decorators/entity.ts +27 -24
  34. package/src/decorators/relations.ts +231 -149
  35. package/src/decorators/transformers/transformer-decorators.ts +26 -29
  36. package/src/decorators/validators/country-validators-decorators.ts +9 -15
  37. package/src/dto/apply-filter.ts +568 -551
  38. package/src/index.ts +16 -9
  39. package/src/orm/entity-hydration.ts +116 -72
  40. package/src/orm/entity-metadata.ts +347 -301
  41. package/src/orm/entity-relations.ts +264 -207
  42. package/src/orm/entity.ts +199 -199
  43. package/src/orm/execute.ts +13 -13
  44. package/src/orm/lazy-batch/morph-many.ts +70 -0
  45. package/src/orm/lazy-batch/morph-one.ts +69 -0
  46. package/src/orm/lazy-batch/morph-to.ts +59 -0
  47. package/src/orm/lazy-batch.ts +4 -1
  48. package/src/orm/orm-session.ts +170 -104
  49. package/src/orm/pooled-executor-factory.ts +99 -58
  50. package/src/orm/query-logger.ts +49 -40
  51. package/src/orm/relation-change-processor.ts +198 -96
  52. package/src/orm/relations/belongs-to.ts +143 -143
  53. package/src/orm/relations/has-many.ts +204 -204
  54. package/src/orm/relations/has-one.ts +174 -174
  55. package/src/orm/relations/many-to-many.ts +288 -288
  56. package/src/orm/relations/morph-many.ts +156 -0
  57. package/src/orm/relations/morph-one.ts +151 -0
  58. package/src/orm/relations/morph-to.ts +162 -0
  59. package/src/orm/save-graph.ts +116 -1
  60. package/src/query-builder/expression-table-mapper.ts +5 -0
  61. package/src/query-builder/hydration-manager.ts +345 -345
  62. package/src/query-builder/hydration-planner.ts +178 -148
  63. package/src/query-builder/relation-conditions.ts +171 -151
  64. package/src/query-builder/relation-cte-builder.ts +5 -1
  65. package/src/query-builder/relation-filter-utils.ts +9 -6
  66. package/src/query-builder/relation-include-strategies.ts +44 -2
  67. package/src/query-builder/relation-join-strategies.ts +8 -1
  68. package/src/query-builder/relation-service.ts +250 -241
  69. package/src/query-builder/select/cursor-pagination.ts +323 -0
  70. package/src/query-builder/select/select-operations.ts +110 -105
  71. package/src/query-builder/select.ts +42 -1
  72. package/src/query-builder/update-include.ts +4 -0
  73. package/src/schema/relation.ts +296 -188
  74. package/src/schema/types.ts +138 -123
  75. package/src/tree/tree-decorator.ts +127 -137
@@ -1,20 +1,33 @@
1
1
  // src/core/execution/executors/sqlite-executor.ts
2
- import {
3
- DbExecutor,
4
- toExecutionPayload,
5
- rowsToQueryResult
6
- } from '../db-executor.js';
2
+ import {
3
+ DbExecutor,
4
+ toExecutionPayload,
5
+ rowsToQueryResult
6
+ } from '../db-executor.js';
7
7
 
8
- export interface SqliteClientLike {
8
+ export interface SqliteClientLike {
9
9
  all(
10
10
  sql: string,
11
11
  params?: unknown[]
12
12
  ): Promise<Array<Record<string, unknown>>>;
13
13
  run?(sql: string, params?: unknown[]): Promise<unknown>;
14
- beginTransaction?(): Promise<void>;
15
- commitTransaction?(): Promise<void>;
16
- rollbackTransaction?(): Promise<void>;
17
- }
14
+ beginTransaction?(): Promise<void>;
15
+ commitTransaction?(): Promise<void>;
16
+ rollbackTransaction?(): Promise<void>;
17
+ savepoint?(name: string): Promise<void>;
18
+ releaseSavepoint?(name: string): Promise<void>;
19
+ rollbackToSavepoint?(name: string): Promise<void>;
20
+ }
21
+
22
+ const SAVEPOINT_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
23
+
24
+ const sanitizeSavepointName = (name: string): string => {
25
+ const trimmed = name.trim();
26
+ if (!SAVEPOINT_NAME_PATTERN.test(trimmed)) {
27
+ throw new Error(`Invalid savepoint name: "${name}"`);
28
+ }
29
+ return trimmed;
30
+ };
18
31
 
19
32
  /**
20
33
  * Creates a database executor for SQLite.
@@ -24,20 +37,30 @@ export interface SqliteClientLike {
24
37
  export function createSqliteExecutor(
25
38
  client: SqliteClientLike
26
39
  ): DbExecutor {
27
- const supportsTransactions =
28
- typeof client.beginTransaction === 'function' &&
29
- typeof client.commitTransaction === 'function' &&
30
- typeof client.rollbackTransaction === 'function';
40
+ const supportsTransactions =
41
+ typeof client.beginTransaction === 'function' &&
42
+ typeof client.commitTransaction === 'function' &&
43
+ typeof client.rollbackTransaction === 'function';
44
+ const supportsSavepoints = supportsTransactions;
45
+
46
+ const executeControlStatement = async (sql: string): Promise<void> => {
47
+ if (typeof client.run === 'function') {
48
+ await client.run(sql);
49
+ return;
50
+ }
51
+ await client.all(sql);
52
+ };
31
53
 
32
54
  return {
33
- capabilities: {
34
- transactions: supportsTransactions,
35
- },
36
- async executeSql(sql, params) {
37
- const rows = await client.all(sql, params);
38
- const result = rowsToQueryResult(rows);
39
- return toExecutionPayload([result]);
55
+ capabilities: {
56
+ transactions: supportsTransactions,
57
+ ...(supportsSavepoints ? { savepoints: true } : {}),
40
58
  },
59
+ async executeSql(sql, params) {
60
+ const rows = await client.all(sql, params);
61
+ const result = rowsToQueryResult(rows);
62
+ return toExecutionPayload([result]);
63
+ },
41
64
  async beginTransaction() {
42
65
  if (!supportsTransactions) {
43
66
  throw new Error('Transactions are not supported by this executor');
@@ -50,14 +73,47 @@ export function createSqliteExecutor(
50
73
  }
51
74
  await client.commitTransaction!();
52
75
  },
53
- async rollbackTransaction() {
54
- if (!supportsTransactions) {
55
- throw new Error('Transactions are not supported by this executor');
56
- }
57
- await client.rollbackTransaction!();
58
- },
59
- async dispose() {
60
- // Connection lifecycle is owned by the caller/driver. Pool lease executors should implement dispose.
61
- },
76
+ async rollbackTransaction() {
77
+ if (!supportsTransactions) {
78
+ throw new Error('Transactions are not supported by this executor');
79
+ }
80
+ await client.rollbackTransaction!();
81
+ },
82
+ async savepoint(name: string) {
83
+ if (!supportsSavepoints) {
84
+ throw new Error('Savepoints are not supported by this executor');
85
+ }
86
+ const savepoint = sanitizeSavepointName(name);
87
+ if (typeof client.savepoint === 'function') {
88
+ await client.savepoint(savepoint);
89
+ return;
90
+ }
91
+ await executeControlStatement(`SAVEPOINT ${savepoint}`);
92
+ },
93
+ async releaseSavepoint(name: string) {
94
+ if (!supportsSavepoints) {
95
+ throw new Error('Savepoints are not supported by this executor');
96
+ }
97
+ const savepoint = sanitizeSavepointName(name);
98
+ if (typeof client.releaseSavepoint === 'function') {
99
+ await client.releaseSavepoint(savepoint);
100
+ return;
101
+ }
102
+ await executeControlStatement(`RELEASE SAVEPOINT ${savepoint}`);
103
+ },
104
+ async rollbackToSavepoint(name: string) {
105
+ if (!supportsSavepoints) {
106
+ throw new Error('Savepoints are not supported by this executor');
107
+ }
108
+ const savepoint = sanitizeSavepointName(name);
109
+ if (typeof client.rollbackToSavepoint === 'function') {
110
+ await client.rollbackToSavepoint(savepoint);
111
+ return;
112
+ }
113
+ await executeControlStatement(`ROLLBACK TO SAVEPOINT ${savepoint}`);
114
+ },
115
+ async dispose() {
116
+ // Connection lifecycle is owned by the caller/driver. Pool lease executors should implement dispose.
117
+ },
62
118
  };
63
119
  }