egg_server/db/apiKey/index.ts

49 lines
882 B
TypeScript

import { RecordModel } from "pocketbase"
import { AppInfoModel } from "../../constant/config"
import { managePbError } from "../../utils/pbTools"
import pbClient from "../pbClient"
const DB_NAME = "apiKey"
export interface ApiKey {
name: string
owner: string
app: string
}
export type ApiKeyModel = ApiKey & RecordModel
export interface ApiKeyModelWithApp extends ApiKeyModel {
expand: {
app: AppInfoModel
}
}
/**
* 获取单个 API Key 对应的 App 信息
* @param id
* @returns
*/
const getOne = (id: string) =>
managePbError<ApiKeyModelWithApp>(() =>
pbClient.collection(DB_NAME).getOne(id, {
expand: "app",
})
)
/**
* 创建 API Key
* @param data
* @returns
*/
const create = (data: ApiKey) =>
managePbError<ApiKey>(() => pbClient.collection(DB_NAME).create(data))
const apiKey = {
create,
getOne,
}
export default apiKey