Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Lowdb Example

This val demonstrates the integration between valtown and lowdb.

Read the Lodash section if you want to give superpowers to your DB.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
type Post = {
id: number;
title: string;
created: string;
};
type Data = {
posts: Post[];
};
const { key } = extractValInfo(import.meta.url);
export const db = await BlobPreset<Data>(key, { posts: [] });
export default async (req: Request): Promise<Response> => {
if (req.method === "GET") {
return new Response(JSON.stringify({ ok: "false, use Post" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
const { title } = await req.json();
const date = new Date();
const created = date.toLocaleString("en-US", { timeZone: "America/Chicago" });
db.data
.posts
.push({ id: db.data.posts.length + 1, title, created });
await db.write();
return new Response(JSON.stringify(db.data), { headers: { "Content-Type": "application/json" } });
};
nbbaier-lowdbtest.web.val.run
July 4, 2024