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,34 +1,37 @@
1
- import type { ColumnDef } from '../../../schema/column-types.js';
2
- import type { OpenApiSchema } from '../types.js';
3
- import { columnTypeToOpenApiType, columnTypeToOpenApiFormat } from '../type-mappings.js';
4
-
5
- export function columnToOpenApiSchema(col: ColumnDef): OpenApiSchema {
6
- const schema: OpenApiSchema = {
7
- type: columnTypeToOpenApiType(col),
8
- format: columnTypeToOpenApiFormat(col),
9
- };
10
-
11
- if (!col.notNull) {
12
- schema.nullable = true;
13
- }
14
-
15
- if (col.comment) {
16
- schema.description = col.comment;
17
- }
18
-
19
- if (col.type.toUpperCase() === 'ENUM' && col.args && Array.isArray(col.args) && col.args.every(v => typeof v === 'string')) {
20
- schema.enum = col.args as string[];
21
- }
22
-
23
- const args = col.args;
24
- if (args && args.length > 0) {
25
- if (col.type.toUpperCase() === 'VARCHAR' || col.type.toUpperCase() === 'CHAR') {
26
- const length = args[0] as number;
27
- if (length) {
28
- schema.example = 'a'.repeat(length);
29
- }
30
- }
31
- }
32
-
33
- return schema;
34
- }
1
+ import type { ColumnDef } from '../../../schema/column-types.js';
2
+ import type { OpenApiSchema, OpenApiDialect } from '../types.js';
3
+ import { columnTypeToOpenApiType, columnTypeToOpenApiFormat } from '../type-mappings.js';
4
+ import { applyNullability, isNullableColumn } from '../utilities.js';
5
+
6
+ export function columnToOpenApiSchema(
7
+ col: ColumnDef,
8
+ dialect: OpenApiDialect = 'openapi-3.1'
9
+ ): OpenApiSchema {
10
+ const schema: OpenApiSchema = {
11
+ type: columnTypeToOpenApiType(col),
12
+ format: columnTypeToOpenApiFormat(col),
13
+ };
14
+
15
+ const nullable = isNullableColumn(col);
16
+ const result = applyNullability(schema, nullable, dialect);
17
+
18
+ if (col.comment) {
19
+ result.description = col.comment;
20
+ }
21
+
22
+ if (col.type.toUpperCase() === 'ENUM' && col.args && Array.isArray(col.args) && col.args.every(v => typeof v === 'string')) {
23
+ result.enum = col.args as string[];
24
+ }
25
+
26
+ const args = col.args;
27
+ if (args && args.length > 0) {
28
+ if (col.type.toUpperCase() === 'VARCHAR' || col.type.toUpperCase() === 'CHAR') {
29
+ const length = args[0] as number;
30
+ if (length) {
31
+ result.example = 'a'.repeat(length);
32
+ }
33
+ }
34
+ }
35
+
36
+ return result;
37
+ }
@@ -1,94 +1,94 @@
1
- import type { TableDef } from '../../../schema/table.js';
2
- import type { EntityConstructor } from '../../../orm/entity-metadata.js';
3
- import type { OpenApiSchema } from '../types.js';
4
- import { columnToOpenApiSchema } from './column.js';
5
- import { getColumnMap } from './base.js';
6
-
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- export function dtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
9
- target: T,
10
- exclude?: TExclude[]
11
- ): OpenApiSchema {
12
- const columns = getColumnMap(target);
13
- const properties: Record<string, OpenApiSchema> = {};
14
- const required: string[] = [];
15
-
16
- for (const [key, col] of Object.entries(columns)) {
17
- if (exclude?.includes(key as TExclude)) {
18
- continue;
19
- }
20
-
21
- properties[key] = columnToOpenApiSchema(col);
22
-
23
- if (col.notNull || col.primary) {
24
- required.push(key);
25
- }
26
- }
27
-
28
- return {
29
- type: 'object',
30
- properties,
31
- ...(required.length > 0 && { required }),
32
- };
33
- }
34
-
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- export function createDtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
37
- target: T,
38
- exclude?: TExclude[]
39
- ): OpenApiSchema {
40
- const columns = getColumnMap(target);
41
- const properties: Record<string, OpenApiSchema> = {};
42
- const required: string[] = [];
43
-
44
- for (const [key, col] of Object.entries(columns)) {
45
- if (exclude?.includes(key as TExclude)) {
46
- continue;
47
- }
48
-
49
- if (col.autoIncrement || col.generated) {
50
- continue;
51
- }
52
-
53
- properties[key] = columnToOpenApiSchema(col);
54
-
55
- if (col.notNull && !col.default) {
56
- required.push(key);
57
- }
58
- }
59
-
60
- return {
61
- type: 'object',
62
- properties,
63
- ...(required.length > 0 && { required }),
64
- };
65
- }
66
-
67
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
- export function updateDtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
69
- target: T,
70
- exclude?: TExclude[]
71
- ): OpenApiSchema {
72
- const columns = getColumnMap(target);
73
- const properties: Record<string, OpenApiSchema> = {};
74
-
75
- for (const [key, col] of Object.entries(columns)) {
76
- if (exclude?.includes(key as TExclude)) {
77
- continue;
78
- }
79
-
80
- if (col.autoIncrement || col.generated) {
81
- continue;
82
- }
83
-
84
- properties[key] = {
85
- ...columnToOpenApiSchema(col),
86
- ...(!col.notNull ? { nullable: true } : {}),
87
- };
88
- }
89
-
90
- return {
91
- type: 'object',
92
- properties,
93
- };
94
- }
1
+ import type { TableDef } from '../../../schema/table.js';
2
+ import type { EntityConstructor } from '../../../orm/entity-metadata.js';
3
+ import type { OpenApiSchema, OpenApiDialect } from '../types.js';
4
+ import { columnToOpenApiSchema } from './column.js';
5
+ import { getColumnMap } from './base.js';
6
+
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ export function dtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
9
+ target: T,
10
+ exclude?: TExclude[],
11
+ dialect: OpenApiDialect = 'openapi-3.1'
12
+ ): OpenApiSchema {
13
+ const columns = getColumnMap(target);
14
+ const properties: Record<string, OpenApiSchema> = {};
15
+ const required: string[] = [];
16
+
17
+ for (const [key, col] of Object.entries(columns)) {
18
+ if (exclude?.includes(key as TExclude)) {
19
+ continue;
20
+ }
21
+
22
+ properties[key] = columnToOpenApiSchema(col, dialect);
23
+
24
+ if (col.notNull || col.primary) {
25
+ required.push(key);
26
+ }
27
+ }
28
+
29
+ return {
30
+ type: 'object',
31
+ properties,
32
+ ...(required.length > 0 && { required }),
33
+ };
34
+ }
35
+
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ export function createDtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
38
+ target: T,
39
+ exclude?: TExclude[],
40
+ dialect: OpenApiDialect = 'openapi-3.1'
41
+ ): OpenApiSchema {
42
+ const columns = getColumnMap(target);
43
+ const properties: Record<string, OpenApiSchema> = {};
44
+ const required: string[] = [];
45
+
46
+ for (const [key, col] of Object.entries(columns)) {
47
+ if (exclude?.includes(key as TExclude)) {
48
+ continue;
49
+ }
50
+
51
+ if (col.autoIncrement || col.generated) {
52
+ continue;
53
+ }
54
+
55
+ properties[key] = columnToOpenApiSchema(col, dialect);
56
+
57
+ if (col.notNull && !col.default) {
58
+ required.push(key);
59
+ }
60
+ }
61
+
62
+ return {
63
+ type: 'object',
64
+ properties,
65
+ ...(required.length > 0 && { required }),
66
+ };
67
+ }
68
+
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ export function updateDtoToOpenApiSchema<T extends TableDef | EntityConstructor, TExclude extends keyof any>(
71
+ target: T,
72
+ exclude?: TExclude[],
73
+ dialect: OpenApiDialect = 'openapi-3.1'
74
+ ): OpenApiSchema {
75
+ const columns = getColumnMap(target);
76
+ const properties: Record<string, OpenApiSchema> = {};
77
+
78
+ for (const [key, col] of Object.entries(columns)) {
79
+ if (exclude?.includes(key as TExclude)) {
80
+ continue;
81
+ }
82
+
83
+ if (col.autoIncrement || col.generated) {
84
+ continue;
85
+ }
86
+
87
+ properties[key] = columnToOpenApiSchema(col, dialect);
88
+ }
89
+
90
+ return {
91
+ type: 'object',
92
+ properties,
93
+ };
94
+ }
@@ -1,74 +1,75 @@
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 type { OpenApiSchema } from '../types.js';
5
- import { columnTypeToOpenApiType } from '../type-mappings.js';
6
- import { getColumnMap } from './base.js';
7
-
8
- function filterFieldToOpenApiSchema(col: ColumnDef): OpenApiSchema {
9
- const normalizedType = col.type.toUpperCase();
10
- columnTypeToOpenApiType(col);
11
-
12
- let filterProperties: Record<string, OpenApiSchema> = {};
13
-
14
- if (['INT', 'INTEGER', 'BIGINT', 'DECIMAL', 'FLOAT', 'DOUBLE'].includes(normalizedType)) {
15
- filterProperties = {
16
- equals: { type: 'number' },
17
- not: { type: 'number' },
18
- in: { type: 'array', items: { type: 'number' } },
19
- notIn: { type: 'array', items: { type: 'number' } },
20
- lt: { type: 'number' },
21
- lte: { type: 'number' },
22
- gt: { type: 'number' },
23
- gte: { type: 'number' },
24
- };
25
- } else if (['BOOLEAN'].includes(normalizedType)) {
26
- filterProperties = {
27
- equals: { type: 'boolean' },
28
- not: { type: 'boolean' },
29
- };
30
- } else if (['DATE', 'DATETIME', 'TIMESTAMP', 'TIMESTAMPTZ'].includes(normalizedType)) {
31
- filterProperties = {
32
- equals: { type: 'string', format: 'date-time' },
33
- not: { type: 'string', format: 'date-time' },
34
- in: { type: 'array', items: { type: 'string', format: 'date-time' } },
35
- notIn: { type: 'array', items: { type: 'string', format: 'date-time' } },
36
- lt: { type: 'string', format: 'date-time' },
37
- lte: { type: 'string', format: 'date-time' },
38
- gt: { type: 'string', format: 'date-time' },
39
- gte: { type: 'string', format: 'date-time' },
40
- };
41
- } else {
42
- filterProperties = {
43
- equals: { type: 'string' },
44
- not: { type: 'string' },
45
- in: { type: 'array', items: { type: 'string' } },
46
- notIn: { type: 'array', items: { type: 'string' } },
47
- contains: { type: 'string' },
48
- startsWith: { type: 'string' },
49
- endsWith: { type: 'string' },
50
- mode: { type: 'string', enum: ['default', 'insensitive'] },
51
- };
52
- }
53
-
54
- return {
55
- type: 'object',
56
- properties: filterProperties,
57
- };
58
- }
59
-
60
- export function whereInputToOpenApiSchema<T extends TableDef | EntityConstructor>(
61
- target: T
62
- ): OpenApiSchema {
63
- const columns = getColumnMap(target);
64
- const properties: Record<string, OpenApiSchema> = {};
65
-
66
- for (const [key, col] of Object.entries(columns)) {
67
- properties[key] = filterFieldToOpenApiSchema(col);
68
- }
69
-
70
- return {
71
- type: 'object',
72
- properties,
73
- };
74
- }
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 type { OpenApiSchema, OpenApiDialect } from '../types.js';
5
+ import { columnTypeToOpenApiType, columnTypeToOpenApiFormat } from '../type-mappings.js';
6
+ import { getColumnMap } from './base.js';
7
+ import { applyNullability, isNullableColumn } from '../utilities.js';
8
+
9
+ function filterFieldToOpenApiSchema(col: ColumnDef): OpenApiSchema {
10
+ const openApiType = columnTypeToOpenApiType(col);
11
+ const openApiFormat = columnTypeToOpenApiFormat(col);
12
+
13
+ const filterProperties: Record<string, OpenApiSchema> = {};
14
+
15
+ filterProperties.equals = { type: openApiType, format: openApiFormat };
16
+ filterProperties.not = { type: openApiType, format: openApiFormat };
17
+ filterProperties.in = { type: 'array', items: { type: openApiType, format: openApiFormat } };
18
+ filterProperties.notIn = { type: 'array', items: { type: openApiType, format: openApiFormat } };
19
+
20
+ const isNumeric = openApiType === 'integer' || openApiType === 'number';
21
+ const isDateOrTime = openApiType === 'string' && (openApiFormat === 'date' || openApiFormat === 'date-time');
22
+ const isString = openApiType === 'string' && !isDateOrTime;
23
+
24
+ if (isNumeric) {
25
+ filterProperties.lt = { type: openApiType, format: openApiFormat };
26
+ filterProperties.lte = { type: openApiType, format: openApiFormat };
27
+ filterProperties.gt = { type: openApiType, format: openApiFormat };
28
+ filterProperties.gte = { type: openApiType, format: openApiFormat };
29
+ }
30
+
31
+ if (isDateOrTime) {
32
+ filterProperties.lt = { type: openApiType, format: openApiFormat };
33
+ filterProperties.lte = { type: openApiType, format: openApiFormat };
34
+ filterProperties.gt = { type: openApiType, format: openApiFormat };
35
+ filterProperties.gte = { type: openApiType, format: openApiFormat };
36
+ }
37
+
38
+ if (isString) {
39
+ filterProperties.contains = { type: 'string' };
40
+ filterProperties.startsWith = { type: 'string' };
41
+ filterProperties.endsWith = { type: 'string' };
42
+ filterProperties.mode = { type: 'string', enum: ['default', 'insensitive'] };
43
+ }
44
+
45
+ return {
46
+ type: 'object',
47
+ properties: filterProperties,
48
+ };
49
+ }
50
+
51
+ export function columnToFilterSchema(
52
+ col: ColumnDef,
53
+ dialect: OpenApiDialect = 'openapi-3.1'
54
+ ): OpenApiSchema {
55
+ const filterSchema = filterFieldToOpenApiSchema(col);
56
+ const nullable = isNullableColumn(col);
57
+ return applyNullability(filterSchema, nullable, dialect);
58
+ }
59
+
60
+ export function whereInputToOpenApiSchema<T extends TableDef | EntityConstructor>(
61
+ target: T,
62
+ dialect: OpenApiDialect = 'openapi-3.1'
63
+ ): OpenApiSchema {
64
+ const columns = getColumnMap(target);
65
+ const properties: Record<string, OpenApiSchema> = {};
66
+
67
+ for (const [key, col] of Object.entries(columns)) {
68
+ properties[key] = columnToFilterSchema(col, dialect);
69
+ }
70
+
71
+ return {
72
+ type: 'object',
73
+ properties,
74
+ };
75
+ }