Skip to content

adrienZ/drizzle-schema-checker

Repository files navigation

drizzle-schema-checker

Checks if a Drizzle schema is also valid in a database.

uses unjs/db0 to connect your database.


npm version npm downloads License Codecov


Installation

npm install drizzle-schema-checker
# or
pnpm add drizzle-schema-checker

Usage

First, create your schemas using Zod:

// schema.ts
import { sqliteTable, text, integer, primaryKey } from "drizzle-orm/sqlite-core";
import { sql } from "drizzle-orm";

export const userTable = sqliteTable("users", {
  id: text("id").primaryKey().notNull(),
  password: text("password"),
  email: text("email").notNull().unique(),
  created_at: integer("created_at", { mode: "timestamp" }).notNull().default(sql`CURRENT_TIMESTAMP`),
  updated_at: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`CURRENT_TIMESTAMP`),
});

Then, use the checkDatabaseValidity function to validate your database:

// your db stuff
import { createChecker } from 'drizzle-schema-checker';
import sqlite from "db0/connectors/better-sqlite3";
import { createDatabase } from "db0";
import { userTable } from './schema';

const db = createDatabase(sqlite());
const dbChecker = createChecker(db, "sqlite");

try {
  const validatedTable = await checker.checkTable("user", userTable);
  console.log('table user is valid in db');
} catch (error) {
  console.error(error.message);
}

Roadmap

Development

Prerequisites

  • pnpm
  • Bun (for testing)

Setup

  1. Clone the repository:

    git clone https://github.com/adrienZ/drizzle-schema-checker.git
    cd drizzle-schema-checker
    
  2. Install dependencies:

    pnpm install
    
  3. Build the project:

    pnpm build
    

Testing

To run tests using Vitest:

pnpm test

To run tests with coverage:

pnpm test:ci

Linting and Formatting

  • Lint: pnpm lint
  • Format: pnpm format

License

MIT