Thanks - I didn’t realise Durable Objects had an SQLite component
Thanks - I didn’t realise Durable Objects had an SQLite component


createDB() function?createDB function messing up something.
env across Worker requests, which is why you receive it in every request.
createDB()createDBCREATE TRIGGER deduct_coins_on_consumable
BEFORE INSERT ON ActiveConsumables
BEGIN
UPDATE accounts
SET pseudoCoins = pseudoCoins - (SELECT cost FROM Consumables WHERE id = NEW.consumableId)
WHERE
id = NEW.accountId
AND
pseudoCoins >= (SELECT cost FROM Consumables WHERE id = NEW.consumableId);
SELECT RAISE(ROLLBACK, 'Insufficient coins') WHERE changes() = 0;
ENDexport class Auth {
static initialized = false;
private static instance: ReturnType<typeof betterAuth>;
static async init(c: Context<Env>) {
if (Auth.instance) {
await Auth.getSession(c);
Auth.initialized = true;
return Object.freeze(Auth.instance);
}
Auth.instance = betterAuth({
database: drizzleAdapter(
{
dialect: new D1Dialect({
database: c.env.DB,
}),
},
),
});
Auth.initialized = true;
await Auth.getSession(c);
return Object.freeze(Auth.instance);
}
static async getSession(c: Context) {
const session = await Auth.instance.api.getSession({
headers: c.req.raw.headers,
});
c.set("user", session?.user ?? null);
c.set("session", session?.session ?? null);
return session;
}
static getInstance() {
if (!Auth.instance) {
throw new Error("Auth instance not initialized");
}
return Object.freeze(Auth.instance);
}
}const app = new Hono<Env>();
app.use("*", async (c, next) => {
if (Auth.initialized) return next();
await Auth.init(c);
return next();
});
app.all("/api/**", (c) => Auth.getInstance().handler(c.req.raw));
export default app;
export type ApiRoutes = typeof apiRoutes;