Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turso Connector + Drizzle ORM #64

Open
1 task done
justserdar opened this issue Mar 13, 2024 · 1 comment
Open
1 task done

Turso Connector + Drizzle ORM #64

justserdar opened this issue Mar 13, 2024 · 1 comment

Comments

@justserdar
Copy link

justserdar commented Mar 13, 2024

Describe the feature

  • db0 to Turso with Drizzle ORM support.

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

  • Would you be willing to help implement this feature?
@justserdar
Copy link
Author

justserdar commented Mar 15, 2024

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.

  • No need to create useDatabase() as it's already here now.
  • No need to setup useDatabase(), it configures itself from nuxt/nitro.config
  • No need to setup your own dev database at all, ever, it configures itself.
    Combined with Nuxt Auth Utils, this will become an even bigger banger feature imo. making it a no-brainer.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant