S
SolidJS7mo ago
Hussein

how to use bun:sqlite without it leaking to the browser?

I'm using a Drizzle DB. how do i initialize it and use it in a route file in solid start?
7 Replies
brenelz
brenelz7mo ago
You would use a server function async function getFromDb() { "use server" // Access db }
Hussein
HusseinOP7mo ago
i have this in a file:
export const db = drizzle(new Database("db.sqlite"), { schema });
export const db = drizzle(new Database("db.sqlite"), { schema });
how do i use it only on the server? like in a form action.
brenelz
brenelz7mo ago
It depends where its used and it get treeshaken appropriately
Hussein
HusseinOP7mo ago
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import { drizzle } from "drizzle-orm/bun-sqlite";
import Database from "bun:sqlite";
import * as schema from "../lib/schema";

const db = drizzle(new Database("db.sqlite"), { schema });

migrate(db, { migrationsFolder: "drizzle" });

const add = action(async () => {
("use server");

db.insert(id).values({ id: crypto.randomUUID() });
}, "add");
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import { drizzle } from "drizzle-orm/bun-sqlite";
import Database from "bun:sqlite";
import * as schema from "../lib/schema";

const db = drizzle(new Database("db.sqlite"), { schema });

migrate(db, { migrationsFolder: "drizzle" });

const add = action(async () => {
("use server");

db.insert(id).values({ id: crypto.randomUUID() });
}, "add");
i'm doing this and its leaking to the client...
brenelz
brenelz7mo ago
use server shouldn't have the round brackets
Hussein
HusseinOP7mo ago
i don't think that's the issue
rmacfie
rmacfie7mo ago
You may get some guidance from this example with Prisma: https://github.com/solidjs/solid-start/tree/main/examples/with-prisma Pay attention to how the const db is declared in one file (src/lib/db.ts) and the actions with "use server" are declared in another (src/lib/index.ts), and then the components that use them are in yet another file. This separation is necessary to enable complete treeshaking so that the code that you only want on the server does not leak to the client. I'm hoping there will be more thorough documentation on this, because I also find these things to be rather confusing.
GitHub
solid-start/examples/with-prisma at main · solidjs/solid-start
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
Want results from more Discord servers?
Add your server