drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23k stars 550 forks source link

[BUG]: No way to use `bun:sqlite` together with dirzzle-kit #1520

Open TheKnarf opened 9 months ago

TheKnarf commented 9 months ago

What version of drizzle-orm are you using?

v0.29.0

What version of drizzle-kit are you using?

v0.20.1

Describe the Bug

Ran the following command where driver is set to bun:sqlite:

$ bun drizzle-kit push:sqlite
No config path provided, using default path
Reading config file '/Users/knarf/projects/git-repo-backup/drizzle.config.ts'
 Invalid input  Either "turso", "libsql", "better-sqlite" are available options for "--driver"
error: "drizzle-kit" exited with code 1 (SIGHUP)

Expected behavior

For this to work when running with bun drizzle-kit

Environment & setup

No response

dinfer commented 9 months ago

I'm using bun + drizzle-kit. it's very quick to use push:sqlite to change tables in dev environment. Is there any progress here?

rifaldhiaw commented 8 months ago

using driver: "better-sqlite", seems working just fine with bun sqlite

MickL commented 4 months ago

@rifaldhiaw How did you configure it exactly? It tells me "url" is a required option for driver "better-sqlite" but bun:sqlite doesnt have a url

MickL commented 4 months ago

I also dont know how to use bun:sqlite with Drizzle Kit / Drizzle Studio.

This issue is 1,5 years old. I wonder if Drizzle is still in development or where the focus lies? @dankochetov

Another related issue (maybe a duplicate) with 10 upvotes: #1795 Also related: https://github.com/drizzle-team/drizzle-bun/issues/5

P.S. The same goes for Vercel and Neon -> Why cant I use this drivers within drizzle.config ? Issue: #1612

keisanng commented 4 months ago

@dankochetov Will drizzle team fully implement support for Bun SQLite in Drizzle Kit?

MickL commented 4 months ago

@AndriiSherman @dankochetov

doroved commented 4 months ago

I confirm that drizzle studio does not work with bun:sqlite

Please install either 'better-sqlite3' or '@libsql/client' for Drizzle Kit to connect to SQLite databases
error: script "db:studio" exited with code 1
StevenStavrakis commented 2 months ago

still an issue haha

MickL commented 2 months ago

There seems to be some development within Drizzle but I dont feel they look at any Github issues at all. For example I had an open issue which they fixed "by accident" but they didnt close my issue or reacted in any way.

IslamZaoui commented 2 months ago

I found the solution in bunjs docs here

just add this:

// database/index.ts
import { drizzle } from 'drizzle-orm/bun-sqlite';
import { Database } from 'bun:sqlite';
import { migrate } from 'drizzle-orm/bun-sqlite/migrator';

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);
migrate(db, { migrationsFolder: './drizzle' });

export { db };
// drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
    dialect: 'sqlite',
    schema: './src/lib/server/database/schema.ts',
    out: './drizzle',
    dbCredentials: {
        url: 'file:./sqlite.db'
    }
});

run bunx drizzle-kit generate to generate migrations in ./drizzle and everything should be fine

edit: you need to restart your dev server to load the migration 😑

fo-rk commented 2 months ago

bunx drizzle-kit studio seems to be working for me on latest drizzle-kit and bun with following config.

{
    "dialect": "sqlite",
    "schema": "./data/schema.js",
    "out": "./drizzle",
    "dbCredentials": {
        "url": "file:./data/db.sqlite"
    },
    "verbose": "true"
}
doroved commented 2 months ago

bunx drizzle-kit studio seems to be working for me on latest drizzle-kit and bun with following config.

{
    "dialect": "sqlite",
    "schema": "./data/schema.js",
    "out": "./drizzle",
    "dbCredentials": {
        "url": "file:./data/db.sqlite"
    },
    "verbose": "true"
}
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: 'sqlite',
  schema: './src/db/schema.ts',
  out: './src/db/migrations',
  dbCredentials: {
    url: './data/db/app.db',
  },
  verbose: true,
})

Please install either 'better-sqlite3' or '@libsql/client' for Drizzle Kit to connect to SQLite databases

fo-rk commented 2 months ago

I get that with drizzle-kit studio command @doroved, but with bun drizzle-kit studio or bunx drizzle-kit studio it's working.

shmshd commented 1 month ago

Please install either 'better-sqlite3' or '@libsql/client' for Drizzle Kit to connect to SQLite databases

I think better-sqlite3 is unnecessary for bun:sqlite. However, the generate command checks for the existence of better-sqlite3 or @libsql/client to do the job when the dialect is SQLite. I had to install better-sqlite3 only to satisfy the generate command, even though I don't use it.

For anyone needing a working configuration, here's mine:

// db.ts
import { Database } from 'bun:sqlite';
import { drizzle } from 'drizzle-orm/bun-sqlite';

import * as schema from '@/db/schema';

const sqlite = new Database();
export const db = drizzle(sqlite, { schema });
// drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  dbCredentials: {
    url: 'file:path/to/local.db',
  },
  dialect: 'sqlite',
  out: './src/db/migrations',
  schema: './src/db/schema.ts',
});
MickL commented 1 month ago

Bun natively implements a high-performance SQLite3 driver. And it is already usable with Drizzle. Why should we install another third party sqlite driver just for Drizzle Kit?

The issue is marked as a bug but please consider this as a feature request to support both Bun Database and Bun native driver bun:sqlite.

jazoom commented 1 month ago

I can confirm introspect and push refuse to work with bun SQLite.

bunx drizzle-kit introspect
drizzle-kit: v0.23.0
drizzle-orm: v0.32.0

No config path provided, using default path
Reading config file '<file>'
Pulling from ['public'] list of schemas

Please install either 'better-sqlite3' or '@libsql/client' for Drizzle Kit to connect to SQLite databases
jazoom commented 1 month ago

Also worth noting that even after installing better-sqlite3 push still fails.

TypeError: This statement does not return data. Use run() instead

migrate did seem to work.

jazoom commented 1 week ago

I'll also add that better-sqlite3 requires python to be installed, which I found out when testing pushing to production. So not only does better-sqlite3 need to be unnecessarily installed, so does python.