InternetProgramming/Lab3/node_modules/lowdb/examples/fp.md
2023-12-15 14:42:22 +04:00

1.2 KiB

lowdb/lib/fp

⚠️ Experimental

lowdb/lib/fp lets you use lodash/fp, Ramda or simple JavaScript functions with lowdb.

It can help reducing the size of your bundle.js

Usage

import low from 'lowdb/lib/fp'
import { concat, find, sortBy, take, random } from 'lodash/fp'

const db = low()

// Get posts
const defaultValue = []
const posts = db('posts', defaultValue)

// replace posts with a new array resulting from concat
// and persist database
posts.write(
  concat({ title: 'lowdb is awesome', views: random(0, 5) })
)

// Find post by id
const post = posts(
  find({ id: 1 })
)

// Find top 5 fives posts
const popular = posts([
  sortBy('views'),
  take(5)
])

API

lowdb/lib/fp shares the same API as lowdb except for the two following methods.

db(path, [defaultValue])([funcs])

Returns a new array or object without modifying the database state.

db('posts')(filter({ published: true }))

db(path, [defaultValue]).write([funcs])

Add .write when you want the result to be written back to path

db('posts').write(concat(newPost))
db('user.name').write(set('typicode'))