Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip d1
Browse files Browse the repository at this point in the history
Lars-Erik Roald committed Nov 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7c5941d commit a44afee
Showing 38 changed files with 1,065 additions and 237 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -7,4 +7,5 @@ package-lock.json
.vscode
demo.db
demo*.db*
coverage
coverage
.env
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -66,7 +66,8 @@
"node-cls": "^1.0.5",
"promise": "^8.0.3",
"rfdc": "^1.2.0",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"wrangler": "^3.86.0"
},
"peerDependencies": {
"msnodesqlv8": "^4.1.0",
@@ -102,6 +103,8 @@
}
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241106.0",
"@miniflare/d1": "^2.14.4",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.0.0",
"@typescript-eslint/eslint-plugin": "^6.x",
2 changes: 2 additions & 0 deletions src/client/clientMap.js
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ function map(index, _fn) {
dbMap.sap = throwDb;
dbMap.oracle = throwDb;
dbMap.sqlite = throwDb;
dbMap.d1 = throwDb;

function throwDb() {
throw new Error('Cannot create pool for database outside node');
@@ -65,6 +66,7 @@ function map(index, _fn) {
onFinal.sap = () => index({ db: throwDb, providers: dbMap });
onFinal.oracle = () => index({ db: throwDb, providers: dbMap });
onFinal.sqlite = () => index({ db: throwDb, providers: dbMap });
onFinal.d1 = () => index({ db: throwDb, providers: dbMap });

return new Proxy(onFinal, handler);
}
12 changes: 12 additions & 0 deletions src/client/createProviders.js
Original file line number Diff line number Diff line change
@@ -48,6 +48,11 @@ function createProviders(index) {
return createPool.bind(null, 'sqlite');
}
});
Object.defineProperty(dbMap, 'd1', {
get: function() {
return createPool.bind(null, 'd1');
}
});
Object.defineProperty(dbMap, 'http', {
get: function() {
return createPool.bind(null, 'http');
@@ -97,12 +102,19 @@ function negotiateCachedPool(fn, providers) {
get sqlite() {
return createPool.bind(null, 'sqlite');
},
get d1() {
return createPool.bind(null, 'd1');
},
get http() {
return createPool.bind(null, 'http');
}
};

function createPool(providerName, ...args) {
//todo
if (providerName === 'd1') {
return providers[providerName].apply(null, args);
}
const key = JSON.stringify(args);
if (!cache[providerName])
cache[providerName] = {};
4 changes: 3 additions & 1 deletion src/client/index.js
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@ function rdbClient(options = {}) {
client.mssqlNative = onProvider.bind(null, 'mssqlNative');
client.pg = onProvider.bind(null, 'pg');
client.postgres = onProvider.bind(null, 'postgres');
client.d1 = onProvider.bind(null, 'd1');
client.sqlite = onProvider.bind(null, 'sqlite');
client.sap = onProvider.bind(null, 'sap');
client.oracle = onProvider.bind(null, 'oracle');
@@ -128,7 +129,8 @@ function rdbClient(options = {}) {
}

async function query() {
return netAdapter(baseUrl, undefined, { tableOptions: { db: baseUrl, transaction } }).query.apply(null, arguments);
const adapter = netAdapter(baseUrl, undefined, { tableOptions: { db: baseUrl, transaction } });
return adapter.query.apply(null, arguments);
}

function express(arg) {
Loading

0 comments on commit a44afee

Please sign in to comment.