67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import PocketBase, { RecordModel } from "pocketbase"
|
||
import { Logger } from "winston"
|
||
|
||
import PbToolBase from "../base"
|
||
|
||
const DB_NAME = "sheet"
|
||
|
||
/**
|
||
* 表格数据接口,定义了表格的基本属性
|
||
*/
|
||
export interface Sheet {
|
||
/**
|
||
* 表格区间,例如 "A1:Z100"
|
||
*/
|
||
range: string
|
||
/**
|
||
* 表格访问令牌,用于API验证
|
||
*/
|
||
sheetToken: string
|
||
/**
|
||
* 表格的完整URL地址
|
||
*/
|
||
sheetUrl: string
|
||
}
|
||
|
||
/**
|
||
* 表格数据模型类型,继承基础数据接口并添加RecordModel属性
|
||
*
|
||
* RecordModel提供了id、created、updated等基础字段
|
||
*/
|
||
export type SheetModel = Sheet & RecordModel
|
||
|
||
/**
|
||
* 表格数据库操作类
|
||
*
|
||
* 提供表格数据的增删改查及特定查询方法
|
||
* 继承自PbToolBase类,复用基础CRUD操作
|
||
*/
|
||
class SheetDB extends PbToolBase<SheetModel> {
|
||
/**
|
||
* 创建表格数据库操作实例
|
||
*
|
||
* @param pbClient - PocketBase客户端实例
|
||
* @param logger - 日志记录器实例
|
||
*/
|
||
constructor(pbClient: PocketBase, logger: Logger) {
|
||
super(DB_NAME, pbClient, logger)
|
||
}
|
||
|
||
/**
|
||
* 根据表格URL获取表格记录
|
||
*
|
||
* @param sheetUrl - 表格的URL地址
|
||
* @returns 成功时返回匹配的表格记录,失败时返回null
|
||
*
|
||
* @example
|
||
* ```typescript
|
||
* const sheet = await sheetDB.getByUrl("https://example.com/sheets/12345");
|
||
* ```
|
||
*/
|
||
public getByUrl = (sheetUrl: string) => {
|
||
return this.getFirstOne(`sheetUrl = "${sheetUrl}"`)
|
||
}
|
||
}
|
||
|
||
export default SheetDB
|