2019-02-07 01:20:27 +01:00
|
|
|
import {iterateCursor} from "./utils.js";
|
2019-01-09 11:06:09 +01:00
|
|
|
|
2019-02-05 00:21:50 +01:00
|
|
|
export default class QueryTarget {
|
2019-02-07 01:20:27 +01:00
|
|
|
constructor(target) {
|
|
|
|
this._target = target;
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:06:09 +01:00
|
|
|
reduce(range, reducer, initialValue) {
|
|
|
|
return this._reduce(range, reducer, initialValue, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
reduceReverse(range, reducer, initialValue) {
|
2019-03-21 21:36:02 +01:00
|
|
|
return this._reduce(range, reducer, initialValue, "prev");
|
2019-01-09 11:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
selectLimit(range, amount) {
|
|
|
|
return this._selectLimit(range, amount, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLimitReverse(range, amount) {
|
|
|
|
return this._selectLimit(range, amount, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectWhile(range, predicate) {
|
|
|
|
return this._selectWhile(range, predicate, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectWhileReverse(range, predicate) {
|
|
|
|
return this._selectWhile(range, predicate, "prev");
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:29 +01:00
|
|
|
async selectAll(range, direction) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
const results = [];
|
2019-02-10 21:25:29 +01:00
|
|
|
await iterateCursor(cursor, (value) => {
|
2019-01-09 11:06:09 +01:00
|
|
|
results.push(value);
|
2019-02-16 02:53:49 +01:00
|
|
|
return false;
|
2019-01-09 11:06:09 +01:00
|
|
|
});
|
2019-02-10 21:25:29 +01:00
|
|
|
return results;
|
2019-01-09 11:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
selectFirst(range) {
|
|
|
|
return this._find(range, () => true, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLast(range) {
|
|
|
|
return this._find(range, () => true, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
find(range, predicate) {
|
|
|
|
return this._find(range, predicate, "next");
|
|
|
|
}
|
|
|
|
|
|
|
|
findReverse(range, predicate) {
|
|
|
|
return this._find(range, predicate, "prev");
|
|
|
|
}
|
|
|
|
|
|
|
|
_reduce(range, reducer, initialValue, direction) {
|
|
|
|
let reducedValue = initialValue;
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
return iterateCursor(cursor, (value) => {
|
|
|
|
reducedValue = reducer(reducedValue, value);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_selectLimit(range, amount, direction) {
|
|
|
|
return this._selectWhile(range, (results) => {
|
|
|
|
return results.length === amount;
|
|
|
|
}, direction);
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:29 +01:00
|
|
|
async _selectWhile(range, predicate, direction) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
const results = [];
|
2019-02-10 21:25:29 +01:00
|
|
|
await iterateCursor(cursor, (value) => {
|
2019-01-09 11:06:09 +01:00
|
|
|
results.push(value);
|
|
|
|
return predicate(results);
|
|
|
|
});
|
2019-02-10 21:25:29 +01:00
|
|
|
return results;
|
2019-01-09 11:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async _find(range, predicate, direction) {
|
2019-02-07 01:20:27 +01:00
|
|
|
const cursor = this._target.openCursor(range, direction);
|
2019-01-09 11:06:09 +01:00
|
|
|
let result;
|
|
|
|
const found = await iterateCursor(cursor, (value) => {
|
2019-02-07 01:20:27 +01:00
|
|
|
const found = predicate(value);
|
|
|
|
if (found) {
|
2019-01-09 11:06:09 +01:00
|
|
|
result = value;
|
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
return found;
|
2019-01-09 11:06:09 +01:00
|
|
|
});
|
2019-02-07 01:20:27 +01:00
|
|
|
if (found) {
|
|
|
|
return result;
|
2019-01-09 11:06:09 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 21:36:02 +01:00
|
|
|
}
|