d1-orm
Version:
A simple strictly typed ORM for Cloudflare's D1 product
36 lines • 1.05 kB
JavaScript
/**
* The D1Orm class is the main class for the ORM. It is used to create models, and to run queries.
*
* It's methods generally don't need to be called directly, but are instead to be used by the models.
*/
export class D1Orm {
constructor(database) {
if (!isDatabase(database)) {
throw new Error("Invalid database, should contain prepare, dump, batch, and exec methods");
}
this.database = database;
}
prepare(query) {
return this.database.prepare(query);
}
async dump() {
return this.database.dump();
}
async batch(statements) {
return this.database.batch(statements);
}
async exec(query) {
return this.database.exec(query);
}
}
/**
* @private
* @hidden
*/
export function isDatabase(database) {
return (!!database &&
["prepare", "dump", "batch", "exec"].every(
// @ts-expect-error - We're checking if the database is valid
(x) => typeof database[x] === "function"));
}
//# sourceMappingURL=database.js.map