© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•3y ago•
2 replies
xokaido

Cloudflare Worker with Hono + D1 + D1-ORM

Hello all, I am new to this thing and need some help. Currently, I've successfully setup worker with Hono + D1. Now I want to use D1-ORM (https://github.com/Interactions-as-a-Service/d1-orm) but don't know how to pass env.DB which is declared inside hono context like this:
app.get('/', async (c) => {
  const db = c.env.DB;
});
app.get('/', async (c) => {
  const db = c.env.DB;
});

I've got
./src/Models/Customer.js
./src/Models/Customer.js
file which contains this:
import { D1Orm, DataTypes, Model } from "d1-orm";

const orm = new D1Orm(env.DB);

const customer = new Model(
    {
        D1Orm: orm,
        tableName: "customers",
        primaryKeys: "id",
        autoIncrement: "id",
    },
    {
        CustomerId: {
            type: DataTypes.INTEGER,
            notNull: true,
        },
        CompanyName: {
            type: DataTypes.STRING,
            notNull: true,
        },
        ContactName: {
            type: DataTypes.STRING,
        },
    }
);

export default customer;
import { D1Orm, DataTypes, Model } from "d1-orm";

const orm = new D1Orm(env.DB);

const customer = new Model(
    {
        D1Orm: orm,
        tableName: "customers",
        primaryKeys: "id",
        autoIncrement: "id",
    },
    {
        CustomerId: {
            type: DataTypes.INTEGER,
            notNull: true,
        },
        CompanyName: {
            type: DataTypes.STRING,
            notNull: true,
        },
        ContactName: {
            type: DataTypes.STRING,
        },
    }
);

export default customer;

When I run
wrangler dev
wrangler dev
I'm getting error:
service core:user:cf-api: Uncaught ReferenceError: env is not defined
service core:user:cf-api: Uncaught ReferenceError: env is not defined

Can anyone help me figure out how to properly pass env.DB to the model?
Thanks in advance. ❤️
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Hono and Cloudflare worker ratelimiting
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
15mo ago
Testing hono with isolated d1
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2mo ago
How D1 work with Drizzle ORM?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
12mo ago
Svelte, Hono, and D1 database
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
12mo ago