typicode / lowdb

Simple and fast JSON database
MIT License
21.49k stars 923 forks source link

lodash set() and defaults() not working #487

Closed amsam0 closed 3 years ago

amsam0 commented 3 years ago

Hi,

I recently came across a problem when using db.chain. It may also affect other methods but I'm not sure.

db.chain.set()

If your database is this: { "test": { "test2": 1 } } And your code is this:

import lodash from "lodash";
import { LowSync, JSONFileSync } from "lowdb";

const db = new LowSync(new JSONFileSync("test.json"));

db.read();

db.chain = lodash.chain(db.data);

console.log("Before: " + db.data.test.test2);

db.chain.get("test").set("test2", 2);

console.log("After: " + db.data.test.test2);

Running it will output this:

Before: 1
After: 1

db.chain.defaults()

If your database is empty ({}) and your code is this:

import lodash from "lodash";
import { LowSync, JSONFileSync } from "lowdb";

const db = new LowSync(new JSONFileSync("test.json"));

db.read();

db.chain = lodash.chain(db.data);

db.chain.defaults({
    test: 1,
});

console.log(db.data);

It will output this:

{}

This only started happening when I moved to v2.

Thanks for looking at this issue and sorry for the large size

typicode commented 3 years ago

Could you try adding db.chain.defaults(...).value(), value executes chain.

amsam0 commented 3 years ago

That fixed both of them, maybe this should be added to the lodash part of the readme or something?

typicode commented 3 years ago

I've added a comment, thanks for the feedback