`` refactor GetUserById function and add Create/UpdateUser APIs with type definitions
``
This commit is contained in:
0
002_source/cms/src/db/Users/ChangeUserState.tsx
Normal file
0
002_source/cms/src/db/Users/ChangeUserState.tsx
Normal 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);
|
||||
}
|
||||
|
10
002_source/cms/src/db/Users/UpdateById.tsx
Normal file
10
002_source/cms/src/db/Users/UpdateById.tsx
Normal 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);
|
||||
}
|
@@ -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
15
002_source/cms/src/db/Users/type.d.ts
vendored
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user