Skip to content

Commit

Permalink
refactor: 使用 sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Jul 14, 2024
1 parent 5e7245b commit 57584b1
Show file tree
Hide file tree
Showing 30 changed files with 9,234 additions and 8,212 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "1.8.2",
"private": true,
"packageManager": "pnpm@8.6.5",
"packageManager": "pnpm@9.4.0",
"description": "",
"author": "aooiuu <aooiu@qq.com>",
"license": "GPL3",
Expand Down
73 changes: 0 additions & 73 deletions packages/shared/src/RecordFile.ts

This file was deleted.

83 changes: 26 additions & 57 deletions packages/shared/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import type { Rule } from '@any-reader/rule-utils'
import { ContentType } from '@any-reader/rule-utils'
import { RuleManager } from '@any-reader/core'
import { LOCAL_BOOK_DIR } from './constants'
import * as ruleFileManager from './ruleFileManager'
import * as ruleExtraManager from './ruleExtraManager'
import { favoritesManager } from './favoritesManager'
import { historyManager } from './historyManager'
import type { BookChapter } from './localBookManager'
import localBookManager from './localBookManager'
import { db } from './database'
import * as ruleFileManager from './utils/ruleFileManager'
import * as ruleExtraManager from './utils/ruleExtraManager'
import type { BookChapter } from './utils/localBookManager'
import localBookManager from './utils/localBookManager'
import { db } from './data-source'
import { ChapterHistory } from './controller/ChapterHistory'
import { ResourceHistory } from './controller/ResourceHistory'
import { ResourceFavorites } from './controller/ResourceFavorites'

// 发现页分类
async function discoverMap(ruleId: string) {
Expand All @@ -31,22 +32,6 @@ async function discover({ ruleId, data }: any) {
return ruleManager.discover(data.value)
}

/**
* 收藏列表
* @returns
*/
function getFavorites() {
return favoritesManager.list()
}

/**
* 历史记录
* @returns
*/
function getHistory() {
return historyManager.list()
}

/**
* 本地书籍
* @returns
Expand All @@ -55,25 +40,6 @@ function getLocalBooks(dir: string) {
return localBookManager.getBookList(dir)
}

/**
* 收藏
* @param param0
* @returns
*/
async function star(data: any) {
await favoritesManager.add(data)
return true
}

/**
* 取消收藏
* @returns
*/
async function unstar(data: any) {
await favoritesManager.del(data)
return true
}

/**
* 规则列表
* @returns
Expand Down Expand Up @@ -249,7 +215,7 @@ export class Api {
async useApi(register: any) {
await db.initialize()

const registerApi = async (apiPath: string, handle: Function, log?: {
const registerApi = async (apiPath: string, handle: (...arg: any) => Promise<any>, log?: {
ruleId: (...arg: any) => string
check: (arg: any) => boolean
}) => {
Expand Down Expand Up @@ -293,17 +259,6 @@ export class Api {
registerApi('get@readConfig', async () => this.config)
registerApi('post@updateConfig', async (data: any) => await this.updateConfig(data))

// 历史记录
registerApi('get@getHistory', async () => await getHistory())
registerApi('post@history/remove', async ({ ruleId, url }: { ruleId: string; url: string }) => historyManager.del({ url } as any, { id: ruleId } as any))
registerApi('post@history/add', async (data: any) => await historyManager.add(data))
registerApi('post@history/del', async (data: any) => await historyManager.del(data))

// 收藏
registerApi('get@getFavorites', async () => await getFavorites())
registerApi('post@star', async (data: any) => await star(data))
registerApi('post@unstar', async (data: any) => await unstar(data))

// 本地
registerApi('get@getLocalBooks', async () => await getLocalBooks(this.bookDir))

Expand All @@ -320,8 +275,22 @@ export class Api {
registerApi('post@importCMS', async (data: any) => await ruleFileManager.importCMS(data))
registerApi('post@ping', async (data: any) => await ping(data))

// 章节历史记录
registerApi('post@chapterHistory/save', async (data: any) => await db.getChapterHistory().save(data))
registerApi('post@chapterHistory/list', async (data: any) => await db.getChapterHistory().list(data))
// 章节进度记录
const chapter = new ChapterHistory(db)
registerApi('post@chapter-history/list', chapter.list.bind(chapter))
registerApi('post@chapter-history/save', chapter.save.bind(chapter))

// 历史记录
const resourceHistory = new ResourceHistory(db)
registerApi('post@resource-history/list', resourceHistory.list.bind(resourceHistory))
registerApi('post@resource-history/remove', resourceHistory.remove.bind(resourceHistory))
registerApi('post@resource-history/add', resourceHistory.create.bind(resourceHistory))
registerApi('post@resource-history/del', resourceHistory.remove.bind(resourceHistory))

// 收藏
const resourceFavorites = new ResourceFavorites(db)
registerApi('post@resource-favorites/list', resourceFavorites.list.bind(resourceFavorites))
registerApi('post@resource-favorites/star', resourceFavorites.star.bind(resourceFavorites))
registerApi('post@resource-favorites/unstar', resourceFavorites.unstar.bind(resourceFavorites))
}
}
12 changes: 12 additions & 0 deletions packages/shared/src/controller/BaseController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DB } from '../data-source'

export class BaseController {
private _db: DB
constructor(db: DB) {
this._db = db
}

get db() {
return this._db
}
}
11 changes: 11 additions & 0 deletions packages/shared/src/controller/ChapterHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseController } from './BaseController'

export class ChapterHistory extends BaseController {
save(data: any) {
return this.db.getChapterHistory().save(data)
}

list(data: any) {
return this.db.getChapterHistory().list(data)
}
}
18 changes: 18 additions & 0 deletions packages/shared/src/controller/ResourceFavorites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseController } from './BaseController'

export class ResourceFavorites extends BaseController {
// 获取列表
list() {
return this.db.getResourceFavorites().list()
}

// 删除
unstar(data: any) {
return this.db.getResourceFavorites().remove(data)
}

// 添加
star(data: any) {
return this.db.getResourceFavorites().create(data)
}
}
18 changes: 18 additions & 0 deletions packages/shared/src/controller/ResourceHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseController } from './BaseController'

export class ResourceHistory extends BaseController {
// 获取列表
list() {
return this.db.getResourceHistory().list()
}

// 删除一个记录
remove(data: any) {
return this.db.getResourceHistory().remove(data)
}

// 添加一个记录
create(data: any) {
return this.db.getResourceHistory().create(data)
}
}
44 changes: 44 additions & 0 deletions packages/shared/src/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DataSource } from 'typeorm'
import { DB_PATH } from './constants'
import { ChapterHistory } from './entity/ChapterHistory'
import { ResourceHistory } from './entity/ResourceHistory'
import { ResourceFavorites } from './entity/ResourceFavorites'
import { ChapterHistoryService } from './service/ChapterHistory'
import { ResourceHistoryService } from './service/ResourceHistory'
import { ResourceFavoritesService } from './service/ResourceFavorites'

export const AppDataSource = new DataSource({
entityPrefix: 'ar_',
type: 'sqlite',
database: DB_PATH,
synchronize: true,
logging: true,
entities: [ChapterHistory, ResourceHistory, ResourceFavorites],
subscribers: [],
migrations: [],
})

export class DB {
private _initialize = false

async initialize() {
if (this._initialize)
return
this._initialize = true
return AppDataSource.initialize()
}

getChapterHistory() {
return new ChapterHistoryService(AppDataSource.getRepository(ChapterHistory))
}

getResourceHistory() {
return new ResourceHistoryService(AppDataSource.getRepository(ResourceHistory))
}

getResourceFavorites() {
return new ResourceFavoritesService(AppDataSource.getRepository(ResourceFavorites))
}
}

export const db = new DB()
14 changes: 0 additions & 14 deletions packages/shared/src/database/data-source.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/shared/src/database/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'

@Entity({ name: 'resource_favorites' })
@Index(['ruleId', 'filePath', 'url'], { unique: true })
@Index(['ruleId', 'url'], { unique: true })
export class ResourceFavorites {
@PrimaryGeneratedColumn()
id!: number
Expand All @@ -12,14 +12,11 @@ export class ResourceFavorites {
@Column({ name: 'rule_id', type: 'text', default: '' })
ruleId!: string

@Column({ name: 'filePath', type: 'text', default: '' })
filePath!: string

// =====================================================

@Column({ name: 'url', type: 'text', default: '' })
url!: string

// =====================================================

@Column({ name: 'name', type: 'text', default: '' })
name!: string

Expand Down
Loading

0 comments on commit 57584b1

Please sign in to comment.