From 1dbfca40579d11e807f84e97008905bc5a0430a7 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Tue, 18 Mar 2025 10:27:30 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=90=8D=E7=A7=B0=E4=BB=A5=E4=BE=BF=E4=BA=8E?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/base/index.ts | 53 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/db/base/index.ts b/db/base/index.ts index 63abe93..742097f 100644 --- a/db/base/index.ts +++ b/db/base/index.ts @@ -47,13 +47,21 @@ class PbToolBase { * * @template R - 返回结果类型 * @param fn - 需要执行的异步函数 + * @param operation - 操作名称,用于日志记录 * @returns 成功时返回执行结果,失败时返回 null 并记录错误 */ - protected managePbError = async (fn: () => Promise) => { + protected managePbError = async ( + fn: () => Promise, + operation: string + ) => { try { - return await fn() + const result = await fn() + this.logger.debug(`PocketBase ${operation} 成功 [${this.dbName}]`) + return result } catch (err: any) { - this.logger.error(`pocketbase error: ${err.message}`) + this.logger.error( + `PocketBase ${operation} 失败 [${this.dbName}]: ${err.message}` + ) return null } } @@ -70,8 +78,10 @@ class PbToolBase { * ``` */ public create = async (data: Partial) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).create(data) + this.logger.info(`创建${this.dbName}记录`) + return this.managePbError( + () => this.pbClient.collection(this.dbName).create(data), + "create" ) } @@ -89,8 +99,10 @@ class PbToolBase { * ``` */ public get = async (id: string, options?: RecordOptions) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).getOne(id, options) + this.logger.info(`获取${this.dbName}记录 [ID: ${id}]`) + return this.managePbError( + () => this.pbClient.collection(this.dbName).getOne(id, options), + "get" ) } @@ -113,8 +125,10 @@ class PbToolBase { data: Partial, options?: RecordOptions ) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).update(id, data, options) + this.logger.info(`更新${this.dbName}记录 [ID: ${id}]`) + return this.managePbError( + () => this.pbClient.collection(this.dbName).update(id, data, options), + "update" ) } @@ -131,8 +145,10 @@ class PbToolBase { * ``` */ public delete = async (id: string, options?: CommonOptions) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).delete(id, options) + this.logger.info(`删除${this.dbName}记录 [ID: ${id}]`) + return this.managePbError( + () => this.pbClient.collection(this.dbName).delete(id, options), + "delete" ) } @@ -152,8 +168,12 @@ class PbToolBase { * ``` */ public list = async (options?: RecordFullListOptions) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).getFullList(options) + const filterInfo = options?.filter ? ` [过滤: ${options.filter}]` : "" + const sortInfo = options?.sort ? ` [排序: ${options.sort}]` : "" + this.logger.info(`获取${this.dbName}记录列表${filterInfo}${sortInfo}`) + return this.managePbError( + () => this.pbClient.collection(this.dbName).getFullList(options), + "list" ) } @@ -174,8 +194,11 @@ class PbToolBase { filter: string, options?: RecordListOptions ) => { - return this.managePbError(() => - this.pbClient.collection(this.dbName).getFirstListItem(filter, options) + this.logger.info(`获取${this.dbName}第一条匹配记录 [过滤: ${filter}]`) + return this.managePbError( + () => + this.pbClient.collection(this.dbName).getFirstListItem(filter, options), + "getFirstOne" ) } }