deno.land / x / simpledb@0.3.0 / example.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import { Duck } from "https://raw.githubusercontent.com/EntenKoeniq/Duck/master/mod.ts";import { SimpleDB } from "./mod.ts";
const PORT = 80, db = new SimpleDB({filePath: "./test.json"}), duck = new Duck();
duck.get("/insert/:url", async (req, res) => { let result = await db.insert({ url: req.params.url, short: [ ...Array(4) ].map(() => (~~(Math.random() * 36)).toString(36)).join(''), clicks: 0 });
if (result === undefined) return res.status(500).send("ERROR!"); await db.save();
res.send(result);})
duck.get("/delete/:short", async (req, res) => { if (await db.delete({ short: req.params.short })) { await db.save(); return res.send("Deleted!"); }
res.send("Not deleted!");})
duck.get("/:short", async (req, res) => { let result = await db.findOne({ short: req.params.short });
if (result === undefined) return res.status(404).send("not found!");
res.send(`URL: ${result.url}`);})
duck.get("/exists/:url", async (req, res) => { if (await db.exists({ url: req.params.url })) return res.send("found"); res.send("not found");})
duck.get("/replace", async (req, res) => { let result = await db.findOneAndUpdate({ _id: "innha4xa63p9fppyujci1x3c", clicks: 0 }, { url: "google.com" });
if (result === undefined) return res.status(500).send("Not found!");
db.save(); res.send(result);});
// Finally, listenduck.listen({ port: PORT }).then(() => { console.log(`Listening on ${PORT}`);});
Version Info
External Dependencies