A lightweight ORM for Cloudflare Workers (D1 and Durable Objects), designed to be intuitive, productive and focused on essential functionality
npm install cl-orm[npm-image]: https://img.shields.io/npm/v/cl-orm.svg
[npm-url]: https://npmjs.org/package/cl-orm
[downloads-image]: https://img.shields.io/npm/dt/cl-orm.svg
[downloads-url]: https://npmjs.org/package/cl-orm
ā ļø A lightweight ORM for Cloudflare Workers (D1 and Durable Objects), designed to be intuitive and productive, focused on essential functionality.
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
---
š Documentation
---
- Supports both Cloudflare D1 and Durable Objects SQL Storage.
- Unified Connection interface across different database drivers.
- User-friendly ORM for INSERT, SELECT, UPDATE, DELETE and WHERE clauses.
- Automatic Prepared Statements.
---
See detailed specifications and usage in Documentation section for queries, advanced concepts and much more.
---
``shell`
npm i cl-orm
---
#### D1
`ts
import { useD1 } from 'cl-orm';
export default {
async fetch(request: Request, env: Env): Promise
const db = useD1(env.DB);
await db.query('SELECT 1');
// ...
},
};
`
#### Durable Objects
`ts
import { useDO } from 'cl-orm';
import { DurableObject } from 'cloudflare:workers';
export class MyDurableObject extends DurableObject {
async fetch(request: Request): Promise
const db = useDO(this.ctx.storage.sql);
await db.query('SELECT 1');
// ...
}
}
`
---
The following example is based on TypeScript and ES Modules.
`ts
import type { Connection } from 'cl-orm';
import { OP } from 'cl-orm';
export const getUser = async (db: Connection, id: number) => {
const user = await db.select({
from: 'users',
where: OP.eq('id', id),
limit: 1,
});
return user;
};
// Usage: await getUser(db, 15);
`
- See all available operators (OP) here.
- Due to limit: 1, it returns a direct object with the row result or null`.
---
- The operator names eq, ne, gt, lt, gte and lte are inspired by Sequelize.
- Contributors.
- This project is adapted from MySQL2 ORM.