nedb-async
Version:
Promise wrapper for Nedb that makes it possible to use all Nedb all cursor modifiers.
64 lines (63 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.justPromise = exports.promisefy = void 0;
function promisefy(name, args) {
return new Promise((rs, rj) => {
/**
* Check if the last argument is
* an Array.
* I choose array because all Nedb
* parameters are objects.
*/
const arg = [...args];
if (Array.isArray(arg[arg.length - 1])) {
const modifies = arg.pop();
/**
* nedb methods like
* find, count, and findOne
* returns cursor when a
* callback is not passed
* to them.
*/
// @ts-ignore
const cursor = this[name](...arg);
let count = 1;
for (const [method, value] of modifies) {
const valid = ['sort', 'limit', 'skip'];
if (!valid.includes(method)) {
throw new Error(`${method} is not recognized, available methods are ${valid.join(',')}`);
}
if (typeof method !== 'string') {
throw new Error(`${method} must be a type of string, available methods are ${valid.join(',')}`);
}
// call exec method if it's the last
if (count === arg.length) {
cursor[method](value).exec((err, docs) => (err ? rj(err) : rs(docs)));
}
else {
cursor[method](value);
}
count++;
}
}
else {
// @ts-ignore
this[name](...arg, (err, docs) => (err ? rj(err) : rs(docs)));
}
});
}
exports.promisefy = promisefy;
function justPromise(name, arg) {
return new Promise((rs, rj) => {
// @ts-ignore
this[name](...arg, (err, docs) => {
if (err) {
rj(err);
}
else {
rs(docs);
}
});
});
}
exports.justPromise = justPromise;