Just another custom URL store made with CloudFlare Workers and KV.
- KV for Slug and Link storage
- D1 for View count tracking
- Workers for Deployment
- Clone the repo and head inside it
git clone https://github.com/BRAVO68WEB/url-store-kv-worker
cd url-store-kv-worker
- Install all dependencies
pnpm i
- Login to wrangler cli, if not done
pnpm wrangler login
- Handle KV Namespace
pnpm wrangler kv:namespace create you_pref_kv_name
{ binding = "you_pref_kv_name", id = "xxxxxxxxxxxxxxxxxxxxxxxxxx" }
Replace existing id with this id xxxxxxxxxxxxxxxxxxxxxxxxxx
in wrangler.toml file
pnpm wrangler kv:namespace create you_pref_kv_name_preview
{ binding = "you_pref_kv_name_preview", id = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" }
Replace existing preview_id with this id yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
in wrangler.toml file
Finally, it should look like
kv_namespaces = [
{ binding = "URLSTORE", preview_id = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", id = "xxxxxxxxxxxxxxxxxxxxxxxxxx" }
]
- Handle D1 Database
pnpm wrangler d1 create you_pref_db_name
Output :-
[[ d1_databases ]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "you_pref_db_name"
database_id = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
Replace existing database_id with this id wwwwwwwwwwwwwwwwwwwwwwwwwwwwww
in wrangler.toml file
Replace existing database_name with this id you_pref_db_name
in wrangler.toml file
pnpm wrangler d1 create you_pref_db_name_preview
Output :-
[[ d1_databases ]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "you_pref_db_name_preview"
database_id = "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww"
Replace existing preview_database_id with this id wwwwwwwwwwwwwwwwwwwwwwwwwwwwww
in wrangler.toml file
- Perform SQL table migrations
pnpm wrangler d1 execute URLSTORE --file=./schema.sql
- Test locally, to see if the config works
pnpm start
- Deploy to Cloudflare Workers
pnpm run deploy
Happy Hacking 🎉 !!