4.1.0 • Published 2 months ago

mniam v4.1.0

Weekly downloads
10
License
MIT
Repository
github
Last release
2 months ago

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

4.1.0

2 months ago

4.0.1

5 months ago

4.0.0

5 months ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.0

2 years ago

2.2.0

3 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.2

7 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.6.2

7 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.2

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.3.0

11 years ago

0.2.0

11 years ago

0.1.0

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago