Add all database tables to setup route
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> EOF )
This commit is contained in:
parent
4c7535e716
commit
804f12ac29
1 changed files with 110 additions and 0 deletions
|
|
@ -8,6 +8,116 @@ export async function GET() {
|
||||||
await sql.unsafe("CREATE TYPE child_stage AS ENUM('newborn', 'infant', 'solids_start', 'toddler_early', 'toddler_late', 'preschool')").catch(() => {});
|
await sql.unsafe("CREATE TYPE child_stage AS ENUM('newborn', 'infant', 'solids_start', 'toddler_early', 'toddler_late', 'preschool')").catch(() => {});
|
||||||
await sql.unsafe("CREATE TYPE member_role AS ENUM('admin', 'caregiver', 'viewer')").catch(() => {});
|
await sql.unsafe("CREATE TYPE member_role AS ENUM('admin', 'caregiver', 'viewer')").catch(() => {});
|
||||||
|
|
||||||
|
// Create users table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
name TEXT,
|
||||||
|
email TEXT NOT NULL UNIQUE,
|
||||||
|
email_verified TIMESTAMP,
|
||||||
|
image TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW() NOT NULL,
|
||||||
|
updated_at TIMESTAMP DEFAULT NOW() NOT NULL
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create accounts table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS accounts (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id),
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
provider TEXT NOT NULL,
|
||||||
|
provider_account_id TEXT NOT NULL,
|
||||||
|
refresh_token TEXT,
|
||||||
|
access_token TEXT,
|
||||||
|
expires_at TIMESTAMP,
|
||||||
|
token_type TEXT,
|
||||||
|
scope TEXT,
|
||||||
|
id_token TEXT,
|
||||||
|
session_state TEXT,
|
||||||
|
UNIQUE(provider, provider_account_id)
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create sessions table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
session_token TEXT NOT NULL UNIQUE,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id),
|
||||||
|
expires TIMESTAMP NOT NULL
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create verification_tokens table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS verification_tokens (
|
||||||
|
identifier TEXT NOT NULL,
|
||||||
|
token TEXT NOT NULL,
|
||||||
|
expires TIMESTAMP NOT NULL,
|
||||||
|
UNIQUE(identifier, token)
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create families table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS families (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW() NOT NULL,
|
||||||
|
updated_at TIMESTAMP DEFAULT NOW() NOT NULL
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create family_members table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS family_members (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
family_id UUID NOT NULL REFERENCES families(id),
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id),
|
||||||
|
role member_role NOT NULL DEFAULT 'caregiver',
|
||||||
|
created_at TIMESTAMP DEFAULT NOW() NOT NULL,
|
||||||
|
UNIQUE(family_id, user_id)
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create children table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS children (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
family_id UUID NOT NULL REFERENCES families(id),
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
birth_date DATE NOT NULL,
|
||||||
|
sex child_sex,
|
||||||
|
stage child_stage NOT NULL DEFAULT 'newborn',
|
||||||
|
image_url TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW() NOT NULL,
|
||||||
|
updated_at TIMESTAMP DEFAULT NOW() NOT NULL
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
// Create family_invites table
|
||||||
|
await sql.unsafe(`
|
||||||
|
CREATE TABLE IF NOT EXISTS family_invites (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
family_id UUID NOT NULL REFERENCES families(id),
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
role member_role NOT NULL DEFAULT 'viewer',
|
||||||
|
token TEXT NOT NULL UNIQUE,
|
||||||
|
expires_at TIMESTAMP NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW() NOT NULL,
|
||||||
|
UNIQUE(family_id, email)
|
||||||
|
)
|
||||||
|
`).catch(() => {});
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, message: "All tables created" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Types created" });
|
return NextResponse.json({ success: true, message: "Types created" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue