aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-05-09 00:26:09 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-05-09 00:26:09 -0400
commitc7b2ccddb6d75283a7255b612693c5e809b68f5f (patch)
treed6c74d190a6b5e20bd0856afc2b6bdb1da656ef0 /src
parent823b04d8084f14e298a408615eccf712dd76e2b9 (diff)
Added lists and documents to search
Diffstat (limited to 'src')
-rw-r--r--src/client/documents/Documents.ts1
-rw-r--r--src/server/index.ts51
2 files changed, 38 insertions, 14 deletions
diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts
index a770ccc93..00233a989 100644
--- a/src/client/documents/Documents.ts
+++ b/src/client/documents/Documents.ts
@@ -175,6 +175,7 @@ export namespace Docs {
if (!("creationDate" in protoProps)) {
protoProps.creationDate = new DateField;
}
+ protoProps.isPrototype = true;
return SetDelegateOptions(SetInstanceOptions(proto, protoProps, data), delegateProps);
}
diff --git a/src/server/index.ts b/src/server/index.ts
index 5c54babb2..93e4cafbf 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -295,7 +295,7 @@ function GetRefFields([ids, callback]: [string[], (result?: Transferable[]) => v
}
-const suffixMap: { [type: string]: string | [string, string] | [string, string, (json: any) => any] } = {
+const suffixMap: { [type: string]: (string | [string, string | ((json: any) => any)]) } = {
"number": "_n",
"string": "_t",
"image": ["_t", "url"],
@@ -303,9 +303,40 @@ const suffixMap: { [type: string]: string | [string, string] | [string, string,
"pdf": ["_t", "url"],
"audio": ["_t", "url"],
"web": ["_t", "url"],
- "date": ["_d", "date", millis => new Date(millis).toISOString()],
+ "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");
@@ -318,20 +349,12 @@ function UpdateField(socket: Socket, diff: Diff) {
for (let key in docfield) {
if (!key.startsWith("fields.")) continue;
let val = docfield[key];
- const type = val.__type || typeof val;
- let suffix = suffixMap[type];
- if (suffix !== undefined) {
- if (Array.isArray(suffix)) {
- val = val[suffix[1]];
- const func = suffix[2];
- if (func) {
- val = func(val);
- }
- suffix = suffix[0];
- }
+ 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;
}
}