metal-orm 1.0.87 → 1.0.88
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.
- package/dist/index.cjs +175 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +175 -61
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/openapi/schema-extractor-input.ts +139 -0
- package/src/openapi/schema-extractor-output.ts +427 -0
- package/src/openapi/schema-extractor-utils.ts +110 -0
- package/src/openapi/schema-extractor.ts +41 -516
- package/src/openapi/schema-types.ts +18 -0
package/dist/index.cjs
CHANGED
|
@@ -7249,47 +7249,52 @@ var getTemporalFormat = (sqlType) => {
|
|
|
7249
7249
|
}
|
|
7250
7250
|
};
|
|
7251
7251
|
|
|
7252
|
-
// src/openapi/schema-extractor.ts
|
|
7253
|
-
var
|
|
7254
|
-
var
|
|
7255
|
-
|
|
7256
|
-
|
|
7257
|
-
|
|
7258
|
-
|
|
7259
|
-
|
|
7260
|
-
|
|
7252
|
+
// src/openapi/schema-extractor-utils.ts
|
|
7253
|
+
var hasComputedProjection = (projectionNodes) => Boolean(projectionNodes && projectionNodes.some((node) => node.type !== "Column"));
|
|
7254
|
+
var shouldUseSelectedSchema = (options, plan, projectionNodes) => {
|
|
7255
|
+
if (!plan || options.mode !== "selected") return false;
|
|
7256
|
+
if (hasComputedProjection(projectionNodes)) return false;
|
|
7257
|
+
if (options.refMode === "components" && options.selectedRefMode !== "components") return false;
|
|
7258
|
+
return true;
|
|
7259
|
+
};
|
|
7260
|
+
var resolveComponentName = (table, options) => options.componentName ? options.componentName(table) : table.name;
|
|
7261
|
+
var normalizeColumns = (columns) => Array.from(new Set(columns)).sort((a, b) => a.localeCompare(b));
|
|
7262
|
+
var buildSelectionSignature = (plan) => {
|
|
7263
|
+
const relations = plan.relations.map((relation) => ({
|
|
7264
|
+
name: relation.name,
|
|
7265
|
+
columns: normalizeColumns(relation.columns)
|
|
7266
|
+
})).sort((a, b) => a.name.localeCompare(b.name));
|
|
7267
|
+
return JSON.stringify({
|
|
7268
|
+
root: normalizeColumns(plan.rootColumns),
|
|
7269
|
+
relations
|
|
7270
|
+
});
|
|
7271
|
+
};
|
|
7272
|
+
var hashString = (value) => {
|
|
7273
|
+
let hash = 2166136261;
|
|
7274
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
7275
|
+
hash ^= value.charCodeAt(i);
|
|
7276
|
+
hash = hash * 16777619 >>> 0;
|
|
7261
7277
|
}
|
|
7262
|
-
|
|
7263
|
-
const input = extractInputSchema(table, inputContext, inputOptions);
|
|
7264
|
-
return { output, input };
|
|
7278
|
+
return hash.toString(16).padStart(8, "0");
|
|
7265
7279
|
};
|
|
7266
|
-
var
|
|
7267
|
-
|
|
7268
|
-
|
|
7269
|
-
|
|
7270
|
-
|
|
7271
|
-
|
|
7272
|
-
|
|
7273
|
-
|
|
7274
|
-
|
|
7275
|
-
|
|
7276
|
-
|
|
7277
|
-
|
|
7278
|
-
|
|
7279
|
-
|
|
7280
|
-
|
|
7281
|
-
|
|
7282
|
-
|
|
7283
|
-
|
|
7284
|
-
includeEnums: input.includeEnums ?? options.includeEnums,
|
|
7285
|
-
includeExamples: input.includeExamples ?? options.includeExamples,
|
|
7286
|
-
includeDefaults: input.includeDefaults ?? options.includeDefaults,
|
|
7287
|
-
includeNullable: input.includeNullable ?? options.includeNullable,
|
|
7288
|
-
maxDepth: input.maxDepth ?? options.maxDepth ?? DEFAULT_MAX_DEPTH,
|
|
7289
|
-
omitReadOnly: input.omitReadOnly ?? true,
|
|
7290
|
-
excludePrimaryKey: input.excludePrimaryKey ?? false,
|
|
7291
|
-
requirePrimaryKey: input.requirePrimaryKey ?? mode === "update"
|
|
7292
|
-
};
|
|
7280
|
+
var resolveSelectedComponentName = (table, plan, options) => {
|
|
7281
|
+
const base = resolveComponentName(table, options);
|
|
7282
|
+
const signature = buildSelectionSignature(plan);
|
|
7283
|
+
return `${base}__sel_${hashString(signature)}`;
|
|
7284
|
+
};
|
|
7285
|
+
var ensureComponentRef = (table, componentName, context, schemaFactory) => {
|
|
7286
|
+
if (context.components && !context.components.schemas[componentName]) {
|
|
7287
|
+
if (!context.visitedTables.has(table.name)) {
|
|
7288
|
+
context.components.schemas[componentName] = schemaFactory();
|
|
7289
|
+
}
|
|
7290
|
+
}
|
|
7291
|
+
return { $ref: `#/components/schemas/${componentName}` };
|
|
7292
|
+
};
|
|
7293
|
+
var registerComponentSchema = (name, schema, context) => {
|
|
7294
|
+
if (!context.components) return;
|
|
7295
|
+
if (!context.components.schemas[name]) {
|
|
7296
|
+
context.components.schemas[name] = schema;
|
|
7297
|
+
}
|
|
7293
7298
|
};
|
|
7294
7299
|
var createContext = (maxDepth) => ({
|
|
7295
7300
|
visitedTables: /* @__PURE__ */ new Set(),
|
|
@@ -7297,19 +7302,18 @@ var createContext = (maxDepth) => ({
|
|
|
7297
7302
|
depth: 0,
|
|
7298
7303
|
maxDepth
|
|
7299
7304
|
});
|
|
7300
|
-
var
|
|
7301
|
-
|
|
7302
|
-
|
|
7303
|
-
|
|
7304
|
-
|
|
7305
|
-
|
|
7306
|
-
|
|
7307
|
-
}
|
|
7308
|
-
|
|
7309
|
-
|
|
7310
|
-
|
|
7311
|
-
|
|
7312
|
-
};
|
|
7305
|
+
var buildCircularReferenceSchema = (tableName, kind) => ({
|
|
7306
|
+
type: "object",
|
|
7307
|
+
properties: {
|
|
7308
|
+
_ref: {
|
|
7309
|
+
type: "string",
|
|
7310
|
+
description: `Circular ${kind} reference to ${tableName}`
|
|
7311
|
+
}
|
|
7312
|
+
},
|
|
7313
|
+
required: []
|
|
7314
|
+
});
|
|
7315
|
+
|
|
7316
|
+
// src/openapi/schema-extractor-input.ts
|
|
7313
7317
|
var extractInputSchema = (table, context, options) => {
|
|
7314
7318
|
const cacheKey = `${table.name}:${options.mode ?? "create"}`;
|
|
7315
7319
|
if (context.schemaCache.has(cacheKey)) {
|
|
@@ -7397,6 +7401,18 @@ var extractInputRelationSchema = (relation, context, options) => {
|
|
|
7397
7401
|
nullable: isNullable
|
|
7398
7402
|
};
|
|
7399
7403
|
};
|
|
7404
|
+
|
|
7405
|
+
// src/openapi/schema-extractor-output.ts
|
|
7406
|
+
var extractOutputSchema = (table, plan, projectionNodes, context, options) => {
|
|
7407
|
+
const hasComputedFields = hasComputedProjection(projectionNodes);
|
|
7408
|
+
if (hasComputedFields) {
|
|
7409
|
+
return extractFromProjectionNodes(table, projectionNodes, context, options);
|
|
7410
|
+
}
|
|
7411
|
+
if (shouldUseSelectedSchema(options, plan, projectionNodes)) {
|
|
7412
|
+
return extractSelectedSchema(table, plan, context, options);
|
|
7413
|
+
}
|
|
7414
|
+
return extractFullTableSchema(table, context, options);
|
|
7415
|
+
};
|
|
7400
7416
|
var extractFromProjectionNodes = (table, projectionNodes, context, options) => {
|
|
7401
7417
|
const properties = {};
|
|
7402
7418
|
const required = [];
|
|
@@ -7602,6 +7618,52 @@ var extractFullTableSchema = (table, context, options) => {
|
|
|
7602
7618
|
var extractRelationSchema = (relation, relationPlan, selectedColumns, context, options) => {
|
|
7603
7619
|
const targetTable = relation.target;
|
|
7604
7620
|
const { type: relationType, isNullable } = mapRelationType(relation.type);
|
|
7621
|
+
if (options.refMode === "components" && context.components) {
|
|
7622
|
+
if (relationPlan && selectedColumns.length > 0 && options.selectedRefMode === "components") {
|
|
7623
|
+
const plan = {
|
|
7624
|
+
rootTable: targetTable.name,
|
|
7625
|
+
rootPrimaryKey: relationPlan.targetPrimaryKey,
|
|
7626
|
+
rootColumns: selectedColumns,
|
|
7627
|
+
relations: []
|
|
7628
|
+
};
|
|
7629
|
+
const componentName2 = resolveSelectedComponentName(targetTable, plan, options);
|
|
7630
|
+
const ref2 = ensureComponentRef(
|
|
7631
|
+
targetTable,
|
|
7632
|
+
componentName2,
|
|
7633
|
+
context,
|
|
7634
|
+
() => extractSelectedSchema(targetTable, plan, context, options)
|
|
7635
|
+
);
|
|
7636
|
+
if (relationType === "array") {
|
|
7637
|
+
return {
|
|
7638
|
+
type: "array",
|
|
7639
|
+
items: ref2,
|
|
7640
|
+
nullable: isNullable
|
|
7641
|
+
};
|
|
7642
|
+
}
|
|
7643
|
+
return {
|
|
7644
|
+
...ref2,
|
|
7645
|
+
nullable: isNullable
|
|
7646
|
+
};
|
|
7647
|
+
}
|
|
7648
|
+
const componentName = resolveComponentName(targetTable, options);
|
|
7649
|
+
const ref = ensureComponentRef(
|
|
7650
|
+
targetTable,
|
|
7651
|
+
componentName,
|
|
7652
|
+
context,
|
|
7653
|
+
() => extractFullTableSchema(targetTable, context, options)
|
|
7654
|
+
);
|
|
7655
|
+
if (relationType === "array") {
|
|
7656
|
+
return {
|
|
7657
|
+
type: "array",
|
|
7658
|
+
items: ref,
|
|
7659
|
+
nullable: isNullable
|
|
7660
|
+
};
|
|
7661
|
+
}
|
|
7662
|
+
return {
|
|
7663
|
+
...ref,
|
|
7664
|
+
nullable: isNullable
|
|
7665
|
+
};
|
|
7666
|
+
}
|
|
7605
7667
|
let targetSchema;
|
|
7606
7668
|
if (relationPlan && selectedColumns.length > 0) {
|
|
7607
7669
|
const plan = {
|
|
@@ -7629,16 +7691,68 @@ var extractRelationSchema = (relation, relationPlan, selectedColumns, context, o
|
|
|
7629
7691
|
description: targetSchema.description
|
|
7630
7692
|
};
|
|
7631
7693
|
};
|
|
7632
|
-
|
|
7633
|
-
|
|
7634
|
-
|
|
7635
|
-
|
|
7636
|
-
|
|
7637
|
-
|
|
7638
|
-
|
|
7639
|
-
|
|
7640
|
-
|
|
7694
|
+
|
|
7695
|
+
// src/openapi/schema-extractor.ts
|
|
7696
|
+
var DEFAULT_MAX_DEPTH = 5;
|
|
7697
|
+
var extractSchema = (table, plan, projectionNodes, options = {}) => {
|
|
7698
|
+
const outputOptions = resolveOutputOptions(options);
|
|
7699
|
+
const outputContext = createContext(outputOptions.maxDepth ?? DEFAULT_MAX_DEPTH);
|
|
7700
|
+
if (outputOptions.refMode === "components") {
|
|
7701
|
+
outputContext.components = { schemas: {} };
|
|
7702
|
+
}
|
|
7703
|
+
const output = extractOutputSchema(table, plan, projectionNodes, outputContext, outputOptions);
|
|
7704
|
+
const useSelected = shouldUseSelectedSchema(outputOptions, plan, projectionNodes);
|
|
7705
|
+
const hasComputedFields = hasComputedProjection(projectionNodes);
|
|
7706
|
+
if (outputOptions.refMode === "components" && outputContext.components && !hasComputedFields) {
|
|
7707
|
+
const componentName = useSelected && plan ? resolveSelectedComponentName(table, plan, outputOptions) : resolveComponentName(table, outputOptions);
|
|
7708
|
+
registerComponentSchema(componentName, output, outputContext);
|
|
7709
|
+
}
|
|
7710
|
+
const inputOptions = resolveInputOptions(options);
|
|
7711
|
+
if (!inputOptions) {
|
|
7712
|
+
return {
|
|
7713
|
+
output,
|
|
7714
|
+
components: outputContext.components && Object.keys(outputContext.components.schemas).length ? outputContext.components : void 0
|
|
7715
|
+
};
|
|
7716
|
+
}
|
|
7717
|
+
const inputContext = createContext(inputOptions.maxDepth ?? DEFAULT_MAX_DEPTH);
|
|
7718
|
+
const input = extractInputSchema(table, inputContext, inputOptions);
|
|
7719
|
+
return {
|
|
7720
|
+
output,
|
|
7721
|
+
input,
|
|
7722
|
+
components: outputContext.components && Object.keys(outputContext.components.schemas).length ? outputContext.components : void 0
|
|
7723
|
+
};
|
|
7724
|
+
};
|
|
7725
|
+
var resolveOutputOptions = (options) => ({
|
|
7726
|
+
mode: options.mode ?? "full",
|
|
7727
|
+
includeDescriptions: options.includeDescriptions,
|
|
7728
|
+
includeEnums: options.includeEnums,
|
|
7729
|
+
includeExamples: options.includeExamples,
|
|
7730
|
+
includeDefaults: options.includeDefaults,
|
|
7731
|
+
includeNullable: options.includeNullable,
|
|
7732
|
+
maxDepth: options.maxDepth ?? DEFAULT_MAX_DEPTH,
|
|
7733
|
+
refMode: options.refMode ?? "inline",
|
|
7734
|
+
selectedRefMode: options.selectedRefMode ?? "inline",
|
|
7735
|
+
componentName: options.componentName
|
|
7641
7736
|
});
|
|
7737
|
+
var resolveInputOptions = (options) => {
|
|
7738
|
+
if (options.input === false) return void 0;
|
|
7739
|
+
const input = options.input ?? {};
|
|
7740
|
+
const mode = input.mode ?? "create";
|
|
7741
|
+
return {
|
|
7742
|
+
mode,
|
|
7743
|
+
includeRelations: input.includeRelations ?? true,
|
|
7744
|
+
relationMode: input.relationMode ?? "mixed",
|
|
7745
|
+
includeDescriptions: input.includeDescriptions ?? options.includeDescriptions,
|
|
7746
|
+
includeEnums: input.includeEnums ?? options.includeEnums,
|
|
7747
|
+
includeExamples: input.includeExamples ?? options.includeExamples,
|
|
7748
|
+
includeDefaults: input.includeDefaults ?? options.includeDefaults,
|
|
7749
|
+
includeNullable: input.includeNullable ?? options.includeNullable,
|
|
7750
|
+
maxDepth: input.maxDepth ?? options.maxDepth ?? DEFAULT_MAX_DEPTH,
|
|
7751
|
+
omitReadOnly: input.omitReadOnly ?? true,
|
|
7752
|
+
excludePrimaryKey: input.excludePrimaryKey ?? false,
|
|
7753
|
+
requirePrimaryKey: input.requirePrimaryKey ?? mode === "update"
|
|
7754
|
+
};
|
|
7755
|
+
};
|
|
7642
7756
|
var schemaToJson = (schema, pretty = false) => {
|
|
7643
7757
|
return JSON.stringify(schema, null, pretty ? 2 : 0);
|
|
7644
7758
|
};
|