npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@abtnode/mongoose-nedb

v1.0.32

Published

A Mongoose driver for NeDB.

Downloads

38

Readme

mongoose-nedb

A Mongoose driver for NeDB, most APIs are compatible.

Limitations

  • Operators
    • $pushAll, already removed in latest mongoose and mongodb
    • $isolated
  • Indexes
    • 2dsphere
  • API
    • update: can not return affected just as mongodb does, https://github.com/louischatriot/nedb#updating-documents
    • findOneAndUpdate
    • findAndRemove
    • findAndModify

Installation

npm install @abtnode/mongoose-nedb @abtnode/nedb [email protected] --save

Usage

1. Setup

require('@abtnode/mongoose-nedb').install();

const os = require('os');
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');

mongoose.set('debug', true);
mongoose.Promise = Promise;

const dbPath = path.join(os.tmpdir(), 'nedb');
fs.mkdirSync(dbPath, { recursive: true });

const ensureConnection = () => {
  return new Promise((resolve, reject) => {
    // This is needed
    mongoose.connect('mongodb://localhost/test', { dbPath });
    const db = mongoose.connection;
    db.on('error', (err) => {
      console.error.bind(console, 'connection error:');
      reject(err);
    });
    db.once('open', async () => {
      console.log('connected', dbPath);
      resolve(db);
    });
  });
};

2. Schema and Models

const Kitten = mongoose.model(
  'Kitten',
  new mongoose.Schema(
    {
      name: String,
    },
    { collection: 'kitties' }
  )
);

3. Query Documents

const ensureCallbackQuery = () =>
  new Promise((resolve, reject) => {
    Kitten.find({}, (err, items) => {
      if (err) {
        console.error('items find callback error', err);
        reject(err);
        return;
      }
      console.log(
        'find callback',
        items.map((x) => x.toJSON())
      );
      resolve(items);
    });
  });

const ensureComposeQuery = () =>
  new Promise((resolve, reject) => {
    const query = Kitten.find({});
    query.skip(2).limit(2).sort({ name: -1 });
    query.exec((err, items) => {
      if (err) {
        console.error('items find exec error', err);
        reject(err);
        return;
      }
      console.log(
        'find exec',
        items.map((x) => x.toJSON())
      );
      resolve(items);
    });
  });

const ensurePromiseQuery = () =>
  new Promise((resolve, reject) => {
    Kitten.find({})
      .then((items) => {
        console.log(
          'find promise',
          items.map((x) => x.toJSON())
        );
        resolve(items);
      })
      .catch((err) => {
        console.error('items find promise error', err);
        reject(err);
      });
  });

const ensurePromiseQueryOne = () =>
  new Promise((resolve, reject) => {
    Kitten.findOne({ _id: '5f81a4b1d877087c9e237f32' })
      .then((item) => {
        console.log('findOne promise', item.toJSON());
        resolve(item);
      })
      .catch((err) => {
        console.error('findOne promise error', err);
        reject(err);
      });
  });

const ensureCollectionAPI = (db) =>
  new Promise((resolve, reject) => {
    const collection = db.collection('kitties');
    collection.insert({ name: 'hello' }, (err, result) => {
      console.log('item inserted', err, result);
      if (err) {
        reject(err);
        return;
      }

      collection.find({}, (e, results) => {
        console.log('items find', e, results);
        if (e) {
          reject(e);
          return;
        }
        resolve();
      });
    });
  });

4. Update Documents

const ensureCallbackCreate = () =>
  new Promise((resolve, reject) => {
    const kitten = new Kitten();

    kitten.on('error', (err) => {
      console.error('create callback error', err);
      reject(err);
    });

    kitten.name = 'hello world';
    kitten.save((err, result) => {
      if (err) {
        console.error('create callback error', err);
        reject(err);
        return;
      }

      console.log('create callback success', { err, result: result.toJSON() });
      resolve(result);
    });
  });

const ensureCallbackUpdate = (doc) =>
  new Promise((resolve, reject) => {
    Kitten.update({ _id: doc._id }, { name: `${doc.name} ${Math.random()}` }, (err, result) => {
      if (err) {
        console.error('update callback error', err);
        reject(err);
        return;
      }

      console.log('update callback success', { err, result });
      resolve(result);
    });
  });

5. Put things together

ensureConnection().then(async (db) => {
  try {
    const doc = await ensureCallbackCreate();
    await ensureCallbackUpdate(doc);
    await ensureCallbackQuery();
    await ensureComposeQuery();
    await ensurePromiseQuery();
    await ensurePromiseQueryOne();
    await ensureCollectionAPI(db);
  } catch (err) {
    console.error(err);
  }
});