Skip to content

Commit

Permalink
Try implementing a d1 binding
Browse files Browse the repository at this point in the history
  • Loading branch information
brettimus committed Aug 27, 2024
1 parent 5dd209f commit 6a88421
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
18 changes: 15 additions & 3 deletions packages/client-library-otel/src/cf-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { measure } from "./measure";
// TODO - Can we use a Symbol here instead?
const IS_PROXIED_KEY = "__fpx_proxied";

export function isCloudflareAiBinding(o: unknown) {
function isCloudflareAiBinding(o: unknown) {
const constructorName = getConstructorName(o);
if (constructorName !== "Ai") {
return false;
Expand All @@ -13,7 +13,7 @@ export function isCloudflareAiBinding(o: unknown) {
return true;
}

export function isCloudflareR2Binding(o: unknown) {
function isCloudflareR2Binding(o: unknown) {
const constructorName = getConstructorName(o);
if (constructorName !== "R2Bucket") {
return false;
Expand All @@ -23,9 +23,19 @@ export function isCloudflareR2Binding(o: unknown) {
return true;
}

export function isCloudflareD1Binding(o: unknown) {
const constructorName = getConstructorName(o);
if (constructorName !== "D1Database") {
return false;
}

console.log("D1 shall be proxied", o);
return true;
}

// TODO - Remove this, it is temporary
function isCloudflareBinding(o: unknown): o is object {
return isCloudflareAiBinding(o) || isCloudflareR2Binding(o);
return isCloudflareAiBinding(o) || isCloudflareR2Binding(o) || isCloudflareD1Binding(o);
}

/**
Expand Down Expand Up @@ -60,6 +70,8 @@ export function proxyCloudflareBinding(o: unknown, bindingName: string) {
return o;
}

console.log("Proxying binding", bindingName, o);

const proxiedBinding = new Proxy(o, {
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/goosify/.dev.vars.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# TODO
FPX_ENDPOINT=...
4 changes: 3 additions & 1 deletion sample-apps/goosify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { drizzle } from "drizzle-orm/d1";
import { Hono } from "hono";
import { instrument } from "@fiberplane/hono-otel";
import * as schema from "./db/schema";

type Bindings = {
DB: D1Database;
AI: Ai;
GOOSIFY_KV: KVNamespace;
GOOSIFY_R2: R2Bucket;
FPX_ENDPOINT: string;
};

const app = new Hono<{ Bindings: Bindings }>();
Expand All @@ -23,4 +25,4 @@ app.get("/api/geese", async (c) => {
return c.json({ geese });
});

export default app;
export default instrument(app);
3 changes: 3 additions & 0 deletions sample-apps/goosify/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "goosify"
compatibility_date = "2024-08-27"
compatibility_flags = [ "nodejs_compat" ]

# [dev]
# port = 3003

[[kv_namespaces]]
binding = "GOOSIFY_KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand Down

0 comments on commit 6a88421

Please sign in to comment.