Skip to content

Instantly share code, notes, and snippets.

@khalidx
Last active July 29, 2020 09:25
Show Gist options
  • Save khalidx/dd457cb045582582db91c9e331aeb444 to your computer and use it in GitHub Desktop.
Save khalidx/dd457cb045582582db91c9e331aeb444 to your computer and use it in GitHub Desktop.
Using lowdb with TypeScript; instantly instantiates a typed db with defaults and compile-time-checked queries
import low from 'lowdb'
import FileSync from 'lowdb/adapters/FileSync'
export const database = <T> (defaults: T) => {
const adapter = new FileSync<T>('db.json')
const db = low(adapter)
db.defaults(defaults).write()
return db
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment