diff options
Diffstat (limited to 'packages')
22 files changed, 4418 insertions, 1 deletions
diff --git a/packages/api/index.ts b/packages/api/index.ts index 7bf9084d..3df7b429 100644 --- a/packages/api/index.ts +++ b/packages/api/index.ts @@ -10,6 +10,7 @@ import { Context } from "@karakeep/trpc"; import trpcAdapter from "./middlewares/trpcAdapter"; import admin from "./routes/admin"; import assets from "./routes/assets"; +import backups from "./routes/backups"; import bookmarks from "./routes/bookmarks"; import health from "./routes/health"; import highlights from "./routes/highlights"; @@ -37,7 +38,8 @@ const v1 = new Hono<{ .route("/users", users) .route("/assets", assets) .route("/admin", admin) - .route("/rss", rss); + .route("/rss", rss) + .route("/backups", backups); const app = new Hono<{ Variables: { diff --git a/packages/api/routes/backups.ts b/packages/api/routes/backups.ts new file mode 100644 index 00000000..949c826f --- /dev/null +++ b/packages/api/routes/backups.ts @@ -0,0 +1,44 @@ +import { Hono } from "hono"; + +import { authMiddleware } from "../middlewares/auth"; + +const app = new Hono() + .use(authMiddleware) + + // GET /backups + .get("/", async (c) => { + const backups = await c.var.api.backups.list(); + return c.json(backups, 200); + }) + + // POST /backups + .post("/", async (c) => { + const backup = await c.var.api.backups.triggerBackup(); + return c.json(backup, 201); + }) + + // GET /backups/[backupId] + .get("/:backupId", async (c) => { + const backupId = c.req.param("backupId"); + const backup = await c.var.api.backups.get({ backupId }); + return c.json(backup, 200); + }) + + // GET /backups/[backupId]/download + .get("/:backupId/download", async (c) => { + const backupId = c.req.param("backupId"); + const backup = await c.var.api.backups.get({ backupId }); + if (!backup.assetId) { + return c.json({ error: "Backup not found" }, 404); + } + return c.redirect(`/api/assets/${backup.assetId}`); + }) + + // DELETE /backups/[backupId] + .delete("/:backupId", async (c) => { + const backupId = c.req.param("backupId"); + await c.var.api.backups.delete({ backupId }); + return c.body(null, 204); + }); + +export default app; diff --git a/packages/db/drizzle/0067_add_backups_table.sql b/packages/db/drizzle/0067_add_backups_table.sql new file mode 100644 index 00000000..2f8c4bec --- /dev/null +++ b/packages/db/drizzle/0067_add_backups_table.sql @@ -0,0 +1,18 @@ +CREATE TABLE `backups` ( + `id` text PRIMARY KEY NOT NULL, + `userId` text NOT NULL, + `assetId` text, + `createdAt` integer NOT NULL, + `size` integer NOT NULL, + `bookmarkCount` integer NOT NULL, + `status` text DEFAULT 'pending' NOT NULL, + `errorMessage` text, + FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade, + FOREIGN KEY (`assetId`) REFERENCES `assets`(`id`) ON UPDATE no action ON DELETE cascade +); +--> statement-breakpoint +CREATE INDEX `backups_userId_idx` ON `backups` (`userId`);--> statement-breakpoint +CREATE INDEX `backups_createdAt_idx` ON `backups` (`createdAt`);--> statement-breakpoint +ALTER TABLE `user` ADD `backupsEnabled` integer DEFAULT false NOT NULL;--> statement-breakpoint +ALTER TABLE `user` ADD `backupsFrequency` text DEFAULT 'weekly' NOT NULL;--> statement-breakpoint +ALTER TABLE `user` ADD `backupsRetentionDays` integer DEFAULT 30 NOT NULL;
\ No newline at end of file diff --git a/packages/db/drizzle/meta/0067_snapshot.json b/packages/db/drizzle/meta/0067_snapshot.json new file mode 100644 index 00000000..b231c65e --- /dev/null +++ b/packages/db/drizzle/meta/0067_snapshot.json @@ -0,0 +1,2909 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "6efcc681-eaac-44a6-a224-f97a7df01ff3", + "prevId": "f5049a1f-7de4-4107-9b8c-c13118699381", + "tables": { + "account": { + "name": "account", + "columns": { + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "providerAccountId": { + "name": "providerAccountId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expires_at": { + "name": "expires_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "token_type": { + "name": "token_type", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "session_state": { + "name": "session_state", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "account_userId_user_id_fk": { + "name": "account_userId_user_id_fk", + "tableFrom": "account", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "account_provider_providerAccountId_pk": { + "columns": [ + "provider", + "providerAccountId" + ], + "name": "account_provider_providerAccountId_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "apiKey": { + "name": "apiKey", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "keyId": { + "name": "keyId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "keyHash": { + "name": "keyHash", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "apiKey_keyId_unique": { + "name": "apiKey_keyId_unique", + "columns": [ + "keyId" + ], + "isUnique": true + }, + "apiKey_name_userId_unique": { + "name": "apiKey_name_userId_unique", + "columns": [ + "name", + "userId" + ], + "isUnique": true + } + }, + "foreignKeys": { + "apiKey_userId_user_id_fk": { + "name": "apiKey_userId_user_id_fk", + "tableFrom": "apiKey", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "assets": { + "name": "assets", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "assetType": { + "name": "assetType", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "size": { + "name": "size", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "contentType": { + "name": "contentType", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "fileName": { + "name": "fileName", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "assets_bookmarkId_idx": { + "name": "assets_bookmarkId_idx", + "columns": [ + "bookmarkId" + ], + "isUnique": false + }, + "assets_assetType_idx": { + "name": "assets_assetType_idx", + "columns": [ + "assetType" + ], + "isUnique": false + }, + "assets_userId_idx": { + "name": "assets_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "assets_bookmarkId_bookmarks_id_fk": { + "name": "assets_bookmarkId_bookmarks_id_fk", + "tableFrom": "assets", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "assets_userId_user_id_fk": { + "name": "assets_userId_user_id_fk", + "tableFrom": "assets", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "backups": { + "name": "backups", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "assetId": { + "name": "assetId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "size": { + "name": "size", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "bookmarkCount": { + "name": "bookmarkCount", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'pending'" + }, + "errorMessage": { + "name": "errorMessage", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "backups_userId_idx": { + "name": "backups_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "backups_createdAt_idx": { + "name": "backups_createdAt_idx", + "columns": [ + "createdAt" + ], + "isUnique": false + } + }, + "foreignKeys": { + "backups_userId_user_id_fk": { + "name": "backups_userId_user_id_fk", + "tableFrom": "backups", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "backups_assetId_assets_id_fk": { + "name": "backups_assetId_assets_id_fk", + "tableFrom": "backups", + "tableTo": "assets", + "columnsFrom": [ + "assetId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarkAssets": { + "name": "bookmarkAssets", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "assetType": { + "name": "assetType", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "assetId": { + "name": "assetId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "metadata": { + "name": "metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "fileName": { + "name": "fileName", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sourceUrl": { + "name": "sourceUrl", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "bookmarkAssets_id_bookmarks_id_fk": { + "name": "bookmarkAssets_id_bookmarks_id_fk", + "tableFrom": "bookmarkAssets", + "tableTo": "bookmarks", + "columnsFrom": [ + "id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarkLinks": { + "name": "bookmarkLinks", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "author": { + "name": "author", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "publisher": { + "name": "publisher", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "datePublished": { + "name": "datePublished", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "dateModified": { + "name": "dateModified", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "imageUrl": { + "name": "imageUrl", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "favicon": { + "name": "favicon", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "htmlContent": { + "name": "htmlContent", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "contentAssetId": { + "name": "contentAssetId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "crawledAt": { + "name": "crawledAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "crawlStatus": { + "name": "crawlStatus", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'pending'" + }, + "crawlStatusCode": { + "name": "crawlStatusCode", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": 200 + } + }, + "indexes": { + "bookmarkLinks_url_idx": { + "name": "bookmarkLinks_url_idx", + "columns": [ + "url" + ], + "isUnique": false + } + }, + "foreignKeys": { + "bookmarkLinks_id_bookmarks_id_fk": { + "name": "bookmarkLinks_id_bookmarks_id_fk", + "tableFrom": "bookmarkLinks", + "tableTo": "bookmarks", + "columnsFrom": [ + "id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarkLists": { + "name": "bookmarkLists", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "query": { + "name": "query", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "parentId": { + "name": "parentId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "rssToken": { + "name": "rssToken", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "public": { + "name": "public", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + } + }, + "indexes": { + "bookmarkLists_userId_idx": { + "name": "bookmarkLists_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "bookmarkLists_userId_id_idx": { + "name": "bookmarkLists_userId_id_idx", + "columns": [ + "userId", + "id" + ], + "isUnique": true + } + }, + "foreignKeys": { + "bookmarkLists_userId_user_id_fk": { + "name": "bookmarkLists_userId_user_id_fk", + "tableFrom": "bookmarkLists", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "bookmarkLists_parentId_bookmarkLists_id_fk": { + "name": "bookmarkLists_parentId_bookmarkLists_id_fk", + "tableFrom": "bookmarkLists", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "parentId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarkTags": { + "name": "bookmarkTags", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "bookmarkTags_name_idx": { + "name": "bookmarkTags_name_idx", + "columns": [ + "name" + ], + "isUnique": false + }, + "bookmarkTags_userId_idx": { + "name": "bookmarkTags_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "bookmarkTags_userId_name_unique": { + "name": "bookmarkTags_userId_name_unique", + "columns": [ + "userId", + "name" + ], + "isUnique": true + }, + "bookmarkTags_userId_id_idx": { + "name": "bookmarkTags_userId_id_idx", + "columns": [ + "userId", + "id" + ], + "isUnique": true + } + }, + "foreignKeys": { + "bookmarkTags_userId_user_id_fk": { + "name": "bookmarkTags_userId_user_id_fk", + "tableFrom": "bookmarkTags", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarkTexts": { + "name": "bookmarkTexts", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "text": { + "name": "text", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sourceUrl": { + "name": "sourceUrl", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "bookmarkTexts_id_bookmarks_id_fk": { + "name": "bookmarkTexts_id_bookmarks_id_fk", + "tableFrom": "bookmarkTexts", + "tableTo": "bookmarks", + "columnsFrom": [ + "id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarks": { + "name": "bookmarks", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "modifiedAt": { + "name": "modifiedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "archived": { + "name": "archived", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "favourited": { + "name": "favourited", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "taggingStatus": { + "name": "taggingStatus", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'pending'" + }, + "summarizationStatus": { + "name": "summarizationStatus", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'pending'" + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "note": { + "name": "note", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "bookmarks_userId_idx": { + "name": "bookmarks_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "bookmarks_archived_idx": { + "name": "bookmarks_archived_idx", + "columns": [ + "archived" + ], + "isUnique": false + }, + "bookmarks_favourited_idx": { + "name": "bookmarks_favourited_idx", + "columns": [ + "favourited" + ], + "isUnique": false + }, + "bookmarks_createdAt_idx": { + "name": "bookmarks_createdAt_idx", + "columns": [ + "createdAt" + ], + "isUnique": false + } + }, + "foreignKeys": { + "bookmarks_userId_user_id_fk": { + "name": "bookmarks_userId_user_id_fk", + "tableFrom": "bookmarks", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "bookmarksInLists": { + "name": "bookmarksInLists", + "columns": { + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "listId": { + "name": "listId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "addedAt": { + "name": "addedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "listMembershipId": { + "name": "listMembershipId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "bookmarksInLists_bookmarkId_idx": { + "name": "bookmarksInLists_bookmarkId_idx", + "columns": [ + "bookmarkId" + ], + "isUnique": false + }, + "bookmarksInLists_listId_idx": { + "name": "bookmarksInLists_listId_idx", + "columns": [ + "listId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "bookmarksInLists_bookmarkId_bookmarks_id_fk": { + "name": "bookmarksInLists_bookmarkId_bookmarks_id_fk", + "tableFrom": "bookmarksInLists", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "bookmarksInLists_listId_bookmarkLists_id_fk": { + "name": "bookmarksInLists_listId_bookmarkLists_id_fk", + "tableFrom": "bookmarksInLists", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "listId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "bookmarksInLists_listMembershipId_listCollaborators_id_fk": { + "name": "bookmarksInLists_listMembershipId_listCollaborators_id_fk", + "tableFrom": "bookmarksInLists", + "tableTo": "listCollaborators", + "columnsFrom": [ + "listMembershipId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "bookmarksInLists_bookmarkId_listId_pk": { + "columns": [ + "bookmarkId", + "listId" + ], + "name": "bookmarksInLists_bookmarkId_listId_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "config": { + "name": "config", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "customPrompts": { + "name": "customPrompts", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "text": { + "name": "text", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "appliesTo": { + "name": "appliesTo", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "customPrompts_userId_idx": { + "name": "customPrompts_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "customPrompts_userId_user_id_fk": { + "name": "customPrompts_userId_user_id_fk", + "tableFrom": "customPrompts", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "highlights": { + "name": "highlights", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "startOffset": { + "name": "startOffset", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "endOffset": { + "name": "endOffset", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'yellow'" + }, + "text": { + "name": "text", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "note": { + "name": "note", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "highlights_bookmarkId_idx": { + "name": "highlights_bookmarkId_idx", + "columns": [ + "bookmarkId" + ], + "isUnique": false + }, + "highlights_userId_idx": { + "name": "highlights_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "highlights_bookmarkId_bookmarks_id_fk": { + "name": "highlights_bookmarkId_bookmarks_id_fk", + "tableFrom": "highlights", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "highlights_userId_user_id_fk": { + "name": "highlights_userId_user_id_fk", + "tableFrom": "highlights", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "importSessionBookmarks": { + "name": "importSessionBookmarks", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "importSessionId": { + "name": "importSessionId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "importSessionBookmarks_sessionId_idx": { + "name": "importSessionBookmarks_sessionId_idx", + "columns": [ + "importSessionId" + ], + "isUnique": false + }, + "importSessionBookmarks_bookmarkId_idx": { + "name": "importSessionBookmarks_bookmarkId_idx", + "columns": [ + "bookmarkId" + ], + "isUnique": false + }, + "importSessionBookmarks_importSessionId_bookmarkId_unique": { + "name": "importSessionBookmarks_importSessionId_bookmarkId_unique", + "columns": [ + "importSessionId", + "bookmarkId" + ], + "isUnique": true + } + }, + "foreignKeys": { + "importSessionBookmarks_importSessionId_importSessions_id_fk": { + "name": "importSessionBookmarks_importSessionId_importSessions_id_fk", + "tableFrom": "importSessionBookmarks", + "tableTo": "importSessions", + "columnsFrom": [ + "importSessionId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "importSessionBookmarks_bookmarkId_bookmarks_id_fk": { + "name": "importSessionBookmarks_bookmarkId_bookmarks_id_fk", + "tableFrom": "importSessionBookmarks", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "importSessions": { + "name": "importSessions", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "rootListId": { + "name": "rootListId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "modifiedAt": { + "name": "modifiedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "importSessions_userId_idx": { + "name": "importSessions_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "importSessions_userId_user_id_fk": { + "name": "importSessions_userId_user_id_fk", + "tableFrom": "importSessions", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "importSessions_rootListId_bookmarkLists_id_fk": { + "name": "importSessions_rootListId_bookmarkLists_id_fk", + "tableFrom": "importSessions", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "rootListId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "invites": { + "name": "invites", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "usedAt": { + "name": "usedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "invitedBy": { + "name": "invitedBy", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "invites_token_unique": { + "name": "invites_token_unique", + "columns": [ + "token" + ], + "isUnique": true + } + }, + "foreignKeys": { + "invites_invitedBy_user_id_fk": { + "name": "invites_invitedBy_user_id_fk", + "tableFrom": "invites", + "tableTo": "user", + "columnsFrom": [ + "invitedBy" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "listCollaborators": { + "name": "listCollaborators", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "listId": { + "name": "listId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "addedBy": { + "name": "addedBy", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "listCollaborators_listId_idx": { + "name": "listCollaborators_listId_idx", + "columns": [ + "listId" + ], + "isUnique": false + }, + "listCollaborators_userId_idx": { + "name": "listCollaborators_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "listCollaborators_listId_userId_unique": { + "name": "listCollaborators_listId_userId_unique", + "columns": [ + "listId", + "userId" + ], + "isUnique": true + } + }, + "foreignKeys": { + "listCollaborators_listId_bookmarkLists_id_fk": { + "name": "listCollaborators_listId_bookmarkLists_id_fk", + "tableFrom": "listCollaborators", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "listId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "listCollaborators_userId_user_id_fk": { + "name": "listCollaborators_userId_user_id_fk", + "tableFrom": "listCollaborators", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "listCollaborators_addedBy_user_id_fk": { + "name": "listCollaborators_addedBy_user_id_fk", + "tableFrom": "listCollaborators", + "tableTo": "user", + "columnsFrom": [ + "addedBy" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "listInvitations": { + "name": "listInvitations", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "listId": { + "name": "listId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'pending'" + }, + "invitedAt": { + "name": "invitedAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "invitedEmail": { + "name": "invitedEmail", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "invitedBy": { + "name": "invitedBy", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "listInvitations_listId_idx": { + "name": "listInvitations_listId_idx", + "columns": [ + "listId" + ], + "isUnique": false + }, + "listInvitations_userId_idx": { + "name": "listInvitations_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "listInvitations_status_idx": { + "name": "listInvitations_status_idx", + "columns": [ + "status" + ], + "isUnique": false + }, + "listInvitations_listId_userId_unique": { + "name": "listInvitations_listId_userId_unique", + "columns": [ + "listId", + "userId" + ], + "isUnique": true + } + }, + "foreignKeys": { + "listInvitations_listId_bookmarkLists_id_fk": { + "name": "listInvitations_listId_bookmarkLists_id_fk", + "tableFrom": "listInvitations", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "listId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "listInvitations_userId_user_id_fk": { + "name": "listInvitations_userId_user_id_fk", + "tableFrom": "listInvitations", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "listInvitations_invitedBy_user_id_fk": { + "name": "listInvitations_invitedBy_user_id_fk", + "tableFrom": "listInvitations", + "tableTo": "user", + "columnsFrom": [ + "invitedBy" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "passwordResetToken": { + "name": "passwordResetToken", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "expires": { + "name": "expires", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "passwordResetToken_token_unique": { + "name": "passwordResetToken_token_unique", + "columns": [ + "token" + ], + "isUnique": true + }, + "passwordResetTokens_userId_idx": { + "name": "passwordResetTokens_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "passwordResetToken_userId_user_id_fk": { + "name": "passwordResetToken_userId_user_id_fk", + "tableFrom": "passwordResetToken", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "rssFeedImports": { + "name": "rssFeedImports", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "entryId": { + "name": "entryId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "rssFeedId": { + "name": "rssFeedId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "rssFeedImports_feedIdIdx_idx": { + "name": "rssFeedImports_feedIdIdx_idx", + "columns": [ + "rssFeedId" + ], + "isUnique": false + }, + "rssFeedImports_entryIdIdx_idx": { + "name": "rssFeedImports_entryIdIdx_idx", + "columns": [ + "entryId" + ], + "isUnique": false + }, + "rssFeedImports_rssFeedId_entryId_unique": { + "name": "rssFeedImports_rssFeedId_entryId_unique", + "columns": [ + "rssFeedId", + "entryId" + ], + "isUnique": true + } + }, + "foreignKeys": { + "rssFeedImports_rssFeedId_rssFeeds_id_fk": { + "name": "rssFeedImports_rssFeedId_rssFeeds_id_fk", + "tableFrom": "rssFeedImports", + "tableTo": "rssFeeds", + "columnsFrom": [ + "rssFeedId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "rssFeedImports_bookmarkId_bookmarks_id_fk": { + "name": "rssFeedImports_bookmarkId_bookmarks_id_fk", + "tableFrom": "rssFeedImports", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "rssFeeds": { + "name": "rssFeeds", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "importTags": { + "name": "importTags", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "lastFetchedAt": { + "name": "lastFetchedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "lastFetchedStatus": { + "name": "lastFetchedStatus", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'pending'" + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "rssFeeds_userId_idx": { + "name": "rssFeeds_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "rssFeeds_userId_user_id_fk": { + "name": "rssFeeds_userId_user_id_fk", + "tableFrom": "rssFeeds", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "ruleEngineActions": { + "name": "ruleEngineActions", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "ruleId": { + "name": "ruleId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "listId": { + "name": "listId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tagId": { + "name": "tagId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "ruleEngineActions_userId_idx": { + "name": "ruleEngineActions_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "ruleEngineActions_ruleId_idx": { + "name": "ruleEngineActions_ruleId_idx", + "columns": [ + "ruleId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "ruleEngineActions_userId_user_id_fk": { + "name": "ruleEngineActions_userId_user_id_fk", + "tableFrom": "ruleEngineActions", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "ruleEngineActions_ruleId_ruleEngineRules_id_fk": { + "name": "ruleEngineActions_ruleId_ruleEngineRules_id_fk", + "tableFrom": "ruleEngineActions", + "tableTo": "ruleEngineRules", + "columnsFrom": [ + "ruleId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "ruleEngineActions_userId_tagId_fk": { + "name": "ruleEngineActions_userId_tagId_fk", + "tableFrom": "ruleEngineActions", + "tableTo": "bookmarkTags", + "columnsFrom": [ + "userId", + "tagId" + ], + "columnsTo": [ + "userId", + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "ruleEngineActions_userId_listId_fk": { + "name": "ruleEngineActions_userId_listId_fk", + "tableFrom": "ruleEngineActions", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "userId", + "listId" + ], + "columnsTo": [ + "userId", + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "ruleEngineRules": { + "name": "ruleEngineRules", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "enabled": { + "name": "enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "event": { + "name": "event", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "condition": { + "name": "condition", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "listId": { + "name": "listId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tagId": { + "name": "tagId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "ruleEngine_userId_idx": { + "name": "ruleEngine_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "ruleEngineRules_userId_user_id_fk": { + "name": "ruleEngineRules_userId_user_id_fk", + "tableFrom": "ruleEngineRules", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "ruleEngineRules_userId_tagId_fk": { + "name": "ruleEngineRules_userId_tagId_fk", + "tableFrom": "ruleEngineRules", + "tableTo": "bookmarkTags", + "columnsFrom": [ + "userId", + "tagId" + ], + "columnsTo": [ + "userId", + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "ruleEngineRules_userId_listId_fk": { + "name": "ruleEngineRules_userId_listId_fk", + "tableFrom": "ruleEngineRules", + "tableTo": "bookmarkLists", + "columnsFrom": [ + "userId", + "listId" + ], + "columnsTo": [ + "userId", + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "session": { + "name": "session", + "columns": { + "sessionToken": { + "name": "sessionToken", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "expires": { + "name": "expires", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "session_userId_user_id_fk": { + "name": "session_userId_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "subscriptions": { + "name": "subscriptions", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "stripeCustomerId": { + "name": "stripeCustomerId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "stripeSubscriptionId": { + "name": "stripeSubscriptionId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tier": { + "name": "tier", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'free'" + }, + "priceId": { + "name": "priceId", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "cancelAtPeriodEnd": { + "name": "cancelAtPeriodEnd", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": false + }, + "startDate": { + "name": "startDate", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "endDate": { + "name": "endDate", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "modifiedAt": { + "name": "modifiedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "subscriptions_userId_unique": { + "name": "subscriptions_userId_unique", + "columns": [ + "userId" + ], + "isUnique": true + }, + "subscriptions_userId_idx": { + "name": "subscriptions_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + }, + "subscriptions_stripeCustomerId_idx": { + "name": "subscriptions_stripeCustomerId_idx", + "columns": [ + "stripeCustomerId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "subscriptions_userId_user_id_fk": { + "name": "subscriptions_userId_user_id_fk", + "tableFrom": "subscriptions", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "tagsOnBookmarks": { + "name": "tagsOnBookmarks", + "columns": { + "bookmarkId": { + "name": "bookmarkId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tagId": { + "name": "tagId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "attachedAt": { + "name": "attachedAt", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "attachedBy": { + "name": "attachedBy", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "tagsOnBookmarks_tagId_idx": { + "name": "tagsOnBookmarks_tagId_idx", + "columns": [ + "tagId" + ], + "isUnique": false + }, + "tagsOnBookmarks_bookmarkId_idx": { + "name": "tagsOnBookmarks_bookmarkId_idx", + "columns": [ + "bookmarkId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "tagsOnBookmarks_bookmarkId_bookmarks_id_fk": { + "name": "tagsOnBookmarks_bookmarkId_bookmarks_id_fk", + "tableFrom": "tagsOnBookmarks", + "tableTo": "bookmarks", + "columnsFrom": [ + "bookmarkId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tagsOnBookmarks_tagId_bookmarkTags_id_fk": { + "name": "tagsOnBookmarks_tagId_bookmarkTags_id_fk", + "tableFrom": "tagsOnBookmarks", + "tableTo": "bookmarkTags", + "columnsFrom": [ + "tagId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "tagsOnBookmarks_bookmarkId_tagId_pk": { + "columns": [ + "bookmarkId", + "tagId" + ], + "name": "tagsOnBookmarks_bookmarkId_tagId_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "user": { + "name": "user", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "emailVerified": { + "name": "emailVerified", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "salt": { + "name": "salt", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "''" + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'user'" + }, + "bookmarkQuota": { + "name": "bookmarkQuota", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "storageQuota": { + "name": "storageQuota", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "browserCrawlingEnabled": { + "name": "browserCrawlingEnabled", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "bookmarkClickAction": { + "name": "bookmarkClickAction", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'open_original_link'" + }, + "archiveDisplayBehaviour": { + "name": "archiveDisplayBehaviour", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'show'" + }, + "timezone": { + "name": "timezone", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'UTC'" + }, + "backupsEnabled": { + "name": "backupsEnabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "backupsFrequency": { + "name": "backupsFrequency", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'weekly'" + }, + "backupsRetentionDays": { + "name": "backupsRetentionDays", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 30 + } + }, + "indexes": { + "user_email_unique": { + "name": "user_email_unique", + "columns": [ + "email" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "verificationToken": { + "name": "verificationToken", + "columns": { + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "expires": { + "name": "expires", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "verificationToken_identifier_token_pk": { + "columns": [ + "identifier", + "token" + ], + "name": "verificationToken_identifier_token_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "webhooks": { + "name": "webhooks", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "userId": { + "name": "userId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "events": { + "name": "events", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "webhooks_userId_idx": { + "name": "webhooks_userId_idx", + "columns": [ + "userId" + ], + "isUnique": false + } + }, + "foreignKeys": { + "webhooks_userId_user_id_fk": { + "name": "webhooks_userId_user_id_fk", + "tableFrom": "webhooks", + "tableTo": "user", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +}
\ No newline at end of file diff --git a/packages/db/drizzle/meta/_journal.json b/packages/db/drizzle/meta/_journal.json index 80b91ea6..47934ff8 100644 --- a/packages/db/drizzle/meta/_journal.json +++ b/packages/db/drizzle/meta/_journal.json @@ -470,6 +470,13 @@ "when": 1763854050669, "tag": "0066_collaborative_lists_invites", "breakpoints": true + }, + { + "idx": 67, + "version": "6", + "when": 1764418020312, + "tag": "0067_add_backups_table", + "breakpoints": true } ] }
\ No newline at end of file diff --git a/packages/db/schema.ts b/packages/db/schema.ts index d5fd162d..8f259d04 100644 --- a/packages/db/schema.ts +++ b/packages/db/schema.ts @@ -58,6 +58,17 @@ export const users = sqliteTable("user", { .notNull() .default("show"), timezone: text("timezone").default("UTC"), + + // Backup Settings + backupsEnabled: integer("backupsEnabled", { mode: "boolean" }) + .notNull() + .default(false), + backupsFrequency: text("backupsFrequency", { + enum: ["daily", "weekly"], + }) + .notNull() + .default("weekly"), + backupsRetentionDays: integer("backupsRetentionDays").notNull().default(30), }); export const accounts = sqliteTable( @@ -229,6 +240,7 @@ export const enum AssetTypes { LINK_HTML_CONTENT = "linkHtmlContent", BOOKMARK_ASSET = "bookmarkAsset", USER_UPLOADED = "userUploaded", + BACKUP = "backup", UNKNOWN = "unknown", } @@ -248,6 +260,7 @@ export const assets = sqliteTable( AssetTypes.LINK_HTML_CONTENT, AssetTypes.BOOKMARK_ASSET, AssetTypes.USER_UPLOADED, + AssetTypes.BACKUP, AssetTypes.UNKNOWN, ], }).notNull(), @@ -574,6 +587,35 @@ export const rssFeedImportsTable = sqliteTable( ], ); +export const backupsTable = sqliteTable( + "backups", + { + id: text("id") + .notNull() + .primaryKey() + .$defaultFn(() => createId()), + userId: text("userId") + .notNull() + .references(() => users.id, { onDelete: "cascade" }), + assetId: text("assetId").references(() => assets.id, { + onDelete: "cascade", + }), + createdAt: createdAtField(), + size: integer("size").notNull(), + bookmarkCount: integer("bookmarkCount").notNull(), + status: text("status", { + enum: ["pending", "success", "failure"], + }) + .notNull() + .default("pending"), + errorMessage: text("errorMessage"), + }, + (b) => [ + index("backups_userId_idx").on(b.userId), + index("backups_createdAt_idx").on(b.createdAt), + ], +); + export const config = sqliteTable("config", { key: text("key").notNull().primaryKey(), value: text("value").notNull(), @@ -766,6 +808,7 @@ export const userRelations = relations(users, ({ many, one }) => ({ subscription: one(subscriptions), importSessions: many(importSessions), listCollaborations: many(listCollaborators), + backups: many(backupsTable), listInvitations: many(listInvitations), })); @@ -989,3 +1032,14 @@ export const importSessionBookmarksRelations = relations( }), }), ); + +export const backupsRelations = relations(backupsTable, ({ one }) => ({ + user: one(users, { + fields: [backupsTable.userId], + references: [users.id], + }), + asset: one(assets, { + fields: [backupsTable.assetId], + references: [assets.id], + }), +})); diff --git a/packages/e2e_tests/package.json b/packages/e2e_tests/package.json index 7532f5b0..45d512bb 100644 --- a/packages/e2e_tests/package.json +++ b/packages/e2e_tests/package.json @@ -26,6 +26,8 @@ "devDependencies": { "@karakeep/prettier-config": "workspace:^0.1.0", "@karakeep/tsconfig": "workspace:^0.1.0", + "@types/adm-zip": "^0.5.7", + "adm-zip": "^0.5.16", "vite-tsconfig-paths": "^4.3.1", "vitest": "^3.2.4" }, diff --git a/packages/e2e_tests/tests/api/backups.test.ts b/packages/e2e_tests/tests/api/backups.test.ts new file mode 100644 index 00000000..51c16c5e --- /dev/null +++ b/packages/e2e_tests/tests/api/backups.test.ts @@ -0,0 +1,285 @@ +import AdmZip from "adm-zip"; +import { beforeEach, describe, expect, inject, it } from "vitest"; + +import { createKarakeepClient } from "@karakeep/sdk"; + +import { createTestUser } from "../../utils/api"; + +describe("Backups API", () => { + const port = inject("karakeepPort"); + + if (!port) { + throw new Error("Missing required environment variables"); + } + + let client: ReturnType<typeof createKarakeepClient>; + let apiKey: string; + + beforeEach(async () => { + apiKey = await createTestUser(); + client = createKarakeepClient({ + baseUrl: `http://localhost:${port}/api/v1/`, + headers: { + "Content-Type": "application/json", + authorization: `Bearer ${apiKey}`, + }, + }); + }); + + it("should list backups", async () => { + const { data: backupsData, response } = await client.GET("/backups"); + + expect(response.status).toBe(200); + expect(backupsData).toBeDefined(); + expect(backupsData!.backups).toBeDefined(); + expect(Array.isArray(backupsData!.backups)).toBe(true); + }); + + it("should trigger a backup and return the backup record", async () => { + const { data: backup, response } = await client.POST("/backups"); + + expect(response.status).toBe(201); + expect(backup).toBeDefined(); + expect(backup!.id).toBeDefined(); + expect(backup!.userId).toBeDefined(); + expect(backup!.assetId).toBeDefined(); + expect(backup!.status).toBe("pending"); + expect(backup!.size).toBe(0); + expect(backup!.bookmarkCount).toBe(0); + + // Verify the backup appears in the list + const { data: backupsData } = await client.GET("/backups"); + expect(backupsData).toBeDefined(); + expect(backupsData!.backups).toBeDefined(); + expect(backupsData!.backups.some((b) => b.id === backup!.id)).toBe(true); + }); + + it("should get and delete a backup", async () => { + // First trigger a backup + const { data: createdBackup } = await client.POST("/backups"); + expect(createdBackup).toBeDefined(); + + const backupId = createdBackup!.id; + + // Get the specific backup + const { data: backup, response: getResponse } = await client.GET( + "/backups/{backupId}", + { + params: { + path: { + backupId, + }, + }, + }, + ); + + expect(getResponse.status).toBe(200); + expect(backup).toBeDefined(); + expect(backup!.id).toBe(backupId); + expect(backup!.userId).toBeDefined(); + expect(backup!.assetId).toBeDefined(); + expect(backup!.status).toBe("pending"); + + // Delete the backup + const { response: deleteResponse } = await client.DELETE( + "/backups/{backupId}", + { + params: { + path: { + backupId, + }, + }, + }, + ); + + expect(deleteResponse.status).toBe(204); + + // Verify it's deleted + const { response: getDeletedResponse } = await client.GET( + "/backups/{backupId}", + { + params: { + path: { + backupId, + }, + }, + }, + ); + + expect(getDeletedResponse.status).toBe(404); + }); + + it("should return 404 for non-existent backup", async () => { + const { response } = await client.GET("/backups/{backupId}", { + params: { + path: { + backupId: "non-existent-backup-id", + }, + }, + }); + + expect(response.status).toBe(404); + }); + + it("should return 404 when deleting non-existent backup", async () => { + const { response } = await client.DELETE("/backups/{backupId}", { + params: { + path: { + backupId: "non-existent-backup-id", + }, + }, + }); + + expect(response.status).toBe(404); + }); + + it("should handle multiple backups", async () => { + // Trigger multiple backups + const { data: backup1 } = await client.POST("/backups"); + const { data: backup2 } = await client.POST("/backups"); + + expect(backup1).toBeDefined(); + expect(backup2).toBeDefined(); + expect(backup1!.id).not.toBe(backup2!.id); + + // Get all backups + const { data: backupsData, response } = await client.GET("/backups"); + + expect(response.status).toBe(200); + expect(backupsData).toBeDefined(); + expect(backupsData!.backups).toBeDefined(); + expect(Array.isArray(backupsData!.backups)).toBe(true); + expect(backupsData!.backups.length).toBeGreaterThanOrEqual(2); + expect(backupsData!.backups.some((b) => b.id === backup1!.id)).toBe(true); + expect(backupsData!.backups.some((b) => b.id === backup2!.id)).toBe(true); + }); + + it("should validate full backup lifecycle", async () => { + // Step 1: Create some test bookmarks + const bookmarks = []; + for (let i = 0; i < 3; i++) { + const { data: bookmark } = await client.POST("/bookmarks", { + body: { + type: "text", + title: `Test Bookmark ${i + 1}`, + text: `This is test bookmark number ${i + 1}`, + }, + }); + expect(bookmark).toBeDefined(); + bookmarks.push(bookmark!); + } + + // Step 2: Trigger a backup + const { data: createdBackup, response: createResponse } = + await client.POST("/backups"); + + expect(createResponse.status).toBe(201); + expect(createdBackup).toBeDefined(); + expect(createdBackup!.id).toBeDefined(); + expect(createdBackup!.status).toBe("pending"); + expect(createdBackup!.bookmarkCount).toBe(0); + expect(createdBackup!.size).toBe(0); + + const backupId = createdBackup!.id; + + // Step 3: Poll until backup is completed or failed + let backup; + let attempts = 0; + const maxAttempts = 60; // Wait up to 60 seconds + const pollInterval = 1000; // Poll every second + + while (attempts < maxAttempts) { + const { data: currentBackup } = await client.GET("/backups/{backupId}", { + params: { + path: { + backupId, + }, + }, + }); + + backup = currentBackup; + + if (backup!.status === "success" || backup!.status === "failure") { + break; + } + + await new Promise((resolve) => setTimeout(resolve, pollInterval)); + attempts++; + } + + // Step 4: Verify backup completed successfully + expect(backup).toBeDefined(); + expect(backup!.status).toBe("success"); + expect(backup!.bookmarkCount).toBeGreaterThanOrEqual(3); + expect(backup!.size).toBeGreaterThan(0); + expect(backup!.errorMessage).toBeNull(); + + // Step 5: Download the backup + const downloadResponse = await fetch( + `http://localhost:${port}/api/v1/backups/${backupId}/download`, + { + headers: { + authorization: `Bearer ${apiKey}`, + }, + }, + ); + + expect(downloadResponse.status).toBe(200); + expect(downloadResponse.headers.get("content-type")).toContain( + "application/zip", + ); + + const backupBlob = await downloadResponse.blob(); + expect(backupBlob.size).toBeGreaterThan(0); + expect(backupBlob.size).toBe(backup!.size); + + // Step 6: Unzip and validate the backup contents + const arrayBuffer = await backupBlob.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + + // Verify it's a valid ZIP file (starts with PK signature) + expect(buffer[0]).toBe(0x50); // 'P' + expect(buffer[1]).toBe(0x4b); // 'K' + + // Unzip the backup file + const zip = new AdmZip(buffer); + const zipEntries = zip.getEntries(); + + // Should contain exactly one JSON file + expect(zipEntries.length).toBe(1); + const jsonEntry = zipEntries[0]; + expect(jsonEntry.entryName).toMatch(/^karakeep-backup-.*\.json$/); + + // Extract and parse the JSON content + const jsonContent = jsonEntry.getData().toString("utf8"); + const backupData = JSON.parse(jsonContent); + + // Validate the backup structure + expect(backupData).toBeDefined(); + expect(backupData.bookmarks).toBeDefined(); + expect(Array.isArray(backupData.bookmarks)).toBe(true); + expect(backupData.bookmarks.length).toBeGreaterThanOrEqual(3); + + // Validate that our test bookmarks are in the backup + const backupTitles = backupData.bookmarks.map( + (b: { title: string }) => b.title, + ); + expect(backupTitles).toContain("Test Bookmark 1"); + expect(backupTitles).toContain("Test Bookmark 2"); + expect(backupTitles).toContain("Test Bookmark 3"); + + // Validate bookmark structure + const firstBookmark = backupData.bookmarks[0]; + expect(firstBookmark).toHaveProperty("content"); + expect(firstBookmark.content).toHaveProperty("type"); + + // Step 7: Verify the backup appears in the list with updated status + const { data: backupsData } = await client.GET("/backups"); + const listedBackup = backupsData!.backups.find((b) => b.id === backupId); + + expect(listedBackup).toBeDefined(); + expect(listedBackup!.status).toBe("success"); + expect(listedBackup!.bookmarkCount).toBe(backup!.bookmarkCount); + expect(listedBackup!.size).toBe(backup!.size); + }); +}); diff --git a/packages/open-api/index.ts b/packages/open-api/index.ts index 6f14807d..18f3cf75 100644 --- a/packages/open-api/index.ts +++ b/packages/open-api/index.ts @@ -7,6 +7,7 @@ import { import { registry as adminRegistry } from "./lib/admin"; import { registry as assetsRegistry } from "./lib/assets"; +import { registry as backupsRegistry } from "./lib/backups"; import { registry as bookmarksRegistry } from "./lib/bookmarks"; import { registry as commonRegistry } from "./lib/common"; import { registry as highlightsRegistry } from "./lib/highlights"; @@ -24,6 +25,7 @@ function getOpenApiDocumentation() { userRegistry, assetsRegistry, adminRegistry, + backupsRegistry, ]); const generator = new OpenApiGeneratorV3(registry.definitions); diff --git a/packages/open-api/karakeep-openapi-spec.json b/packages/open-api/karakeep-openapi-spec.json index c151f42d..4532cd98 100644 --- a/packages/open-api/karakeep-openapi-spec.json +++ b/packages/open-api/karakeep-openapi-spec.json @@ -45,6 +45,10 @@ "type": "string", "example": "ieidlxygmwj87oxz5hxttoc8" }, + "BackupId": { + "type": "string", + "example": "ieidlxygmwj87oxz5hxttoc8" + }, "Bookmark": { "type": "object", "properties": { @@ -596,6 +600,14 @@ "required": true, "name": "assetId", "in": "path" + }, + "BackupId": { + "schema": { + "$ref": "#/components/schemas/BackupId" + }, + "required": true, + "name": "backupId", + "in": "path" } } }, @@ -3703,6 +3715,341 @@ } } } + }, + "/backups": { + "get": { + "description": "Get all backups", + "summary": "Get all backups", + "tags": [ + "Backups" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "responses": { + "200": { + "description": "Object with all backups data.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "backups": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "userId": { + "type": "string" + }, + "assetId": { + "type": "string", + "nullable": true + }, + "createdAt": { + "type": "string" + }, + "size": { + "type": "number" + }, + "bookmarkCount": { + "type": "number" + }, + "status": { + "type": "string", + "enum": [ + "pending", + "success", + "failure" + ] + }, + "errorMessage": { + "type": "string", + "nullable": true + } + }, + "required": [ + "id", + "userId", + "assetId", + "createdAt", + "size", + "bookmarkCount", + "status" + ] + } + } + }, + "required": [ + "backups" + ] + } + } + } + } + } + }, + "post": { + "description": "Trigger a new backup", + "summary": "Trigger a new backup", + "tags": [ + "Backups" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "responses": { + "201": { + "description": "Backup created successfully", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "userId": { + "type": "string" + }, + "assetId": { + "type": "string", + "nullable": true + }, + "createdAt": { + "type": "string" + }, + "size": { + "type": "number" + }, + "bookmarkCount": { + "type": "number" + }, + "status": { + "type": "string", + "enum": [ + "pending", + "success", + "failure" + ] + }, + "errorMessage": { + "type": "string", + "nullable": true + } + }, + "required": [ + "id", + "userId", + "assetId", + "createdAt", + "size", + "bookmarkCount", + "status" + ] + } + } + } + } + } + } + }, + "/backups/{backupId}": { + "get": { + "description": "Get backup by its id", + "summary": "Get a single backup", + "tags": [ + "Backups" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "parameters": [ + { + "$ref": "#/components/parameters/BackupId" + } + ], + "responses": { + "200": { + "description": "Object with backup data.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "userId": { + "type": "string" + }, + "assetId": { + "type": "string", + "nullable": true + }, + "createdAt": { + "type": "string" + }, + "size": { + "type": "number" + }, + "bookmarkCount": { + "type": "number" + }, + "status": { + "type": "string", + "enum": [ + "pending", + "success", + "failure" + ] + }, + "errorMessage": { + "type": "string", + "nullable": true + } + }, + "required": [ + "id", + "userId", + "assetId", + "createdAt", + "size", + "bookmarkCount", + "status" + ] + } + } + } + }, + "404": { + "description": "Backup not found", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "code": { + "type": "string" + }, + "message": { + "type": "string" + } + }, + "required": [ + "code", + "message" + ] + } + } + } + } + } + }, + "delete": { + "description": "Delete backup by its id", + "summary": "Delete a backup", + "tags": [ + "Backups" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "parameters": [ + { + "$ref": "#/components/parameters/BackupId" + } + ], + "responses": { + "204": { + "description": "No content - the backup was deleted" + }, + "404": { + "description": "Backup not found", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "code": { + "type": "string" + }, + "message": { + "type": "string" + } + }, + "required": [ + "code", + "message" + ] + } + } + } + } + } + } + }, + "/backups/{backupId}/download": { + "get": { + "description": "Download backup file", + "summary": "Download a backup", + "tags": [ + "Backups" + ], + "security": [ + { + "bearerAuth": [] + } + ], + "parameters": [ + { + "$ref": "#/components/parameters/BackupId" + } + ], + "responses": { + "200": { + "description": "Backup file (zip archive)", + "content": { + "application/zip": { + "schema": {} + } + } + }, + "404": { + "description": "Backup not found", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "code": { + "type": "string" + }, + "message": { + "type": "string" + } + }, + "required": [ + "code", + "message" + ] + } + } + } + } + } + } } } }
\ No newline at end of file diff --git a/packages/open-api/lib/backups.ts b/packages/open-api/lib/backups.ts new file mode 100644 index 00000000..0ad29057 --- /dev/null +++ b/packages/open-api/lib/backups.ts @@ -0,0 +1,149 @@ +import { + extendZodWithOpenApi, + OpenAPIRegistry, +} from "@asteasolutions/zod-to-openapi"; +import { z } from "zod"; + +import { zBackupSchema } from "@karakeep/shared/types/backups"; + +import { BearerAuth } from "./common"; +import { ErrorSchema } from "./errors"; + +export const registry = new OpenAPIRegistry(); +extendZodWithOpenApi(z); + +export const BackupIdSchema = registry.registerParameter( + "BackupId", + z.string().openapi({ + param: { + name: "backupId", + in: "path", + }, + example: "ieidlxygmwj87oxz5hxttoc8", + }), +); + +registry.registerPath({ + method: "get", + path: "/backups", + description: "Get all backups", + summary: "Get all backups", + tags: ["Backups"], + security: [{ [BearerAuth.name]: [] }], + responses: { + 200: { + description: "Object with all backups data.", + content: { + "application/json": { + schema: z.object({ + backups: z.array(zBackupSchema), + }), + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "post", + path: "/backups", + description: "Trigger a new backup", + summary: "Trigger a new backup", + tags: ["Backups"], + security: [{ [BearerAuth.name]: [] }], + responses: { + 201: { + description: "Backup created successfully", + content: { + "application/json": { + schema: zBackupSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "get", + path: "/backups/{backupId}", + description: "Get backup by its id", + summary: "Get a single backup", + tags: ["Backups"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ backupId: BackupIdSchema }), + }, + responses: { + 200: { + description: "Object with backup data.", + content: { + "application/json": { + schema: zBackupSchema, + }, + }, + }, + 404: { + description: "Backup not found", + content: { + "application/json": { + schema: ErrorSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "get", + path: "/backups/{backupId}/download", + description: "Download backup file", + summary: "Download a backup", + tags: ["Backups"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ backupId: BackupIdSchema }), + }, + responses: { + 200: { + description: "Backup file (zip archive)", + content: { + "application/zip": { + schema: z.instanceof(Blob), + }, + }, + }, + 404: { + description: "Backup not found", + content: { + "application/json": { + schema: ErrorSchema, + }, + }, + }, + }, +}); + +registry.registerPath({ + method: "delete", + path: "/backups/{backupId}", + description: "Delete backup by its id", + summary: "Delete a backup", + tags: ["Backups"], + security: [{ [BearerAuth.name]: [] }], + request: { + params: z.object({ backupId: BackupIdSchema }), + }, + responses: { + 204: { + description: "No content - the backup was deleted", + }, + 404: { + description: "Backup not found", + content: { + "application/json": { + schema: ErrorSchema, + }, + }, + }, + }, +}); diff --git a/packages/sdk/src/karakeep-api.d.ts b/packages/sdk/src/karakeep-api.d.ts index 7960e982..d327f1fa 100644 --- a/packages/sdk/src/karakeep-api.d.ts +++ b/packages/sdk/src/karakeep-api.d.ts @@ -68,6 +68,16 @@ export interface paths { /** @enum {string} */ crawlPriority?: "low" | "normal"; importSessionId?: string; + /** @enum {string} */ + source?: + | "api" + | "web" + | "cli" + | "mobile" + | "extension" + | "singlefile" + | "rss" + | "import"; } & ( | { /** @enum {string} */ @@ -322,6 +332,18 @@ export interface paths { summarizationStatus: "success" | "failure" | "pending" | null; note?: string | null; summary?: string | null; + /** @enum {string|null} */ + source?: + | "api" + | "web" + | "cli" + | "mobile" + | "extension" + | "singlefile" + | "rss" + | "import" + | null; + userId: string; }; }; }; @@ -384,6 +406,18 @@ export interface paths { summarizationStatus: "success" | "failure" | "pending" | null; note?: string | null; summary?: string | null; + /** @enum {string|null} */ + source?: + | "api" + | "web" + | "cli" + | "mobile" + | "extension" + | "singlefile" + | "rss" + | "import" + | null; + userId: string; }; }; }; @@ -668,6 +702,7 @@ export interface paths { | "video" | "bookmarkAsset" | "precrawledArchive" + | "userUploaded" | "unknown"; }; }; @@ -691,7 +726,9 @@ export interface paths { | "video" | "bookmarkAsset" | "precrawledArchive" + | "userUploaded" | "unknown"; + fileName?: string | null; }; }; }; @@ -1684,6 +1721,7 @@ export interface paths { "application/json": { /** @enum {string} */ color?: "yellow" | "red" | "green" | "blue"; + note?: string | null; }; }; }; @@ -1822,6 +1860,20 @@ export interface paths { name: string; count: number; }[]; + bookmarksBySource: { + /** @enum {string|null} */ + source: + | "api" + | "web" + | "cli" + | "mobile" + | "extension" + | "singlefile" + | "rss" + | "import" + | null; + count: number; + }[]; }; }; }; @@ -2017,6 +2069,241 @@ export interface paths { patch?: never; trace?: never; }; + "/backups": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * Get all backups + * @description Get all backups + */ + get: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Object with all backups data. */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + backups: { + id: string; + userId: string; + assetId: string; + createdAt: string; + size: number; + bookmarkCount: number; + /** @enum {string} */ + status: "pending" | "success" | "failure"; + errorMessage?: string | null; + }[]; + }; + }; + }; + }; + }; + put?: never; + /** + * Trigger a new backup + * @description Trigger a new backup + */ + post: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Backup created successfully */ + 201: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id: string; + userId: string; + assetId: string; + createdAt: string; + size: number; + bookmarkCount: number; + /** @enum {string} */ + status: "pending" | "success" | "failure"; + errorMessage?: string | null; + }; + }; + }; + }; + }; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/backups/{backupId}": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * Get a single backup + * @description Get backup by its id + */ + get: { + parameters: { + query?: never; + header?: never; + path: { + backupId: components["parameters"]["BackupId"]; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Object with backup data. */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id: string; + userId: string; + assetId: string; + createdAt: string; + size: number; + bookmarkCount: number; + /** @enum {string} */ + status: "pending" | "success" | "failure"; + errorMessage?: string | null; + }; + }; + }; + /** @description Backup not found */ + 404: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + code: string; + message: string; + }; + }; + }; + }; + }; + put?: never; + post?: never; + /** + * Delete a backup + * @description Delete backup by its id + */ + delete: { + parameters: { + query?: never; + header?: never; + path: { + backupId: components["parameters"]["BackupId"]; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description No content - the backup was deleted */ + 204: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + /** @description Backup not found */ + 404: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + code: string; + message: string; + }; + }; + }; + }; + }; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/backups/{backupId}/download": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * Download a backup + * @description Download backup file + */ + get: { + parameters: { + query?: never; + header?: never; + path: { + backupId: components["parameters"]["BackupId"]; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Backup file (zip archive) */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/zip": unknown; + }; + }; + /** @description Backup not found */ + 404: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + code: string; + message: string; + }; + }; + }; + }; + }; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; } export type webhooks = Record<string, never>; export interface components { @@ -2031,6 +2318,8 @@ export interface components { HighlightId: string; /** @example ieidlxygmwj87oxz5hxttoc8 */ AssetId: string; + /** @example ieidlxygmwj87oxz5hxttoc8 */ + BackupId: string; Bookmark: { id: string; createdAt: string; @@ -2044,6 +2333,18 @@ export interface components { summarizationStatus: "success" | "failure" | "pending" | null; note?: string | null; summary?: string | null; + /** @enum {string|null} */ + source?: + | "api" + | "web" + | "cli" + | "mobile" + | "extension" + | "singlefile" + | "rss" + | "import" + | null; + userId: string; tags: { id: string; name: string; @@ -2105,7 +2406,9 @@ export interface components { | "video" | "bookmarkAsset" | "precrawledArchive" + | "userUploaded" | "unknown"; + fileName?: string | null; }[]; }; PaginatedBookmarks: { @@ -2126,6 +2429,9 @@ export interface components { type: "manual" | "smart"; query?: string | null; public: boolean; + hasCollaborators: boolean; + /** @enum {string} */ + userRole: "owner" | "editor" | "viewer" | "public"; }; Highlight: { bookmarkId: string; @@ -2170,6 +2476,7 @@ export interface components { TagId: components["schemas"]["TagId"]; HighlightId: components["schemas"]["HighlightId"]; AssetId: components["schemas"]["AssetId"]; + BackupId: components["schemas"]["BackupId"]; }; requestBodies: never; headers: never; diff --git a/packages/shared-server/src/queues.ts b/packages/shared-server/src/queues.ts index 742ebd4d..0748bd92 100644 --- a/packages/shared-server/src/queues.ts +++ b/packages/shared-server/src/queues.ts @@ -234,3 +234,19 @@ export async function triggerRuleEngineOnEvent( opts, ); } + +// Backup worker +export const zBackupRequestSchema = z.object({ + userId: z.string(), + backupId: z.string().optional(), +}); +export type ZBackupRequest = z.infer<typeof zBackupRequestSchema>; +export const BackupQueue = QUEUE_CLIENT.createQueue<ZBackupRequest>( + "backup_queue", + { + defaultJobArgs: { + numRetries: 2, + }, + keepFailedJobs: false, + }, +); diff --git a/packages/shared/assetdb.ts b/packages/shared/assetdb.ts index ff87e847..2e22faf7 100644 --- a/packages/shared/assetdb.ts +++ b/packages/shared/assetdb.ts @@ -26,6 +26,7 @@ export const enum ASSET_TYPES { IMAGE_PNG = "image/png", IMAGE_WEBP = "image/webp", APPLICATION_PDF = "application/pdf", + APPLICATION_ZIP = "application/zip", TEXT_HTML = "text/html", VIDEO_MP4 = "video/mp4", @@ -65,6 +66,7 @@ export const SUPPORTED_ASSET_TYPES: Set<string> = new Set<string>([ ...SUPPORTED_UPLOAD_ASSET_TYPES, ASSET_TYPES.TEXT_HTML, ASSET_TYPES.VIDEO_MP4, + ASSET_TYPES.APPLICATION_ZIP, ]); export const zAssetMetadataSchema = z.object({ diff --git a/packages/shared/types/backups.ts b/packages/shared/types/backups.ts new file mode 100644 index 00000000..f54d4824 --- /dev/null +++ b/packages/shared/types/backups.ts @@ -0,0 +1,14 @@ +import { z } from "zod"; + +export const zBackupSchema = z.object({ + id: z.string(), + userId: z.string(), + assetId: z.string().nullable(), + createdAt: z.date(), + size: z.number(), + bookmarkCount: z.number(), + status: z.enum(["pending", "success", "failure"]), + errorMessage: z.string().nullable().optional(), +}); + +export type ZBackup = z.infer<typeof zBackupSchema>; diff --git a/packages/shared/types/users.ts b/packages/shared/types/users.ts index 830fe87b..2fad4f83 100644 --- a/packages/shared/types/users.ts +++ b/packages/shared/types/users.ts @@ -108,6 +108,9 @@ export const zUserSettingsSchema = z.object({ ]), archiveDisplayBehaviour: z.enum(["show", "hide"]), timezone: z.string(), + backupsEnabled: z.boolean(), + backupsFrequency: z.enum(["daily", "weekly"]), + backupsRetentionDays: z.number().int().min(1).max(365), }); export type ZUserSettings = z.infer<typeof zUserSettingsSchema>; @@ -116,4 +119,13 @@ export const zUpdateUserSettingsSchema = zUserSettingsSchema.partial().pick({ bookmarkClickAction: true, archiveDisplayBehaviour: true, timezone: true, + backupsEnabled: true, + backupsFrequency: true, + backupsRetentionDays: true, +}); + +export const zUpdateBackupSettingsSchema = zUpdateUserSettingsSchema.pick({ + backupsEnabled: true, + backupsFrequency: true, + backupsRetentionDays: true, }); diff --git a/packages/trpc/lib/attachments.ts b/packages/trpc/lib/attachments.ts index 7a4e2668..25d9be94 100644 --- a/packages/trpc/lib/attachments.ts +++ b/packages/trpc/lib/attachments.ts @@ -17,6 +17,7 @@ export function mapDBAssetTypeToUserType(assetType: AssetTypes): ZAssetType { [AssetTypes.LINK_HTML_CONTENT]: "linkHtmlContent", [AssetTypes.BOOKMARK_ASSET]: "bookmarkAsset", [AssetTypes.USER_UPLOADED]: "userUploaded", + [AssetTypes.BACKUP]: "unknown", // Backups are not displayed as regular assets [AssetTypes.UNKNOWN]: "bannerImage", }; return map[assetType]; diff --git a/packages/trpc/models/backups.ts b/packages/trpc/models/backups.ts new file mode 100644 index 00000000..c7ab99ba --- /dev/null +++ b/packages/trpc/models/backups.ts @@ -0,0 +1,172 @@ +import { TRPCError } from "@trpc/server"; +import { and, desc, eq, lt } from "drizzle-orm"; +import { z } from "zod"; + +import { assets, backupsTable } from "@karakeep/db/schema"; +import { BackupQueue } from "@karakeep/shared-server"; +import { deleteAsset } from "@karakeep/shared/assetdb"; +import { zBackupSchema } from "@karakeep/shared/types/backups"; + +import { AuthedContext } from ".."; + +export class Backup { + private constructor( + private ctx: AuthedContext, + private backup: z.infer<typeof zBackupSchema>, + ) {} + + static async fromId(ctx: AuthedContext, backupId: string): Promise<Backup> { + const backup = await ctx.db.query.backupsTable.findFirst({ + where: and( + eq(backupsTable.id, backupId), + eq(backupsTable.userId, ctx.user.id), + ), + }); + + if (!backup) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Backup not found", + }); + } + + return new Backup(ctx, backup); + } + + private static fromData( + ctx: AuthedContext, + backup: z.infer<typeof zBackupSchema>, + ): Backup { + return new Backup(ctx, backup); + } + + static async getAll(ctx: AuthedContext): Promise<Backup[]> { + const backups = await ctx.db.query.backupsTable.findMany({ + where: eq(backupsTable.userId, ctx.user.id), + orderBy: [desc(backupsTable.createdAt)], + }); + + return backups.map((b) => new Backup(ctx, b)); + } + + static async create(ctx: AuthedContext): Promise<Backup> { + const [backup] = await ctx.db + .insert(backupsTable) + .values({ + userId: ctx.user.id, + size: 0, + bookmarkCount: 0, + status: "pending", + }) + .returning(); + return new Backup(ctx, backup!); + } + + async triggerBackgroundJob({ + delayMs, + idempotencyKey, + }: { delayMs?: number; idempotencyKey?: string } = {}): Promise<void> { + await BackupQueue.enqueue( + { + userId: this.ctx.user.id, + backupId: this.backup.id, + }, + { + delayMs, + idempotencyKey, + }, + ); + } + + /** + * Generic update method for backup records + */ + async update( + data: Partial<{ + size: number; + bookmarkCount: number; + status: "pending" | "success" | "failure"; + assetId: string | null; + errorMessage: string | null; + }>, + ): Promise<void> { + await this.ctx.db + .update(backupsTable) + .set(data) + .where( + and( + eq(backupsTable.id, this.backup.id), + eq(backupsTable.userId, this.ctx.user.id), + ), + ); + + // Update local state + this.backup = { ...this.backup, ...data }; + } + + async delete(): Promise<void> { + if (this.backup.assetId) { + // Delete asset + await deleteAsset({ + userId: this.ctx.user.id, + assetId: this.backup.assetId, + }); + } + + await this.ctx.db.transaction(async (db) => { + // Delete asset first + if (this.backup.assetId) { + await db + .delete(assets) + .where( + and( + eq(assets.id, this.backup.assetId), + eq(assets.userId, this.ctx.user.id), + ), + ); + } + + // Delete backup record + await db + .delete(backupsTable) + .where( + and( + eq(backupsTable.id, this.backup.id), + eq(backupsTable.userId, this.ctx.user.id), + ), + ); + }); + } + + /** + * Finds backups older than the retention period + */ + static async findOldBackups( + ctx: AuthedContext, + retentionDays: number, + ): Promise<Backup[]> { + const cutoffDate = new Date(); + cutoffDate.setDate(cutoffDate.getDate() - retentionDays); + + const oldBackups = await ctx.db.query.backupsTable.findMany({ + where: and( + eq(backupsTable.userId, ctx.user.id), + lt(backupsTable.createdAt, cutoffDate), + ), + }); + + return oldBackups.map((backup) => Backup.fromData(ctx, backup)); + } + + asPublic(): z.infer<typeof zBackupSchema> { + return this.backup; + } + + get id() { + return this.backup.id; + } + + get assetId() { + return this.backup.assetId; + } +} diff --git a/packages/trpc/models/users.ts b/packages/trpc/models/users.ts index 97b062f0..a1f32f02 100644 --- a/packages/trpc/models/users.ts +++ b/packages/trpc/models/users.ts @@ -430,6 +430,9 @@ export class User { bookmarkClickAction: true, archiveDisplayBehaviour: true, timezone: true, + backupsEnabled: true, + backupsFrequency: true, + backupsRetentionDays: true, }, }); @@ -444,6 +447,9 @@ export class User { bookmarkClickAction: settings.bookmarkClickAction, archiveDisplayBehaviour: settings.archiveDisplayBehaviour, timezone: settings.timezone || "UTC", + backupsEnabled: settings.backupsEnabled, + backupsFrequency: settings.backupsFrequency, + backupsRetentionDays: settings.backupsRetentionDays, }; } @@ -463,6 +469,9 @@ export class User { bookmarkClickAction: input.bookmarkClickAction, archiveDisplayBehaviour: input.archiveDisplayBehaviour, timezone: input.timezone, + backupsEnabled: input.backupsEnabled, + backupsFrequency: input.backupsFrequency, + backupsRetentionDays: input.backupsRetentionDays, }) .where(eq(users.id, this.user.id)); } diff --git a/packages/trpc/routers/_app.ts b/packages/trpc/routers/_app.ts index 1d548ee4..bae69130 100644 --- a/packages/trpc/routers/_app.ts +++ b/packages/trpc/routers/_app.ts @@ -2,6 +2,7 @@ import { router } from "../index"; import { adminAppRouter } from "./admin"; import { apiKeysAppRouter } from "./apiKeys"; import { assetsAppRouter } from "./assets"; +import { backupsAppRouter } from "./backups"; import { bookmarksAppRouter } from "./bookmarks"; import { feedsAppRouter } from "./feeds"; import { highlightsAppRouter } from "./highlights"; @@ -25,6 +26,7 @@ export const appRouter = router({ prompts: promptsAppRouter, admin: adminAppRouter, feeds: feedsAppRouter, + backups: backupsAppRouter, highlights: highlightsAppRouter, importSessions: importSessionsRouter, webhooks: webhooksAppRouter, diff --git a/packages/trpc/routers/backups.ts b/packages/trpc/routers/backups.ts new file mode 100644 index 00000000..7a7a9896 --- /dev/null +++ b/packages/trpc/routers/backups.ts @@ -0,0 +1,54 @@ +import { z } from "zod"; + +import { zBackupSchema } from "@karakeep/shared/types/backups"; + +import { authedProcedure, createRateLimitMiddleware, router } from "../index"; +import { Backup } from "../models/backups"; + +export const backupsAppRouter = router({ + list: authedProcedure + .output(z.object({ backups: z.array(zBackupSchema) })) + .query(async ({ ctx }) => { + const backups = await Backup.getAll(ctx); + return { backups: backups.map((b) => b.asPublic()) }; + }), + + get: authedProcedure + .input( + z.object({ + backupId: z.string(), + }), + ) + .output(zBackupSchema) + .query(async ({ ctx, input }) => { + const backup = await Backup.fromId(ctx, input.backupId); + return backup.asPublic(); + }), + + delete: authedProcedure + .input( + z.object({ + backupId: z.string(), + }), + ) + .mutation(async ({ input, ctx }) => { + const backup = await Backup.fromId(ctx, input.backupId); + await backup.delete(); + }), + + triggerBackup: authedProcedure + .use( + createRateLimitMiddleware({ + name: "backups.triggerBackup", + windowMs: 60 * 60 * 1000, // 1 hour window + maxRequests: 5, // Max 5 backup triggers per hour + }), + ) + .output(zBackupSchema) + .mutation(async ({ ctx }) => { + const backup = await Backup.create(ctx); + await backup.triggerBackgroundJob(); + + return backup.asPublic(); + }), +}); diff --git a/packages/trpc/routers/users.test.ts b/packages/trpc/routers/users.test.ts index 3b16e1a4..a2f2be9f 100644 --- a/packages/trpc/routers/users.test.ts +++ b/packages/trpc/routers/users.test.ts @@ -155,11 +155,17 @@ describe("User Routes", () => { bookmarkClickAction: "open_original_link", archiveDisplayBehaviour: "show", timezone: "UTC", + backupsEnabled: false, + backupsFrequency: "weekly", + backupsRetentionDays: 30, }); // Update settings await caller.users.updateSettings({ bookmarkClickAction: "expand_bookmark_preview", + backupsEnabled: true, + backupsFrequency: "daily", + backupsRetentionDays: 7, }); // Verify updated settings @@ -168,6 +174,9 @@ describe("User Routes", () => { bookmarkClickAction: "expand_bookmark_preview", archiveDisplayBehaviour: "show", timezone: "UTC", + backupsEnabled: true, + backupsFrequency: "daily", + backupsRetentionDays: 7, }); // Test invalid update (e.g., empty input, if schema enforces it) |
