Back

Version 6

10/3/2024
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";

export default async function server(request: Request): Promise<Response> {
const SCHEMA_VERSION = 3; // Increment this to create new tables
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);

await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_calendars_${SCHEMA_VERSION} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
creator TEXT,
start_time INTEGER,
end_time INTEGER
)
`);

await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_availability_${SCHEMA_VERSION} (
calendar_id INTEGER,
user_id TEXT,
day INTEGER,
hour INTEGER,
PRIMARY KEY (calendar_id, user_id, day, hour)
)
`);

const url = new URL(request.url);
const path = url.pathname;

if (path === '/calendars') {
if (request.method === 'POST') {
const { name, creator, startTime, endTime } = await request.json();
const result = await sqlite.execute(
`INSERT INTO ${KEY}_calendars_${SCHEMA_VERSION} (name, creator, start_time, end_time) VALUES (?, ?, ?, ?)`,
[name, creator, startTime, endTime]
);
elliotbraem-scheduler.web.val.run
Updated: October 3, 2024