metal-orm 1.0.90 → 1.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,193 +1,197 @@
1
- /**
2
- * DTO transformation utilities for working with DTO instances.
3
- *
4
- * These helpers make it easier to convert between DTO types, apply defaults,
5
- * and handle auto-generated fields without manual object construction.
6
- */
7
-
8
- // ─────────────────────────────────────────────────────────────────────────────
9
- // Transform utilities
10
- // ─────────────────────────────────────────────────────────────────────────────
11
-
12
- /**
13
- * Transforms a CreateDto or UpdateDto into a ResponseDto by merging with auto-generated fields.
14
- *
15
- * @example
16
- * ```ts
17
- * const newUser: UserResponse = toResponse(body, {
18
- * id: newId,
19
- * createdAt: new Date().toISOString()
20
- * });
21
- *
22
- * // Or use the curried form for reuse
23
- * const createUserResponse = toResponseBuilder<UserCreateDto, UserResponse>({
24
- * id: () => nextId++,
25
- * createdAt: () => new Date().toISOString()
26
- * });
27
- * const user = createUserResponse(body);
28
- * ```
29
- */
30
- export function toResponse<TInput, TOutput>(
31
- input: TInput,
32
- autoFields: Partial<TOutput>
33
- ): TOutput {
34
- return {
35
- ...input,
36
- ...autoFields
37
- } as unknown as TOutput;
38
- }
39
-
40
- /**
41
- * Creates a toResponse function with pre-configured auto-fields.
42
- * Useful for reusing same transformation logic across multiple endpoints.
43
- *
44
- * @example
45
- * ```ts
46
- * const userResponseBuilder = toResponseBuilder<CreateUserDto, UserResponse>({
47
- * id: () => generateId(),
48
- * createdAt: () => new Date().toISOString(),
49
- * active: true
50
- * });
51
- *
52
- * app.post('/users', (req, res) => {
53
- * const user = userResponseBuilder(req.body);
54
- * res.status(201).json(user);
55
- * });
56
- * ```
57
- */
58
- export function toResponseBuilder<TInput, TOutput>(
59
- autoFields: Partial<TOutput> | (() => Partial<TOutput>)
60
- ): (input: TInput) => TOutput {
61
- return (input: TInput) => {
62
- const fields: Partial<TOutput> =
63
- typeof autoFields === 'function' ? (autoFields as () => Partial<TOutput>)() : autoFields;
64
- return {
65
- ...input,
66
- ...fields
67
- } as unknown as TOutput;
68
- };
69
- }
70
-
71
- /**
72
- * Merges default values into a DTO. Returns a complete object with all defaults applied.
73
- *
74
- * @example
75
- * ```ts
76
- * const user = withDefaults(body, {
77
- * active: true,
78
- * createdAt: new Date().toISOString()
79
- * });
80
- * ```
81
- */
82
- export function withDefaults<T>(dto: Partial<T>, defaults: T): T {
83
- return {
84
- ...defaults,
85
- ...dto
86
- };
87
- }
88
-
89
- /**
90
- * Creates a withDefaults function with pre-configured defaults.
91
- *
92
- * @example
93
- * ```ts
94
- * const userWithDefaults = withDefaultsBuilder<CreateUserDto>({
95
- * active: true
96
- * });
97
- *
98
- * app.post('/users', (req, res) => {
99
- * const userInput = userWithDefaults(req.body);
100
- * // ...
101
- * });
102
- * ```
103
- */
104
- export function withDefaultsBuilder<T>(
105
- defaults: T | (() => T)
106
- ): (dto: Partial<T>) => T {
107
- return (dto: Partial<T>) => {
108
- const resolvedDefaults: T =
109
- typeof defaults === 'function' ? (defaults as () => T)() : defaults;
110
- return {
111
- ...resolvedDefaults,
112
- ...dto
113
- };
114
- };
115
- }
116
-
117
- /**
118
- * Excludes specified fields from an object. Useful for removing sensitive fields
119
- * from database results before sending to the client.
120
- *
121
- * @example
122
- * ```ts
123
- * const userFromDb = await db.select().from(users).where(...).first();
124
- * const userResponse = exclude(userFromDb, 'passwordHash', 'apiKey');
125
- * ```
126
- */
127
- export function exclude<T extends object, K extends keyof T>(
128
- obj: T,
129
- ...keys: K[]
130
- ): Omit<T, K> {
131
- const result = { ...obj };
132
- for (const key of keys) {
133
- delete result[key];
134
- }
135
- return result as Omit<T, K>;
136
- }
137
-
138
- /**
139
- * Picks only specified fields from an object. Useful for creating DTOs from
140
- * larger database entities.
141
- *
142
- * @example
143
- * ```ts
144
- * const userFromDb = await db.select().from(users).where(...).first();
145
- * const userResponse = pick(userFromDb, 'id', 'name', 'email');
146
- * ```
147
- */
148
- export function pick<T extends object, K extends keyof T>(
149
- obj: T,
150
- ...keys: K[]
151
- ): Pick<T, K> {
152
- const result = {} as Pick<T, K>;
153
- for (const key of keys) {
154
- result[key] = obj[key];
155
- }
156
- return result;
157
- }
158
-
159
- /**
160
- * Maps field names from one DTO to another. Useful when your DTO has different
161
- * field names than your entity (e.g., camelCase vs snake_case).
162
- *
163
- * @example
164
- * ```ts
165
- * type ApiUser = { firstName: string; lastName: string };
166
- * type DbUser = { first_name: string; last_name: string };
167
- *
168
- * const dbUser = mapFields(apiUser, {
169
- * firstName: 'first_name',
170
- * lastName: 'last_name'
171
- * });
172
- * ```
173
- */
174
- export function mapFields<T extends object>(
175
- obj: T,
176
- fieldMap: Partial<Record<keyof T, string>>
177
- ): Record<string, unknown> {
178
- const result: Record<string, unknown> = {};
179
- const keys = Object.keys(fieldMap) as (keyof T)[];
180
- for (const sourceKey of keys) {
181
- const targetKey = fieldMap[sourceKey];
182
- if (targetKey) {
183
- result[targetKey] = obj[sourceKey];
184
- }
185
- }
186
- // Copy any unmapped fields
187
- for (const [key, value] of Object.entries(obj)) {
188
- if (!(key in fieldMap)) {
189
- result[key] = value;
190
- }
191
- }
192
- return result;
193
- }
1
+ /**
2
+ * DTO transformation utilities for working with DTO instances.
3
+ *
4
+ * These helpers make it easier to convert between DTO types, apply defaults,
5
+ * and handle auto-generated fields without manual object construction.
6
+ */
7
+
8
+ // ─────────────────────────────────────────────────────────────────────────────
9
+ // Transform utilities
10
+ // ─────────────────────────────────────────────────────────────────────────────
11
+
12
+ /**
13
+ * Transforms a CreateDto or UpdateDto into a ResponseDto by merging with auto-generated fields.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const newUser: UserResponse = toResponse(body, {
18
+ * id: newId,
19
+ * createdAt: new Date().toISOString()
20
+ * });
21
+ *
22
+ * // Or use the curried form for reuse
23
+ * const createUserResponse = toResponseBuilder<UserCreateDto, UserResponse>({
24
+ * id: () => nextId++,
25
+ * createdAt: () => new Date().toISOString()
26
+ * });
27
+ * const user = createUserResponse(body);
28
+ * ```
29
+ */
30
+ export function toResponse<TInput, TOutput>(
31
+ input: TInput,
32
+ autoFields: Partial<TOutput>
33
+ ): TOutput {
34
+ return {
35
+ ...input,
36
+ ...autoFields
37
+ } as unknown as TOutput;
38
+ }
39
+
40
+ /**
41
+ * Creates a toResponse function with pre-configured auto-fields.
42
+ * Useful for reusing same transformation logic across multiple endpoints.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * const userResponseBuilder = toResponseBuilder<CreateUserDto, UserResponse>({
47
+ * id: () => generateId(),
48
+ * createdAt: () => new Date().toISOString(),
49
+ * active: true
50
+ * });
51
+ *
52
+ * app.post('/users', (req, res) => {
53
+ * const user = userResponseBuilder(req.body);
54
+ * res.status(201).json(user);
55
+ * });
56
+ * ```
57
+ */
58
+ export function toResponseBuilder<TInput, TOutput>(
59
+ autoFields: Partial<TOutput> | (() => Partial<TOutput>)
60
+ ): (input: TInput) => TOutput {
61
+ return (input: TInput) => {
62
+ const fields: Partial<TOutput> =
63
+ typeof autoFields === 'function' ? (autoFields as () => Partial<TOutput>)() : autoFields;
64
+ return {
65
+ ...input,
66
+ ...fields
67
+ } as unknown as TOutput;
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Merges default values into a DTO. Returns a complete object with all defaults applied.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const user = withDefaults(body, {
77
+ * active: true,
78
+ * createdAt: new Date().toISOString()
79
+ * });
80
+ * ```
81
+ */
82
+ export function withDefaults<T>(dto: Partial<T>, defaults: T): T {
83
+ return {
84
+ ...defaults,
85
+ ...dto
86
+ };
87
+ }
88
+
89
+ /**
90
+ * Creates a withDefaults function with pre-configured defaults.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * const userWithDefaults = withDefaultsBuilder<CreateUserDto>({
95
+ * active: true
96
+ * });
97
+ *
98
+ * app.post('/users', (req, res) => {
99
+ * const userInput = userWithDefaults(req.body);
100
+ * // ...
101
+ * });
102
+ * ```
103
+ */
104
+ export function withDefaultsBuilder<T>(
105
+ defaults: T | (() => T)
106
+ ): (dto: Partial<T>) => T {
107
+ return (dto: Partial<T>) => {
108
+ const resolvedDefaults: T =
109
+ typeof defaults === 'function' ? (defaults as () => T)() : defaults;
110
+ return {
111
+ ...resolvedDefaults,
112
+ ...dto
113
+ };
114
+ };
115
+ }
116
+
117
+ /**
118
+ * Excludes specified fields from an object. Useful for removing sensitive fields
119
+ * from database results before sending to the client.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * const userFromDb = await db.select().from(users).where(...).first();
124
+ * const userResponse = exclude(userFromDb, 'passwordHash', 'apiKey');
125
+ * ```
126
+ */
127
+ export function exclude<T extends object, K extends keyof T>(
128
+ obj: T,
129
+ ...keys: K[]
130
+ ): Omit<T, K> {
131
+ const result = { ...obj };
132
+ for (const key of keys) {
133
+ delete result[key];
134
+ }
135
+ return result as Omit<T, K>;
136
+ }
137
+
138
+ /**
139
+ * Picks only specified fields from an object. Useful for creating DTOs from
140
+ * larger database entities.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const userFromDb = await db.select().from(users).where(...).first();
145
+ * const userResponse = pick(userFromDb, 'id', 'name', 'email');
146
+ * ```
147
+ */
148
+ export function pick<T extends object, K extends keyof T>(
149
+ obj: T,
150
+ ...keys: K[]
151
+ ): Pick<T, K> {
152
+ const result = {} as Pick<T, K>;
153
+ for (const key of keys) {
154
+ result[key] = obj[key];
155
+ }
156
+ return result;
157
+ }
158
+
159
+ /**
160
+ * Maps field names from one DTO to another. Useful when your DTO has different
161
+ * field names than your entity (e.g., camelCase vs snake_case).
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * type ApiUser = { firstName: string; lastName: string };
166
+ * type DbUser = { first_name: string; last_name: string };
167
+ *
168
+ * const dbUser = mapFields(apiUser, {
169
+ * firstName: 'first_name',
170
+ * lastName: 'last_name'
171
+ * });
172
+ * ```
173
+ */
174
+ type MappedFields<T, M extends Partial<Record<keyof T, string>>> = {
175
+ [K in keyof M as M[K] extends string ? M[K] : never]: K extends keyof T ? T[K] : never;
176
+ };
177
+
178
+ export function mapFields<T extends object, M extends Partial<Record<keyof T, string>>>(
179
+ obj: T,
180
+ fieldMap: M
181
+ ): Omit<T, keyof M> & MappedFields<T, M> {
182
+ const result: Record<string, unknown> = {};
183
+ const keys = Object.keys(fieldMap) as (keyof T)[];
184
+ for (const sourceKey of keys) {
185
+ const targetKey = fieldMap[sourceKey];
186
+ if (targetKey) {
187
+ result[targetKey] = obj[sourceKey];
188
+ }
189
+ }
190
+ // Copy any unmapped fields
191
+ for (const [key, value] of Object.entries(obj)) {
192
+ if (!(key in fieldMap)) {
193
+ result[key] = value;
194
+ }
195
+ }
196
+ return result as Omit<T, keyof M> & MappedFields<T, M>;
197
+ }
package/src/index.ts CHANGED
@@ -1,69 +1,69 @@
1
- /**
2
- * MetalORM core exports.
3
- * Provides schema definition, query building, and ORM capabilities.
4
- */
5
- export * from './schema/table.js';
6
- export * from './schema/column-types.js';
7
- export * from './schema/relation.js';
8
- export * from './schema/types.js';
9
- export * from './query-builder/select.js';
10
- export * from './query-builder/select-helpers.js';
11
- export * from './query-builder/insert.js';
12
- export * from './query-builder/update.js';
13
- export * from './query-builder/delete.js';
14
- export * from './query/index.js';
15
- export * from './core/ast/expression.js';
16
- export * from './core/ast/window-functions.js';
17
- export * from './core/hydration/types.js';
18
- export * from './core/dialect/mysql/index.js';
19
- export * from './core/dialect/mssql/index.js';
20
- export * from './core/dialect/sqlite/index.js';
21
- export * from './core/dialect/postgres/index.js';
22
- export * from './core/ddl/schema-generator.js';
23
- export * from './core/ddl/schema-types.js';
24
- export * from './core/ddl/schema-diff.js';
25
- export * from './core/ddl/schema-introspect.js';
26
- export * from './core/ddl/introspect/registry.js';
27
- export * from './core/functions/text.js';
28
- export * from './core/functions/numeric.js';
29
- export * from './core/functions/datetime.js';
30
- export * from './core/functions/control-flow.js';
31
- export * from './core/functions/json.js';
32
- export * from './core/functions/array.js';
33
- export * from './orm/als.js';
34
- export * from './orm/hydration.js';
35
- export * from './codegen/typescript.js';
36
- export * from './orm/orm-session.js';
37
- export * from './orm/orm.js';
38
- export * from './orm/entity.js';
39
- export * from './orm/lazy-batch.js';
40
- export * from './orm/relations/has-many.js';
41
- export * from './orm/relations/belongs-to.js';
42
- export * from './orm/relations/many-to-many.js';
43
- export * from './orm/execute.js';
44
- export type { EntityContext } from './orm/entity-context.js';
45
- export type { PrimaryKey as EntityPrimaryKey } from './orm/entity-context.js';
46
- export * from './orm/execution-context.js';
47
- export * from './orm/hydration-context.js';
48
- export * from './orm/domain-event-bus.js';
49
- export * from './orm/runtime-types.js';
50
- export * from './orm/query-logger.js';
51
- export * from './orm/interceptor-pipeline.js';
52
- export * from './orm/jsonify.js';
53
- export * from './orm/save-graph-types.js';
54
- export * from './decorators/index.js';
55
-
56
- // NEW: execution abstraction + helpers
57
- export * from './core/execution/db-executor.js';
58
- export * from './core/execution/pooling/pool-types.js';
59
- export * from './core/execution/pooling/pool.js';
60
- export * from './core/execution/executors/postgres-executor.js';
61
- export * from './core/execution/executors/mysql-executor.js';
62
- export * from './core/execution/executors/sqlite-executor.js';
63
- export * from './core/execution/executors/mssql-executor.js';
64
-
65
- // NEW: first-class pooling integration
66
- export * from './orm/pooled-executor-factory.js';
67
-
68
- // DTO module for REST API integration
69
- export * from './dto/index.js';
1
+ /**
2
+ * MetalORM core exports.
3
+ * Provides schema definition, query building, and ORM capabilities.
4
+ */
5
+ export * from './schema/table.js';
6
+ export * from './schema/column-types.js';
7
+ export * from './schema/relation.js';
8
+ export * from './schema/types.js';
9
+ export * from './query-builder/select.js';
10
+ export * from './query-builder/select-helpers.js';
11
+ export * from './query-builder/insert.js';
12
+ export * from './query-builder/update.js';
13
+ export * from './query-builder/delete.js';
14
+ export * from './query/index.js';
15
+ export * from './core/ast/expression.js';
16
+ export * from './core/ast/window-functions.js';
17
+ export * from './core/hydration/types.js';
18
+ export * from './core/dialect/mysql/index.js';
19
+ export * from './core/dialect/mssql/index.js';
20
+ export * from './core/dialect/sqlite/index.js';
21
+ export * from './core/dialect/postgres/index.js';
22
+ export * from './core/ddl/schema-generator.js';
23
+ export * from './core/ddl/schema-types.js';
24
+ export * from './core/ddl/schema-diff.js';
25
+ export * from './core/ddl/schema-introspect.js';
26
+ export * from './core/ddl/introspect/registry.js';
27
+ export * from './core/functions/text.js';
28
+ export * from './core/functions/numeric.js';
29
+ export * from './core/functions/datetime.js';
30
+ export * from './core/functions/control-flow.js';
31
+ export * from './core/functions/json.js';
32
+ export * from './core/functions/array.js';
33
+ export * from './orm/als.js';
34
+ export * from './orm/hydration.js';
35
+ export * from './codegen/typescript.js';
36
+ export * from './orm/orm-session.js';
37
+ export * from './orm/orm.js';
38
+ export * from './orm/entity.js';
39
+ export * from './orm/lazy-batch.js';
40
+ export * from './orm/relations/has-many.js';
41
+ export * from './orm/relations/belongs-to.js';
42
+ export * from './orm/relations/many-to-many.js';
43
+ export * from './orm/execute.js';
44
+ export type { EntityContext } from './orm/entity-context.js';
45
+ export type { PrimaryKey as EntityPrimaryKey } from './orm/entity-context.js';
46
+ export * from './orm/execution-context.js';
47
+ export * from './orm/hydration-context.js';
48
+ export * from './orm/domain-event-bus.js';
49
+ export * from './orm/runtime-types.js';
50
+ export * from './orm/query-logger.js';
51
+ export * from './orm/interceptor-pipeline.js';
52
+ export * from './orm/jsonify.js';
53
+ export * from './orm/save-graph-types.js';
54
+ export * from './decorators/index.js';
55
+
56
+ // NEW: execution abstraction + helpers
57
+ export * from './core/execution/db-executor.js';
58
+ export * from './core/execution/pooling/pool-types.js';
59
+ export * from './core/execution/pooling/pool.js';
60
+ export * from './core/execution/executors/postgres-executor.js';
61
+ export * from './core/execution/executors/mysql-executor.js';
62
+ export * from './core/execution/executors/sqlite-executor.js';
63
+ export * from './core/execution/executors/mssql-executor.js';
64
+
65
+ // NEW: first-class pooling integration
66
+ export * from './orm/pooled-executor-factory.js';
67
+
68
+ // DTO module for REST API integration
69
+ export * from './dto/index.js';
@@ -2,9 +2,9 @@ import { Dialect } from '../core/dialect/abstract.js';
2
2
  import type { DbExecutor } from '../core/execution/db-executor.js';
3
3
  import { TableDef } from '../schema/table.js';
4
4
  import { RelationDef } from '../schema/relation.js';
5
- import { RelationChange, RelationKey, TrackedEntity } from './runtime-types.js';
6
-
7
- export type PrimaryKey = string | number;
5
+ import { RelationChange, RelationKey, TrackedEntity } from './runtime-types.js';
6
+
7
+ export type PrimaryKey = string | number;
8
8
 
9
9
  /**
10
10
  * Interface for entity context providing entity tracking and management.
@@ -21,7 +21,7 @@ export interface EntityContext {
21
21
  * @param pk - The primary key value
22
22
  * @returns The entity or undefined
23
23
  */
24
- getEntity(table: TableDef, pk: PrimaryKey): object | undefined;
24
+ getEntity(table: TableDef, pk: PrimaryKey): object | undefined;
25
25
 
26
26
  /**
27
27
  * Sets an entity in the context.
@@ -29,7 +29,7 @@ export interface EntityContext {
29
29
  * @param pk - The primary key value
30
30
  * @param entity - The entity to set
31
31
  */
32
- setEntity(table: TableDef, pk: PrimaryKey, entity: object): void;
32
+ setEntity(table: TableDef, pk: PrimaryKey, entity: object): void;
33
33
 
34
34
  /**
35
35
  * Tracks a new entity.
@@ -37,7 +37,7 @@ export interface EntityContext {
37
37
  * @param entity - The new entity
38
38
  * @param pk - Optional primary key
39
39
  */
40
- trackNew(table: TableDef, entity: object, pk?: PrimaryKey): void;
40
+ trackNew(table: TableDef, entity: object, pk?: PrimaryKey): void;
41
41
 
42
42
  /**
43
43
  * Tracks a managed entity.
@@ -45,19 +45,19 @@ export interface EntityContext {
45
45
  * @param pk - The primary key
46
46
  * @param entity - The managed entity
47
47
  */
48
- trackManaged(table: TableDef, pk: PrimaryKey, entity: object): void;
48
+ trackManaged(table: TableDef, pk: PrimaryKey, entity: object): void;
49
49
 
50
50
  /**
51
51
  * Marks an entity as dirty.
52
52
  * @param entity - The entity to mark
53
53
  */
54
- markDirty(entity: object): void;
54
+ markDirty(entity: object): void;
55
55
 
56
56
  /**
57
57
  * Marks an entity as removed.
58
58
  * @param entity - The entity to mark
59
59
  */
60
- markRemoved(entity: object): void;
60
+ markRemoved(entity: object): void;
61
61
 
62
62
  /**
63
63
  * Gets all tracked entities for a table.