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

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!");
December 19, 2023