Pavan
Pavan
CDCloudflare Developers
Created by PneuTueur on 8/9/2024 in #workers-help
Access D1 database outside a request
Do you use Hono with cloudflare workers, right? Do u can help with that: https://discord.com/channels/595317990191398933/1276333679907704984/1276333679907704984
18 replies
CDCloudflare Developers
Created by PneuTueur on 8/9/2024 in #workers-help
Access D1 database outside a request
You can create a middleware that you set a d1 clousure instance and use it without passing context.d1 to your functions that need dababase: ex:
class D1 {
private _db: D1Database | undefined;

get db() {
return this._db as D1Database;
}

setDb(db: D1Database) {
this._db = db;
}
}

export default new D1();
class D1 {
private _db: D1Database | undefined;

get db() {
return this._db as D1Database;
}

setDb(db: D1Database) {
this._db = db;
}
}

export default new D1();
the middleware before all req could be like that:
import D1 from ''

return async function PrepareDBMiddleware(c: Context, next: () => any) {
D1.setDb(c.D1);
await next();
}
import D1 from ''

return async function PrepareDBMiddleware(c: Context, next: () => any) {
D1.setDb(c.D1);
await next();
}
to use
import D1 from ''

const clients = await D1.db.prepare('').........
import D1 from ''

const clients = await D1.db.prepare('').........
18 replies
CDCloudflare Developers
Created by Pavan on 8/23/2024 in #workers-help
Cors blocking using Hono.js and workers
app.use("*", async (c, next) => {
const corsMiddleware = cors({
origin: ["*"],
allowMethods: ["GET", "OPTIONS", "POST", "PUT", "DELETE"],
credentials: true,
});
return corsMiddleware(c, next);
});
app.use("*", async (c, next) => {
const corsMiddleware = cors({
origin: ["*"],
allowMethods: ["GET", "OPTIONS", "POST", "PUT", "DELETE"],
credentials: true,
});
return corsMiddleware(c, next);
});
This way it works when calling from localhost:3000, but not from cloudflare pages (https://develop.scheduling-next-front-end.pages.dev)
3 replies
CDCloudflare Developers
Created by Pavan on 8/23/2024 in #workers-help
Cors blocking using Hono.js and workers
I think that the blocking is happening before it reaches my application. Because when I remove the origin from http://localhost:3000 and try to access from localhost:3000 I have see the OPTIONS logs in the WORKER panel, but when I try to access from "https://develop.scheduling-next-front-end.pages.dev" I havent see the OPTIONS logs in the worker panel. Is there any CORS blocking in Cloud Flare before it reaches my application?
3 replies