mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 15:35:25 +02:00
Add docs about nedb query options
This commit is contained in:
parent
f3cff0973b
commit
7496897dd9
4 changed files with 784 additions and 16 deletions
|
@ -93,15 +93,31 @@ class Database {
|
|||
* @param {Object} query - query object
|
||||
* @param {Object} update - fields to update
|
||||
* @param {Object} options
|
||||
* @returns {Promise<number|Error>} - number of updated rows or Error object
|
||||
* @param {Boolean} options.multi - (false) allows update several documents
|
||||
* @param {Boolean} options.upsert - (false) if true, upsert document with update fields.
|
||||
* Method will return inserted doc or number of affected docs if doc hasn't been inserted
|
||||
* @param {Boolean} options.returnUpdatedDocs - (false) if true, returns affected docs
|
||||
* @returns {Promise<number|Object|Object[]|Error>} - number of updated rows or affected docs or Error object
|
||||
*/
|
||||
async update (query, update, options = {}) {
|
||||
return new Promise((res, rej) => this.db.update(query, update, options, (err, result) => {
|
||||
return new Promise((res, rej) => this.db.update(query, update, options, (err, result, affectedDocs) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
}
|
||||
|
||||
res(result);
|
||||
switch (true) {
|
||||
case options.returnUpdatedDocs:
|
||||
res(affectedDocs);
|
||||
break;
|
||||
case options.upsert:
|
||||
if (affectedDocs) {
|
||||
res(affectedDocs);
|
||||
}
|
||||
res(result);
|
||||
break;
|
||||
default:
|
||||
res(result)
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -111,6 +127,7 @@ class Database {
|
|||
*
|
||||
* @param {Object} query - query object
|
||||
* @param {Object} options
|
||||
* @param {Boolean} options.multi - (false) if true, remove several docs
|
||||
* @returns {Promise<number|Error>} - number of removed rows or Error object
|
||||
*/
|
||||
async remove (query, options = {}) {
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
"devDependencies": {
|
||||
"chai": "^4.1.2",
|
||||
"chai-http": "^4.0.0",
|
||||
"mocha": "^5.2.0"
|
||||
"mocha": "^5.2.0",
|
||||
"nyc": "^12.0.2"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,6 +68,33 @@ describe('Database', () => {
|
|||
expect(updatedDoc.data).to.equal(updatedData);
|
||||
});
|
||||
|
||||
it('Updating documents with options', async () => {
|
||||
const data = {update: true, data: 'Text data'};
|
||||
|
||||
await db.insert(data);
|
||||
await db.insert(data);
|
||||
|
||||
let numberOfUpdatedDocs = await db.update({update: true}, {$set: {data: 'First update'}}, {multi: true});
|
||||
|
||||
expect(numberOfUpdatedDocs).to.equal(2);
|
||||
|
||||
const affectedDocs = await db.update({update: true}, {$set: {data: 'Second update'}}, {multi: true, returnUpdatedDocs: true});
|
||||
|
||||
expect(affectedDocs).to.be.a('array');
|
||||
affectedDocs.forEach(doc => {
|
||||
expect(doc.data).to.equal('Second update');
|
||||
});
|
||||
|
||||
const upsertedDoc = await db.update({update: true, data: 'First update'}, {$set: {data: 'Third update'}}, {upsert: true});
|
||||
|
||||
expect(upsertedDoc.update).to.be.true;
|
||||
expect(upsertedDoc.data).to.equal('Third update');
|
||||
|
||||
numberOfUpdatedDocs = await db.update({data: 'Third update'}, {$set: {data: 'Fourth update'}}, {upsert: true});
|
||||
|
||||
expect(numberOfUpdatedDocs).to.equal(1);
|
||||
});
|
||||
|
||||
it('Finding documents', async () => {
|
||||
const data1 = 'Text data 1';
|
||||
const data2 = 'Text data 2';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue