2023-08-07 04:36:27 -04:00
|
|
|
// Picgo Store 相关类型
|
2021-08-01 02:50:25 -04:00
|
|
|
import { IObject, IResult, IGetResult, IFilter } from '@picgo/store/dist/types'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// Electron 相关
|
2021-07-25 11:25:36 -04:00
|
|
|
import { ipcRenderer, IpcRendererEvent } from 'electron'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// UUID
|
2023-01-16 10:27:33 -05:00
|
|
|
import { v4 as uuid } from 'uuid'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// 数据库操作常量
|
2021-07-25 11:25:36 -04:00
|
|
|
import {
|
|
|
|
PICGO_GET_DB,
|
|
|
|
PICGO_INSERT_DB,
|
|
|
|
PICGO_INSERT_MANY_DB,
|
|
|
|
PICGO_UPDATE_BY_ID_DB,
|
|
|
|
PICGO_GET_BY_ID_DB,
|
|
|
|
PICGO_REMOVE_BY_ID_DB
|
|
|
|
} from '#/events/constants'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// 数据库类型声明
|
2021-07-25 11:25:36 -04:00
|
|
|
import { IGalleryDB } from '#/types/extra-vue'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// 公共工具函数
|
2023-01-06 04:21:27 -05:00
|
|
|
import { getRawData } from './common'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
2021-07-25 11:25:36 -04:00
|
|
|
export class GalleryDB implements IGalleryDB {
|
2021-08-01 02:50:25 -04:00
|
|
|
async get<T> (filter?: IFilter): Promise<IGetResult<T>> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<IGetResult<T>>(PICGO_GET_DB, filter)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-25 11:25:36 -04:00
|
|
|
async insert<T> (value: T): Promise<IResult<T>> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<IResult<T>>(PICGO_INSERT_DB, value)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-25 11:25:36 -04:00
|
|
|
async insertMany<T> (value: T[]): Promise<IResult<T>[]> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<IResult<T>[]>(PICGO_INSERT_MANY_DB, value)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-25 11:25:36 -04:00
|
|
|
async updateById (id: string, value: IObject): Promise<boolean> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<boolean>(PICGO_UPDATE_BY_ID_DB, id, value)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-26 12:15:11 -04:00
|
|
|
async getById<T> (id: string): Promise<IResult<T> | undefined> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<IResult<T> | undefined>(PICGO_GET_BY_ID_DB, id)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-25 11:25:36 -04:00
|
|
|
async removeById (id: string): Promise<void> {
|
2024-04-15 11:12:02 -04:00
|
|
|
const res = await this.#msgHandler<void>(PICGO_REMOVE_BY_ID_DB, id)
|
2021-07-25 11:25:36 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2024-04-15 11:12:02 -04:00
|
|
|
#msgHandler<T> (method: string, ...args: any[]): Promise<T> {
|
2021-07-25 11:25:36 -04:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const callbackId = uuid()
|
2024-05-25 09:06:27 -04:00
|
|
|
const callback = (_: IpcRendererEvent, data: T, returnCallbackId: string) => {
|
2021-07-25 11:25:36 -04:00
|
|
|
if (returnCallbackId === callbackId) {
|
|
|
|
resolve(data)
|
|
|
|
ipcRenderer.removeListener(method, callback)
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 04:21:27 -05:00
|
|
|
const data = getRawData(args)
|
2021-07-25 11:25:36 -04:00
|
|
|
ipcRenderer.on(method, callback)
|
2023-01-06 04:21:27 -05:00
|
|
|
ipcRenderer.send(method, ...data, callbackId)
|
2021-07-25 11:25:36 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new GalleryDB()
|