typicode / lowdb

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

does lowdb support dot syntax? #561

Closed imxuedi closed 1 year ago

imxuedi commented 1 year ago

I'm from 'electron-store', the dot syntax attracts me, like:

const store = new Store()
const obj = {
  key1: 'hello',
  key2: [
    {subkey1: 100},
    {subkey2: 200}
  ]
}

I can visit subkey1 by store.get("obj['key2'][0]"), I just need to prepare a path string.

I konw that Js supports dynamic property like:

const key = 'key2'
obj[key]

but what if multi dynamic property?

I have no idea about how to realize above by lowdb or could anyone give me some suggestions? Thanks.

imxuedi commented 1 year ago

Definitely Sure!

I find that lodash can do this.

Lodash YYDS. LowDB YYDS.

typicode commented 1 year ago

It doesn't support it but it can be added with another library.

For example, with dot-prop: https://github.com/sindresorhus/dot-prop

getProperty(db.data, 'foo.bar')

With lodash, see: https://github.com/typicode/lowdb#lodash

Lodash get() supports it.

imxuedi commented 1 year ago

Thanks for your generous answer. It indeed solves my problem.

typicode commented 1 year ago

You're welcome. Glad it helped :)