unjs / db0

📚 Lightweight SQL Connector
https://db0.unjs.io
MIT License
127 stars 10 forks source link

Turso Connector + Drizzle ORM #64

Open justserdar opened 3 months ago

justserdar commented 3 months ago

Describe the feature

I believe I'm close to having it working, will PR soon if nobody else does. image

import { describe } from "vitest";
import libSql from "../../src/connectors/libsql/turso";
import { testConnector } from "./_tests";

describe("connectors: turso", () => {
  testConnector({
    connector: libSql({
        url: process.env.TURSO_DB_URL!,
        authToken: process.env.TURSO_DB_AUTH_TOKEN!,
    }),
  });
});

Runs the tests fine, I have visual output in browser with Drizzle Studio. I started running some quick Nuxt 3 tests with forked builds of db0. Also noticed there are multiple ways to go by this... .

The connector runs on ./libsql/node and relies on url and authToken instead of local filePath. The way I went about it is just by creating a ./libsql/turso.ts version of ./libsql/node. You could technically do the same if the 'file:' prefix wasnt hardcoded for local dev db's.

From a Full Stack development pov, I would personally love to automagically detect wether your running better-sqlite3 locally by simply ommitting or including your Turso .env variables.

Here's an example just using db0:

import { createDatabase } from "db0";
import { drizzle } from "db0/integrations/drizzle"; 
import { libSqlConnector } from "db0/connectors/libsql/turso"; 

const db = createDatabase(libSqlConnector());
const drizzleDb = drizzle(db);

Here's basic config with Nuxt 3 / Nitro

export default defineNuxtConfig({
    nitro: {
        experimental: {
            database: true,
        },
        database: {
            default: {
                connector: 'turso',
                options: {
                    url: process.env.TURSO_DB_URL,
                    authToken: process.env.TURSO_DB_AUTH_TOKEN,
                }
            }
        }
    },
})

Basic usecase in Nuxt 3/ Nitro

./server/utils/db.ts
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";

export const database = createClient({
  url: process.env.TURSO_DB_URL!,
  authToken: process.env.TURSO_DB_AUTH_TOKEN!,
});

export const orm = drizzle(database);

Experimental route Nuxt 3/ Nitro/ db0

./server/utils/db.ts

import { drizzle } from "db0/integrations/drizzle"; 
export const orm = drizzle(useDatabase());

Would love to see this happen! Even sounds catchy like, Drizzle... UseDatabase!!

Additional information

justserdar commented 3 months ago

I've finished my research and testing. There is no need to create a new adapter/connector or integration for Turso DB support, it already works as it is. The hardcoding of 'file:' in the libsql module apparently somewhere overrides itself when you enter a libsql: url, which just dissolves the entire problem to begin with.

I have a article available with the current build of Nuxt (excluding the experimental databse(db0) module for comparison sake). You can test the current build (Nuxt 3.10) with @libsql/client directly in Nitro 'server/utils/db.ts' route. Article Github Repo

I'm finishing up my update post about the new experimental route. "Nuxt Experimental Features: Connect your Turso DB natively with db0 and Drizzle" Github Repo Nightly Branch

The new db0 module offers so much extra DX, I wish you knew how happy I am with it already.

Great work! I really hope this makes it into the main Nuxt build. With your permission I'll update the docs for usage inside Nitro/Nuxt, because currently I don't think this is clear at all.