2024-05-06 03:28:58 +00:00

47 lines
1.1 KiB
TypeScript

import { RecordModel } from "pocketbase";
import { managePb404 } from "../../utils/pbTools";
import pbClient from "../pbClient";
export interface UserRecordModel extends RecordModel {
user_id: number;
username: string;
name: string;
avatar_url: string;
web_url: string;
}
const getOne = (id: string) =>
managePb404(
async () => await pbClient.collection("user").getOne(id)
) as Promise<UserRecordModel>;
const getOneByUserId = (user_id: number) => {
return managePb404(
async () =>
await pbClient
.collection("user")
.getFirstListItem(`user_id="${user_id}"`, {
sort: "-created",
})
) as Promise<UserRecordModel>;
};
const create = async (data: Partial<UserRecordModel>) =>
await pbClient.collection("user").create<UserRecordModel>(data);
const upsert = async (data: Partial<UserRecordModel>) => {
if (!data.user_id) return null;
const userInfo = await getOneByUserId(data.user_id);
if (userInfo) return userInfo;
return await create(data);
};
const user = {
create,
upsert,
getOne,
getOneByUserId,
};
export default user;