cloudflare-d1-http-knex
Version:
An npm package that query [Cloudflare's D1](https://developers.cloudflare.com/d1/) through [Query D1 Database API](https://developers.cloudflare.com/api/operations/cloudflare-d1-query-database-query) and [Knex](https://knexjs.org/).
121 lines (113 loc) • 3.72 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var K = require('knex');
var Client = require('knex/lib/dialects/sqlite3/index.js');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var K__default = /*#__PURE__*/_interopDefault(K);
var Client__default = /*#__PURE__*/_interopDefault(Client);
// src/client.ts
var CloudflareD1HttpClient = class extends Client__default.default {
constructor(config) {
config.connection.filename = ":memory:";
config.useNullAsDefault = false;
super(config);
if (!config.connection.mockedFetch) {
if (!config.connection?.account_id) {
throw Error("Missing required account_id");
}
if (!config.connection?.database_id) {
throw Error("Missing required database_id");
}
if (!config.connection?.key) {
throw Error("Missing required key");
}
}
this.config = config;
}
_driver() {
return this;
}
acquireRawConnection() {
return Promise.resolve(this);
}
async _query(_, obj) {
if (!obj.sql) return Promise.reject(Error("The query is empty"));
if (["BEGIN;", "COMMIT;", "ROLLBACK;"].includes(obj.sql)) {
console.warn(
"[WARN] D1 doesn't support transactions, see https://blog.cloudflare.com/whats-new-with-d1/"
);
return Promise.resolve();
}
if (this.config.connection.mockedFetch)
return this.config.connection.mockedFetch(
`https://api.cloudflare.com/client/v4/accounts/${this.config.connection.account_id}/d1/database/${this.config.connection.database_id}/query`,
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
sql: obj.sql,
params: obj.bindings
})
}
).then((res) => this._processResponse(res, obj));
return fetch(
`https://api.cloudflare.com/client/v4/accounts/${this.config.connection.account_id}/d1/database/${this.config.connection.database_id}/query`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.config.connection.key}`
},
body: JSON.stringify({
sql: obj.sql,
params: obj.bindings
})
}
).then((res) => this._processResponse(res, obj));
}
async _processResponse(res, obj) {
return res.json().then((body) => {
if (body.success) {
if (obj.output) return obj.output.call(null, body.result[0].results);
switch (obj.method) {
case "first":
return body.result[0].results[0];
case "insert":
if (obj.returning) {
return body.result[0].results;
}
return [body.result[0].meta.changes];
case "update":
if (obj.returning) {
return body.result[0].results;
}
return body.result[0].meta.changes;
case "del":
case "counter":
return body.result[0].meta.changes;
case "pluck":
return body.result[0].results.map((row) => row[obj.pluck]);
default:
return body.result[0].results;
}
}
throw Error(body.errors[0].message);
});
}
async processResponse(res) {
return res;
}
};
function createConnection(connection) {
return K__default.default({
client: CloudflareD1HttpClient,
connection
});
}
// src/index.ts
var index_default = CloudflareD1HttpClient;
exports.CloudflareD1HttpClient = CloudflareD1HttpClient;
exports.createConnection = createConnection;
exports.default = index_default;