Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

schema.ts

242 lines6.6 KB

1import {
2 pgTable,
3 uuid,
4 text,
5 integer,
6 timestamp,
7 jsonb,
8 primaryKey,
9 index,
10 uniqueIndex,
11} from "drizzle-orm/pg-core";
12import { sql } from "drizzle-orm";
13export const worlds = pgTable("worlds", {
14 id: uuid().primaryKey(),
15 slug: text().notNull().unique(),
16 title: text().notNull(),
17 owner: uuid(),
18 generation: integer().notNull().default(1),
19 catalogue: text().notNull(),
20 status: text().notNull().default("open"),
21 expiresAt: timestamp("expires_at", { withTimezone: true }).default(
22 sql`now()+interval '24 hours'`,
23 ),
24 createdAt: timestamp("created_at", { withTimezone: true })
25 .notNull()
26 .defaultNow(),
27});
28export const aiGenerations = pgTable(
29 "ai_generations",
30 {
31 id: uuid().primaryKey(),
32 guest: uuid().notNull(),
33 session: uuid(),
34 prompt: text().notNull(),
35 model: text().notNull(),
36 status: text().notNull().default("queued"),
37 attempt: integer().notNull().default(0),
38 blueprint: jsonb(),
39 design: jsonb(),
40 source: uuid(),
41 reference: jsonb(),
42 target: uuid().references(() => worlds.id),
43 snapshot: jsonb(),
44 preview: jsonb(),
45 activity: text(),
46 previewVersion: integer("preview_version").notNull().default(0),
47 attempts: jsonb().notNull().default({}),
48 issues: jsonb(),
49 error: text(),
50 inputTokens: integer("input_tokens").notNull().default(0),
51 outputTokens: integer("output_tokens").notNull().default(0),
52 world: uuid().references(() => worlds.id),
53 leaseUntil: timestamp("lease_until", { withTimezone: true }),
54 createdAt: timestamp("created_at", { withTimezone: true })
55 .notNull()
56 .defaultNow(),
57 updatedAt: timestamp("updated_at", { withTimezone: true })
58 .notNull()
59 .defaultNow(),
60 },
61 (t) => [
62 index("ai_generations_guest_created").on(t.guest, t.createdAt),
63 index("ai_generations_status_lease").on(t.status, t.leaseUntil),
64 index("ai_generations_target_created").on(t.target, t.createdAt, t.id),
65 index("ai_generations_legacy_created")
66 .on(t.world, t.guest, t.createdAt, t.id)
67 .where(sql`${t.target} IS NULL`),
68 ],
69);
70export const guests = pgTable("guests", {
71 id: uuid().primaryKey(),
72 name: text().notNull(),
73 color: text().notNull(),
74});
75export const sessions = pgTable("guest_sessions", {
76 id: uuid().primaryKey(),
77 guest: uuid()
78 .notNull()
79 .references(() => guests.id),
80 hash: text().notNull().unique(),
81 expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
82 absoluteExpiry: timestamp("absolute_expiry", {
83 withTimezone: true,
84 }).notNull(),
85 revoked: integer().notNull().default(0),
86});
87export const memberships = pgTable(
88 "world_memberships",
89 {
90 world: uuid()
91 .notNull()
92 .references(() => worlds.id),
93 guest: uuid()
94 .notNull()
95 .references(() => guests.id),
96 role: text().notNull().default("editor"),
97 active: integer().notNull().default(1),
98 },
99 (t) => [primaryKey({ columns: [t.world, t.guest] })],
100);
101export const parts = pgTable(
102 "part_instances",
103 {
104 id: uuid().primaryKey(),
105 world: uuid()
106 .notNull()
107 .references(() => worlds.id),
108 key: text().notNull(),
109 color: text().notNull(),
110 x: integer().notNull(),
111 y: integer().notNull(),
112 z: integer().notNull(),
113 rotation: integer().notNull(),
114 revision: integer().notNull(),
115 deleted: integer().notNull().default(0),
116 minx: integer().notNull(),
117 miny: integer().notNull(),
118 minz: integer().notNull(),
119 maxx: integer().notNull(),
120 maxy: integer().notNull(),
121 maxz: integer().notNull(),
122 },
123 (t) => [
124 index("parts_world_bounds")
125 .on(t.world, t.minx, t.maxx)
126 .where(sql`${t.deleted}=0`),
127 ],
128);
129export const connections = pgTable(
130 "part_connections",
131 {
132 id: uuid().primaryKey(),
133 world: uuid()
134 .notNull()
135 .references(() => worlds.id),
136 a: uuid()
137 .notNull()
138 .references(() => parts.id),
139 b: uuid()
140 .notNull()
141 .references(() => parts.id),
142 ca: text().notNull(),
143 cb: text().notNull(),
144 revision: integer().notNull(),
145 deleted: integer().notNull().default(0),
146 },
147 (t) => [
148 uniqueIndex("connection_pair").on(t.world, t.a, t.b, t.ca, t.cb),
149 index("connections_a").on(t.world, t.a),
150 index("connections_b").on(t.world, t.b),
151 ],
152);
153export const claims = pgTable(
154 "connector_slot_claims",
155 {
156 world: uuid()
157 .notNull()
158 .references(() => worlds.id),
159 part: uuid()
160 .notNull()
161 .references(() => parts.id),
162 slot: text().notNull(),
163 connection: uuid()
164 .notNull()
165 .references(() => connections.id),
166 },
167 (t) => [primaryKey({ columns: [t.world, t.part, t.slot] })],
168);
169export const buckets = pgTable(
170 "spatial_buckets",
171 {
172 world: uuid()
173 .notNull()
174 .references(() => worlds.id),
175 x: integer().notNull(),
176 y: integer().notNull(),
177 z: integer().notNull(),
178 },
179 (t) => [primaryKey({ columns: [t.world, t.x, t.y, t.z] })],
180);
181export const viewers = pgTable(
182 "viewer_sessions",
183 {
184 id: uuid().primaryKey(),
185 world: uuid()
186 .notNull()
187 .references(() => worlds.id),
188 guest: uuid()
189 .notNull()
190 .references(() => guests.id),
191 session: uuid()
192 .notNull()
193 .references(() => sessions.id),
194 generation: integer().notNull(),
195 selection: jsonb().notNull().default([]),
196 preview: jsonb(),
197 cursor: jsonb(),
198 sequence: integer().notNull().default(0),
199 previewAt: timestamp("preview_at", { withTimezone: true }),
200 cursorAt: timestamp("cursor_at", { withTimezone: true }),
201 expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(),
202 },
203 (t) => [index("viewers_expiry").on(t.world, t.expiresAt)],
204);
205export const operations = pgTable(
206 "build_operations",
207 {
208 world: uuid()
209 .notNull()
210 .references(() => worlds.id),
211 guest: uuid()
212 .notNull()
213 .references(() => guests.id),
214 id: uuid().notNull(),
215 generation: integer().notNull(),
216 digest: text().notNull(),
217 receipt: jsonb(),
218 before: jsonb(),
219 after: jsonb(),
220 beforeConnections: jsonb("before_connections"),
221 afterConnections: jsonb("after_connections"),
222 createdAt: timestamp("created_at", { withTimezone: true })
223 .notNull()
224 .defaultNow(),
225 },
226 (t) => [primaryKey({ columns: [t.world, t.guest, t.id] })],
227);
228export const histories = pgTable(
229 "guest_world_history",
230 {
231 world: uuid()
232 .notNull()
233 .references(() => worlds.id),
234 guest: uuid()
235 .notNull()
236 .references(() => guests.id),
237 generation: integer().notNull(),
238 undo: jsonb().notNull().default([]),
239 redo: jsonb().notNull().default([]),
240 },
241 (t) => [primaryKey({ columns: [t.world, t.guest] })],
242);