Bonadio
Bonadio
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
No description
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
You need to run wrangler dev and access any route that make a call to the database the first time it will be blank and the call will fail but it will create the sqlite file. them you can open it and create the tables. In dev I create my tables using drizzle generate than I copy and paste the SQL in "DB Browser Sqlite" to create the tables
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
At you project there will be a hidden folder .wrangler if you expand it there will be a d1 subfolder and the sqlite file will be there.
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
Yes, only one db instance in wangler. and this is my drizzle config import type { Config } from 'drizzle-kit' export default { schema: './app/db/customerSchema.ts', out: './drizzle', } satisfies Config
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
You define a single DB, when you run on dev the DB is a local sqlite in your hard drive. On the server is D1
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
I apply my middeware in the beginning with app.use('*', dbMiddleware) Before I define the routes
15 replies
CDCloudflare Developers
Created by Sithu Khant on 2/13/2025 in #workers-help
Svelte, Hono, and D1 database
Create a file called InitDb.ts
export let db: DrizzleD1Database;

export const initDb = (c: Context) => {
db = drizzle(c.env.DB);
}
export let db: DrizzleD1Database;

export const initDb = (c: Context) => {
db = drizzle(c.env.DB);
}
Then in your middleware do
export const middleware = createMiddleware<Env>(async (c, next) => {
initDb(c)
await next()
});
export const middleware = createMiddleware<Env>(async (c, next) => {
initDb(c)
await next()
});
In your routes
import {db} from InitDb

.get("/getAll", middleware, async (c) => {

try {
const subscriptions = await db.select().from(SubscriptionTable);
return c.json({ data: subscriptions });
} catch (error) {
const message = handleError(error);
return c.json({ message }, 500);
}
})
import {db} from InitDb

.get("/getAll", middleware, async (c) => {

try {
const subscriptions = await db.select().from(SubscriptionTable);
return c.json({ data: subscriptions });
} catch (error) {
const message = handleError(error);
return c.json({ message }, 500);
}
})
15 replies