Last active
August 10, 2023 17:01
-
-
Save juriadams/c945aef1878cdbc347d6c46ce37f386e to your computer and use it in GitHub Desktop.
Cloudflare Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Env { | |
DATABASE: D1Database; | |
} | |
export default { | |
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { | |
const tables = await env.DATABASE.prepare( | |
"SELECT tbl_name, sql FROM sqlite_master WHERE type='table' AND tbl_name NOT LIKE '_cf_KV' AND tbl_name NOT LIKE 'sqlite_stat1';" | |
) | |
.all() | |
.then(({ results }) => results); | |
const tableNames = tables.map((res) => res.tbl_name); | |
const tableDefinitions = tables.map((res) => res.sql).map((sql) => (sql as string).replaceAll('\n', '')); | |
console.log(tableDefinitions); | |
const statements = tableNames.map((table) => env.DATABASE.prepare(`SELECT * FROM ${table} LIMIT 10;`).bind()); | |
const records = await env.DATABASE.batch(statements).then((res) => res.map(({ results }) => results)); | |
const inserts: string[] = []; | |
records.map((batch, index) => { | |
const columnNames = Object.keys(batch[0] || {}); | |
const tableName = tableNames[index]; | |
batch.map((item) => { | |
const values = Object.values(item).map((value) => `'${new String(value).split("'").join("''")}'`); | |
inserts.push(`INSERT INTO ${tableName} (${columnNames.join(', ')}) VALUES (${values.join(', ')});`); | |
}); | |
}); | |
const lines = [...tableDefinitions, ...inserts]; | |
return new Response(lines.join('\n'), { | |
headers: { | |
'content-type': 'text/plain', | |
}, | |
}); | |
}, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script clears all keys from the specified environment (preview or prod) of a Cloudflare Workers KV store. | |
# Replace the values of PREVIEW_NAMESPACE_ID and PROD_NAMESPACE_ID with your own namespace IDs. | |
PREVIEW_NAMESPACE_ID="" | |
PROD_NAMESPACE_ID="" | |
if [ "$#" -ne 2 ] || { [ "$1" != "clear" ] && [ "$2" != "preview" ] && [ "$2" != "prod" ]; }; then | |
echo "Usage: kv clear <preview|prod>" | |
exit 1 | |
fi | |
if [ "$2" == "preview" ]; then | |
namespace_id="$PREVIEW_NAMESPACE_ID" | |
else | |
namespace_id="$PROD_NAMESPACE_ID" | |
fi | |
wrangler kv:key list --namespace-id "$namespace_id" | jq "map(.[])" > keys.json \ | |
&& wrangler kv:bulk delete keys.json --namespace-id "$namespace_id" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Log headers inside Cloudflare Worker. | |
console.log(JSON.stringify(Object.fromEntries(object.headers), null, 4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment