metal-orm 1.0.88 → 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.
package/dist/index.cjs CHANGED
@@ -7341,6 +7341,7 @@ var extractInputSchema = (table, context, options) => {
7341
7341
  if (options.includeRelations && context.depth < context.maxDepth) {
7342
7342
  for (const [relationName, relation] of Object.entries(table.relations)) {
7343
7343
  properties[relationName] = extractInputRelationSchema(
7344
+ relationName,
7344
7345
  relation,
7345
7346
  { ...context, depth: context.depth + 1 },
7346
7347
  options
@@ -7375,7 +7376,7 @@ var buildPrimaryKeySchema = (table, options) => {
7375
7376
  }
7376
7377
  return mapColumnType(column, options);
7377
7378
  };
7378
- var extractInputRelationSchema = (relation, context, options) => {
7379
+ var extractInputRelationSchema = (relationName, relation, context, options) => {
7379
7380
  const { type: relationType, isNullable } = mapRelationType(relation.type);
7380
7381
  const relationMode = options.relationMode ?? "mixed";
7381
7382
  const allowIds = relationMode !== "objects";
@@ -7385,7 +7386,11 @@ var extractInputRelationSchema = (relation, context, options) => {
7385
7386
  variants.push(buildPrimaryKeySchema(relation.target, options));
7386
7387
  }
7387
7388
  if (allowObjects) {
7388
- const targetSchema = extractInputSchema(relation.target, context, options);
7389
+ let targetSchema = extractInputSchema(relation.target, context, options);
7390
+ targetSchema = applyRelationSelection(targetSchema, options.relationSelections?.[relationName]);
7391
+ if (options.excludeRelationForeignKeys && isRelationForeignKeyToParent(relation)) {
7392
+ targetSchema = removeForeignKey(targetSchema, relation.foreignKey);
7393
+ }
7389
7394
  variants.push(targetSchema);
7390
7395
  }
7391
7396
  const itemSchema = variants.length === 1 ? variants[0] : { anyOf: variants };
@@ -7401,6 +7406,43 @@ var extractInputRelationSchema = (relation, context, options) => {
7401
7406
  nullable: isNullable
7402
7407
  };
7403
7408
  };
7409
+ var applyRelationSelection = (schema, selection) => {
7410
+ if (!selection || selection.pick === void 0 && selection.omit === void 0) {
7411
+ return schema;
7412
+ }
7413
+ const hasPick = selection.pick !== void 0;
7414
+ const pick = hasPick ? new Set(selection.pick ?? []) : void 0;
7415
+ const omit = selection.omit !== void 0 ? new Set(selection.omit ?? []) : void 0;
7416
+ const properties = Object.entries(schema.properties).reduce(
7417
+ (acc, [key, value]) => {
7418
+ if (pick && !pick.has(key)) return acc;
7419
+ if (omit && omit.has(key)) return acc;
7420
+ acc[key] = value;
7421
+ return acc;
7422
+ },
7423
+ {}
7424
+ );
7425
+ const required = schema.required.filter((name) => properties[name] !== void 0);
7426
+ return {
7427
+ ...schema,
7428
+ properties,
7429
+ required
7430
+ };
7431
+ };
7432
+ var removeForeignKey = (schema, foreignKey) => {
7433
+ if (!foreignKey || !schema.properties[foreignKey]) return schema;
7434
+ const properties = { ...schema.properties };
7435
+ delete properties[foreignKey];
7436
+ const required = schema.required.filter((name) => name !== foreignKey);
7437
+ return {
7438
+ ...schema,
7439
+ properties,
7440
+ required
7441
+ };
7442
+ };
7443
+ var isRelationForeignKeyToParent = (relation) => {
7444
+ return relation.type === RelationKinds.HasMany || relation.type === RelationKinds.HasOne;
7445
+ };
7404
7446
 
7405
7447
  // src/openapi/schema-extractor-output.ts
7406
7448
  var extractOutputSchema = (table, plan, projectionNodes, context, options) => {
@@ -7700,12 +7742,17 @@ var extractSchema = (table, plan, projectionNodes, options = {}) => {
7700
7742
  if (outputOptions.refMode === "components") {
7701
7743
  outputContext.components = { schemas: {} };
7702
7744
  }
7703
- const output = extractOutputSchema(table, plan, projectionNodes, outputContext, outputOptions);
7745
+ const outputSchema = extractOutputSchema(table, plan, projectionNodes, outputContext, outputOptions);
7746
+ let output = outputSchema;
7704
7747
  const useSelected = shouldUseSelectedSchema(outputOptions, plan, projectionNodes);
7705
7748
  const hasComputedFields = hasComputedProjection(projectionNodes);
7706
- if (outputOptions.refMode === "components" && outputContext.components && !hasComputedFields) {
7749
+ const canUseComponents = outputOptions.refMode === "components" && outputContext.components && !hasComputedFields;
7750
+ if (canUseComponents) {
7707
7751
  const componentName = useSelected && plan ? resolveSelectedComponentName(table, plan, outputOptions) : resolveComponentName(table, outputOptions);
7708
- registerComponentSchema(componentName, output, outputContext);
7752
+ registerComponentSchema(componentName, outputSchema, outputContext);
7753
+ if (outputOptions.outputAsRef) {
7754
+ output = { $ref: `#/components/schemas/${componentName}` };
7755
+ }
7709
7756
  }
7710
7757
  const inputOptions = resolveInputOptions(options);
7711
7758
  if (!inputOptions) {
@@ -7732,7 +7779,8 @@ var resolveOutputOptions = (options) => ({
7732
7779
  maxDepth: options.maxDepth ?? DEFAULT_MAX_DEPTH,
7733
7780
  refMode: options.refMode ?? "inline",
7734
7781
  selectedRefMode: options.selectedRefMode ?? "inline",
7735
- componentName: options.componentName
7782
+ componentName: options.componentName,
7783
+ outputAsRef: options.outputAsRef ?? false
7736
7784
  });
7737
7785
  var resolveInputOptions = (options) => {
7738
7786
  if (options.input === false) return void 0;
@@ -7750,7 +7798,9 @@ var resolveInputOptions = (options) => {
7750
7798
  maxDepth: input.maxDepth ?? options.maxDepth ?? DEFAULT_MAX_DEPTH,
7751
7799
  omitReadOnly: input.omitReadOnly ?? true,
7752
7800
  excludePrimaryKey: input.excludePrimaryKey ?? false,
7753
- requirePrimaryKey: input.requirePrimaryKey ?? mode === "update"
7801
+ requirePrimaryKey: input.requirePrimaryKey ?? mode === "update",
7802
+ excludeRelationForeignKeys: input.excludeRelationForeignKeys ?? false,
7803
+ relationSelections: input.relationSelections
7754
7804
  };
7755
7805
  };
7756
7806
  var schemaToJson = (schema, pretty = false) => {