github typicode/lowdb v0.13.0

latest releases: v7.0.1, v7.0.0, v6.1.1...
7 years ago

This release contains breaking changes, please read the migration guide. In particular, db('name') is removed in favor of lodash _.get() and it's possible now to use _.set() to update database.

Overall, this version lets you use more of lodash API and is more flexible.

Changes

  • db('name') has been removed. It was redundant with _.get().
  • .value() won't return a promise anymore. There was a bug with it and it wasn't clear when a promise would be returned or not.
  • db.object has been replaced by db.getState() and db.setState(). Future versions of lowdb should support arrays and not only objects as root structures.
  • _.set() support makes it simpler to replace collections compared to db.object.prop = newValue; db.write().
  • low() signature has changed, writeOnChange param is now part of the option param.
  • lodash is now a peerDependency so you can have more control on which version is used.

In this release, you MUST ALWAYS call .value() at the end of a query to execute it

// won't be executed and won't create posts array in database
const chain = db.get('posts', []) 

// posts array will be created
chain.value() 

Migration guide

Installation

Since lodash is now a peerDependency, you need to add it to your dependencies.

npm install lowdb lodash@4 --save

Getting a collection

// v0.12
var posts = db('posts')
posts.value()

// v0.13
var posts = db.get('posts', [])
posts.value()

Pushing an item

// v0.12
posts.push({ title:'foo' })

// v0.13
posts.push({ title:'foo' }).value()

Replacing a collection

// v0.12
var filteredPosts = posts.filter({ published: true })

db.objects.posts = filteredPosts
db.write()

// v0.13
var filteredPosts = posts
  .filter({ published: true })
  .value()

db.set('posts', filteredPosts)
  .value()

If you're using file-async

// v0.12
var db = low('db.json', { storage: fileAsync })

db('posts')
  .push({ title: 'foo' })
  .then(() => console.log('done'))

// v0.13
var db = low('db.json', { storage: fileAsync })

// data is persisted in the background now
db.get('posts')
  .push({ title: 'foo' })
  .value()

If you need to control/know when data is written, disable writeOnChange and call db.write() manually:

var db = low('db.json', {
  storage: fileAsync,
  writeOnChange: false
})

db('posts')
  .push({ title: 'foo' })
  .value()

db.write()
  .then(() => console.log('done')

Don't miss a new lowdb release

NewReleases is sending notifications on new releases.