Svelte, Hono, and D1 database

This is my middleware:
import { drizzle, type DrizzleD1Database } from "drizzle-orm/d1";
import { createMiddleware } from "hono/factory";

type Env = {
Bindings: {
DB: D1Database;
};
Variables: {
db: DrizzleD1Database;
};
};

export const middleware = createMiddleware<Env>(async (c, next) => {
const db = drizzle(c.env.DB);

c.set("db", db);

await next();
});
import { drizzle, type DrizzleD1Database } from "drizzle-orm/d1";
import { createMiddleware } from "hono/factory";

type Env = {
Bindings: {
DB: D1Database;
};
Variables: {
db: DrizzleD1Database;
};
};

export const middleware = createMiddleware<Env>(async (c, next) => {
const db = drizzle(c.env.DB);

c.set("db", db);

await next();
});
This is how I used:
.get("/getAll", middleware, async (c) => {
const db = c.get("db");

try {
const subscriptions = await db.select().from(SubscriptionTable);

return c.json({ data: subscriptions });
} catch (error) {
const message = handleError(error);
return c.json({ message }, 500);
}
})
.get("/getAll", middleware, async (c) => {
const db = c.get("db");

try {
const subscriptions = await db.select().from(SubscriptionTable);

return c.json({ data: subscriptions });
} catch (error) {
const message = handleError(error);
return c.json({ message }, 500);
}
})
I also set like this in my wrangler.json file:
"d1_databases": [
{
"binding": "DB",
"database_name": "*****",
"database_id": "*****"
}
]
"d1_databases": [
{
"binding": "DB",
"database_name": "*****",
"database_id": "*****"
}
]
Why I am getting this error in my svlete project?
TypeError: Cannot read properties of undefined (reading 'DB')
at eval (/Users/sithu/ht-saas/recurrify/src/lib/features/subscriptions/route.ts:32:40)
TypeError: Cannot read properties of undefined (reading 'DB')
at eval (/Users/sithu/ht-saas/recurrify/src/lib/features/subscriptions/route.ts:32:40)
1 Reply
Bonadio
Bonadio7d ago
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);
}
})
I apply my middeware in the beginning with app.use('*', dbMiddleware) Before I define the routes

Did you find this page helpful?