query/index.jsView |
---|
| 1 | +var pull = require('pull-stream') |
| 2 | +var path = require('path') |
| 3 | +var FlumeQuery = require('flumeview-query') |
| 4 | +var explain = require('explain-error') |
| 5 | + |
| 6 | +function isString(s) { |
| 7 | + return 'string' === typeof s |
| 8 | +} |
| 9 | + |
| 10 | +exports.name = 'query' |
| 11 | +exports.version = require('./package.json').version |
| 12 | +exports.manifest = { |
| 13 | + read: 'source', |
| 14 | + explain: 'sync' |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +var INDEX_VERSION = 8 |
| 25 | +var indexes = [ |
| 26 | + {key: 'log', value: ['timestamp']}, |
| 27 | + {key: 'clk', value: [['value', 'author'], ['value', 'sequence']] }, |
| 28 | + {key: 'typ', value: [['value', 'content', 'type'], ['timestamp']] }, |
| 29 | + {key: 'tya', value: [['value', 'content', 'type'], ['value', 'timestamp']] }, |
| 30 | + {key: 'cha', value: [['value', 'content', 'channel'], ['timestamp']] }, |
| 31 | + {key: 'aty', value: [['value', 'author'], ['value', 'content', 'type'], ['timestamp']]}, |
| 32 | + {key: 'ata', value: [['value', 'author'], ['value', 'content', 'type'], ['value', 'timestamp']]}, |
| 33 | + {key: 'art', value: [['value', 'content', 'root'], ['value', 'timestamp']]}, |
| 34 | + {key: 'lor', value: [['value', 'timestamp' ]]} |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +exports.init = function (ssb, config) { |
| 45 | + var s = ssb._flumeUse('query', FlumeQuery(INDEX_VERSION, {indexes: indexes})) |
| 46 | + var read = s.read |
| 47 | + var explain = s.explain |
| 48 | + s.explain = function (opts) { |
| 49 | + if(!opts) opts = {} |
| 50 | + if(isString(opts)) |
| 51 | + opts = {query: JSON.parse(opts)} |
| 52 | + else if(isString(opts.query)) |
| 53 | + opts.query = JSON.parse(opts.query) |
| 54 | + return explain(opts) |
| 55 | + } |
| 56 | + |
| 57 | + s.read = function (opts) { |
| 58 | + if(!opts) opts = {} |
| 59 | + if(isString(opts)) |
| 60 | + opts = {query: JSON.parse(opts)} |
| 61 | + else if(isString(opts.query)) |
| 62 | + opts.query = JSON.parse(opts.query) |
| 63 | + return read(opts) |
| 64 | + } |
| 65 | + return s |
| 66 | +} |
| 67 | + |
| 68 | + |