Giter VIP home page Giter VIP logo

nedb-promise's Introduction

nedb-promise

A promise wrapper for NeDB.

Installation

Install with NPM:

$ npm i --save nedb-promise

Usage

Example using ES7 async/await:

import datastore from 'nedb-promise'

async function doDatabaseStuff() {
  let DB = datastore({
     // these options are passed through to nedb.Datastore

     filename: 'my-db.json',

     autoload: true // so that we don't have to call loadDatabase()
  })

  await DB.insert([{
    num: 1, alpha: 'a'
  }, {
    num: 2, alpha: 'b'
  }])

  let document = await DB.findOne({ num: 1 })

  // use NeDB cursors:
  let documents = await DB.cfind({})
    .projection({ num: 1, _id: 0 })
    .exec()
}

doDatabaseStuff()

API

datastore(options)

Returns an object that proxies to an internal nedb.Datastore instance (options are passed through to the NeDB constructor), with promisified methods.

It also includes extension methods cfind(...), cfindOne(...), and ccount(...) that return promisified cursors, so that you may do:

let results = await myDataStore.cfind({ moo: 'goo' })
  .projection({ moo: 1, _id: 0 }) // see NeDB cursor methods
  .exec()

datastore.fromInstance(nedbInstance)

Use this method if you already have a NeDB instance that you want to wrap with the promisified methods.

Testing

Run tests with mocha (after building):

$ mocha test.js

License

Copyright (c) 2015, Jonathan Apodaca [email protected] Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

nedb-promise's People

Contributors

elisaado avatar jrop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nedb-promise's Issues

Update NeDB

Hi. Could you update NeDB to latest version ?

Promise swallowing error from datastoreInstance.find()

datastoreInstance.find() hangs forever, but works when datastoreInstance.find({}) is used instead.
The issue is that the predicate argument is required.

The error that explains this should be bubbled to the top so it can be caught instead of waiting forever.

.count() ends process

Using database.count() ends the current process.
Other functions working as expected.

Datastore.persistence is undefined.

Datastore.persistence is undefined, therefore can't do NeDB documented functions like Datastore.persistence.compactDatafile();

Was not able to find temporary workaround unlike the very similar library "nedb-promises" (extra "s"), which allows Datastore.__original.persistence.compactDatafile(); etc.

Reported this problem to both "nedb-promise" and "nedb-promises" projects. Similar project, similar problem :)

Although the code of the "nedb-promise" version (this version) looks better.

cannot update twice

callback style:

const Datastore = require('nedb-promise')
app.get('/reset', (req, res) => {
  db.update({}, {$set: {open: false}}, { multi: true }, (err, result) => {
    if (err) {
      res.send(err)
    }else{
      res.send('result')
    }
  })
})

problem: if request twice, the process will blocked, But i switch to native nedb works

Async/Await style:

app.get('/reset', async (req, res) => {
  let result = await db.update({}, {$set: {open: false}}, { multi: true })
  res.send(result)
})

problem: first request will throw following error in console

DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

.limit(10) not working

this code

    return app.dbPages.find(slctr).limit(10).then(resp => {
      return {
        pages: resp,
        allTags: app.allTags
      }
    }).catch(err => console.log(err))

generate error:
app.dbPages.find(...).limit is not a function

Bump major version for non-backwards compatible api changes

The recent changes in b09020e change the api in a non-backwards compatible way.

Please bump the package.json major version to prevent erorrs when installing this as dependency with ^1.0.0 specified as the version. I believe this is the default semantic version npm uses.

Is it hard to add browser support?

Before anything else I just want to thank you for the wrapper. I'm interested in using the browser version of nedb nedb/browser-version/out/nedb.min.js, but as it seems the wrapper does not support it.
In theory there are two options:
1 - Create a copy of the wrapper, but insted of importing nedb, importing nedb/browser-version/out/nedb.min.js
2 - Create a exclusive option like _browser. If this option is enabled, the wrapper imports the browser version instead of node's.

TypeError: Cannot read property 'setAutocompactionInterval' of undefined

My database has several duplicates (same _id), even after restarting and using autoload: true. This is strange, but I decided to solve it by using the setAutocompactionInterval as is suggested in the guide in the repository, and the result was simply TypeError: Cannot read property 'setAutocompactionInterval' of undefined.

const Db = require('nedb-promise')
	, curry = new Db({
		filename: 'curry'
		, autoload: true
		, onload: (e) => e && console.err(e)
	})

curry.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'setAutocompactionInterval' of undefined
Db.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'setAutocompactionInterval' of undefined
Db.curry.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'persistence' of undefined
Db.compactDatafile()
// TypeError: Db.compactDatafile is not a function
curry.compactDatafile()
// TypeError: curry.compactDatafile is not a function

I don't know what causes this error. Am I misunderstanding the way it should work? Nobody seems to have this error, according to my Google searches.

Maximum call stack size exceeded at BinarySearchTree.compareThings

this happens when trying to delete some specific records
record itself is perfectly valid and can be read without issues, just cannot be deleted
note this happens to only small number of records in database, but its fully reproducible with specific record in question

const data = await db.findOne({ image: param });
if (data) await db.remove({ image: data.image }, { multi: false });

causes immediate crash with trace as follows:

/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/nedb/lib/model.js:175
const compareThings = (a, b, _compareStrings) => {
                      ^

RangeError: Maximum call stack size exceeded
    at BinarySearchTree.compareThings [as compareKeys] (/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/nedb/lib/model.js:175:23)
    at BinarySearchTree.delete (/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/binary-search-tree/lib/bst.js:348:14)
    at BinarySearchTree.delete (/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/binary-search-tree/lib/bst.js:354:40)
    at BinarySearchTree.delete (/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/binary-search-tree/lib/bst.js:354:40)
    at BinarySearchTree.delete (/home/vlado/dev/pigallery/node_modules/.pnpm/@[email protected]/node_modules/@seald-io/binary-search-tree/lib/bst.js:354:40)

workaround is to run node --stack-size=8192 and then there are no issues,
but this really should not be necessary for a DB of this size
(database itself is ~500MB with 10K records in a table)

package versions are nedb-promises 5.0.0 with included version of @seald-io/nedb 2.0.3

environment is nodejs 16.0.0 on ubuntu 21.04

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.