metal-orm 1.0.87 → 1.0.89

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.
@@ -0,0 +1,110 @@
1
+ import type { TableDef } from '../schema/table.js';
2
+ import type { HydrationPlan } from '../core/hydration/types.js';
3
+ import type { ProjectionNode } from '../query-builder/select-query-state.js';
4
+ import type {
5
+ OpenApiSchema,
6
+ SchemaExtractionContext,
7
+ OutputSchemaOptions,
8
+ JsonSchemaProperty,
9
+ JsonSchemaType
10
+ } from './schema-types.js';
11
+
12
+ export const hasComputedProjection = (projectionNodes?: ProjectionNode[]): boolean =>
13
+ Boolean(projectionNodes && projectionNodes.some(node => node.type !== 'Column'));
14
+
15
+ export const shouldUseSelectedSchema = (
16
+ options: OutputSchemaOptions,
17
+ plan: HydrationPlan | undefined,
18
+ projectionNodes: ProjectionNode[] | undefined
19
+ ): boolean => {
20
+ if (!plan || options.mode !== 'selected') return false;
21
+ if (hasComputedProjection(projectionNodes)) return false;
22
+ if (options.refMode === 'components' && options.selectedRefMode !== 'components') return false;
23
+ return true;
24
+ };
25
+
26
+ export const resolveComponentName = (table: TableDef, options: OutputSchemaOptions): string =>
27
+ options.componentName ? options.componentName(table) : table.name;
28
+
29
+ const normalizeColumns = (columns: string[]): string[] =>
30
+ Array.from(new Set(columns)).sort((a, b) => a.localeCompare(b));
31
+
32
+ const buildSelectionSignature = (plan: HydrationPlan): string => {
33
+ const relations = plan.relations
34
+ .map(relation => ({
35
+ name: relation.name,
36
+ columns: normalizeColumns(relation.columns)
37
+ }))
38
+ .sort((a, b) => a.name.localeCompare(b.name));
39
+
40
+ return JSON.stringify({
41
+ root: normalizeColumns(plan.rootColumns),
42
+ relations
43
+ });
44
+ };
45
+
46
+ const hashString = (value: string): string => {
47
+ let hash = 2166136261;
48
+ for (let i = 0; i < value.length; i += 1) {
49
+ hash ^= value.charCodeAt(i);
50
+ hash = (hash * 16777619) >>> 0;
51
+ }
52
+ return hash.toString(16).padStart(8, '0');
53
+ };
54
+
55
+ export const resolveSelectedComponentName = (
56
+ table: TableDef,
57
+ plan: HydrationPlan,
58
+ options: OutputSchemaOptions
59
+ ): string => {
60
+ const base = resolveComponentName(table, options);
61
+ const signature = buildSelectionSignature(plan);
62
+ return `${base}__sel_${hashString(signature)}`;
63
+ };
64
+
65
+ export const ensureComponentRef = (
66
+ table: TableDef,
67
+ componentName: string,
68
+ context: SchemaExtractionContext,
69
+ schemaFactory: () => OpenApiSchema
70
+ ): JsonSchemaProperty => {
71
+ if (context.components && !context.components.schemas[componentName]) {
72
+ if (!context.visitedTables.has(table.name)) {
73
+ context.components.schemas[componentName] = schemaFactory();
74
+ }
75
+ }
76
+
77
+ return { $ref: `#/components/schemas/${componentName}` };
78
+ };
79
+
80
+ export const registerComponentSchema = (
81
+ name: string,
82
+ schema: OpenApiSchema,
83
+ context: SchemaExtractionContext
84
+ ): void => {
85
+ if (!context.components) return;
86
+ if (!context.components.schemas[name]) {
87
+ context.components.schemas[name] = schema;
88
+ }
89
+ };
90
+
91
+ export const createContext = (maxDepth: number): SchemaExtractionContext => ({
92
+ visitedTables: new Set(),
93
+ schemaCache: new Map(),
94
+ depth: 0,
95
+ maxDepth
96
+ });
97
+
98
+ export const buildCircularReferenceSchema = (
99
+ tableName: string,
100
+ kind: 'input' | 'output'
101
+ ): OpenApiSchema => ({
102
+ type: 'object',
103
+ properties: {
104
+ _ref: {
105
+ type: 'string' as JsonSchemaType,
106
+ description: `Circular ${kind} reference to ${tableName}`
107
+ }
108
+ },
109
+ required: []
110
+ });