S
SolidJS2mo ago
Hussein

how do i use Drizzle?

I'm trying to use Drizzle db on the server only and its giving me "Uncaught TypeError: promisify is not a function" in the browser.
import { cache, createAsync } from "@solidjs/router";
import { db } from "~/lib/db";

const getData = cache(() => {
return db.query.chat.findMany();
}, "data");

export const route = { load: getData };

export default function Home() {
const data = createAsync(() => getData());

return <p>{data()?.length}</p>;
}
import { cache, createAsync } from "@solidjs/router";
import { db } from "~/lib/db";

const getData = cache(() => {
return db.query.chat.findMany();
}, "data");

export const route = { load: getData };

export default function Home() {
const data = createAsync(() => getData());

return <p>{data()?.length}</p>;
}
this is what lib/db looks like
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";

export const db = drizzle(new Database("db.sqlite"), { schema });
migrate(db, { migrationsFolder: "drizzle" });
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";

export const db = drizzle(new Database("db.sqlite"), { schema });
migrate(db, { migrationsFolder: "drizzle" });
40 Replies
Brendonovich
Brendonovich2mo ago
i think you're missing a use server lol, it's trying to run the query on the client
Hussein
Hussein2mo ago
in cache?
Brendonovich
Brendonovich2mo ago
yeah
Hussein
Hussein2mo ago
same error
Brendonovich
Brendonovich2mo ago
where'd u put it?
Hussein
Hussein2mo ago
import { cache, createAsync } from "@solidjs/router";
import { db } from "~/lib/db";

const getData = cache(() => {
"use server";
return db.query.chat.findMany();
}, "data");

export const route = { load: getData };

export default function Home() {
const data = createAsync(() => getData());

return <p>{data()?.length}</p>;
}
import { cache, createAsync } from "@solidjs/router";
import { db } from "~/lib/db";

const getData = cache(() => {
"use server";
return db.query.chat.findMany();
}, "data");

export const route = { load: getData };

export default function Home() {
const data = createAsync(() => getData());

return <p>{data()?.length}</p>;
}
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
vite: { ssr: { external: ["better-sqlite3", "drizzle-orm"] } },
});
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
vite: { ssr: { external: ["better-sqlite3", "drizzle-orm"] } },
});
and i also put this in config didn't work
Brendonovich
Brendonovich2mo ago
oh definitely remove those
Brendonovich
Brendonovich2mo ago
no idea why they do that
Hussein
Hussein2mo ago
removing it didn't fix tho...
Brendonovich
Brendonovich2mo ago
try deleting node_modules/.vinxi and restart the dev server, and remove the migrate call
Hussein
Hussein2mo ago
i deleted node_modules/.vinxi, but what is the migrate call? oh in drizzle ok did it, it didn't work
apollo79
apollo792mo ago
I had some issues with prisma as well, moving the data functions (cache) to a different file made them go away (make the whole file "use server")
Brendonovich
Brendonovich2mo ago
ah you wouldn't want the whole file to be use server if you're calling cache in there but yea a separate file might help
Hussein
Hussein2mo ago
even if i get it work, this is honestly not as straightforward as sveltekit. in sveltekit you keep all your stuff in a file designed for server, +page.server.ts so its much more easy and noob-friendly doing that i got |- Error: Export from a 'use server' module must be a function
Brendonovich
Brendonovich2mo ago
are you doing
const getData = cache(() => {
"use server";
return db.query.chat.findMany();
}, "data");
const getData = cache(() => {
"use server";
return db.query.chat.findMany();
}, "data");
or
"use server";

const getData = cache(() => {
return db.query.chat.findMany();
}, "data");
"use server";

const getData = cache(() => {
return db.query.chat.findMany();
}, "data");
i'd say this is kinda by design - start & router try to be fairly hands off. you're given primitives to build how you want
Hussein
Hussein2mo ago
"use server";

import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { cache } from "@solidjs/router";
import { getCookie } from "vinxi/http";
import { eq } from "drizzle-orm";

export const db = drizzle(new Database("db.sqlite"), { schema });
export const getChat = cache(async () => {
"use server";

const cookie = getCookie("chat_session");
if (!cookie) return null;

const currChat = await db.query.chat.findFirst({
where: eq(schema.chat.id, cookie),
});

return currChat;
}, "chat");
"use server";

import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { cache } from "@solidjs/router";
import { getCookie } from "vinxi/http";
import { eq } from "drizzle-orm";

export const db = drizzle(new Database("db.sqlite"), { schema });
export const getChat = cache(async () => {
"use server";

const cookie = getCookie("chat_session");
if (!cookie) return null;

const currChat = await db.query.chat.findFirst({
where: eq(schema.chat.id, cookie),
});

return currChat;
}, "chat");
Brendonovich
Brendonovich2mo ago
if you're after a simpler (and more stable lol) dx then sveltekit is defs a better option
Hussein
Hussein2mo ago
i'm doing this i like solid because of the <Suspense> and cache architecture
Brendonovich
Brendonovich2mo ago
remove the top level use server - you can only use that if all the exports are plain functions
Hussein
Hussein2mo ago
so should i create a getDb function and return the drizzle db?
Brendonovich
Brendonovich2mo ago
no just remove the top level use server
Hussein
Hussein2mo ago
ok and then? it doesn't work
Brendonovich
Brendonovich2mo ago
what did you do about your original getData function
Hussein
Hussein2mo ago
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { cache } from "@solidjs/router";
import { getCookie } from "vinxi/http";
import { eq } from "drizzle-orm";

export const db = drizzle(new Database("db.sqlite"), { schema });
export const getChat = cache(async () => {
"use server";

const cookie = getCookie("chat_session");
if (!cookie) return null;

const currChat = await db.query.chat.findFirst({
where: eq(schema.chat.id, cookie),
});

return currChat;
}, "chat");
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import { cache } from "@solidjs/router";
import { getCookie } from "vinxi/http";
import { eq } from "drizzle-orm";

export const db = drizzle(new Database("db.sqlite"), { schema });
export const getChat = cache(async () => {
"use server";

const cookie = getCookie("chat_session");
if (!cookie) return null;

const currChat = await db.query.chat.findFirst({
where: eq(schema.chat.id, cookie),
});

return currChat;
}, "chat");
its the same problem i just changed my cache function xd
Brendonovich
Brendonovich2mo ago
ah right yeah i'm not quite sure, that's really weird are u able to share the source?
Hussein
Hussein2mo ago
repo?
Brendonovich
Brendonovich2mo ago
yea
Hussein
Hussein2mo ago
my first message has a full repro does it not bug for u?
Brendonovich
Brendonovich2mo ago
i use drizzle and start without ssr and it's fine. a full repro would be something i can download and run
Brendonovich
Brendonovich2mo ago
cache + server function in one file db in another file produces a different error but it's fine
Hussein
Hussein2mo ago
🤣 did u find any solution?
Katja (katywings)
"use serve" <- typo 🤓
Brendonovich
Brendonovich2mo ago
yea ik it still gives that error haha that is my solution, that async_hooks error isn't that important
Hussein
Hussein2mo ago
😐 are u being sarcastic bc those modules are leaking into client and making the bundle bigger
Brendonovich
Brendonovich2mo ago
put the server functions and cache/action uses in separate files
Hussein
Hussein2mo ago
create pr in the repo and i'll merge
Brendonovich
Brendonovich2mo ago
i mean you've got the solution, implement it in your app nevermind the repro
Hussein
Hussein2mo ago
cool it works i'll close this post
Want results from more Discord servers?
Add your server
More Posts
Problems changing the value of a reactive signal returned from a functionI was testing what would happen if I changed the entire value of the board, not the squares but the OptionalParams in route not workingI'm not getting the expected result using optional params - routes -- users ---- [[id]].tsx ---- inReactivity resulting from updates in a websocket event listenerI am struggling to understand why setting a signal in an event listener is not driving re-rendering How do i conditionally add something to <head> based on what createAsync returns?I need to wait for a resource coming from `createAsync` so then i can decide whether i should add goHow do you delete an entry in an array when using `createStore`In the following documentation, this case is not presented: https://docs.solidjs.com/guides/complex-Uncaught ReferenceError: React is not definedHey there! Please help. I can't figure out this error. I have a tiny solid-js project that works okWhat do I do if I have to wait for things to happen, meanwhile define extra state or whatthis piece of code come up as crucial functionality all over the place for me, for an interactive apVinxi build failing in CIHi, I wanted to use SolidStart for my project and the website builds fine locally, but in GItHub actReactivity in single table cell from memoized data[Moving from this thread (https://discord.com/channels/722131463138705510/1241054227485425734/124162Insights on my component libraryHello, i'm building my own component library to fit my project needs. I'm having a bit of a problem