---
title: "Drizzle"
description: "Required Drizzle schema, adapter wiring, and migration flow for Blyp database mode."
canonical_url: "https://www.blyp.dev/docs/database/drizzle"
markdown_url: "https://www.blyp.dev/docs/database/drizzle.md"
last_updated: "2018-10-20"
x_farming_labs_generated_preamble: true
agent:
  task: "Add the Blyp Drizzle table, generate and apply its migration, and wire the Drizzle adapter."
  outcome: "Blyp inserts a normalized test record into the expected blyp_logs table."
  appliesTo:
    package:
      - "@blyp/core"
      - "drizzle-orm"
      - "drizzle-kit"
  prerequisites:
    - "A PostgreSQL or MySQL Drizzle connection and config file already work."
  files:
    - "drizzle.config.ts"
    - "src/db/schema.ts"
    - "blyp.config.ts"
  commands:
    - "blyp db:init"
    - "blyp db:migrate"
  sideEffects:
    - "A migration creates or alters the Blyp log table and indexes."
  verification:
    - "Emit and flush a record"
    - "then query it through the configured Drizzle database."
  rollback:
    - "Revert the migration only after preserving retained log rows."
  failureModes:
    - symptom: "Inserts fail with missing or mismatched columns."
      resolution: "Compare the live table to the Blyp schema contract and apply the generated migration."
---

# Drizzle
URL: /docs/database/drizzle
LLM index: /llms.txt
Description: Required Drizzle schema, adapter wiring, and migration flow for Blyp database mode.
Related: /docs/database, /docs/database/schema, /docs/database/migrations, /docs/database/troubleshooting

<!-- farming-labs:agent-contract:start -->
## Agent Contract

Task: Add the Blyp Drizzle table, generate and apply its migration, and wire the Drizzle adapter.
Outcome: Blyp inserts a normalized test record into the expected blyp_logs table.

### Applies To

- Package: `@blyp/core`, `drizzle-orm`, `drizzle-kit`

### Prerequisites

- A PostgreSQL or MySQL Drizzle connection and config file already work.

### Files

- `drizzle.config.ts`
- `src/db/schema.ts`
- `blyp.config.ts`

### Commands

- `blyp db:init`
- `blyp db:migrate`

### Side Effects

- A migration creates or alters the Blyp log table and indexes.

### Verification

- Emit and flush a record
- then query it through the configured Drizzle database.

### Rollback

- Revert the migration only after preserving retained log rows.

### Failure Modes

- Inserts fail with missing or mismatched columns. — Recovery: Compare the live table to the Blyp schema contract and apply the generated migration.
<!-- farming-labs:agent-contract:end -->

# Drizzle

Use the documented table name and columns, include that schema in Drizzle Kit, generate and apply the
migration, then pass the working database instance to Blyp. Verify a persisted record. Column errors
mean the live database and checked-in schema differ; repair the migration before changing adapter code.

Use this path when your project stores Blyp logs through Drizzle and your dialect is Postgres or MySQL.

## Prerequisites

- `drizzle-orm` installed
- `drizzle-kit` installed
- a Drizzle config file exists at the project root
- a schema target exists or can be created
- the CLI can discover your DB module

The CLI looks for Drizzle config files such as `drizzle.config.ts`, `drizzle.config.js`, `drizzle.config.mjs`, or `drizzle.config.cjs`.

## Naming contract

- SQL table name: `blyp_logs`
- Drizzle export: `blypLogs`
- adapter config uses `table: blypLogs`

## Generated `blypLogs` schema for Postgres

```ts
import {
  boolean,
  doublePrecision,
  index,
  integer,
  jsonb,
  pgTable,
  text,
  timestamp,
  uuid,
  varchar,
} from "drizzle-orm/pg-core";

export const blypLogs = pgTable(
  "blyp_logs",
  {
    id: uuid("id").primaryKey(),
    timestamp: timestamp("timestamp", { withTimezone: true, precision: 6 }).notNull(),
    level: varchar("level", { length: 32 }).notNull(),
    message: text("message").notNull(),
    caller: text("caller"),
    type: varchar("type", { length: 64 }),
    groupId: varchar("group_id", { length: 191 }),
    method: varchar("method", { length: 16 }),
    path: text("path"),
    status: integer("status"),
    duration: doublePrecision("duration"),
    hasError: boolean("has_error").notNull(),
    data: jsonb("data"),
    bindings: jsonb("bindings"),
    error: jsonb("error"),
    events: jsonb("events"),
    record: jsonb("record").notNull(),
    createdAt: timestamp("created_at", { withTimezone: true, precision: 6 })
      .defaultNow()
      .notNull(),
  },
  (table) => [
    index("blyp_logs_timestamp_idx").on(table.timestamp),
    index("blyp_logs_level_timestamp_idx").on(table.level, table.timestamp),
    index("blyp_logs_type_timestamp_idx").on(table.type, table.timestamp),
    index("blyp_logs_group_id_timestamp_idx").on(table.groupId, table.timestamp),
  ],
);
```

## Generated `blypLogs` schema for MySQL

```ts
import {
  boolean,
  datetime,
  double,
  index,
  int,
  json,
  mysqlTable,
  text,
  varchar,
} from "drizzle-orm/mysql-core";

export const blypLogs = mysqlTable(
  "blyp_logs",
  {
    id: varchar("id", { length: 36 }).primaryKey(),
    timestamp: datetime("timestamp", { fsp: 6, mode: "date" }).notNull(),
    level: varchar("level", { length: 32 }).notNull(),
    message: text("message").notNull(),
    caller: text("caller"),
    type: varchar("type", { length: 64 }),
    groupId: varchar("group_id", { length: 191 }),
    method: varchar("method", { length: 16 }),
    path: text("path"),
    status: int("status"),
    duration: double("duration"),
    hasError: boolean("has_error").notNull(),
    data: json("data"),
    bindings: json("bindings"),
    error: json("error"),
    events: json("events"),
    record: json("record").notNull(),
    createdAt: datetime("created_at", { fsp: 6, mode: "date" }).defaultNow().notNull(),
  },
  (table) => [
    index("blyp_logs_timestamp_idx").on(table.timestamp),
    index("blyp_logs_level_timestamp_idx").on(table.level, table.timestamp),
    index("blyp_logs_type_timestamp_idx").on(table.type, table.timestamp),
    index("blyp_logs_group_id_timestamp_idx").on(table.groupId, table.timestamp),
  ],
);
```

For the field-by-field contract, see [Schema Contract](/docs/database/schema).

## `blyp.config.ts` example

```ts
import { createDrizzleDatabaseAdapter } from "@blyp/core/database";
import { db } from "./db";
import { blypLogs } from "./db/schema/blyp";

export default {
  destination: "database",
  database: {
    dialect: "postgres",
    adapter: createDrizzleDatabaseAdapter({
      db,
      table: blypLogs,
    }),
  },
};
```

## Migration flow

```bash
blyp db:init
blyp db:migrate
```

Drizzle projects do not use `blyp db:generate`.

## What `blyp db:init` does for Drizzle

The CLI resolves the Drizzle config, locates the schema target, finds the DB module, and then writes the Blyp schema in one of two ways:

- if the schema target is a directory, it creates `blyp.ts`
- if the schema target is a file, it appends the Blyp schema to that file

It then prepares the migration workflow so the generated schema can be applied.

## Failure cases

Likely Drizzle-specific failures include:

- no Drizzle config file at the project root
- no Drizzle schema hints, so the CLI cannot determine where the schema belongs
- `drizzle-orm` missing from `package.json`
- `drizzle-kit` missing from `package.json`
- the requested Blyp dialect does not match the Drizzle config dialect
- an existing `blypLogs` schema exists but does not match the Blyp contract
- the runtime adapter receives the wrong table symbol instead of `blypLogs`

## Related docs

- [Schema Contract](/docs/database/schema)
- [Migrations](/docs/database/migrations)
- [Troubleshooting](/docs/database/troubleshooting)

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
