typicode / lowdb

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

Writing data doesn't work when using the Lodash Example #565

Closed orangecoding closed 1 year ago

orangecoding commented 1 year ago

Hi,

If I'm building a class extending LowSync to use LowDash, writing the actual content to the file doesn't work anymore.

import lodash from 'lodash';
import { LowSync } from 'lowdb';

export default class Lowdash extends LowSync {
  constructor(adapter) {
    super(adapter);
    this.chain = lodash.chain(this).get('data');
  }
}

This is an example of how I write data.

  db.chain.set("someKey", someValue);
  db.write();
typicode commented 1 year ago

Hi, You need to call .value() at the end of lodash chain

orangecoding commented 1 year ago

I did that and it still does not write anything to the file.

  db.chain.set("someKey", someValue).value();
  db.write();

Complete Example:

import { JSONFileSync } from 'lowdb/node';
import { getDirName } from '../../utils.js';
import path from 'path';
import LowdashAdapter from './LowDashAdapter.js';

const file = path.join(getDirName(), '../', 'db/listings.json');
const adapter = new JSONFileSync(file);
const db = new LowdashAdapter(adapter);
db.read();

(...)

  db.chain.set("someKey", someValue).value();
  db.write();
orangecoding commented 1 year ago

ok sry, I did something wrong. works now.