typicode / lowdb

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

Is it still possible to get the value in lodash? #499

Closed hoonion90 closed 3 years ago

hoonion90 commented 3 years ago

Is it still possible to get the value in lodash? I can't..

import { join, dirname } from 'path'
import { Low, JSONFile } from 'lowdb'
import { fileURLToPath } from 'url'
import lodash from 'lodash'

const __dirname = dirname(fileURLToPath(import.meta.url));

// Use JSON file for storage
const file = join(__dirname, 'db.json')
const adapter = new JSONFile(file)
const db = new Low(adapter)

// ...
// Note: db.data needs to be initialized before lodash.chain is called.
db.chain = lodash.chain(db.data)

// Instead of db.data, you can now use db.chain if you want to use the powerful API that lodash provides
const post = db.chain
  .get('posts')
  .find({ id: 1 })
  .value() // Important: value() needs to be called to execute chain

console.log(post)

console.log(db.chain
    .get('posts'))

db.json

{
  "posts": [
    {"id": 1, "title":"lowdb is awesome"}
  ]
}

result

undefined
LodashWrapper {
  __wrapped__: null,
  __actions__: [ { func: [Function: get], args: [Object], thisArg: [Function] } ],
  __chain__: true,
  __index__: 0,
  __values__: undefined
}
hoonion90 commented 3 years ago

import { Low, JSONFile } from 'lowdb'

convert sync and I solved it