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 }), {
status: 405,
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" } });
};

Sqlite Table

Usage:

  1. Fork this Val
  2. Replace the existing migrations by your own table

The table name will match the val name.

To update the table, just add new items to the migrations array, and re-run the val

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
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
const { slug, name: tableName } = extractValInfo(import.meta.url);
const migrations = [
`CREATE TABLE IF NOT EXISTS ${tableName} (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
email TEXT UNIQUE
);`,
`ALTER TABLE ${tableName} ADD COLUMN address TEXT;`,
`ALTER TABLE ${tableName} ADD COLUMN city TEXT;`,
];
const storage = await BlobPreset(slug, { version: 0 });
console.log(`Schema Version: ${storage.data.version}`);
if (storage.data.version == migrations.length) {
console.log("Up To Date!");
Deno.exit(0);
}
const statements = migrations.slice(storage.data.version);
console.log(`Running ${statements.length} migrations`);
await sqlite.batch(statements, "write");
storage.data.version = migrations.length;
await storage.write();
console.log("Success!");

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
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
type Data = {
posts: {
id: number;
title: string;
}[];
};
// use the val name as a key ("@pomdtr/testLowDB")
const { slug } = extractValInfo(import.meta.url);
// Read or initialize DB from blob
const db = await BlobPreset<Data>(slug, { posts: [] });
// Edit db content using plain JavaScript (updates are automatically persisted)
await db.update(({ posts }) => posts.push({ id: posts.length + 1, title: "lowdb is awesome" }));
console.log(db.data);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { BlobPreset } from "https://esm.town/v/pomdtr/lowdb";
interface Day {
id: number;
input: string;
riddleUrl: string;
}
type Data = { days: Day[] };
const { key } = extractValInfo(import.meta.url);
const aocDb = await BlobPreset<Data>(key, { days: [] });
type AocDB = typeof aocDb;
async function updateAocDB(db: AocDB, day: number, input: string, riddleUrl: string) {
db.data.days.push({ id: day, input, riddleUrl });
return await db.write();
}
export { aocDb, updateAocDB };

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
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { LocalStoragePreset } from "npm:lowdb/browser";
type Data = {
posts: {
id: number;
title: string;
}[];
};
// use the val name as a key ("@pomdtr/testLowDB")
const { slug } = extractValInfo(import.meta.url);
// Read or initialize DB from blob
const db = await LocalStoragePreset<Data>(slug, { posts: [] });
// Edit db content using plain JavaScript (updates are automatically persisted)
await db.update(({ posts }) => posts.push({ id: posts.length + 1, title: "lowdb is awesome" }));
console.log(db.data);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// SPDX-License-Identifier: 0BSD
import { Blob } from "https://esm.town/v/pomdtr/lowdb?v=17";
import { KeyvLow } from "https://esm.town/v/vladimyr/keyvhqLow";
import { type Store } from "npm:@keyvhq/core";
export type KeyvBlobOptions = {
key?: string;
};
export const blobStore = createStore();
export function createStore<T>(options?: KeyvBlobOptions): Store<T> {
const key = options?.key ?? "keyv";
return new KeyvLow<T>({
adapter: new Blob(key),
});
}
1
Next