-
Notifications
You must be signed in to change notification settings - Fork 17
/
cursor.js
74 lines (60 loc) · 1.82 KB
/
cursor.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var _ = require('lodash')
function Cursor (query, db, opts) {
if (!(this instanceof Cursor)) return new Cursor(query, db, opts)
var defaults = {
pageSize: 50
}
if (!opts) opts = defaults
this.pageSize = opts.pageSize || defaults.pageSize
this.lastOffset = null
this.query = { AND: { '*': db.preprocessor.naturalize(query) } }
this.db = db
}
Cursor.prototype.first = function (cb) {
return this.queryWithOffset(0, cb)
}
Cursor.prototype.next = function (cb) {
var offset = (this.lastOffset === null) ? 0 : this.lastOffset + this.pageSize
return this.queryWithOffset(offset, cb)
}
Cursor.prototype.prev = function (cb) {
var offset = (this.lastOffset === null) ? 0 : this.lastOffset - this.pageSize
return this.queryWithOffset(offset, cb)
}
Cursor.prototype.last = function (cb) {
if (this.totalHits) {
var penultimatePage = Math.floor(this.totalHits / this.pageSize)
var lastPageOffset = penultimatePage * this.pageSize
return this.queryWithOffset(lastPageOffset, cb)
}
cb(new Error('cannot get last page until initial query has run (try cursor.first() first)'))
}
Cursor.prototype.queryWithOffset = function (offset, cb) {
this.lastOffset = offset
var self = this
var q = {
query: this.query,
offset: offset,
pageSize: this.pageSize
}
this.db.index.search(q, (err, results) => {
if (err) return cb(err)
self.totalHits = results.totalHits
results.offset = offset
self.fullResults(results, cb)
})
}
Cursor.prototype.fullResults = function (results, cb) {
var self = this
var done = _.after(results.hits.length, function () {
cb(null, results)
})
results.hits.map((hit, i) => {
self.db.docstore.get(hit.id, (err, document) => {
if (err) cb(err)
results.hits[i].document = document
done(null)
})
})
}
module.exports = Cursor