typicode / lowdb

Simple and fast JSON database
MIT License
21.35k stars 918 forks source link

lodash not writing to database #511

Closed SpellChucker closed 2 years ago

SpellChucker commented 2 years ago

I've tried just about everything, but I cannot get lodash to update my database. Maybe I have a fairly complex db but I don't think so:

{
  "strategies": [
    {
      "id": 1,
      "name": "strat",
      "orders": []
    }
  ]
}

I am trying to add an order to this strategy:

I've tried:

db.chain.get('strategies').find({ id: 1 }).get('orders').push(order).value();
await db.write();
const orders = db.chain.get('strategies').find({ id: 1 }).get('orders').push(order).value();

db.chain.get('strategies').find({ id: 1 }).get('orders').assign(orders).value();
await db.write();

The DB data just does not change. Any idea? It works if I just use plain javascript of course, but not with lodash.

Arondight commented 2 years ago

how you solve it ?

SpellChucker commented 2 years ago

I didn't 😆 I switched to sqlite. The only way I could think of fixing it was overwriting the entire sub-collection (orders in my example) with the update. Or splitting the sub collection out into it's own top-level key. But in that case it would just be a relational DB so I just switched.

Arondight commented 2 years ago

@SpellChucker i found push is not a lodash function, so i use db.data[key].push(obj) instead of db.chain.get(key).push(obj).value(), it is work well