diff options
| author | MohamedBassem <me@mbassem.com> | 2024-02-09 01:50:35 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-02-09 01:57:31 +0000 |
| commit | 08a5694e451218f1bcb2ad9eb42fd93250afbb96 (patch) | |
| tree | 2cc2351f26b0ab98268db4bc463c0c3aa3f78a3b /packages/db | |
| parent | c5bfa5000f178475d0b019b5a960916134b2ecfb (diff) | |
| download | karakeep-08a5694e451218f1bcb2ad9eb42fd93250afbb96.tar.zst | |
[refactor] Extract the bookmark model to be a high level model to support other type of bookmarks
Diffstat (limited to 'packages/db')
| -rw-r--r-- | packages/db/prisma/migrations/20240209013653_toplevel_bookmark/migration.sql | 62 | ||||
| -rw-r--r-- | packages/db/prisma/schema.prisma | 54 |
2 files changed, 93 insertions, 23 deletions
diff --git a/packages/db/prisma/migrations/20240209013653_toplevel_bookmark/migration.sql b/packages/db/prisma/migrations/20240209013653_toplevel_bookmark/migration.sql new file mode 100644 index 00000000..2b5aa370 --- /dev/null +++ b/packages/db/prisma/migrations/20240209013653_toplevel_bookmark/migration.sql @@ -0,0 +1,62 @@ +/* + Warnings: + + - You are about to drop the `BookmarkedLinkDetails` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `TagsOnLinks` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the column `createdAt` on the `BookmarkedLink` table. All the data in the column will be lost. + - You are about to drop the column `userId` on the `BookmarkedLink` table. All the data in the column will be lost. + +*/ +-- DropIndex +DROP INDEX "TagsOnLinks_linkId_tagId_key"; + +-- DropTable +PRAGMA foreign_keys=off; +DROP TABLE "BookmarkedLinkDetails"; +PRAGMA foreign_keys=on; + +-- DropTable +PRAGMA foreign_keys=off; +DROP TABLE "TagsOnLinks"; +PRAGMA foreign_keys=on; + +-- CreateTable +CREATE TABLE "Bookmark" ( + "id" TEXT NOT NULL PRIMARY KEY, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "archived" BOOLEAN NOT NULL DEFAULT false, + "favourited" BOOLEAN NOT NULL DEFAULT false, + "userId" TEXT NOT NULL, + CONSTRAINT "Bookmark_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "TagsOnBookmarks" ( + "bookmarkId" TEXT NOT NULL, + "tagId" TEXT NOT NULL, + "attachedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "attachedBy" TEXT NOT NULL, + CONSTRAINT "TagsOnBookmarks_bookmarkId_fkey" FOREIGN KEY ("bookmarkId") REFERENCES "Bookmark" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "TagsOnBookmarks_tagId_fkey" FOREIGN KEY ("tagId") REFERENCES "BookmarkTags" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_BookmarkedLink" ( + "id" TEXT NOT NULL PRIMARY KEY, + "url" TEXT NOT NULL, + "title" TEXT, + "description" TEXT, + "imageUrl" TEXT, + "favicon" TEXT, + "crawledAt" DATETIME, + CONSTRAINT "BookmarkedLink_id_fkey" FOREIGN KEY ("id") REFERENCES "Bookmark" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_BookmarkedLink" ("id", "url") SELECT "id", "url" FROM "BookmarkedLink"; +DROP TABLE "BookmarkedLink"; +ALTER TABLE "new_BookmarkedLink" RENAME TO "BookmarkedLink"; +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; + +-- CreateIndex +CREATE UNIQUE INDEX "TagsOnBookmarks_bookmarkId_tagId_key" ON "TagsOnBookmarks"("bookmarkId", "tagId"); diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 0e6d080c..623a9f13 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -38,15 +38,15 @@ model Session { } model User { - id String @id @default(cuid()) + id String @id @default(cuid()) name String? - email String? @unique + email String? @unique emailVerified DateTime? image String? accounts Account[] sessions Session[] - links BookmarkedLink[] tags BookmarkTags[] + bookmarks Bookmark[] } model VerificationToken { @@ -57,28 +57,34 @@ model VerificationToken { @@unique([identifier, token]) } -model BookmarkedLink { - id String @id @default(cuid()) - url String - createdAt DateTime @default(now()) +model Bookmark { + id String @id @default(cuid()) + createdAt DateTime @default(now()) + archived Boolean @default(false) + favourited Boolean @default(false) + userId String - userId String + // Content relation + link BookmarkedLink? - details BookmarkedLinkDetails? - - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - tags TagsOnLinks[] + // Other relations + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + tags TagsOnBookmarks[] } -model BookmarkedLinkDetails { - id String @id +model BookmarkedLink { + id String @id + url String + + // Crawled info title String? description String? imageUrl String? favicon String? - createdAt DateTime @default(now()) + crawledAt DateTime? - link BookmarkedLink @relation(fields: [id], references: [id], onDelete: Cascade) + // Relations + parentBookmark Bookmark @relation(fields: [id], references: [id], onDelete: Cascade) } model BookmarkTags { @@ -88,18 +94,20 @@ model BookmarkTags { userId String - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - attachedLinks TagsOnLinks[] + // Relations + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + bookmarks TagsOnBookmarks[] } -model TagsOnLinks { - link BookmarkedLink @relation(fields: [linkId], references: [id], onDelete: Cascade) - linkId String +model TagsOnBookmarks { + bookmark Bookmark @relation(fields: [bookmarkId], references: [id], onDelete: Cascade) + bookmarkId String tag BookmarkTags @relation(fields: [tagId], references: [id], onDelete: Cascade) tagId String - attachedAt DateTime @default(now()) + attachedAt DateTime @default(now()) + attachedBy String // "human" or "ai" (if only prisma sqlite supported enums) - @@unique([linkId, tagId]) + @@unique([bookmarkId, tagId]) } |
