From 1e8456910d6fb3c8ae35ae195a0cd0ad9a37513a Mon Sep 17 00:00:00 2001 From: Olivier Duhart Date: Fri, 8 Dec 2023 15:59:24 +0100 Subject: [PATCH] fixes --- public/_worker.js | 7 +++---- public/d1.js | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/public/_worker.js b/public/_worker.js index 62b0d30..d628e8d 100644 --- a/public/_worker.js +++ b/public/_worker.js @@ -73,10 +73,10 @@ router.get( console.log(`d1 created :>${d1.result.uuid}<`) var creation = `drop table if exists data; create table data (id INT PRIMARY KEY, value TEXT);`; - sql = await ExecuteSQL(context, creation, d1.result.uuid); + sql = await executeSQL(context, creation, d1.result.uuid); logs.push(`sql create executed`) console.log(`sql create executed`) - bind = await BindD1(context, d1.result.uuid, appName); + bind = await bindD1(context, d1.result.uuid, appName); logs.push(`d1 :>${d1.result.uuid}< bound to :>${appName}<`) console.log(`d1 :>${d1.result.uuid}< bound to :>${appName}<`) RenderJSON(env,request,{"d1":d1,"bind":bind,"sql":sql,"logs":logs}); @@ -124,8 +124,7 @@ router.all('*', (request, env) => { }) export default { - async fetch(request, environment, context) { - console.log('routing ',request); + async fetch(request, environment, context) { return router.handle(request, environment, context) }, async scheduled(controller, environment, context) { diff --git a/public/d1.js b/public/d1.js index 41e5ca5..e9dbde7 100644 --- a/public/d1.js +++ b/public/d1.js @@ -26,12 +26,14 @@ async function request(uri, key, method, body) { console.log('d1 fetched '+r.status); if (r.status == 200) { console.log('OK') - var json = await r.json() - console.log(json) - return json + var content = await r.text(); + var d1 = JSON.parse(content); + //var d1 = await r.json() + console.log(d1) + return d1 } else { - var content = await r.json() - console.log('error ' + r.status + ' ' + r.statusText, content) + const content = await r.text(); + console.log(`error >${r.status}< :>${r.statusText}< ::>${content}<`); return null } } @@ -43,6 +45,7 @@ async function request(uri, key, method, body) { } async function post(uri, key, body) { + console.log(`POST >${uri}< >${key}<`,body) return await request(uri, key, 'POST', body) } @@ -71,10 +74,11 @@ export async function createD1(env, dbName) { var payLoad = { name: dbName, } - console.log(`CreateD1(${dbName})`) + const uri = `/accounts/${env.ACCOUNT_ID}/d1/database`; + console.log(`CreateD1(${dbName}) : ${uri}`); console.log(payLoad) var d1 = await post( - `/accounts/${env.ACCOUNT_ID}/d1/database`, + uri, env.API_KEY, payLoad ) @@ -85,7 +89,9 @@ export async function createD1(env, dbName) { } export async function executeSQL(env, sql, d1Id) { + console.log(`d1.executeSQL(${sql},${d1Id})`); var uri = `/accounts/${env.ACCOUNT_ID}/d1/database/${d1Id}/query` + console.log(` => ${uri}`); var result = await post(uri, env.API_KEY, { sql }) return result }