DallasHoff / sqlocal

SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system.
https://sqlocal.dallashoffman.com
MIT License
212 stars 8 forks source link

How to handle adding new tables to the database when using drizzle? #25

Closed ellisjasmine closed 3 months ago

ellisjasmine commented 3 months ago

I'm having problems connecting tables I define with my database. In the drizzle docs they mention migrations however it's not clear how to make this work with SQLocal.

My code follows the readme pretty closely. The database is created successfully but I can't insert anything into the table.

// db/index.ts
import "client-only";

import { drizzle } from "drizzle-orm/sqlite-proxy";
import { SQLocalDrizzle } from 'sqlocal/drizzle';

const { driver } = new SQLocalDrizzle('database.sqlite3');
const db = drizzle(driver);

// db/schema.ts
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const groceries = sqliteTable("groceries", {
  id: int("id").primaryKey({ autoIncrement: true }),
  name: text("name").notNull(),
});

export type Groceries = typeof groceries.$inferSelect; // return type when queried

// component.ts
"use client"
import { database, sqlite } from "../db";
import { groceries } from "../db/schema";

...
database.insert(groceries).values([{ name: "milk" }, { name: "eggs" }]);
fenicento commented 1 week ago

Sorry to reopen this but I have the same problem. I define my schema and then run a query, but I get an error in the console:

SQLite3Error: SQLITE_ERROR: sqlite3 result code 1: no such table: institutions
    at toss3 (sqlite3-bundler-friendly.mjs?v=28ec45dd:10701:17)
    at checkSqlite3Rc (sqlite3-bundler-friendly.mjs?v=28ec45dd:10720:13)
    at DB.checkRc (sqlite3-bundler-friendly.mjs?v=28ec45dd:11033:42)
    at OpfsDb.exec (sqlite3-bundler-friendly.mjs?v=28ec45dd:11167:20)
    at SQLocalProcessor.value [as exec] (processor.js?v=28ec45dd:171:50)
    at SQLocalProcessor.value [as postMessage] (processor.js?v=28ec45dd:112:30)
    at SQLocalProcessor.value [as flushQueue] (processor.js?v=28ec45dd:370:26)
    at SQLocalProcessor.value [as init] (processor.js?v=28ec45dd:91:22)

In the tests I see that tables are manually created: https://github.com/DallasHoff/sqlocal/blob/main/test/drizzle/driver.test.ts but the correct way would be to generate a migration. Is there a way to solve this? @ellisjasmine do you have any suggestions?

DallasHoff commented 2 days ago

See #32