MCPcopy
hub / github.com/ZToolsCenter/ZTools / setupIPC

Method setupIPC

src/main/api/shared/database.ts:63–387  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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)) {

Callers 1

initMethod · 0.95

Calls 15

getPluginPrefixMethod · 0.95
dbPutMethod · 0.95
dbGetMethod · 0.95
_getPluginDataStatsMethod · 0.95
_getPluginDocKeysMethod · 0.95
_getPluginDocMethod · 0.95
_clearPluginDataMethod · 0.95
onMethod · 0.80
getMethod · 0.65
putMethod · 0.45
removeMethod · 0.45
bulkDocsMethod · 0.45

Tested by

no test coverage detected