Code Monkey home page Code Monkey logo

nedb-promises's Introduction

nedb-promises

A dead-simple promise wrapper for nedb.

Check out the docs.

const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')

// #1
datastore.find({ field: true })
  .then(...)
  .catch(...)
  
// #2
datastore.find({ field: true })
  .exec(...)
  .then(...)
  .catch(...)

// #1 and #2 are equivalent

datastore.findOne({ field: true })
  .then(...)
  .catch(...)
  
datastore.insert({ doc: 'yourdoc' })
  .then(...)
  .catch(...)
  
// or in an async function
async function findSorted(page, perPage = 10) {
  return await datastore.find(...)
      .sort(...)
        .limit(perPage)
        .skip(page * perPage)
}

Installation

npm install --save nedb-promises

Usage

Everything works as the original module, with four major exceptions.

  • There are no callbacks.
  • loadDatabase has been renamed to load.
  • You should call Datastore.create(...) instead of new Datastore(...). This way you can access the original nedb properties, such as datastore.persistence.
  • As of v2.0.0 the module supports events ๐Ÿ˜Ž... Check out the docs about events!

Check out the original docs!

load( )

You don't need to call this as the module will automatically detect if the datastore has been loaded or not upon calling any other method.

const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')
datastore.load(...)
  .then(...)
  .catch(...)

find( [query], [projection] ), findOne( [query], [projection] ), count( [query] )

These methods will return a Cursor object that works the same way it did before except when you call "exec" it takes no arguments and returns a Promise. The cool thing about this implementation of the Cursor is that it behaves like a Promise. Meaning that you can await it and you can call .then() on it.

const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')

//outside Promise chain
datastore.find(...)
  .then(...)
  .catch(...)
  
//insinde Promise chain
datastore.insert(...)
  .then(() => {
    return datastore.find(...)
  })
  .then(
    // use the retrieved documents
  )

;(async () => {
  await datastore.find(...).sort(...).limit()
})()

other( ... )

All the other methods will take the same arguments as they did before (except the callback) and will return a Promise.

Check out the docs.

nedb-promises's People

Contributors

bajankristof avatar catmade avatar dependabot[bot] avatar kphrx avatar krocans avatar marcelwaldvogel avatar npeterkamps avatar samdenty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.