Files
Folo/apps/renderer/src/services/base.ts
T
Innei 234471dfdf refactor: move to monorepo (#475)
* init

* fix: electron

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

* update

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

* fix: build

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

* fix: lint

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

* fix: test

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

---------

Signed-off-by: Innei <i@innei.in>
2024-09-18 20:54:48 +08:00

28 lines
634 B
TypeScript

import type Dexie from "dexie"
import type { UpdateSpec } from "dexie"
export abstract class BaseService<T extends { id: string }> {
constructor(public readonly table: Dexie.Table<T, string>) {}
async upsert(data: T): Promise<unknown> {
return this.table.put(data)
}
async upsertMany(data: T[]) {
return this.table.bulkPut(data)
}
async findAll() {
return this.table.toArray()
}
async patch(id: string, data: Partial<T>) {
const oldData = await this.table.get(id)
if (!oldData) return
await this.table.update(id, {
...oldData,
...data,
} as unknown as UpdateSpec<T>)
}
}