数据
文档数据库 ling.db、键值存储 ling.dbStorage,以及插件的数据目录和临时目录。不需要权限,数据只属于本插件。
怎么选、怎么在 Vue 里用,见指南 数据存储。
db
文档是任意 JSON 对象,带 _id(非空,不超过 1024 字节)和 _rev(版本号)。整个文档序列化后不超过 1 MiB。
| 方法 | 说明 |
|---|---|
db.put(doc): Promise<DbResult> | 新建(不带 _rev)或更新(_rev 必须等于当前版本) |
db.get(id): Promise<DbDoc | null> | 读取;不存在或已删除时为 null |
db.remove(docOrId): Promise<DbResult> | 删除。传 ID 删当前版本;传带 _rev 的文档时版本不符为 conflict |
db.bulkDocs(docs): Promise<DbResult[]> | 一个事务里批量写入,_deleted: true 的按删除处理,逐项返回结果 |
db.allDocs(filter?): Promise<DbDoc[]> | 列出文档:省略为全部;字符串为 _id 前缀;数组为按 ID 取(按给定顺序,不存在的跳过)。结果按 _id 排序 |
db.postAttachment(id, data: Uint8Array, type: string): Promise<DbResult> | 给文档加附件(每个文档一个,不超过 10 MiB,只能新建不能覆盖);文档不存在时新建空文档 |
db.getAttachment(id): Promise<Uint8Array | null> | 读附件内容 |
db.getAttachmentType(id): Promise<string | null> | 读附件的 MIME 类型 |
写操作的结果(DbResult)不会 reject:
ts
// 成功
{ id: 'note/1', rev: '2-9f…', ok: true }
// 失败
{ id: 'note/1', error: true, name: 'conflict', message: 'Document update conflict' }name 为 conflict(版本不对或附件已存在)、not_found(删除的文档不存在)或 exception。
读出的文档有附件时带 _attachments.default:{ stub: true, content_type, length, digest }。
dbStorage
键值存储。键非空;值为任意 JSON,序列化后不超过 1 MiB。
| 方法 | 说明 |
|---|---|
dbStorage.setItem(key, value): Promise<void> | 写入 |
dbStorage.getItem(key): Promise<unknown> | 读取;不存在时为 null |
dbStorage.removeItem(key): Promise<void> | 删除;不存在时什么也不做 |
插件目录
| 方法 | 说明 |
|---|---|
getPluginLocalDataPath(): Promise<string> | 数据目录,长期保留;卸载插件时由用户选择是否一起删除 |
getPluginTempPath(): Promise<string> | 临时目录;插件销毁或 lingtools 退出时清空,退到后台时保留 |
两个目录不存在时自动创建,用 ling.fs 读写不需要权限。