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

magnolia

v0.0.5

Published

Beautiful MongoDB wrapper using Q

Downloads

12

Readme

magnolia–A beautiful MongoDB driver w/ Q Build Status

Magnolia implements a coherent, lazy, & chainable interface... with promises! Don't nest callbacks anymore than you have to!

Init

If you don't mind state, you can init the module's state with default options.

var mongo = require('magnolia'),
    ObjectID = mongo.ObjectID;

mongo
    .server({host: 'localhost', port: 27117)
    .db('hello')
    .options({w: 1})
    .init(); // makes all the previous calls stored as defaults

Or you can start a chain with the defaults you would like.

var mongo = require('magnolia')
    .server({host: 'localhost', port: 27117)
    .db('hello')
    .options({w: 1});

Connection

  • magnolia(collection, [db])
  • .collection(collection)
  • .db(db)
  • .server(server)
  • .options(...)
    • m:1
    • journal:true
    • fsync:true
    • slaveOk:true

Find

magnolia('user')
    .filter({_id: ObjectID('4e4e1638c85e808431000003')}) // filter!
    .one() // just find one!
    .then(function (user) { // evaluate as a promise
        console.log('hello', user.name);
    });

magnolia('user')
    .filter({hello: 'world'})
    .toArray(function (err, docs) { /* ... */ });
  • .toArray([cb]) Query the collection, otherwise it will be lazily queried when you evaluate the chain as a promise
  • .filter(criteria) Filter the collection
  • .one() Find one! And return the document, instead of a list.
  • .limit(n).skip(m) to control paging.
  • .sort(fields) Order by the given fields. There are several equivalent syntaxes:
    • .sort([['field1', 'desc'], ['field2', 'asc']])
    • .sort([['field1', 'desc'], 'field2'])
    • .sort('field1') ascending by field1

Find and modifiy

magnolia('user')
    .filter(query)
    .sort(sort)
    .options(options)
    .findAndModify(objNew, [options], [callback]);

Useful options (including the previous options):

  • .filter(...)
  • .sort(...)
  • .options(...)
    • remove:true set to a true to remove the object before returning
    • new:true set to true if you want to return the modified object rather than the original. Ignored for remove.
    • upsert:true Atomically inserts the document if no documents matched.

Remove

magnolia('user')
    .filter(query)
    .remove(extra_query)
    .then(function (remove_count) { /* ... */ });

Insert

magnolia('user')
    .insert({name: 'ryan', company: 'Submersible'}, {safe: true})
    .then(function (doc) { /* ... */ });

magnolia('user')
    .safe()
    .insert([{foo: 'bar'}, {hello: 'world'}], function (err, docs) {
        /* ... */
    });
  • .safe() or .unsafe() Make sure document is in the database before returning
  • .options(...)
    • safe:true

Update; update and insert (upsert)

Signature:

magnolia('user')
    .filter(criteria)
    .update(update, [options], [callback]);
magnolia('user')
    .filter(criteria)
    .upsert(objNew, [options], [callback]);

Useful options:

  • .filter(...)
  • .one() or .multi()
  • .safe() or .unsafe()
  • .options(...)
    • safe:true Should always set if you have a callback.
    • multi:true If set, all matching documents are updated, not just the first.
    • upsert:true Atomically inserts the document if no documents matched.

Save

Performs an update if there's an _id, and an insert if not!

magnolia('user').save({_id: ObjectID('50c03c9c766c8598e0000002'), foo: 'bar'}); // update
magnolia('user').save({hello: 'world'}); // insert

Count

magnolia('user').count([filter], [cb]);

Map/reduce

Data types

magnolia.Long(numberString)
magnolia.ObjectID(hexString)
magnolia.Timestamp()
magnolia.DBRef(collectionName, id, dbName)
magnolia.Binary(buffer)
magnolia.Code(code, [context])
magnolia.Symbol(string)
magnolia.MinKey()
magnolia.MaxKey()
magnolia.Double(number)

TODO

  1. Think of things that are left todo

  2. commands

  3. insert DONE

  4. remove DONE

  5. update DONE

  6. count DONE

  7. upsert DONE

  8. findAndModify DONE

  9. toArray DONE

  10. nextObject

  11. each

  12. ensureIndex?

  13. wtf is aggregation?

  14. mapreduce

  15. db

  16. collection

  17. server

  18. wrap the data types

  19. can i test a bad connection?

  20. features

  21. nextObject

  22. each

  23. ensureIndex

  24. map/reduce

  25. queueing, max connections

  26. raw mongodb connection

  27. fix

  28. find's limit & sort

  29. does server work?

Check this

  1. think of the options, and what can be made coherent
  2. do it!
  3. write test for that method
  4. document