Skip to content

@typicode typicode released this May 20, 2021 · 29 commits to main since this release

  • Out of the box and improved TypeScript support.
  • Uses new steno version for fast async file writes.
  • Uses ECMAScript modules.
  • Plain JS can now be used to modify and query db.data
  • Reduced install size.
  • With native JavaScript improvements, lodash is now optional (you can still use it though as it provides powerful utilities).

To help with OSS funding, lowdb v2 is released under Parity license for a limited time. It'll be released under MIT license once the goal of 100 sponsors is reached (currently at 55) or in five months. See README for complete explanation.

You can sponsor me on GitHub Sponsors.

Thank you!

Migrate

Lowdb v1

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

// Set some defaults
db.defaults({ posts: [], user: {} })
  .write()

// Add a post
db.get('posts')
  .push({ id: 1, title: 'lowdb is awesome'})
  .write()

// Set a user name using Lodash shorthand syntax
db.set('user.name', 'typicode')
  .write()

Lowdb v2

import { Low, FileSync } from 'lowdb'

const adapter = new FileSync('db.json')
const db = new Low(adapter)

// Set some defaults
db.data ||= { posts: [], user: {} })
db.write()

// Add a post
db.data
  .posts
  .push({ id: 1, title: 'lowdb is awesome'})
db.write()

// Set a user name using plain JS
db.data.user.name = 'typicode'
db.write()

If you're using TypeScript, data can now be typed:

type Post = {
  id: number
  title: string
}

type Data = {
  posts: Post[]
}

const db = new Low<Data>(adapter)

To continue using lodash with lowdb:

npm install lodash
import lodash from 'lodash'

// Set a user name using Lodash shorthand syntax
db.chain = lodash.chain(db.data)
db.chain
  .set('user.name', 'typicode')
  .value()
db.write()

Breaking changes

The following methods and properties have been removed:

  • db.getState (use db.data instead)
  • db.setState (use db.data = ... instead)
  • db._
1 person reacted
Assets 2