`` refactor GetUserById function and add Create/UpdateUser APIs with type definitions ``

This commit is contained in:
louiscklaw
2025-05-15 09:26:36 +08:00
parent d4fcc1dd8f
commit 160c93b83d
5 changed files with 35 additions and 13 deletions

View File

@@ -1,15 +1,9 @@
import { pb } from '@/lib/pb';
import { COL_USERS } from '@/constants';
import type { User } from '@/types/user';
export async function getUserById(id: string): Promise<User> {
try {
const user = await pb.collection(COL_USERS).getOne<User>(id);
return user;
} catch (err) {
if (err instanceof Error && err.message.includes('404')) {
throw new Error(`User with ID ${id} not found`);
}
throw err;
}
import { pb } from '@/lib/pb';
import type { User } from './type.d';
export function getUserById(id: string): Promise<User> {
return pb.collection(COL_USERS).getOne<User>(id);
}

View File

@@ -0,0 +1,10 @@
import { COL_USERS } from '@/constants';
import type { RecordModel } from 'pocketbase';
import { pb } from '@/lib/pb';
import type { UpdateUser } from './type.d';
export async function UpdateUserById(id: string, data: Partial<UpdateUser>): Promise<RecordModel> {
return pb.collection(COL_USERS).update(id, data);
}

View File

@@ -21,9 +21,12 @@ the `@` sign refer to `/home/logic/_wsl_workspace/001_github_ws/lettersoup-onlin
simple template:
```typescript
import { pb } from '@/lib/pb';
import { COL_USERS } from '@/constants';
import { pb } from '@/lib/pb';
import type { User } from './type.d';
export async function createUser(data: CreateFormProps) {
// ...content
// use direct return of pb.collection (e.g. return pb.collection(xxx))

15
002_source/cms/src/db/Users/type.d.ts vendored Normal file
View File

@@ -0,0 +1,15 @@
//
// RULES
// pocketbase Users collection schema
//
// User type definitions
export interface User {
verified: boolean;
//
id: string;
createdAt: Date;
}
export interface UpdateUser {
verified?: boolean;
}