# magnolia

> Beautiful MongoDB wrapper using Q

Latest version **0.0.5** (published 2013-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install magnolia
pnpm add magnolia
yarn add magnolia
bun add magnolia
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2013-05-08 |
| First published | 2012-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 66 |
| Author | Ryan Munro |
| Maintainers | munro |
| Keywords | mongodb, mongo, q, promise, easy |

## Links

- npm: https://www.npmjs.com/package/magnolia
- Repository: git@github.com:Submersible/node-magnolia
- npm.io page: https://npm.io/package/magnolia

## Dependencies (4)

- [q](https://npm.io/package/q.md) ~0.8.12
- [dsl](https://npm.io/package/dsl.md) 0.0.2
- [lodash](https://npm.io/package/lodash.md) ~1.0.1
- [mongodb](https://npm.io/package/mongodb.md) ~1.2.14

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.0.5 (latest) — 2013-05-08
- 0.0.4 — 2013-01-27
- 0.0.3 — 2013-01-14
- 0.0.2 — 2012-12-11
- 0.0.1 — 2012-12-07

## README

# magnolia&ndash;A beautiful MongoDB driver w/ Q [![Build Status](https://secure.travis-ci.org/Submersible/node-magnolia.png?branch=master)](http://travis-ci.org/Submersible/node-magnolia)

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.

```javascript
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.

```javascript
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

```javascript
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

```javascript
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

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

## Insert

```javascript
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:

```javascript
magnolia('user')
    .filter(criteria)
    .update(update, [options], [callback]);
```

```javascript
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!

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

## Count

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

## Map/reduce

## Data types

```javascript
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)
```

---
_Source: https://npm.io/package/magnolia · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
