Mark
Mark

Reputation: 5038

lowdb: update record at index N

I'm using lowdb (https://github.com/typicode/lowdb) with a persistent storage (file-async):

const low = require('lowdb');
const db = low('db.json', {storage: require('lowdb/lib/storages/file-async')});
db.defaults({inputs: []}).value();

From a client I receive this object:

var data = {
  "idx": 1,
  "item": { "Name": "blabla", "Surname": "bleble" }
}

I want to update the record number idx with the new information received. This doesn't update anything:

if (data.idx < db.get("inputs").size()) {
    db.get("inputs").nth(data.idx)
        .assign(data.item)
        .write();
}

In my actual data I have more than 2 items.

Upvotes: 1

Views: 1902

Answers (1)

Mark
Mark

Reputation: 5038

Here the solution:

db.get('inputs')
    .nth(data.idx)
    .assign(data.item)
    .value();
db.write();

Please note the last method should be a separate call. Putting the write() call at the end of the chain doesn't work, at least in my setup.

Upvotes: 1

Related Questions