Skip to content

Instantly share code, notes, and snippets.

@ThimoDEV
Created November 12, 2023 15:27
Show Gist options
  • Save ThimoDEV/658a8af7f757611f3c7d66014f8de35b to your computer and use it in GitHub Desktop.
Save ThimoDEV/658a8af7f757611f3c7d66014f8de35b to your computer and use it in GitHub Desktop.
A gist for building a Drizzle local sqlite together with turso, while being able to make partial select queries
import { createClient } from "@libsql/client"
import { env } from "~/env.mjs"
import Database from "better-sqlite3"
import { drizzle as drizzleLocal } from "drizzle-orm/better-sqlite3"
import { drizzle as drizzleTurso } from "drizzle-orm/libsql"
import * as schema from "./schema"
const drizzleType = drizzleTurso({} as any, { schema })
type DBType = typeof drizzleType
function initializeDb() {
if (env.DRIZZLE_ENV === "localhost") {
const sqlite = new Database("local.db")
return drizzleLocal(sqlite, { schema })
} else {
return drizzleTurso(
createClient({
url: env.TURSO_DB_URL as string,
authToken: env.TURSO_DB_AUTH_TOKEN as string,
}),
{ schema }
)
}
}
export const db = initializeDb() as DBType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment