()
| 61 | } |
| 62 | |
| 63 | private setupIPC(): void { |
| 64 | // ============ 同步API(供插件使用) ============ |
| 65 | ipcMain.on('db:put', (event, doc) => { |
| 66 | const prefix = this.getPluginPrefix(event) |
| 67 | if (prefix) { |
| 68 | doc._id = prefix + doc._id |
| 69 | } |
| 70 | const result = lmdbInstance.put(doc) |
| 71 | // 如果是插件调用,需要去除返回结果中的前缀 |
| 72 | if (prefix && result.id && result.id.startsWith(prefix)) { |
| 73 | result.id = result.id.slice(prefix.length) |
| 74 | } |
| 75 | event.returnValue = result |
| 76 | }) |
| 77 | |
| 78 | ipcMain.on('db:get', (event, id) => { |
| 79 | const prefix = this.getPluginPrefix(event) |
| 80 | if (prefix) { |
| 81 | id = prefix + id |
| 82 | } |
| 83 | const doc = lmdbInstance.get(id) |
| 84 | // 如果是插件调用,需要去除返回文档的前缀 |
| 85 | if (doc && prefix && doc._id.startsWith(prefix)) { |
| 86 | doc._id = doc._id.slice(prefix.length) |
| 87 | } |
| 88 | event.returnValue = doc |
| 89 | }) |
| 90 | |
| 91 | ipcMain.on('db:remove', (event, docOrId) => { |
| 92 | const prefix = this.getPluginPrefix(event) |
| 93 | if (prefix) { |
| 94 | if (typeof docOrId === 'string') { |
| 95 | docOrId = prefix + docOrId |
| 96 | } else { |
| 97 | docOrId._id = prefix + docOrId._id |
| 98 | } |
| 99 | } |
| 100 | const result = lmdbInstance.remove(docOrId) |
| 101 | console.log('[Database] sync db:remove', docOrId, 'result', result) |
| 102 | // 如果是插件调用,需要去除返回结果中的前缀 |
| 103 | if (prefix && result.id && result.id.startsWith(prefix)) { |
| 104 | result.id = result.id.slice(prefix.length) |
| 105 | } |
| 106 | event.returnValue = result |
| 107 | }) |
| 108 | |
| 109 | ipcMain.on('db:bulk-docs', (event, docs) => { |
| 110 | const prefix = this.getPluginPrefix(event) |
| 111 | if (prefix) { |
| 112 | docs.forEach((doc: any) => { |
| 113 | doc._id = prefix + doc._id |
| 114 | }) |
| 115 | } |
| 116 | const results = lmdbInstance.bulkDocs(docs) |
| 117 | // 如果是插件调用,需要去除返回结果中的前缀 |
| 118 | if (prefix && Array.isArray(results)) { |
| 119 | results.forEach((result) => { |
| 120 | if (result.id && result.id.startsWith(prefix)) { |
no test coverage detected