Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
olduh29 committed Dec 8, 2023
1 parent 1f9fc70 commit 1e84569
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 3 additions & 4 deletions public/_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 13 additions & 7 deletions public/d1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
)
Expand All @@ -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
}
Expand Down

0 comments on commit 1e84569

Please sign in to comment.