import { AnyPlugin, AnySchema, BetterTableKey, CursorArgs, InsertModelFor, ModelExtensionTypeResolver, PaginationArgs, PluginModelExtensionContext, QueryArgs, ScalarKeysFor, SelectModelFor, TableKey, WhereInput } from "better-drizzle"; import { z } from "zod"; //#region src/plugins/zod/types.d.ts type Any = any; type TypedZodSchema = z.ZodType & { _input: Input; }; type ScalarFieldName> = Extract, string>; type Simplify = { [K in keyof T]: T[K]; } & {}; type RemoveIndexSignature = { [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K]; }; type InsertScalarShape> = { [K in Extract, keyof InsertModelFor>]: InsertModelFor[K]; }; type SelectScalarShape> = { [K in ScalarKeysFor]: SelectModelFor[K]; }; type FieldSchemaOverride = false | z.ZodTypeAny | ((schema: SchemaType) => z.ZodTypeAny); type SchemaBlock> = { extend?: Record; omit?: readonly ScalarFieldName[]; partial?: boolean; }; /** * Controls how the Zod plugin handles type coercion and unknown keys. * * @property coerce - When `true`, coerces all supported types. Accepts a partial * record to enable coercion per type. When `false` or omitted, no coercion is applied. * @property unknownKeys - Determines how Zod handles keys not present in the schema. * - `'strip'` (default): removes unknown keys from parsed output. * - `'passthrough'`: preserves unknown keys as-is. * - `'strict'`: rejects objects containing unknown keys. */ type ZodPluginBehavior = { coerce?: boolean | Partial>; unknownKeys?: 'passthrough' | 'strict' | 'strip'; }; /** * Per-operation validation toggle. Each key corresponds to a Better Drizzle * operation kind. Set a key to `true` to enable validation, `false` to disable, * or omit it to fall back to the plugin's default behavior. */ type ZodPluginValidateOptions = Partial>; /** * Schema customization block for a specific table. Allows extending, omitting, * or marking fields as partial for each operation schema (create, update, select, etc.). * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type ZodPluginTableSchemasConfig> = { create?: SchemaBlock; fields?: Partial<{ [K in ScalarFieldName]: FieldSchemaOverride[K]>>; }>; orderBy?: SchemaBlock; pagination?: SchemaBlock; query?: SchemaBlock; select?: SchemaBlock; update?: SchemaBlock; upsert?: SchemaBlock; where?: SchemaBlock; }; /** * Configuration options for the `better-drizzle/zod` plugin. * * @typeParam Schema - The full Drizzle schema object. * @property behavior - Global coercion and unknown-keys behavior applied to all tables. * @property schemas - Per-table schema customizations (field overrides, extend, omit, partial). * @property validate - Per-operation validation toggles. Overrides can also be set per call * via the `validate` operation arg. */ type ZodPluginOptions = { behavior?: ZodPluginBehavior; schemas?: Partial<{ [K in BetterTableKey]: ZodPluginTableSchemasConfig; }>; validate?: ZodPluginValidateOptions; }; type TableSchemaConfigFor, Options extends ZodPluginOptions> = Options extends { schemas?: infer Schemas; } ? Name extends keyof NonNullable ? NonNullable[Name]> : never : never; type FieldsConfigFor, Options extends ZodPluginOptions> = [TableSchemaConfigFor] extends [never] ? never : TableSchemaConfigFor extends { fields?: infer Fields; } ? NonNullable : never; type BlockConfigFor, Options extends ZodPluginOptions, Key extends keyof ZodPluginTableSchemasConfig> = [TableSchemaConfigFor] extends [never] ? never : TableSchemaConfigFor extends Record ? NonNullable : TableSchemaConfigFor extends { [K in Key]?: infer Block; } ? NonNullable : never; type ResolveOverrideOutput = [Override] extends [never] ? Base : Override extends false ? never : Override extends z.ZodTypeAny ? z.output : Override extends ((schema: Any) => infer Result) ? Result extends z.ZodTypeAny ? z.output : Base : Base; type ResolveOverrideInput = [Override] extends [never] ? Base : Override extends false ? never : Override extends z.ZodTypeAny ? z.input : Override extends ((schema: Any) => infer Result) ? Result extends z.ZodTypeAny ? z.input : Base : Base; type ApplyFieldOverridesOutput, Overrides> = Simplify<{ [K in keyof Shape as ResolveOverrideOutput ? NonNullable[K] : never, Shape[K]> extends never ? never : K]: ResolveOverrideOutput ? NonNullable[K] : never, Shape[K]>; }>; type ApplyFieldOverridesInput, Overrides> = Simplify<{ [K in keyof Shape as ResolveOverrideInput ? NonNullable[K] : never, Shape[K]> extends never ? never : K]: ResolveOverrideInput ? NonNullable[K] : never, Shape[K]>; }>; type ApplyOmit, Block> = [Block] extends [never] ? Shape : Block extends { omit?: readonly PropertyKey[]; } ? Omit[number], keyof Shape>> : Shape; type ApplyExtendOutput, Block> = [Block] extends [never] ? Shape : Block extends { extend?: infer Extends; } ? Extends extends Record ? Simplify; }> : Shape : Shape; type ApplyExtendInput, Block> = [Block] extends [never] ? Shape : Block extends { extend?: infer Extends; } ? Extends extends Record ? Simplify; }> : Shape : Shape; type ApplyPartialFlag, Block> = [Block] extends [never] ? Shape : Block extends { partial?: true; } ? Partial : Shape; type ApplyBlockOutput, Block> = ApplyPartialFlag, Block>, Block>; type ApplyBlockInput, Block> = ApplyPartialFlag, Block>, Block>; type CreateBaseOutput, Options extends ZodPluginOptions> = ApplyFieldOverridesOutput, FieldsConfigFor>; type CreateBaseInput, Options extends ZodPluginOptions> = ApplyFieldOverridesInput, FieldsConfigFor>; type SelectBaseOutput, Options extends ZodPluginOptions> = ApplyFieldOverridesOutput, FieldsConfigFor>; type SelectBaseInput, Options extends ZodPluginOptions> = ApplyFieldOverridesInput, FieldsConfigFor>; type CreateOutput, Options extends ZodPluginOptions> = ApplyBlockOutput, BlockConfigFor>; type CreateInput, Options extends ZodPluginOptions> = ApplyBlockInput, BlockConfigFor>; type UpdateOutput, Options extends ZodPluginOptions> = ApplyBlockOutput>, BlockConfigFor>; type UpdateInput, Options extends ZodPluginOptions> = ApplyBlockInput>, BlockConfigFor>; type SelectOutput, Options extends ZodPluginOptions> = ApplyBlockOutput, BlockConfigFor>; type SelectInputShape, Options extends ZodPluginOptions> = ApplyBlockInput, BlockConfigFor>; type WhereOutput, Options extends ZodPluginOptions> = ApplyBlockOutput>, BlockConfigFor>; type WhereInputShape, Options extends ZodPluginOptions> = ApplyBlockInput>, BlockConfigFor>; type QueryOutput, Options extends ZodPluginOptions> = ApplyBlockOutput>, BlockConfigFor>; type QueryInputShape, Options extends ZodPluginOptions> = ApplyBlockInput>, BlockConfigFor>; type PaginationOutput, Options extends ZodPluginOptions> = ApplyBlockOutput>, BlockConfigFor>; type PaginationInputShape, Options extends ZodPluginOptions> = ApplyBlockInput>, BlockConfigFor>; type OrderByOutput> = ZodPluginOrderByInput; type OrderByInputShape> = ZodPluginOrderByInput; type UpsertOutput, Options extends ZodPluginOptions> = ApplyBlockOutput<{ create: CreateOutput; update: UpdateOutput; where: WhereOutput; }, BlockConfigFor>; type UpsertInputShape, Options extends ZodPluginOptions> = ApplyBlockInput<{ create: CreateInput; update: UpdateInput; where: WhereInputShape; }, BlockConfigFor>; /** * The set of Zod schemas generated for a single table. Exposed on each model * as `db..$zod`. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type BetterDrizzleZodModelSchemas, Options extends ZodPluginOptions = ZodPluginOptions> = { create: TypedZodSchema, CreateInput>; orderBy: TypedZodSchema, OrderByInputShape>; pagination: TypedZodSchema, PaginationInputShape>; query: TypedZodSchema, QueryInputShape>; select: TypedZodSchema, SelectInputShape>; update: TypedZodSchema, UpdateInput>; upsert: TypedZodSchema, UpsertInputShape>; where: TypedZodSchema, WhereInputShape>; }; /** * Model extension added by the Zod plugin. Attaches a `$zod` property to each * table delegate containing the generated Zod schemas. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type BetterDrizzleZodModelExtension, Options extends ZodPluginOptions = ZodPluginOptions> = { $zod: BetterDrizzleZodModelSchemas; }; /** * Resolver function signature used by the plugin system to compute per-table * model extensions. The Zod plugin provides a resolver that attaches `$zod` * schemas to each table delegate. */ type BetterDrizzleZodModelExtensionResolver = ZodPluginOptions> = , Meta, Plugins extends readonly AnyPlugin[]>(context: PluginModelExtensionContext) => BetterDrizzleZodModelExtension>>; /** * Extracts the `orderBy` input type for a given table, excluding `meta`. * Useful for typing order-by arguments in custom validation or extension code. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type ZodPluginOrderByInput> = NonNullable['orderBy']>; /** * The query input type for a given table, with `meta` stripped. * Corresponds to the arguments accepted by `findMany`, `findFirst`, `findOne`, * and `findUnique`. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type ZodPluginQueryInput> = Omit, 'meta'>; /** * The pagination input type for a given table, with `meta` stripped. * Corresponds to the arguments accepted by `paginate`. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type ZodPluginPaginationInput> = Omit, 'meta'>; /** * The cursor input type for a given table, with `meta` stripped. * Corresponds to the arguments accepted by `cursor`. * * @typeParam Schema - The full Drizzle schema object. * @typeParam Name - The table key within the schema. */ type ZodPluginCursorInput> = Omit, 'meta'>; //#endregion //#region src/plugins/zod/version.d.ts /** * Your current version of [better-drizzle](https://npmjs.com/package/better-drizzle). */ export declare const version = "0.3.0"; //#endregion //#region src/plugins/zod/index.d.ts type ExtensibleData = Record; export interface ZodModelExtensionResolver extends ModelExtensionTypeResolver { readonly extension: BetterDrizzleZodModelExtension, Extract>>, Extract>>>; } export declare const zod: = Record>(options?: Options & ZodPluginOptions) => import("better-drizzle").Plugin, Record, Record, { count: { validate?: boolean; }; create: { validate?: boolean; data?: ExtensibleData; }; createMany: { validate?: boolean; data?: readonly ExtensibleData[]; }; cursor: { validate?: boolean; }; delete: { validate?: boolean; }; deleteMany: { validate?: boolean; }; exists: { validate?: boolean; }; findFirst: { validate?: boolean; }; findMany: { validate?: boolean; }; findOne: { validate?: boolean; }; findUnique: { validate?: boolean; }; paginate: { validate?: boolean; }; update: { validate?: boolean; data?: ExtensibleData; }; updateEach: { validate?: boolean; }; updateMany: { validate?: boolean; }; upsert: { validate?: boolean; create?: ExtensibleData; update?: ExtensibleData; }; upsertMany: { validate?: boolean; data?: readonly ExtensibleData[]; }; }, ZodModelExtensionResolver>; //#endregion export { type BetterDrizzleZodModelExtension, type BetterDrizzleZodModelExtensionResolver, type BetterDrizzleZodModelSchemas, type ZodPluginBehavior, type ZodPluginCursorInput, type ZodPluginOptions, type ZodPluginPaginationInput, type ZodPluginQueryInput, type ZodPluginTableSchemasConfig, type ZodPluginValidateOptions, zod as default };