Skip to content

Commit

Permalink
🗃️ feat (library): Created models and generated new migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Mar 4, 2024
1 parent 9042ac7 commit a356ce8
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 1 deletion.
35 changes: 35 additions & 0 deletions apps/library/src/server/db/migrations/0001_blushing_shape.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE TABLE IF NOT EXISTS "book" (
"book_id" text PRIMARY KEY NOT NULL,
"isbn" text,
"title" text,
"author" text,
"year" integer,
"genre" text,
"language" text,
"copies" integer,
"image" text,
"description" text,
CONSTRAINT "book_isbn_unique" UNIQUE("isbn")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "transaction" (
"transaction_id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"book_id" text NOT NULL,
"borrowed_at" date NOT NULL,
"returned_at" date
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "isbn_idx" ON "book" ("isbn");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "title_idx" ON "book" ("title");--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_book_id_book_book_id_fk" FOREIGN KEY ("book_id") REFERENCES "book"("book_id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
253 changes: 253 additions & 0 deletions apps/library/src/server/db/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
{
"id": "02daa765-f65e-49cf-ba2e-20a61b576a1b",
"prevId": "6d76b8e7-d7c0-43e6-98c0-d0133b8939d4",
"version": "5",
"dialect": "pg",
"tables": {
"book": {
"name": "book",
"schema": "",
"columns": {
"book_id": {
"name": "book_id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"isbn": {
"name": "isbn",
"type": "text",
"primaryKey": false,
"notNull": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false
},
"author": {
"name": "author",
"type": "text",
"primaryKey": false,
"notNull": false
},
"year": {
"name": "year",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"genre": {
"name": "genre",
"type": "text",
"primaryKey": false,
"notNull": false
},
"language": {
"name": "language",
"type": "text",
"primaryKey": false,
"notNull": false
},
"copies": {
"name": "copies",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {
"isbn_idx": {
"name": "isbn_idx",
"columns": [
"isbn"
],
"isUnique": false
},
"title_idx": {
"name": "title_idx",
"columns": [
"title"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"book_isbn_unique": {
"name": "book_isbn_unique",
"nullsNotDistinct": false,
"columns": [
"isbn"
]
}
}
},
"session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"session_user_id_user_id_fk": {
"name": "session_user_id_user_id_fk",
"tableFrom": "session",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"transaction": {
"name": "transaction",
"schema": "",
"columns": {
"transaction_id": {
"name": "transaction_id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"book_id": {
"name": "book_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"borrowed_at": {
"name": "borrowed_at",
"type": "date",
"primaryKey": false,
"notNull": true
},
"returned_at": {
"name": "returned_at",
"type": "date",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"transaction_user_id_user_id_fk": {
"name": "transaction_user_id_user_id_fk",
"tableFrom": "transaction",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"transaction_book_id_book_book_id_fk": {
"name": "transaction_book_id_book_book_id_fk",
"tableFrom": "transaction",
"tableTo": "book",
"columnsFrom": [
"book_id"
],
"columnsTo": [
"book_id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": false
},
"hashed_password": {
"name": "hashed_password",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_username_unique": {
"name": "user_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
}
}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
9 changes: 8 additions & 1 deletion apps/library/src/server/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1709434235761,
"tag": "0000_uneven_human_cannonball",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1709531677735,
"tag": "0001_blushing_shape",
"breakpoints": true
}
]
}
}
56 changes: 56 additions & 0 deletions apps/library/src/server/db/schema/main-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
import { relations } from "drizzle-orm";
import { date, index, integer, pgTable, text } from "drizzle-orm/pg-core";

import { session, user } from "./auth-schema";

// ========================================================================
// Books Table
// ========================================================================

export const book = pgTable(
"book",
{
bookId: text("book_id").primaryKey(),
isbn: text("isbn").unique(),
title: text("title"),
author: text("author"),
year: integer("year"),
genre: text("genre"),
language: text("language"),
copies: integer("copies"),
image: text("image"),
description: text("description"),
},
(table) => {
return {
isbnIdx: index("isbn_idx").on(table.isbn),
titleIdx: index("title_idx").on(table.title),
};
},
);

export const bookRelations = relations(book, ({ many }) => ({
transactions: many(transaction),
}));

// ========================================================================
// Transactions Table
// ========================================================================

export const transaction = pgTable("transaction", {
transactionId: text("transaction_id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => user.id),
bookId: text("book_id")
.notNull()
.references(() => book.bookId),
borrowedAt: date("borrowed_at").notNull(),
returnedAt: date("returned_at"),
});

export const transactionRelations = relations(transaction, ({ one }) => ({
books: one(book, {
fields: [transaction.bookId],
references: [book.bookId],
}),
}));

export { session, user };

0 comments on commit a356ce8

Please sign in to comment.