Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type ColumnBuilderRuntimeConfig<TData, TRuntimeConfig extends object = ob
columnType: string;
generated: GeneratedColumnConfig<TData> | undefined;
generatedIdentity: GeneratedIdentityConfig | undefined;
meta: Record<string, unknown> | undefined;
} & TRuntimeConfig;

export interface ColumnBuilderExtraConfig {
Expand Down Expand Up @@ -208,6 +209,7 @@ export abstract class ColumnBuilder<
dataType,
columnType,
generated: undefined,
meta: undefined,
} as ColumnBuilderRuntimeConfig<T['data'], TRuntimeConfig>;
}

Expand Down Expand Up @@ -288,6 +290,22 @@ export abstract class ColumnBuilder<
*/
$onUpdate = this.$onUpdateFn;

/**
* Adds metadata to the column definition. This can be used by external tools like drizzle-zod
* to add additional information to generated schemas.
*
* @example
* ```ts
* const users = pgTable('users', {
* age: integer('age').meta({ description: 'User age in years' }),
* });
* ```
*/
meta(meta: Record<string, unknown>): this {
this.config.meta = { ...this.config.meta, ...meta };
return this;
}

/**
* Adds a `primary key` clause to the column definition. This implicitly makes the column `not null`.
*
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export abstract class Column<
readonly enumValues: T['enumValues'] = undefined;
readonly generated: GeneratedColumnConfig<T['data']> | undefined = undefined;
readonly generatedIdentity: GeneratedIdentityConfig | undefined = undefined;
readonly meta: Record<string, unknown> | undefined;

protected config: ColumnRuntimeConfig<T['data'], TRuntimeConfig>;

Expand All @@ -108,6 +109,7 @@ export abstract class Column<
this.columnType = config.columnType;
this.generated = config.generated;
this.generatedIdentity = config.generatedIdentity;
this.meta = config.meta;
}

abstract getSQLType(): string;
Expand Down
5 changes: 5 additions & 0 deletions drizzle-zod/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export function columnToSchema(
schema = z.any();
}

// Apply metadata if available
if (column.meta && typeof column.meta === 'object') {
schema = schema.meta(column.meta);
}

return schema;
}

Expand Down