metal-orm 1.0.90 → 1.0.92

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 (37) hide show
  1. package/dist/index.cjs +214 -118
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +71 -32
  4. package/dist/index.d.ts +71 -32
  5. package/dist/index.js +206 -118
  6. package/dist/index.js.map +1 -1
  7. package/package.json +4 -2
  8. package/scripts/generate-entities/render.mjs +16 -3
  9. package/src/core/ddl/introspect/utils.ts +45 -45
  10. package/src/decorators/bootstrap.ts +37 -37
  11. package/src/decorators/column-decorator.ts +3 -1
  12. package/src/dto/apply-filter.ts +279 -281
  13. package/src/dto/dto-types.ts +229 -229
  14. package/src/dto/filter-types.ts +193 -193
  15. package/src/dto/index.ts +97 -97
  16. package/src/dto/openapi/generators/base.ts +29 -29
  17. package/src/dto/openapi/generators/column.ts +37 -34
  18. package/src/dto/openapi/generators/dto.ts +94 -94
  19. package/src/dto/openapi/generators/filter.ts +75 -74
  20. package/src/dto/openapi/generators/nested-dto.ts +618 -532
  21. package/src/dto/openapi/generators/pagination.ts +111 -111
  22. package/src/dto/openapi/generators/relation-filter.ts +228 -210
  23. package/src/dto/openapi/index.ts +17 -17
  24. package/src/dto/openapi/type-mappings.ts +191 -191
  25. package/src/dto/openapi/types.ts +101 -83
  26. package/src/dto/openapi/utilities.ts +90 -45
  27. package/src/dto/pagination-utils.ts +150 -150
  28. package/src/dto/transform.ts +197 -193
  29. package/src/index.ts +69 -69
  30. package/src/orm/entity-context.ts +9 -9
  31. package/src/orm/entity-metadata.ts +14 -14
  32. package/src/orm/entity.ts +74 -74
  33. package/src/orm/orm-session.ts +159 -159
  34. package/src/orm/relation-change-processor.ts +3 -3
  35. package/src/orm/runtime-types.ts +5 -5
  36. package/src/schema/column-types.ts +4 -4
  37. package/src/schema/types.ts +5 -1
@@ -1,193 +1,193 @@
1
- /**
2
- * Filter types for building type-safe query filters from JSON input.
3
- * Designed for REST API query parameters.
4
- */
5
-
6
- import type { TableDef } from '../schema/table.js';
7
- import type { ColumnDef } from '../schema/column-types.js';
8
- import type { EntityConstructor } from '../orm/entity-metadata.js';
9
-
10
- // ─────────────────────────────────────────────────────────────────────────────
11
- // Entity support: Extract columns from entity constructor
12
- // ─────────────────────────────────────────────────────────────────────────────
13
-
14
- /**
15
- * Checks if a type is a TableDef.
16
- */
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- type IsTableDef<T> = T extends { name: string; columns: any } ? true : false;
19
-
20
- /**
21
- * Maps TypeScript types to SQL column types for EntityConstructor columns.
22
- */
23
- type TsTypeToColumnType<T> =
24
- T extends string ? 'VARCHAR' :
25
- T extends number ? 'INT' :
26
- T extends boolean ? 'BOOLEAN' :
27
- T extends Date ? 'TIMESTAMP' :
28
- 'VARCHAR';
29
-
30
- /**
31
- * Extracts the column map from either a TableDef or EntityConstructor.
32
- * For EntityConstructor, infers column type from TypeScript property type.
33
- */
34
- type ExtractColumns<T> = IsTableDef<T> extends true
35
- ? T extends TableDef<infer C> ? C : never
36
- : T extends EntityConstructor<infer E>
37
- ? {
38
- [K in keyof E]: ColumnDef<TsTypeToColumnType<E[K]>, E[K]>;
39
- }
40
- : never;
41
-
42
- // ─────────────────────────────────────────────────────────────────────────────
43
- // Scalar filter types per data type
44
- // ─────────────────────────────────────────────────────────────────────────────
45
-
46
- /**
47
- * Filter operators for string columns.
48
- */
49
- export interface StringFilter {
50
- equals?: string;
51
- not?: string;
52
- in?: string[];
53
- notIn?: string[];
54
- contains?: string;
55
- startsWith?: string;
56
- endsWith?: string;
57
- /** Case-insensitive matching (for contains, startsWith, endsWith) */
58
- mode?: 'default' | 'insensitive';
59
- }
60
-
61
- /**
62
- * Filter operators for numeric columns (number, bigint).
63
- */
64
- export interface NumberFilter {
65
- equals?: number;
66
- not?: number;
67
- in?: number[];
68
- notIn?: number[];
69
- lt?: number;
70
- lte?: number;
71
- gt?: number;
72
- gte?: number;
73
- }
74
-
75
- /**
76
- * Filter operators for boolean columns.
77
- */
78
- export interface BooleanFilter {
79
- equals?: boolean;
80
- not?: boolean;
81
- }
82
-
83
- /**
84
- * Filter operators for date/datetime columns.
85
- * Accepts ISO date strings.
86
- */
87
- export interface DateFilter {
88
- equals?: string;
89
- not?: string;
90
- in?: string[];
91
- notIn?: string[];
92
- lt?: string;
93
- lte?: string;
94
- gt?: string;
95
- gte?: string;
96
- }
97
-
98
- // ─────────────────────────────────────────────────────────────────────────────
99
- // Type mapping: ColumnDef → appropriate filter type
100
- // ─────────────────────────────────────────────────────────────────────────────
101
-
102
- type NormalizedType<T extends ColumnDef> = Lowercase<T['type'] & string>;
103
-
104
- /**
105
- * Maps a column definition to its appropriate filter type.
106
- */
107
- export type FieldFilter<TCol extends ColumnDef> =
108
- NormalizedType<TCol> extends 'int' | 'integer' | 'bigint' | 'decimal' | 'float' | 'double'
109
- ? NumberFilter
110
- : NormalizedType<TCol> extends 'boolean'
111
- ? BooleanFilter
112
- : NormalizedType<TCol> extends 'date' | 'datetime' | 'timestamp' | 'timestamptz'
113
- ? DateFilter
114
- : StringFilter; // default to string for varchar, text, uuid, etc.
115
-
116
- // ─────────────────────────────────────────────────────────────────────────────
117
- // SimpleWhereInput: field-only filters (no AND/OR/NOT logic)
118
- // ─────────────────────────────────────────────────────────────────────────────
119
-
120
- /**
121
- * Full where input with all columns filterable.
122
- * All conditions are implicitly AND-ed.
123
- * Works with both TableDef and EntityConstructor.
124
- *
125
- * @example
126
- * ```ts
127
- * // With TableDef
128
- * type UserFilter = WhereInput<typeof usersTable>;
129
- *
130
- * // With Entity class
131
- * type UserFilter = WhereInput<User>;
132
- * ```
133
- */
134
- export type WhereInput<T extends TableDef | EntityConstructor> = {
135
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
136
- [K in keyof ExtractColumns<T>]?: ExtractColumns<T>[K] extends ColumnDef<any, any>
137
- ? FieldFilter<ExtractColumns<T>[K]>
138
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
139
- : FieldFilter<ColumnDef<any, any>>;
140
- };
141
-
142
- /**
143
- * Restricted where input - only specified columns are filterable.
144
- * Use this to limit which fields can be filtered via API.
145
- *
146
- * Works with both TableDef and EntityConstructor.
147
- *
148
- * @example
149
- * ```ts
150
- * // With TableDef - only allow filtering by name and email
151
- * type UserFilter = SimpleWhereInput<typeof usersTable, 'name' | 'email'>;
152
- *
153
- * // With Entity class
154
- * type UserFilter = SimpleWhereInput<User, 'name' | 'email'>;
155
- *
156
- * // Request: { "name": { "contains": "john" }, "email": { "endsWith": "@gmail.com" } }
157
- * ```
158
- */
159
- export type SimpleWhereInput<
160
- T extends TableDef | EntityConstructor,
161
- K extends keyof ExtractColumns<T>
162
- > = {
163
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
- [P in K]?: ExtractColumns<T>[P] extends ColumnDef<any, any>
165
- ? FieldFilter<ExtractColumns<T>[P]>
166
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
167
- : FieldFilter<ColumnDef<any, any>>;
168
- };
169
-
170
- // ─────────────────────────────────────────────────────────────────────────────
171
- // Filter operator value types (for runtime processing)
172
- // ─────────────────────────────────────────────────────────────────────────────
173
-
174
- /**
175
- * All possible filter operators.
176
- */
177
- export type FilterOperator =
178
- | 'equals'
179
- | 'not'
180
- | 'in'
181
- | 'notIn'
182
- | 'lt'
183
- | 'lte'
184
- | 'gt'
185
- | 'gte'
186
- | 'contains'
187
- | 'startsWith'
188
- | 'endsWith';
189
-
190
- /**
191
- * Generic filter value that covers all operator types.
192
- */
193
- export type FilterValue = StringFilter | NumberFilter | BooleanFilter | DateFilter;
1
+ /**
2
+ * Filter types for building type-safe query filters from JSON input.
3
+ * Designed for REST API query parameters.
4
+ */
5
+
6
+ import type { TableDef } from '../schema/table.js';
7
+ import type { ColumnDef } from '../schema/column-types.js';
8
+ import type { EntityConstructor } from '../orm/entity-metadata.js';
9
+
10
+ // ─────────────────────────────────────────────────────────────────────────────
11
+ // Entity support: Extract columns from entity constructor
12
+ // ─────────────────────────────────────────────────────────────────────────────
13
+
14
+ /**
15
+ * Checks if a type is a TableDef.
16
+ */
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ type IsTableDef<T> = T extends { name: string; columns: any } ? true : false;
19
+
20
+ /**
21
+ * Maps TypeScript types to SQL column types for EntityConstructor columns.
22
+ */
23
+ type TsTypeToColumnType<T> =
24
+ T extends string ? 'VARCHAR' :
25
+ T extends number ? 'INT' :
26
+ T extends boolean ? 'BOOLEAN' :
27
+ T extends Date ? 'TIMESTAMP' :
28
+ 'VARCHAR';
29
+
30
+ /**
31
+ * Extracts the column map from either a TableDef or EntityConstructor.
32
+ * For EntityConstructor, infers column type from TypeScript property type.
33
+ */
34
+ type ExtractColumns<T> = IsTableDef<T> extends true
35
+ ? T extends TableDef<infer C> ? C : never
36
+ : T extends EntityConstructor<infer E>
37
+ ? {
38
+ [K in keyof E]: ColumnDef<TsTypeToColumnType<E[K]>, E[K]>;
39
+ }
40
+ : never;
41
+
42
+ // ─────────────────────────────────────────────────────────────────────────────
43
+ // Scalar filter types per data type
44
+ // ─────────────────────────────────────────────────────────────────────────────
45
+
46
+ /**
47
+ * Filter operators for string columns.
48
+ */
49
+ export interface StringFilter {
50
+ equals?: string;
51
+ not?: string;
52
+ in?: string[];
53
+ notIn?: string[];
54
+ contains?: string;
55
+ startsWith?: string;
56
+ endsWith?: string;
57
+ /** Case-insensitive matching (for contains, startsWith, endsWith) */
58
+ mode?: 'default' | 'insensitive';
59
+ }
60
+
61
+ /**
62
+ * Filter operators for numeric columns (number, bigint).
63
+ */
64
+ export interface NumberFilter {
65
+ equals?: number;
66
+ not?: number;
67
+ in?: number[];
68
+ notIn?: number[];
69
+ lt?: number;
70
+ lte?: number;
71
+ gt?: number;
72
+ gte?: number;
73
+ }
74
+
75
+ /**
76
+ * Filter operators for boolean columns.
77
+ */
78
+ export interface BooleanFilter {
79
+ equals?: boolean;
80
+ not?: boolean;
81
+ }
82
+
83
+ /**
84
+ * Filter operators for date/datetime columns.
85
+ * Accepts ISO date strings.
86
+ */
87
+ export interface DateFilter {
88
+ equals?: string;
89
+ not?: string;
90
+ in?: string[];
91
+ notIn?: string[];
92
+ lt?: string;
93
+ lte?: string;
94
+ gt?: string;
95
+ gte?: string;
96
+ }
97
+
98
+ // ─────────────────────────────────────────────────────────────────────────────
99
+ // Type mapping: ColumnDef → appropriate filter type
100
+ // ─────────────────────────────────────────────────────────────────────────────
101
+
102
+ type NormalizedType<T extends ColumnDef> = Lowercase<T['type'] & string>;
103
+
104
+ /**
105
+ * Maps a column definition to its appropriate filter type.
106
+ */
107
+ export type FieldFilter<TCol extends ColumnDef> =
108
+ NormalizedType<TCol> extends 'int' | 'integer' | 'bigint' | 'decimal' | 'float' | 'double'
109
+ ? NumberFilter
110
+ : NormalizedType<TCol> extends 'boolean'
111
+ ? BooleanFilter
112
+ : NormalizedType<TCol> extends 'date' | 'datetime' | 'timestamp' | 'timestamptz'
113
+ ? DateFilter
114
+ : StringFilter; // default to string for varchar, text, uuid, etc.
115
+
116
+ // ─────────────────────────────────────────────────────────────────────────────
117
+ // SimpleWhereInput: field-only filters (no AND/OR/NOT logic)
118
+ // ─────────────────────────────────────────────────────────────────────────────
119
+
120
+ /**
121
+ * Full where input with all columns filterable.
122
+ * All conditions are implicitly AND-ed.
123
+ * Works with both TableDef and EntityConstructor.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * // With TableDef
128
+ * type UserFilter = WhereInput<typeof usersTable>;
129
+ *
130
+ * // With Entity class
131
+ * type UserFilter = WhereInput<User>;
132
+ * ```
133
+ */
134
+ export type WhereInput<T extends TableDef | EntityConstructor> = {
135
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
136
+ [K in keyof ExtractColumns<T>]?: ExtractColumns<T>[K] extends ColumnDef<any, any>
137
+ ? FieldFilter<ExtractColumns<T>[K]>
138
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
139
+ : FieldFilter<ColumnDef<any, any>>;
140
+ };
141
+
142
+ /**
143
+ * Restricted where input - only specified columns are filterable.
144
+ * Use this to limit which fields can be filtered via API.
145
+ *
146
+ * Works with both TableDef and EntityConstructor.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * // With TableDef - only allow filtering by name and email
151
+ * type UserFilter = SimpleWhereInput<typeof usersTable, 'name' | 'email'>;
152
+ *
153
+ * // With Entity class
154
+ * type UserFilter = SimpleWhereInput<User, 'name' | 'email'>;
155
+ *
156
+ * // Request: { "name": { "contains": "john" }, "email": { "endsWith": "@gmail.com" } }
157
+ * ```
158
+ */
159
+ export type SimpleWhereInput<
160
+ T extends TableDef | EntityConstructor,
161
+ K extends keyof ExtractColumns<T>
162
+ > = {
163
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
+ [P in K]?: ExtractColumns<T>[P] extends ColumnDef<any, any>
165
+ ? FieldFilter<ExtractColumns<T>[P]>
166
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
167
+ : FieldFilter<ColumnDef<any, any>>;
168
+ };
169
+
170
+ // ─────────────────────────────────────────────────────────────────────────────
171
+ // Filter operator value types (for runtime processing)
172
+ // ─────────────────────────────────────────────────────────────────────────────
173
+
174
+ /**
175
+ * All possible filter operators.
176
+ */
177
+ export type FilterOperator =
178
+ | 'equals'
179
+ | 'not'
180
+ | 'in'
181
+ | 'notIn'
182
+ | 'lt'
183
+ | 'lte'
184
+ | 'gt'
185
+ | 'gte'
186
+ | 'contains'
187
+ | 'startsWith'
188
+ | 'endsWith';
189
+
190
+ /**
191
+ * Generic filter value that covers all operator types.
192
+ */
193
+ export type FilterValue = StringFilter | NumberFilter | BooleanFilter | DateFilter;
package/src/dto/index.ts CHANGED
@@ -1,97 +1,97 @@
1
- /**
2
- * DTO (Data Transfer Object) module for metal-orm.
3
- *
4
- * Provides type utilities and runtime helpers for building REST APIs
5
- * with automatic DTO generation from entity/table definitions.
6
- *
7
- * Supports both TableDef and Entity classes as type sources.
8
- *
9
- * @example
10
- * ```ts
11
- * import { Dto, WithRelations, CreateDto, UpdateDto, SimpleWhereInput, applyFilter, PagedResponse, toPagedResponse } from 'metal-orm/dto';
12
- *
13
- * // Using Entity classes
14
- * type UserResponse = Dto<User, 'passwordHash'>;
15
- * type PostResponse = Dto<Post, 'authorId'>;
16
- * type UserWithPosts = WithRelations<UserResponse, { posts: PostResponse[] }>;
17
- * type CreateUserDto = CreateDto<User>;
18
- * type UpdateUserDto = UpdateDto<User>;
19
- * type UserFilter = SimpleWhereInput<User, 'name' | 'email'>;
20
- *
21
- * // Enhanced pagination
22
- * type UsersPagedResponse = PagedResponse<UserResponse>;
23
- *
24
- * // Using TableDef directly
25
- * type UserResponse = Dto<typeof UserTable, 'passwordHash'>;
26
- * type PostResponse = Dto<typeof PostTable, 'authorId'>;
27
- * type UserWithPosts = WithRelations<UserResponse, { posts: PostResponse[] }>;
28
- * type CreateUserDto = CreateDto<typeof UserTable>;
29
- * type UpdateUserDto = UpdateDto<typeof UserTable>;
30
- * type UserFilter = SimpleWhereInput<typeof UserTable, 'name' | 'email'>;
31
- *
32
- * // Apply filter in controller
33
- * let query = selectFromEntity(User);
34
- * query = applyFilter(query, User, where);
35
- *
36
- * // Apply enhanced pagination
37
- * const basic = await qb.executePaged(session, { page: 2, pageSize: 20 });
38
- * const response = toPagedResponse(basic);
39
- * // → { items: [...], totalItems: 150, page: 2, pageSize: 20,
40
- * // totalPages: 8, hasNextPage: true, hasPrevPage: true }
41
- * ```
42
- *
43
- * @packageDocumentation
44
- */
45
-
46
- // DTO type utilities
47
- export type {
48
- Simplify,
49
- Dto,
50
- WithRelations,
51
- CreateDto,
52
- UpdateDto,
53
- PagedResponse
54
- } from './dto-types.js';
55
-
56
- // Filter types
57
- export type {
58
- StringFilter,
59
- NumberFilter,
60
- BooleanFilter,
61
- DateFilter,
62
- FieldFilter,
63
- WhereInput,
64
- SimpleWhereInput,
65
- FilterOperator,
66
- FilterValue
67
- } from './filter-types.js';
68
-
69
- // Runtime filter application
70
- export {
71
- applyFilter,
72
- buildFilterExpression
73
- } from './apply-filter.js';
74
-
75
- // Transformation utilities
76
- export {
77
- toResponse,
78
- toResponseBuilder,
79
- withDefaults,
80
- withDefaultsBuilder,
81
- exclude,
82
- pick,
83
- mapFields
84
- } from './transform.js';
85
-
86
- // Pagination utilities
87
- export {
88
- toPagedResponse,
89
- toPagedResponseBuilder,
90
- calculateTotalPages,
91
- hasNextPage as hasNextPageMeta,
92
- hasPrevPage as hasPrevPageMeta,
93
- computePaginationMetadata
94
- } from './pagination-utils.js';
95
-
96
- // OpenAPI 3.1 Schema generation
97
- export * from './openapi/index.js';
1
+ /**
2
+ * DTO (Data Transfer Object) module for metal-orm.
3
+ *
4
+ * Provides type utilities and runtime helpers for building REST APIs
5
+ * with automatic DTO generation from entity/table definitions.
6
+ *
7
+ * Supports both TableDef and Entity classes as type sources.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { Dto, WithRelations, CreateDto, UpdateDto, SimpleWhereInput, applyFilter, PagedResponse, toPagedResponse } from 'metal-orm/dto';
12
+ *
13
+ * // Using Entity classes
14
+ * type UserResponse = Dto<User, 'passwordHash'>;
15
+ * type PostResponse = Dto<Post, 'authorId'>;
16
+ * type UserWithPosts = WithRelations<UserResponse, { posts: PostResponse[] }>;
17
+ * type CreateUserDto = CreateDto<User>;
18
+ * type UpdateUserDto = UpdateDto<User>;
19
+ * type UserFilter = SimpleWhereInput<User, 'name' | 'email'>;
20
+ *
21
+ * // Enhanced pagination
22
+ * type UsersPagedResponse = PagedResponse<UserResponse>;
23
+ *
24
+ * // Using TableDef directly
25
+ * type UserResponse = Dto<typeof UserTable, 'passwordHash'>;
26
+ * type PostResponse = Dto<typeof PostTable, 'authorId'>;
27
+ * type UserWithPosts = WithRelations<UserResponse, { posts: PostResponse[] }>;
28
+ * type CreateUserDto = CreateDto<typeof UserTable>;
29
+ * type UpdateUserDto = UpdateDto<typeof UserTable>;
30
+ * type UserFilter = SimpleWhereInput<typeof UserTable, 'name' | 'email'>;
31
+ *
32
+ * // Apply filter in controller
33
+ * let query = selectFromEntity(User);
34
+ * query = applyFilter(query, User, where);
35
+ *
36
+ * // Apply enhanced pagination
37
+ * const basic = await qb.executePaged(session, { page: 2, pageSize: 20 });
38
+ * const response = toPagedResponse(basic);
39
+ * // → { items: [...], totalItems: 150, page: 2, pageSize: 20,
40
+ * // totalPages: 8, hasNextPage: true, hasPrevPage: true }
41
+ * ```
42
+ *
43
+ * @packageDocumentation
44
+ */
45
+
46
+ // DTO type utilities
47
+ export type {
48
+ Simplify,
49
+ Dto,
50
+ WithRelations,
51
+ CreateDto,
52
+ UpdateDto,
53
+ PagedResponse
54
+ } from './dto-types.js';
55
+
56
+ // Filter types
57
+ export type {
58
+ StringFilter,
59
+ NumberFilter,
60
+ BooleanFilter,
61
+ DateFilter,
62
+ FieldFilter,
63
+ WhereInput,
64
+ SimpleWhereInput,
65
+ FilterOperator,
66
+ FilterValue
67
+ } from './filter-types.js';
68
+
69
+ // Runtime filter application
70
+ export {
71
+ applyFilter,
72
+ buildFilterExpression
73
+ } from './apply-filter.js';
74
+
75
+ // Transformation utilities
76
+ export {
77
+ toResponse,
78
+ toResponseBuilder,
79
+ withDefaults,
80
+ withDefaultsBuilder,
81
+ exclude,
82
+ pick,
83
+ mapFields
84
+ } from './transform.js';
85
+
86
+ // Pagination utilities
87
+ export {
88
+ toPagedResponse,
89
+ toPagedResponseBuilder,
90
+ calculateTotalPages,
91
+ hasNextPage as hasNextPageMeta,
92
+ hasPrevPage as hasPrevPageMeta,
93
+ computePaginationMetadata
94
+ } from './pagination-utils.js';
95
+
96
+ // OpenAPI 3.1 Schema generation
97
+ export * from './openapi/index.js';
@@ -1,29 +1,29 @@
1
- import type { TableDef } from '../../../schema/table.js';
2
- import type { ColumnDef } from '../../../schema/column-types.js';
3
- import type { EntityConstructor } from '../../../orm/entity-metadata.js';
4
- import { getEntityMetadata } from '../../../orm/entity-metadata.js';
5
-
6
- export function isTableDef(target: TableDef | EntityConstructor): target is TableDef {
7
- return 'columns' in target && 'name' in target;
8
- }
9
-
10
- export function getColumnMap(target: TableDef | EntityConstructor): Record<string, ColumnDef> {
11
- if (isTableDef(target)) {
12
- return target.columns;
13
- }
14
- const meta = getEntityMetadata(target);
15
- if (meta && meta.columns) {
16
- const columns: Record<string, ColumnDef> = {};
17
- for (const [key, def] of Object.entries(meta.columns)) {
18
- columns[key] = {
19
- ...def as object,
20
- name: key,
21
- table: meta.tableName
22
- } as ColumnDef;
23
- }
24
- return columns;
25
- }
26
- return {};
27
- }
28
-
29
- export type TargetType<T extends TableDef | EntityConstructor> = T;
1
+ import type { TableDef } from '../../../schema/table.js';
2
+ import type { ColumnDef } from '../../../schema/column-types.js';
3
+ import type { EntityConstructor } from '../../../orm/entity-metadata.js';
4
+ import { getEntityMetadata } from '../../../orm/entity-metadata.js';
5
+
6
+ export function isTableDef(target: TableDef | EntityConstructor): target is TableDef {
7
+ return 'columns' in target && 'name' in target;
8
+ }
9
+
10
+ export function getColumnMap(target: TableDef | EntityConstructor): Record<string, ColumnDef> {
11
+ if (isTableDef(target)) {
12
+ return target.columns;
13
+ }
14
+ const meta = getEntityMetadata(target);
15
+ if (meta && meta.columns) {
16
+ const columns: Record<string, ColumnDef> = {};
17
+ for (const [key, def] of Object.entries(meta.columns)) {
18
+ columns[key] = {
19
+ ...def as object,
20
+ name: key,
21
+ table: meta.tableName
22
+ } as ColumnDef;
23
+ }
24
+ return columns;
25
+ }
26
+ return {};
27
+ }
28
+
29
+ export type TargetType<T extends TableDef | EntityConstructor> = T;