File size: 7,522 Bytes
489b6e2 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import { index, integer, real, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
passwordHash: text("password_hash").notNull(),
createdAt: text("created_at").notNull(),
}, (table) => [uniqueIndex("users_email_unique").on(table.email)]);
export const campsites = sqliteTable("campsites", {
id: text("id").primaryKey(),
ownerId: text("owner_id").references(() => users.id, { onDelete: "set null" }),
slug: text("slug").notNull(),
title: text("title").notNull(),
location: text("location").notNull(),
type: text("type", { enum: ["Tent", "RV", "Cabin", "Glamping"] }).notNull(),
description: text("description").notNull(),
price: real("price").notNull(),
latitude: real("latitude").notNull(),
longitude: real("longitude").notNull(),
amenities: text("amenities", { mode: "json" }).$type<string[]>().notNull(),
status: text("status", { enum: ["draft", "published", "paused"] }).notNull().default("published"),
createdAt: text("created_at").notNull(),
}, (table) => [
uniqueIndex("campsites_slug_unique").on(table.slug),
index("campsites_location_idx").on(table.location),
]);
export const favorites = sqliteTable("favorites", {
id: text("id").primaryKey(),
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
campsiteId: text("campsite_id").notNull(),
createdAt: text("created_at").notNull(),
}, (table) => [
uniqueIndex("favorites_user_camp_unique").on(table.userId, table.campsiteId),
index("favorites_user_idx").on(table.userId),
]);
export const bookings = sqliteTable("bookings", {
id: text("id").primaryKey(),
userId: text("user_id").notNull().references(() => users.id),
campsiteId: text("campsite_id").notNull(),
siteId: text("site_id"),
checkIn: text("check_in").notNull(),
checkOut: text("check_out").notNull(),
guests: integer("guests").notNull(),
adults: integer("adults").notNull().default(1),
children: integer("children").notNull().default(0),
pets: integer("pets").notNull().default(0),
subtotal: real("subtotal").notNull(),
serviceFee: real("service_fee").notNull(),
total: real("total").notNull(),
status: text("status", { enum: ["confirmed", "requested", "cancelled"] }).notNull(),
paymentMode: text("payment_mode").notNull().default("simulation"),
paymentProvider: text("payment_provider", { enum: ["local_test", "stripe"] }).notNull().default("local_test"),
paymentIntentId: text("payment_intent_id").notNull(),
paymentStatus: text("payment_status", { enum: ["succeeded", "requires_capture"] }).notNull().default("succeeded"),
currency: text("currency").notNull().default("usd"),
idempotencyKey: text("idempotency_key"),
createdAt: text("created_at").notNull(),
}, (table) => [
index("bookings_user_created_idx").on(table.userId, table.createdAt),
index("bookings_campsite_dates_idx").on(table.campsiteId, table.siteId, table.checkIn, table.checkOut),
uniqueIndex("bookings_user_idempotency_unique").on(table.userId, table.idempotencyKey),
]);
export const bookingEvents = sqliteTable("booking_events", {
id: text("id").primaryKey(),
bookingId: text("booking_id").notNull().references(() => bookings.id, { onDelete: "cascade" }),
userId: text("user_id").notNull().references(() => users.id),
type: text("type", { enum: ["payment_confirmed", "booking_created", "booking_idempotent_replayed", "booking_cancelled", "payment_refund_simulated"] }).notNull(),
message: text("message").notNull(),
metadata: text("metadata", { mode: "json" }).$type<Record<string, string | number | boolean>>().notNull(),
createdAt: text("created_at").notNull(),
}, (table) => [
index("booking_events_booking_idx").on(table.bookingId),
index("booking_events_user_created_idx").on(table.userId, table.createdAt),
]);
export const hostListings = sqliteTable("host_listings", {
id: text("id").primaryKey(),
ownerId: text("owner_id").notNull().references(() => users.id),
title: text("title").notNull(),
location: text("location").notNull(),
type: text("type", { enum: ["Tent", "RV", "Cabin", "Glamping"] }).notNull(),
price: real("price").notNull(),
description: text("description").notNull(),
accessInstructions: text("access_instructions").notNull(),
rules: text("rules").notNull(),
minimumNights: integer("minimum_nights").notNull().default(1),
checkInWindow: text("check_in_window").notNull(),
availabilityWindow: text("availability_window").notNull(),
cancellationPolicy: text("cancellation_policy", { enum: ["Flexible", "Moderate", "Strict"] }).notNull().default("Flexible"),
status: text("status", { enum: ["draft", "submitted", "published"] }).notNull().default("submitted"),
createdAt: text("created_at").notNull(),
}, (table) => [index("host_listings_owner_idx").on(table.ownerId)]);
export const hostListingEvents = sqliteTable("host_listing_events", {
id: text("id").primaryKey(),
listingId: text("listing_id").notNull().references(() => hostListings.id, { onDelete: "cascade" }),
ownerId: text("owner_id").notNull().references(() => users.id),
type: text("type", { enum: ["host_listing_submitted", "host_listing_status_changed", "host_listing_published", "host_listing_returned_to_draft"] }).notNull(),
message: text("message").notNull(),
metadata: text("metadata", { mode: "json" }).$type<Record<string, string | number | boolean>>().notNull(),
createdAt: text("created_at").notNull(),
}, (table) => [
index("host_listing_events_listing_idx").on(table.listingId),
index("host_listing_events_owner_created_idx").on(table.ownerId, table.createdAt),
]);
export const availabilityAlerts = sqliteTable("availability_alerts", {
id: text("id").primaryKey(),
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
park: text("park").notNull(),
arrival: text("arrival").notNull(),
departure: text("departure").notNull(),
status: text("status", { enum: ["active", "paused", "cancelled"] }).notNull().default("active"),
createdAt: text("created_at").notNull(),
}, (table) => [
index("availability_alerts_user_idx").on(table.userId),
index("availability_alerts_park_dates_idx").on(table.park, table.arrival, table.departure),
]);
export const messages = sqliteTable("messages", {
id: text("id").primaryKey(),
bookingId: text("booking_id").notNull().references(() => bookings.id, { onDelete: "cascade" }),
userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
campsiteId: text("campsite_id").notNull(),
sender: text("sender", { enum: ["guest", "host"] }).notNull(),
body: text("body").notNull(),
createdAt: text("created_at").notNull(),
}, (table) => [
index("messages_booking_created_idx").on(table.bookingId, table.createdAt),
index("messages_user_created_idx").on(table.userId, table.createdAt),
]);
export const accountSettings = sqliteTable("account_settings", {
userId: text("user_id").primaryKey().references(() => users.id, { onDelete: "cascade" }),
tripUpdates: integer("trip_updates", { mode: "boolean" }).notNull().default(true),
hostMessages: integer("host_messages", { mode: "boolean" }).notNull().default(true),
availabilityAlerts: integer("availability_alerts", { mode: "boolean" }).notNull().default(true),
productResearch: integer("product_research", { mode: "boolean" }).notNull().default(false),
updatedAt: text("updated_at").notNull(),
});
|