vikejs / bati

Next-gen scaffolder. Get started with fully-functional apps, and choose any tool you want.
https://batijs.dev
MIT License
228 stars 13 forks source link

cloudflare + drizzle not working by default #329

Closed nathanredblur closed 1 week ago

nathanredblur commented 1 month ago

Describe the bug I'm creating a new project with pnpm create bati --react --tailwindcss --trpc --hono --drizzle --cloudflare after create the DB and check that everything works on run pnpm run dev I'm trying to run pnpm run preview but fails with this and other errors.

> wrangler pages dev

 ⛅️ wrangler 3.69.1 (update available 3.70.0)
-------------------------------------------------------

✘ [ERROR] Could not resolve "fs"

    ../../node_modules/.pnpm/better-sqlite3@11.1.2/node_modules/better-sqlite3/lib/database.js:2:19:
      2 │ const fs = require('fs');
        ╵                    ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "path"

    ../../node_modules/.pnpm/better-sqlite3@11.1.2/node_modules/better-sqlite3/lib/database.js:3:21:
      3 │ const path = require('path');
        ╵                      ~~~~~~

To Reproduce

pnpm create bati --react --tailwindcss --trpc --hono --drizzle --cloudflare
pnpm drizzle:generate
pnpm drizzle:migrate
pnpm drizzle:seed
pnpm run preview

Expected behavior preview should work out of the box. if D1 setup is required, the instructions should be part of the read me. https://orm.drizzle.team/docs/get-started-sqlite#cloudflare-d1

Additional context I just try to follow the drizzle instruction, but I don't know how to make this works. db.ts has this

import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";

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

and export the db object, but in drizzle example, we have a request that has an env attribute that contains the DB.

export default {
  async fetch(request: Request, env: Env) {
    const db = drizzle(env.<BINDING_NAME>);
    const result = await db.select().from(users).all()
    return Response.json(result);
  },
};

so I can not easly export that db object to be used in the rest of the app.

magne4000 commented 1 month ago

Solution: replace better-sqlite3 usage with D1 when cloudflare is selected.

I'll probably have time next week to fix this.