diff options
| author | kamtschatka <simon.schatka@gmx.at> | 2024-06-30 00:34:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-29 23:34:43 +0100 |
| commit | ccbff18a9763e458c07f46cb3a331062df14a9b9 (patch) | |
| tree | 0cefc8cb49ea70594c6af7a947f3c21def1e7846 /packages | |
| parent | e107f8b6c250759ab0f884b2fdd0283fae15cfe5 (diff) | |
| download | karakeep-ccbff18a9763e458c07f46cb3a331062df14a9b9.tar.zst | |
refactor: remove redundant code from crawler worker and refactor handling of asset types (#253)
* refactoring asset types
Extracted out functions to silently delete assets and to update them after crawling
Generalized the mapping of assets to bookmark fields to make extending them easier
* revert silentDeleteAsset and hide better-sqlite3
---------
Co-authored-by: MohamedBassem <me@mbassem.com>
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/db/index.ts | 14 | ||||
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 50 |
2 files changed, 31 insertions, 33 deletions
diff --git a/packages/db/index.ts b/packages/db/index.ts index 433d8db2..b86665d2 100644 --- a/packages/db/index.ts +++ b/packages/db/index.ts @@ -1,3 +1,17 @@ +import Database from "better-sqlite3"; +import { ExtractTablesWithRelations } from "drizzle-orm"; +import { SQLiteTransaction } from "drizzle-orm/sqlite-core"; + +import * as schema from "./schema"; + export { db } from "./drizzle"; export * as schema from "./schema"; export { SqliteError } from "better-sqlite3"; + +// This is exported here to avoid leaking better-sqlite types outside of this package. +export type HoarderDBTransaction = SQLiteTransaction< + "sync", + Database.RunResult, + typeof schema, + ExtractTablesWithRelations<typeof schema> +>; diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 2175f704..8644bbcf 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -73,39 +73,23 @@ export const ensureBookmarkOwnership = experimental_trpcMiddleware<{ return opts.next(); }); -function assetTypeToBookmarkField( - asset: - | { - id: string; - assetType: AssetTypes; - } - | undefined, -) { - if (!asset) { - return undefined; - } - switch (asset.assetType) { - case AssetTypes.LINK_SCREENSHOT: - return { screenshotAssetId: asset.id }; - case AssetTypes.LINK_FULL_PAGE_ARCHIVE: - return { fullPageArchiveAssetId: asset.id }; - case AssetTypes.LINK_BANNER_IMAGE: - return { imageAssetId: asset.id }; - } +interface Asset { + id: string; + assetType: AssetTypes; } -function getBookmarkAssets(assets: { id: string; assetType: AssetTypes }[]) { - return { - ...assetTypeToBookmarkField( - assets.find((a) => a.assetType == AssetTypes.LINK_SCREENSHOT), - ), - ...assetTypeToBookmarkField( - assets.find((a) => a.assetType == AssetTypes.LINK_FULL_PAGE_ARCHIVE), - ), - ...assetTypeToBookmarkField( - assets.find((a) => a.assetType == AssetTypes.LINK_BANNER_IMAGE), - ), - }; +const ASSET_TYE_MAPPING: Record<AssetTypes, string> = { + [AssetTypes.LINK_SCREENSHOT]: "screenshotAssetId", + [AssetTypes.LINK_FULL_PAGE_ARCHIVE]: "fullPageArchiveAssetId", + [AssetTypes.LINK_BANNER_IMAGE]: "imageAssetId", +}; + +function mapAssetsToBookmarkFields(assets: Asset | Asset[] = []) { + const assetsArray = Array.isArray(assets) ? assets : [assets]; + return assetsArray.reduce((result: Record<string, string>, asset: Asset) => { + result[ASSET_TYE_MAPPING[asset.assetType]] = asset.id; + return result; + }, {}); } async function getBookmark(ctx: AuthedContext, bookmarkId: string) { @@ -196,7 +180,7 @@ function toZodSchema(bookmark: BookmarkQueryReturnType): ZBookmark { if (link) { content = { type: "link", - ...getBookmarkAssets(assets), + ...mapAssetsToBookmarkFields(assets), ...link, }; } else if (text) { @@ -616,7 +600,7 @@ export const bookmarksAppRouter = router({ if (row.assets) { acc[bookmarkId].content = { ...acc[bookmarkId].content, - ...assetTypeToBookmarkField(row.assets), + ...mapAssetsToBookmarkFields(row.assets), }; } |
