From 54eb4544f79baa85aee07772301492b39c693646 Mon Sep 17 00:00:00 2001 From: Kan-A-Pesh Date: Fri, 25 Oct 2024 10:08:18 +0200 Subject: [PATCH] chore: create db schema --- database/migrations/0000_light_tinkerer.sql | 4 - .../0000_organic_fantastic_four.sql | 48 +++ database/migrations/meta/0000_snapshot.json | 278 +++++++++++++++++- database/migrations/meta/_journal.json | 4 +- database/schema/acquired.ts | 34 +++ database/schema/challeges.ts | 29 ++ database/schema/clubs.ts | 20 ++ database/schema/granters.ts | 17 ++ database/schema/users.ts | 40 ++- 9 files changed, 458 insertions(+), 16 deletions(-) delete mode 100644 database/migrations/0000_light_tinkerer.sql create mode 100644 database/migrations/0000_organic_fantastic_four.sql create mode 100644 database/schema/acquired.ts create mode 100644 database/schema/challeges.ts create mode 100644 database/schema/clubs.ts create mode 100644 database/schema/granters.ts diff --git a/database/migrations/0000_light_tinkerer.sql b/database/migrations/0000_light_tinkerer.sql deleted file mode 100644 index e121419..0000000 --- a/database/migrations/0000_light_tinkerer.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS "users" ( - "id" serial PRIMARY KEY NOT NULL, - "first_name" text NOT NULL -); diff --git a/database/migrations/0000_organic_fantastic_four.sql b/database/migrations/0000_organic_fantastic_four.sql new file mode 100644 index 0000000..af31052 --- /dev/null +++ b/database/migrations/0000_organic_fantastic_four.sql @@ -0,0 +1,48 @@ +CREATE TABLE IF NOT EXISTS "acquired" ( + "user_uuid" uuid PRIMARY KEY NOT NULL, + "challenge_id" serial PRIMARY KEY NOT NULL, + "created_by" serial NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "challenges" ( + "id" serial PRIMARY KEY NOT NULL, + "club_id" serial NOT NULL, + "score" integer NOT NULL, + "name" text NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "clubs" ( + "id" serial PRIMARY KEY NOT NULL, + "avatar_url" text NOT NULL, + "name" text NOT NULL, + "description" text NOT NULL, + "daily_date" date NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "granters" ( + "id" serial PRIMARY KEY NOT NULL, + "club_id" serial NOT NULL, + "email" text NOT NULL, + "password" text NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "users" ( + "uuid" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "club_id" serial NOT NULL, + "email" text NOT NULL, + "hashpass" text NOT NULL, + "username" text NOT NULL, + "quote" text, + "updated_at" timestamp DEFAULT now() NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE UNIQUE INDEX IF NOT EXISTS "name_unique_idx" ON "clubs" USING btree ("name");--> statement-breakpoint +CREATE UNIQUE INDEX IF NOT EXISTS "email_unique_idx" ON "users" USING btree ("email");--> statement-breakpoint +CREATE UNIQUE INDEX IF NOT EXISTS "username_unique_idx" ON "users" USING btree ("username"); \ No newline at end of file diff --git a/database/migrations/meta/0000_snapshot.json b/database/migrations/meta/0000_snapshot.json index ecf6519..7078def 100644 --- a/database/migrations/meta/0000_snapshot.json +++ b/database/migrations/meta/0000_snapshot.json @@ -1,11 +1,54 @@ { - "id": "85674a8b-3f8c-476f-9ce2-8635c80b2d04", + "id": "0e004108-39e1-4d32-b380-d66778f5ed70", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", "tables": { - "public.users": { - "name": "users", + "public.acquired": { + "name": "acquired", + "schema": "", + "columns": { + "user_uuid": { + "name": "user_uuid", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "challenge_id": { + "name": "challenge_id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "created_by": { + "name": "created_by", + "type": "serial", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "public.challenges": { + "name": "challenges", "schema": "", "columns": { "id": { @@ -14,11 +57,37 @@ "primaryKey": true, "notNull": true }, - "first_name": { - "name": "first_name", + "club_id": { + "name": "club_id", + "type": "serial", + "primaryKey": false, + "notNull": true + }, + "score": { + "name": "score", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", "type": "text", "primaryKey": false, "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" } }, "indexes": {}, @@ -26,6 +95,205 @@ "compositePrimaryKeys": {}, "uniqueConstraints": {}, "checkConstraints": {} + }, + "public.clubs": { + "name": "clubs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "daily_date": { + "name": "daily_date", + "type": "date", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "name_unique_idx": { + "name": "name_unique_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "public.granters": { + "name": "granters", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "club_id": { + "name": "club_id", + "type": "serial", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "club_id": { + "name": "club_id", + "type": "serial", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "hashpass": { + "name": "hashpass", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "quote": { + "name": "quote", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "email_unique_idx": { + "name": "email_unique_idx", + "columns": [ + { + "expression": "email", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "username_unique_idx": { + "name": "username_unique_idx", + "columns": [ + { + "expression": "username", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} } }, "enums": {}, diff --git a/database/migrations/meta/_journal.json b/database/migrations/meta/_journal.json index fccfd13..7f17554 100644 --- a/database/migrations/meta/_journal.json +++ b/database/migrations/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1729680286443, - "tag": "0000_light_tinkerer", + "when": 1729840120930, + "tag": "0000_organic_fantastic_four", "breakpoints": true } ] diff --git a/database/schema/acquired.ts b/database/schema/acquired.ts new file mode 100644 index 0000000..ee7abc2 --- /dev/null +++ b/database/schema/acquired.ts @@ -0,0 +1,34 @@ +import { relations } from "drizzle-orm"; +import { pgTable, serial, timestamp, uuid } from "drizzle-orm/pg-core"; +import { users } from "./users"; +import { challenges } from "./challeges"; + +export const acquired = pgTable("acquired", { + userUuid: uuid("user_uuid").primaryKey(), + challengeId: serial("challenge_id").primaryKey(), + createdBy: serial("created_by"), + updatedAt: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdate(() => new Date()), + createdAt: timestamp("created_at").defaultNow().notNull() +}); + +export const acquiredRelations = relations(acquired, ({ many }) => ({ + user: many(users), + challenge: many(challenges) +})); + +export const acquiredUser = relations(acquired, ({ one }) => ({ + user: one(users, { + fields: [acquired.userUuid], + references: [users.uuid] + }) +})); + +export const acquiredChallenge = relations(acquired, ({ one }) => ({ + challenge: one(challenges, { + fields: [acquired.challengeId], + references: [challenges.id] + }) +})); diff --git a/database/schema/challeges.ts b/database/schema/challeges.ts new file mode 100644 index 0000000..abd1698 --- /dev/null +++ b/database/schema/challeges.ts @@ -0,0 +1,29 @@ +import { relations } from "drizzle-orm"; +import { integer, pgTable, serial, text, timestamp } from "drizzle-orm/pg-core"; +import { acquired } from "./acquired"; +import { clubs } from "./clubs"; +import { granters } from "./granters"; + +export const challenges = pgTable("challenges", { + id: serial("id").primaryKey(), + clubId: serial("club_id"), + score: integer("score").notNull(), + name: text("name").notNull(), + updatedAt: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdate(() => new Date()), + createdAt: timestamp("created_at").defaultNow().notNull() +}); + +export const challengesRelations = relations(challenges, ({ many }) => ({ + acquired: many(acquired), + granters: many(granters) +})); + +export const challengesClub = relations(challenges, ({ one }) => ({ + club: one(clubs, { + fields: [challenges.clubId], + references: [clubs.id] + }) +})); diff --git a/database/schema/clubs.ts b/database/schema/clubs.ts new file mode 100644 index 0000000..56c1aa9 --- /dev/null +++ b/database/schema/clubs.ts @@ -0,0 +1,20 @@ +import { date, pgTable, serial, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core"; + +export const clubs = pgTable( + "clubs", + { + id: serial("id").primaryKey(), + avatarUrl: text("avatar_url").notNull(), + name: text("name").notNull(), + description: text("description").notNull(), + dailyDate: date("daily_date").notNull(), + updatedAt: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdate(() => new Date()), + createdAt: timestamp("created_at").defaultNow().notNull() + }, + (clubs) => ({ + nameUniqueIdx: uniqueIndex("name_unique_idx").on(clubs.name) + }) +); diff --git a/database/schema/granters.ts b/database/schema/granters.ts new file mode 100644 index 0000000..000fae4 --- /dev/null +++ b/database/schema/granters.ts @@ -0,0 +1,17 @@ +import { relations } from "drizzle-orm"; +import { pgTable, serial, text } from "drizzle-orm/pg-core"; +import { clubs } from "./clubs"; + +export const granters = pgTable("granters", { + id: serial("id").primaryKey(), + clubId: serial("club_id").notNull(), + email: text("email").notNull(), + password: text("password").notNull() +}); + +export const grantersClub = relations(granters, ({ one }) => ({ + club: one(clubs, { + fields: [granters.clubId], + references: [clubs.id] + }) +})); diff --git a/database/schema/users.ts b/database/schema/users.ts index 6ec5a55..2aa93d8 100644 --- a/database/schema/users.ts +++ b/database/schema/users.ts @@ -1,6 +1,36 @@ -import { pgTable, serial, text } from "drizzle-orm/pg-core"; +import { relations } from "drizzle-orm"; +import { pgTable, serial, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core"; +import { acquired } from "./acquired"; +import { clubs } from "./clubs"; -export const users = pgTable("users", { - id: serial("id").primaryKey(), - firstName: text("first_name").notNull() -}); +export const users = pgTable( + "users", + { + uuid: uuid("uuid").primaryKey().defaultRandom(), + clubId: serial("club_id"), + email: text("email").notNull(), + hashpass: text("hashpass").notNull(), + username: text("username").notNull(), + quote: text("quote"), + updatedAt: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdate(() => new Date()), + createdAt: timestamp("created_at").defaultNow().notNull() + }, + (users) => ({ + emailUniqueIdx: uniqueIndex("email_unique_idx").on(users.email), + usernameUniqueIdx: uniqueIndex("username_unique_idx").on(users.username) + }) +); + +export const usersRelations = relations(users, ({ many }) => ({ + acquired: many(acquired) +})); + +export const usersClub = relations(users, ({ one }) => ({ + club: one(clubs, { + fields: [users.clubId], + references: [clubs.id] + }) +}));