-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (27 loc) · 868 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import intersection from './utils';
import intersect from './intersect';
import tokenize from './tokenize';
let tokensFor = (fields, object) => {
fields = intersection(fields, Object.keys(object));
let string = fields.map(field => {
let value = object[field];
if (typeof value === 'string') {
return value;
}
}).join(' ');
return tokenize(string);
};
export let addFullText = (table, tokenField, fields) => {
table.hook('creating', (_, obj) => {
obj[tokenField] = tokensFor(fields, obj);
});
table.hook('updating', (mods, _, obj) => {
return { [tokenField]: tokensFor(fields, { ...obj, ...mods }) };
});
}
export default async (table, tokenField, query) => {
let tokens = tokenize(query);
let queries = tokens.map(token =>
table.where(tokenField).startsWith(token));
return await intersect(table, queries);
}