From ed80df0508ab69d9e7ab55d8ab235021e7cdd5a0 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Sat, 19 Aug 2023 18:58:42 +0800 Subject: [PATCH] Added user + auth table --- drizzle/0002_unusual_nico_minoru.sql | 34 +++++ drizzle/meta/0002_snapshot.json | 211 +++++++++++++++++++++++++++ drizzle/meta/_journal.json | 7 + lib/schema.ts | 68 +++++---- 4 files changed, 293 insertions(+), 27 deletions(-) create mode 100644 drizzle/0002_unusual_nico_minoru.sql create mode 100644 drizzle/meta/0002_snapshot.json diff --git a/drizzle/0002_unusual_nico_minoru.sql b/drizzle/0002_unusual_nico_minoru.sql new file mode 100644 index 0000000..e8294be --- /dev/null +++ b/drizzle/0002_unusual_nico_minoru.sql @@ -0,0 +1,34 @@ +CREATE TABLE IF NOT EXISTS "session" ( + "sessionToken" text PRIMARY KEY NOT NULL, + "user_id" integer NOT NULL, + "expires" timestamp NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "users" ( + "id" serial PRIMARY KEY NOT NULL, + "name" text, + "email" text, + "password" text, + "created_at" timestamp, + "updated_at" timestamp +); +--> statement-breakpoint +ALTER TABLE "bookmarks" ADD COLUMN "user_id" integer;--> statement-breakpoint +ALTER TABLE "bookmark_categories" ADD COLUMN "user_id" integer;--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "bookmarks" ADD CONSTRAINT "bookmarks_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "bookmark_categories" ADD CONSTRAINT "bookmark_categories_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "session" ADD CONSTRAINT "session_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; diff --git a/drizzle/meta/0002_snapshot.json b/drizzle/meta/0002_snapshot.json new file mode 100644 index 0000000..b210087 --- /dev/null +++ b/drizzle/meta/0002_snapshot.json @@ -0,0 +1,211 @@ +{ + "version": "5", + "dialect": "pg", + "id": "bd1fa6fc-b40d-4d9a-b024-69acb0a34270", + "prevId": "288ceda5-82ca-464f-a1f1-ab4e1aad475d", + "tables": { + "bookmarks": { + "name": "bookmarks", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "category_id": { + "name": "category_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "link": { + "name": "link", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "bookmarks_user_id_users_id_fk": { + "name": "bookmarks_user_id_users_id_fk", + "tableFrom": "bookmarks", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "bookmark_categories": { + "name": "bookmark_categories", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "bookmark_categories_user_id_users_id_fk": { + "name": "bookmark_categories_user_id_users_id_fk", + "tableFrom": "bookmark_categories", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "session": { + "name": "session", + "schema": "", + "columns": { + "sessionToken": { + "name": "sessionToken", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "expires": { + "name": "expires", + "type": "timestamp", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "session_user_id_users_id_fk": { + "name": "session_user_id_users_id_fk", + "tableFrom": "session", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index eb31c3c..40110cc 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -15,6 +15,13 @@ "when": 1692095367785, "tag": "0001_busy_legion", "breakpoints": true + }, + { + "idx": 2, + "version": "5", + "when": 1692438117496, + "tag": "0002_unusual_nico_minoru", + "breakpoints": true } ] } \ No newline at end of file diff --git a/lib/schema.ts b/lib/schema.ts index 8e65468..966a3ca 100644 --- a/lib/schema.ts +++ b/lib/schema.ts @@ -1,29 +1,23 @@ import { relations } from 'drizzle-orm' -import { - pgTable, - serial, - text, - varchar, - timestamp, - integer, -} from 'drizzle-orm/pg-core' - -// export const user = pgTable('users', { -// id: serial('id').primaryKey(), -// name: text('name'), -// email: text('email'), -// password: text('password'), -// createdAt: timestamp('created_at'), -// updatedAt: timestamp('updated_at'), -// }) -// -// export const userRelations = relations(user, ({ many }) => ({ -// bookmark: many(bookmark), -// })) +import { pgTable, serial, text, timestamp, integer } from 'drizzle-orm/pg-core' + +export const user = pgTable('users', { + id: serial('id').primaryKey(), + name: text('name'), + email: text('email'), + hashedPassword: text('password'), + createdAt: timestamp('created_at'), + updatedAt: timestamp('updated_at'), +}) + +export const userRelations = relations(user, ({ many }) => ({ + bookmark: many(bookmark), + sessions: many(sessions), +})) export const bookmark = pgTable('bookmarks', { id: serial('id').primaryKey(), - // userId: integer('user_id').references(() => user.id), + userId: integer('user_id').references(() => user.id), category_id: integer('category_id'), name: text('name'), link: text('link'), @@ -32,10 +26,10 @@ export const bookmark = pgTable('bookmarks', { }) export const bookmarkRelations = relations(bookmark, ({ one }) => ({ - // user: one(user, { - // fields: [bookmark.userId], - // references: [user.id], - // }), + user: one(user, { + fields: [bookmark.userId], + references: [user.id], + }), bookmarkCategory: one(bookmarkCategory, { fields: [bookmark.category_id], references: [bookmarkCategory.id], @@ -44,12 +38,32 @@ export const bookmarkRelations = relations(bookmark, ({ one }) => ({ export const bookmarkCategory = pgTable('bookmark_categories', { id: serial('id').primaryKey(), + userId: integer('user_id').references(() => user.id), name: text('name'), }) export const bookmarkCategoryRelations = relations( bookmarkCategory, - ({ many }) => ({ + ({ one, many }) => ({ + user: one(user, { + fields: [bookmarkCategory.userId], + references: [user.id], + }), bookmark: many(bookmark), }) ) + +export const sessions = pgTable('session', { + sessionToken: text('sessionToken').notNull().primaryKey(), + userId: integer('user_id') + .notNull() + .references(() => user.id, { onDelete: 'cascade' }), + expires: timestamp('expires', { mode: 'date' }).notNull(), +}) + +export const sessionRelations = relations(sessions, ({ one }) => ({ + user: one(user, { + fields: [sessions.userId], + references: [user.id], + }), +}))