48 lines
1.0 KiB
TypeScript
48 lines
1.0 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<UserRecordModel>(
|
|
async () => await pbClient.collection("user").getOne(id)
|
|
)
|
|
|
|
const getOneByUserId = (user_id: number) => {
|
|
return managePb404<UserRecordModel>(
|
|
async () =>
|
|
await pbClient
|
|
.collection("user")
|
|
.getFirstListItem(`user_id="${user_id}"`, {
|
|
sort: "-created",
|
|
})
|
|
)
|
|
}
|
|
|
|
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
|