Hi there. Sorry in advance for the noob

Hi there. Sorry in advance for the noob question. Does binding values have to be unique when using the Wrangler CLI / wrangler.toml config? E.G: if I have multiple environments, that I want to connect to different D1 databases. I'm a bit confused since in the dashboard it seems like you create 1 binding per 1 database, but when using the wrangler.toml config file and the CLI, the binding name seems to be used more like an environment variable that you can then use in your worker. Would the following 2 environments point to 2 different databases even though the binding names are the same?
[env.staging]
d1_databases = [
{ binding = "DB", database_name = "staging", database_id = "unique_db_id_1", migrations_dir = "migrations", preview_database_id = "DB_STAGING" },
]
workers_dev = true

[env.production]
d1_databases = [
{ binding = "DB", database_name = "production", database_id = "unique_db_id_2", migrations_dir = "migrations"},
]
[env.staging]
d1_databases = [
{ binding = "DB", database_name = "staging", database_id = "unique_db_id_1", migrations_dir = "migrations", preview_database_id = "DB_STAGING" },
]
workers_dev = true

[env.production]
d1_databases = [
{ binding = "DB", database_name = "production", database_id = "unique_db_id_2", migrations_dir = "migrations"},
]
3 Replies
kgni
kgniOP10mo ago
From the documentation it seems like the binding names also have to be unique. Here is the example:
# This is a staging environment
[env.staging]
d1_databases = [
{ binding = "<BINDING_NAME_1>", database_name = "<DATABASE_NAME_1>", database_id = "<UUID1>" },
]

# This is a production environment
[env.production]
d1_databases = [
{ binding = "<BINDING_NAME_2>", database_name = "<DATABASE_NAME_2>", database_id = "<UUID2>" },
]
# This is a staging environment
[env.staging]
d1_databases = [
{ binding = "<BINDING_NAME_1>", database_name = "<DATABASE_NAME_1>", database_id = "<UUID1>" },
]

# This is a production environment
[env.production]
d1_databases = [
{ binding = "<BINDING_NAME_2>", database_name = "<DATABASE_NAME_2>", database_id = "<UUID2>" },
]
But then how would I access the different binding variables in my worker, depending on the environment, since they are now different Here is my worker.ts file (using hono with trpc)
type Bindings = {
DB: D1Database;
JWT_VERIFICATION_KEY: string;
};

const app = new Hono<{ Bindings: Bindings }>();

app.get('/', async (c) => {
return c.text('Hello, world!');
});

// Setup TRPC server with context
app.use('/trpc/*', async (c, next) => {
return await trpcServer({
router: appRouter,
createContext: async (opts) => {
return await createContext(c.env.DB);
},
})(c, next);
});

export default app;
type Bindings = {
DB: D1Database;
JWT_VERIFICATION_KEY: string;
};

const app = new Hono<{ Bindings: Bindings }>();

app.get('/', async (c) => {
return c.text('Hello, world!');
});

// Setup TRPC server with context
app.use('/trpc/*', async (c, next) => {
return await trpcServer({
router: appRouter,
createContext: async (opts) => {
return await createContext(c.env.DB);
},
})(c, next);
});

export default app;
So the bindings in my example above [env.staging] and [env.production] can just be "DB" but then if the database_name and database_id are different they will point to different databases? If so, then the binding name is solely used as an environment variable (and not actually used as a reference to point to specific databases)
Max (@rozenmd)
Max (@rozenmd)10mo ago
effectively used like an environment variable, yeah
kgni
kgniOP10mo ago
Cool, thank you for the quick response both of you! When deploying, would the bindings then show up in the dashboard as well? Thank you!
Want results from more Discord servers?
Add your server