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" ) } }