metal-orm 1.1.9 → 1.1.11

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 (77) hide show
  1. package/README.md +769 -764
  2. package/dist/index.cjs +2255 -284
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +559 -39
  5. package/dist/index.d.ts +559 -39
  6. package/dist/index.js +2227 -284
  7. package/dist/index.js.map +1 -1
  8. package/package.json +17 -12
  9. package/scripts/generate-entities/render.mjs +21 -12
  10. package/scripts/generate-entities/schema.mjs +87 -73
  11. package/scripts/generate-entities/tree-detection.mjs +67 -61
  12. package/src/bulk/bulk-context.ts +83 -0
  13. package/src/bulk/bulk-delete-executor.ts +87 -0
  14. package/src/bulk/bulk-executor.base.ts +73 -0
  15. package/src/bulk/bulk-insert-executor.ts +74 -0
  16. package/src/bulk/bulk-types.ts +70 -0
  17. package/src/bulk/bulk-update-executor.ts +192 -0
  18. package/src/bulk/bulk-upsert-executor.ts +93 -0
  19. package/src/bulk/bulk-utils.ts +91 -0
  20. package/src/bulk/index.ts +18 -0
  21. package/src/codegen/typescript.ts +30 -21
  22. package/src/core/ast/expression-builders.ts +107 -10
  23. package/src/core/ast/expression-nodes.ts +52 -22
  24. package/src/core/ast/expression-visitor.ts +23 -13
  25. package/src/core/ddl/introspect/mysql.ts +113 -36
  26. package/src/core/dialect/abstract.ts +30 -17
  27. package/src/core/dialect/mysql/index.ts +20 -5
  28. package/src/core/execution/db-executor.ts +96 -64
  29. package/src/core/execution/executors/better-sqlite3-executor.ts +94 -0
  30. package/src/core/execution/executors/mssql-executor.ts +66 -34
  31. package/src/core/execution/executors/mysql-executor.ts +98 -66
  32. package/src/core/execution/executors/postgres-executor.ts +33 -11
  33. package/src/core/execution/executors/sqlite-executor.ts +86 -30
  34. package/src/decorators/bootstrap.ts +482 -398
  35. package/src/decorators/column-decorator.ts +87 -96
  36. package/src/decorators/decorator-metadata.ts +100 -24
  37. package/src/decorators/entity.ts +27 -24
  38. package/src/decorators/relations.ts +231 -149
  39. package/src/decorators/transformers/transformer-decorators.ts +26 -29
  40. package/src/decorators/validators/country-validators-decorators.ts +9 -15
  41. package/src/dto/apply-filter.ts +568 -551
  42. package/src/index.ts +16 -9
  43. package/src/orm/entity-hydration.ts +116 -72
  44. package/src/orm/entity-metadata.ts +347 -301
  45. package/src/orm/entity-relations.ts +264 -207
  46. package/src/orm/entity.ts +199 -199
  47. package/src/orm/execute.ts +13 -13
  48. package/src/orm/lazy-batch/morph-many.ts +70 -0
  49. package/src/orm/lazy-batch/morph-one.ts +69 -0
  50. package/src/orm/lazy-batch/morph-to.ts +59 -0
  51. package/src/orm/lazy-batch.ts +4 -1
  52. package/src/orm/orm-session.ts +170 -104
  53. package/src/orm/pooled-executor-factory.ts +99 -58
  54. package/src/orm/query-logger.ts +49 -40
  55. package/src/orm/relation-change-processor.ts +198 -96
  56. package/src/orm/relations/belongs-to.ts +143 -143
  57. package/src/orm/relations/has-many.ts +204 -204
  58. package/src/orm/relations/has-one.ts +174 -174
  59. package/src/orm/relations/many-to-many.ts +288 -288
  60. package/src/orm/relations/morph-many.ts +156 -0
  61. package/src/orm/relations/morph-one.ts +151 -0
  62. package/src/orm/relations/morph-to.ts +162 -0
  63. package/src/orm/save-graph.ts +116 -1
  64. package/src/query-builder/expression-table-mapper.ts +5 -0
  65. package/src/query-builder/hydration-manager.ts +345 -345
  66. package/src/query-builder/hydration-planner.ts +178 -148
  67. package/src/query-builder/relation-conditions.ts +171 -151
  68. package/src/query-builder/relation-cte-builder.ts +5 -1
  69. package/src/query-builder/relation-filter-utils.ts +9 -6
  70. package/src/query-builder/relation-include-strategies.ts +44 -2
  71. package/src/query-builder/relation-join-strategies.ts +8 -1
  72. package/src/query-builder/relation-service.ts +250 -241
  73. package/src/query-builder/select/select-operations.ts +110 -105
  74. package/src/query-builder/update-include.ts +4 -0
  75. package/src/schema/relation.ts +296 -188
  76. package/src/schema/types.ts +138 -123
  77. 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
  }