Skip to content

Commit

Permalink
feat: db cleaner (#151)
Browse files Browse the repository at this point in the history
* feat: db cleaner

Signed-off-by: Innei <i@innei.in>

* chore: add test case

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* refactor: data struct

Signed-off-by: Innei <i@innei.in>

* feat: cleaner old data

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei authored Jul 23, 2024
1 parent 01e087b commit 62aae0d
Show file tree
Hide file tree
Showing 40 changed files with 610 additions and 190 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"electron-vite": "^2.3.0",
"eslint": "^9.7.0",
"eslint-config-hyoban": "3.0.0-beta.27",
"fake-indexeddb": "6.0.0",
"hono": "4.4.7",
"lint-staged": "15.2.7",
"postcss": "8.4.39",
Expand All @@ -157,6 +158,7 @@
"tailwindcss": "3.4.6",
"typescript": "^5.5.3",
"vite": "^5.3.4",
"vite-tsconfig-paths": "4.3.2",
"vitest": "2.0.3"
},
"pnpm": {
Expand Down
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions setup-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "fake-indexeddb/auto"
38 changes: 23 additions & 15 deletions src/renderer/src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import type { Transaction } from "dexie"
import Dexie from "dexie"

import { LOCAL_DB_NAME } from "./constants"
import { dbSchemaV1, dbSchemaV2, dbSchemaV3 } from "./db_schema"
import type { DB_Base } from "./schemas/base"
import type { DB_FeedId } from "./schemas/feed"
import type { DBModel } from "./types"
import {
dbSchemaV1,
dbSchemaV2,
dbSchemaV3,
dbSchemaV4,
dbSchemaV5,
} from "./db_schema"
import type { DB_Cleaner } from "./schemas/cleaner"
import type { DB_Entry, DB_EntryRelated } from "./schemas/entry"
import type { DB_Feed, DB_FeedUnread } from "./schemas/feed"
import type { DB_Subscription } from "./schemas/subscription"

export interface LocalDBSchemaMap {
entries: DB_Base
feeds: DB_Base
subscriptions: DB_Base
entryRelated: DB_Base
feedEntries: DB_FeedId
feedUnreads: DB_FeedId
entries: DB_Entry
feeds: DB_Feed
subscriptions: DB_Subscription
entryRelated: DB_EntryRelated
feedUnreads: DB_FeedUnread
cleaner: DB_Cleaner
}

// Define a local DB
Expand All @@ -22,22 +29,23 @@ export class BrowserDB extends Dexie {
public feeds: BrowserDBTable<"feeds">
public subscriptions: BrowserDBTable<"subscriptions">
public entryRelated: BrowserDBTable<"entryRelated">
public feedEntries: BrowserDBTable<"feedEntries">
public feedUnreads: BrowserDBTable<"feedUnreads">
public cleaner: BrowserDBTable<"cleaner">

constructor() {
super(LOCAL_DB_NAME)
this.version(1).stores(dbSchemaV1)
this.version(2).stores(dbSchemaV2)
.upgrade(this.upgradeToV2)
this.version(2).stores(dbSchemaV2).upgrade(this.upgradeToV2)
this.version(3).stores(dbSchemaV3)
this.version(4).stores(dbSchemaV4)
this.version(5).stores(dbSchemaV5)

this.entries = this.table("entries")
this.feeds = this.table("feeds")
this.subscriptions = this.table("subscriptions")
this.entryRelated = this.table("entryRelated")
this.feedEntries = this.table("feedEntries")
this.feedUnreads = this.table("feedUnreads")
this.cleaner = this.table("cleaner")
}

async upgradeToV2(trans: Transaction) {
Expand All @@ -58,7 +66,7 @@ export const browserDB = new BrowserDB()
export type BrowserDBSchema = {
[t in keyof LocalDBSchemaMap]: {
model: LocalDBSchemaMap[t]
table: Dexie.Table<DBModel<LocalDBSchemaMap[t]>, string>
table: Dexie.Table<LocalDBSchemaMap[t], string>
};
}
type BrowserDBTable<T extends keyof LocalDBSchemaMap> =
Expand Down
16 changes: 14 additions & 2 deletions src/renderer/src/database/db_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export const dbSchemaV1 = {
feeds: "&id",
subscriptions: "&id",
entryRelated: "&id",
feedEntries: "&feedId",
feedUnreads: "&id",
}

Expand All @@ -13,6 +12,19 @@ export const dbSchemaV2 = {
}

export const dbSchemaV3 = {
...dbSchemaV1,
...dbSchemaV2,
feedEntries: null,

subscriptions: "&id, userId, &feedId",
}

export const dbSchemaV4 = {
...dbSchemaV3,
entries: "&id, feedId",
subscriptions: "&id, userId, feedId",
}

export const dbSchemaV5 = {
...dbSchemaV4,
cleaner: "&refId, visitedAt",
}
1 change: 0 additions & 1 deletion src/renderer/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { browserDB } from "./db"

export * from "./db"
export * from "./models"
export * from "./schemas"

export const DB_NOT_READY_OR_DISABLED = "Database is not ready or disabled"
Expand Down
31 changes: 0 additions & 31 deletions src/renderer/src/database/model.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/entry-related.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/entry.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/feed-entry.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/feed.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/renderer/src/database/models/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/subscription.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/renderer/src/database/models/unread.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/renderer/src/database/models/user.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/renderer/src/database/schemas/cleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type DB_Cleaner = {
refId: string
visitedAt: number
type: "entry" | "feed"
}
8 changes: 8 additions & 0 deletions src/renderer/src/database/schemas/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { EntryModel } from "@renderer/models"

export type DB_Entry = EntryModel & { feedId: string }

export type DB_EntryRelated = {
id: string
data: any
}
11 changes: 6 additions & 5 deletions src/renderer/src/database/schemas/feed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { z } from "zod"
import type { FeedModel } from "@renderer/models"

export const DB_FeedIdSchema = z.object({
feedId: z.string(),
})
export type DB_FeedUnread = {
id: string
count: number
}

export type DB_FeedId = z.infer<typeof DB_FeedIdSchema>
export type DB_Feed = FeedModel & { id: string }
5 changes: 5 additions & 0 deletions src/renderer/src/database/schemas/subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SubscriptionFlatModel } from "@renderer/store/subscription"

export type DB_Subscription = SubscriptionFlatModel & {
id: string
}
5 changes: 0 additions & 5 deletions src/renderer/src/database/types.ts

This file was deleted.

Loading

0 comments on commit 62aae0d

Please sign in to comment.