trips) adding initial commit

This commit is contained in:
2026-02-18 20:23:34 -05:00
commit 1068145261
67 changed files with 3602 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import type { Database } from './types.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
export function runMigrations(db: Database): void {
db.run(`
CREATE TABLE IF NOT EXISTS trips (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
description TEXT,
start_date TEXT,
end_date TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS cities (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT NOT NULL,
country_code TEXT NOT NULL,
population INTEGER
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS plans (
id TEXT PRIMARY KEY,
trip_id TEXT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
user_id TEXT NOT NULL,
type TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'idea',
parent_id TEXT REFERENCES plans(id) ON DELETE SET NULL,
title TEXT NOT NULL,
notes TEXT,
city_id INTEGER REFERENCES cities(id),
city_name TEXT,
country TEXT,
country_code TEXT,
start_date TEXT,
end_date TEXT,
position INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS travellers (
id TEXT PRIMARY KEY,
trip_id TEXT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
user_id TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT,
position INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS people (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT,
is_self INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS trip_travellers (
trip_id TEXT NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
person_id TEXT NOT NULL REFERENCES people(id) ON DELETE CASCADE,
position INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (trip_id, person_id)
)
`);
// Seed cities table on first run
const row = db.get<{ count: number }>('SELECT COUNT(*) as count FROM cities');
if ((row?.count ?? 0) === 0) {
seedCities(db);
}
}
function seedCities(db: Database): void {
try {
const data = readFileSync(join(__dirname, '../data/cities.json'), 'utf-8');
const cities: { name: string; country: string; country_code: string; population: number }[] =
JSON.parse(data);
let id = 1;
for (const city of cities) {
db.run(
'INSERT OR IGNORE INTO cities (id, name, country, country_code, population) VALUES (?, ?, ?, ?, ?)',
[id++, city.name, city.country, city.country_code, city.population]
);
}
console.log(`[db] Seeded ${cities.length} cities`);
} catch (e) {
console.error('[db] Failed to seed cities:', e);
}
}