|
| 1 | +# Drizzle Kit SDK |
| 2 | + |
| 3 | +The same JSON contract the `drizzle-kit` CLI emits is available as typed root-level exports for programmatic callers — agents, build tools, custom orchestrators. The CLI and SDK share one implementation; behavior, status discriminators, hint vocabulary, and error codes are identical to what is documented in [JSON_CONTRACT.md](./JSON_CONTRACT.md). |
| 4 | + |
| 5 | +This document covers the public SDK surface. For the full machine-readable contract (status table, hint catalog, error code table), read [JSON_CONTRACT.md](./JSON_CONTRACT.md). |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install drizzle-kit drizzle-orm |
| 11 | +``` |
| 12 | + |
| 13 | +## Public surface |
| 14 | + |
| 15 | +Three root-level values plus six type aliases: |
| 16 | + |
| 17 | +| Export | Kind | Purpose | |
| 18 | +|---|---|---| |
| 19 | +| `defineConfig` | function | Type-checked Drizzle config builder (CLI parity) | |
| 20 | +| `generate` | async function | Programmatic equivalent of `drizzle-kit generate --json` | |
| 21 | +| `push` | async function | Programmatic equivalent of `drizzle-kit push --json` | |
| 22 | +| `Config` | type | Discriminated union of dialect configs | |
| 23 | +| `GenerateOptions` | type | Input shape for `generate` | |
| 24 | +| `PushOptions` | type | Input shape for `push` | |
| 25 | +| `GenerateJsonResponse` | type | Discriminated response union for `generate` | |
| 26 | +| `PushJsonResponse` | type | Discriminated response union for `push` | |
| 27 | +| `MissingHintsResponse` | type | The `status: 'missing_hints'` branch of both responses | |
| 28 | + |
| 29 | +All other public-surface details (status discriminator values, hint kinds, error codes) live in [JSON_CONTRACT.md](./JSON_CONTRACT.md) and apply identically to SDK return values — they are not restated here. |
| 30 | + |
| 31 | +```typescript |
| 32 | +import { defineConfig, generate, push } from 'drizzle-kit'; |
| 33 | +import type { |
| 34 | + Config, |
| 35 | + GenerateJsonResponse, |
| 36 | + GenerateOptions, |
| 37 | + MissingHintsResponse, |
| 38 | + PushJsonResponse, |
| 39 | + PushOptions, |
| 40 | +} from 'drizzle-kit'; |
| 41 | +``` |
| 42 | + |
| 43 | +## Minimal `generate` example |
| 44 | + |
| 45 | +```typescript |
| 46 | +import { generate } from 'drizzle-kit'; |
| 47 | +import type { GenerateJsonResponse } from 'drizzle-kit'; |
| 48 | + |
| 49 | +const response: GenerateJsonResponse = await generate({ |
| 50 | + dialect: 'postgresql', |
| 51 | + schema: './src/db/schema.ts', |
| 52 | + out: './drizzle', |
| 53 | +}); |
| 54 | + |
| 55 | +if (response.status === 'ok') { |
| 56 | + console.log(`Generated migration at ${response.migration_path}`); |
| 57 | +} else if (response.status === 'no_changes') { |
| 58 | + console.log('Schema in sync — no migration needed'); |
| 59 | +} else if (response.status === 'error') { |
| 60 | + console.error(`generate failed (${response.error.code})`); |
| 61 | + process.exit(1); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Minimal `push` example |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { push } from 'drizzle-kit'; |
| 69 | +import type { PushJsonResponse } from 'drizzle-kit'; |
| 70 | + |
| 71 | +const response: PushJsonResponse = await push({ |
| 72 | + dialect: 'postgresql', |
| 73 | + schema: './src/db/schema.ts', |
| 74 | + url: process.env.DATABASE_URL!, |
| 75 | +}); |
| 76 | + |
| 77 | +if (response.status === 'ok') { |
| 78 | + console.log(`Applied schema changes to the live ${response.dialect} database`); |
| 79 | +} else if (response.status === 'no_changes') { |
| 80 | + console.log('Database already in sync'); |
| 81 | +} else if (response.status === 'error') { |
| 82 | + console.error(`push failed (${response.error.code})`); |
| 83 | + process.exit(1); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Handling `status: 'missing_hints'` |
| 88 | + |
| 89 | +The most important agent-facing branch. When `generate` or `push` detects an ambiguity (a rename vs. drop+create, a destructive operation needing confirmation, a new schema declaration), it returns `status: 'missing_hints'` with an `unresolved` array describing each ambiguity. The caller picks a resolution for each item, builds a hint array, and re-invokes with the same options plus `hints`. |
| 90 | + |
| 91 | +Hint vocabulary (which `kind` values are valid, what each `meta` shape contains) is documented in [JSON_CONTRACT.md Hints flow](./JSON_CONTRACT.md#hints-flow) — this guide shows only the SDK call pattern. |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { generate } from 'drizzle-kit'; |
| 95 | +import type { GenerateJsonResponse, MissingHintsResponse } from 'drizzle-kit'; |
| 96 | + |
| 97 | +async function generateWithHints(): Promise<GenerateJsonResponse> { |
| 98 | + const baseOptions = { |
| 99 | + dialect: 'postgresql' as const, |
| 100 | + schema: './src/db/schema.ts', |
| 101 | + out: './drizzle', |
| 102 | + }; |
| 103 | + |
| 104 | + // First call — no hints. |
| 105 | + const first: GenerateJsonResponse = await generate(baseOptions); |
| 106 | + if (first.status !== 'missing_hints') return first; |
| 107 | + // first.status === 'missing_hints' from here on — `unresolved` is now narrowed. |
| 108 | + |
| 109 | + // Build resolutions. Each unresolved item dictates which `kind` and `meta` to send. |
| 110 | + // See JSON_CONTRACT.md "Catalog: kinds in unresolved items" for the full mapping. |
| 111 | + const hints = first.unresolved.map((item) => resolveHint(item)); |
| 112 | + |
| 113 | + // Re-invoke with hints serialized as JSON string (matches CLI --hints flag). |
| 114 | + return generate({ |
| 115 | + ...baseOptions, |
| 116 | + hints: JSON.stringify(hints), |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +function resolveHint(item: MissingHintsResponse['unresolved'][number]): unknown { |
| 121 | + // Application-specific resolution policy — agent prompts, user prompts, or static rules. |
| 122 | + // The returned object must match the `Hint` shape documented in JSON_CONTRACT.md. |
| 123 | + throw new Error(`Provide a hint resolution for ${JSON.stringify(item)}`); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Notes: |
| 128 | + |
| 129 | +- `hints` is passed as a JSON string (mirrors `--hints` on the CLI). For long hint sets, use `hintsFile` with a path to a JSON file instead. |
| 130 | +- A second `missing_hints` response after providing hints means the hint set was incomplete or invalid — see [JSON_CONTRACT.md Error codes](./JSON_CONTRACT.md#error-codes) for `invalid_hints` diagnostics. |
| 131 | +- The same flow applies to `push` — substitute `push` and `PushJsonResponse` everywhere. |
| 132 | + |
| 133 | +## Cross-references |
| 134 | + |
| 135 | +- [JSON_CONTRACT.md Programmatic API](./JSON_CONTRACT.md#programmatic-api) — short SDK pitch in the v1-stable contract doc |
| 136 | +- [JSON_CONTRACT.md Response statuses](./JSON_CONTRACT.md#response-statuses) — full status catalog |
| 137 | +- [JSON_CONTRACT.md Hints flow](./JSON_CONTRACT.md#hints-flow) — hint vocabulary and shapes |
| 138 | +- [JSON_CONTRACT.md Error codes](./JSON_CONTRACT.md#error-codes) — error.code table |
| 139 | +- [JSON_CONTRACT.md Recommended automation flow](./JSON_CONTRACT.md#recommended-automation-flow) — CLI-side equivalent of the SDK pattern above |
0 commit comments