Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions backend/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { relations } from "drizzle-orm";
import {
boolean,
doublePrecision,
jsonb,
pgTable,
text,
timestamp,
Expand Down Expand Up @@ -67,16 +69,65 @@ export const jwks = pgTable("jwks", {
});

// Application tables

export const maps = pgTable("maps", {
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
name: text("name").notNull(),
description: text("description"),
createdAt: timestamp("created_at").defaultNow().notNull(),
});

export type Map = typeof maps.$inferSelect;
export type NewMap = typeof maps.$inferInsert;

export const mapsRelations = relations(maps, ({ many }) => ({
pins: many(pins),
drawings: many(drawings),
}));

// userId is text to match Better Auth user.id
export const pins = pgTable("pins", {
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
mapId: uuid("map_id").references(() => maps.id, { onDelete: "cascade" }),
latitude: doublePrecision("latitude").notNull(),
longitude: doublePrecision("longitude").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const pinsRelations = relations(pins, ({ one }) => ({
map: one(maps, {
fields: [pins.mapId],
references: [maps.id],
}),
}));

export type Pin = typeof pins.$inferSelect;
export type NewPin = typeof pins.$inferInsert;

export const drawings = pgTable("drawings", {
id: uuid("id").primaryKey().defaultRandom(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
mapId: uuid("map_id").references(() => maps.id, { onDelete: "cascade" }),
points: jsonb("points").notNull(),
color: text("color").notNull(),
strokeWidth: doublePrecision("stroke_width").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});

export const drawingsRelations = relations(drawings, ({ one }) => ({
map: one(maps, {
fields: [drawings.mapId],
references: [maps.id],
}),
}));

export type Drawing = typeof drawings.$inferSelect;
export type NewDrawing = typeof drawings.$inferInsert;
Loading