#db0 adapter for sqlite sessions

4 messages · Page 1 of 1 (latest)

glad galleon
#
import { createDatabase } from "db0";
import sqlite from "db0/connectors/node-sqlite";
import { drizzle as Drizzle } from "db0/integrations/drizzle";
import path from "path";
import "dotenv/config";

const dbPath = path.resolve(process.env.DATABASE_PATH);

const db0 = createDatabase(
    sqlite({
        path: dbPath,
    }),
);

const drizzle = Drizzle(db0);

export { db0, drizzle };
import { defineConfig } from "astro/config";
import tailwindcss from "@tailwindcss/vite";
import node from "@astrojs/node";
import vue from "@astrojs/vue";
import { db0 } from "./src/db/db.ts";

export default defineConfig({
    vite: { plugins: [tailwindcss()] },

    adapter: node({
        mode: "standalone",
    }),

    output: "server",

    session: {
        driver: "db0",
        cookie: {
            name: "session",
            sameSite: "lax",
            maxAge: 3600,
        },
        options: {
            database: db0,
            table: "sessions",
        },
    },

    integrations: [vue()],
});

This works fine when running the dev server. Problem is when building, I guess vite bundling gets in the way?

➜  astro-sandbox git:(master) ✗ node dist/server/entry.mjs
02:48:27 [@astrojs/node] Server listening on http://localhost:4321
    [unstorage]: Database driver is experimental and behavior may change in the future.
02:48:30 [ERROR] TypeError: opts.database.sql is not a function

Any ideas of how to properly use the db0 adapter with sessions?

hazy token
#

Not sure what good db0 is for you if you're only using a single SQLite database.

If you want something minimal, nodejs actually has a SQLite API that's ready to try: https://nodejs.org/api/sqlite.html

Out of experiments, if you want a good ORM around the DB instance, drizzle is king nowadays.
Otherwise, I'd recommend better-sqlite3.

glad galleon
#

The problem is db0 seems to be the only supported sql session driver. And unless I'm missing some important details it is expecting an instance to be passed in astro.config.mjs. This works for development, but will break once bundled. That db0 instance then no longer works. That is my issue here.

hazy token
#

Hmm, an experimental API with an experimental node_module, you're bound to have some surprises 🤣
My knowledge of sessions is very shallow, I'm afraid.