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

mniam

v4.1.0

Published

Yet another mongodb facade.

Downloads

32

Readme

NPM version Build Status Dependency Status

mniam

Yet another mongodb native driver facade. Takes care of:

  • mongo URI parsing
  • opening and closing DB connections
  • opening collections

Install by running

npm install mniam

API

database(url, [options])

Connect to database mniam-test and create friends collection with index on name field.

const db = database('mongodb://localhost/mniam-test');
const friends = db.collection({
  name: 'friends',
  indexes: [[{ name: 1 }]]
});

Mniam is using MongoClient to establish the connection: full mongo database URLs are supported. The database function also takes a hash of options divided into db/server/replset/mongos allowing you to tweak options not directly supported by the unified url string format.

const db = database('mongodb://localhost/mniam-test', {
  db: {
    w: -1
  },
  server: {
    ssl: true
  }
});

collection.save

Add a new documents:

const item = await friends.save({
  name: 'Alice',
  age: 14,
};
console.log('Item id:', item._id);

collection.findOneAndUpdate

Update a document:

const item = await friends.findAndModify({ _id: item._id }, {
  $set: { age: 15 }
});
console.log('Alice is now:', item.age);

collection.deleteOne

Remove a document:

await friends.deleteOne({ name: 'Alice' });

Iteration

Use query, fields and options to create and configure cursor. Iterate over the results of the query using toArray, eachSeries, eachLimit methods.

  • items - can be used as async iterator
for await (const friend of friends.query({ age: { $gt: 21 } }).items()) {
  console.log('My friend over 21 years old', friend.name);
}
  • toArray - converts query results into array
const arr = await friends.query({ age: { $gt: 21 } }).toArray();
console.log('My friends over 21 years old', arr);
  • eachSeries - calls onItem sequentially for all results
await friends
  .query()
  .fields({ name: 1 })
  .eachSeries(async item => console.log('Name:', item.name));
console.log('All friends listed.');
  • eachLimit - iterates over all results calling onItem in parallel, but no more than limit at a time
await friends
  .query()
  .options({ sort: { age: 1 } })
  .eachLimit(4, async item => console.log('Friend', item));
console.log('All friends listed.');

Aggregation

Mniam collections provides flex API for aggregation pipeline:

const results = await friends
  .aggregate()      // start pipeline
  .project({ author: 1, tags: 1 })
  .unwind('$tags')
  .group({
    _id : { tags : '$tags' },
    authors : { $addToSet : '$author' },
    count: { $sum: 1 }
  })
  .sort({ count: -1 })
  .toArray();
console.log(results);

In addition to toArray you can use eachSeries and eachLimit to iterate over aggregation results. Each aggregation stage ($project, $unwind, $sort, etc.) has a corresponding function with the same name (without $). You can also pass a traditional array of stages to .pipeline(stages) method, and set options with .options({}) method.

License

MIT