diff options
Diffstat (limited to 'src/server/index.ts')
-rw-r--r-- | src/server/index.ts | 55 |
1 files changed, 45 insertions, 10 deletions
diff --git a/src/server/index.ts b/src/server/index.ts index 6b92e8e8e..93e4cafbf 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -128,7 +128,6 @@ app.get("/pull", (req, res) => app.get("/search", async (req, res) => { let query = req.query.query || "hello"; let results = await Search.Instance.search(query); - console.log(results); res.send(results); }); @@ -296,10 +295,48 @@ function GetRefFields([ids, callback]: [string[], (result?: Transferable[]) => v } -const suffixMap: { [type: string]: string } = { +const suffixMap: { [type: string]: (string | [string, string | ((json: any) => any)]) } = { "number": "_n", - "string": "_t" + "string": "_t", + "image": ["_t", "url"], + "video": ["_t", "url"], + "pdf": ["_t", "url"], + "audio": ["_t", "url"], + "web": ["_t", "url"], + "date": ["_d", value => new Date(value.date).toISOString()], + "proxy": ["_i", "fieldId"], + "list": ["_l", list => { + const results = []; + for (const value of list.fields) { + const term = ToSearchTerm(value); + if (term) { + results.push(term.value); + } + } + return results.length ? results : null; + }] }; + +function ToSearchTerm(val: any): { suffix: string, value: any } | undefined { + const type = val.__type || typeof val; + let suffix = suffixMap[type]; + if (!suffix) { + return; + } + + if (Array.isArray(suffix)) { + const accessor = suffix[1]; + if (typeof accessor === "function") { + val = accessor(val); + } else { + val = val[accessor]; + } + suffix = suffix[0]; + } + + return { suffix, value: val } +} + function UpdateField(socket: Socket, diff: Diff) { Database.Instance.update(diff.id, diff.diff, () => socket.broadcast.emit(MessageStore.UpdateField.Message, diff), false, "newDocuments"); @@ -308,28 +345,26 @@ function UpdateField(socket: Socket, diff: Diff) { return; } const update: any = { id: diff.id }; - console.log("FIELD: ", docfield); let dynfield = false; for (let key in docfield) { if (!key.startsWith("fields.")) continue; - const val = docfield[key]; - const suffix = suffixMap[typeof val]; - if (suffix !== undefined) { + let val = docfield[key]; + let term = ToSearchTerm(val); + if (term !== undefined) { + let { suffix, value } = term; key = key.substring(7); Object.values(suffixMap).forEach(suf => update[key + suf] = null); - update[key + suffix] = { set: val }; + update[key + suffix] = { set: value }; dynfield = true; } } if (dynfield) { - console.log("dynamic field detected!"); Search.Instance.updateDocument(update); } } function CreateField(newValue: any) { Database.Instance.insert(newValue, "newDocuments"); - console.log("created field"); } server.listen(serverPort); |