mongfaux
v0.9.1
Published
A flat JSON file database; reverse engineer of a good portion of the mongodb nodejs API; great for prototyping.
Downloads
3
Maintainers
Readme
Mongfaux
Mongfaux is a reverse engineering of the mongodb interface. It saves the data as flat JSON files for fast prototyping and/or a Git-able datastore.
Shortcomings
Mongfaux is <600 lines of code and only attempts to replicate a small, heavily used, portion of the API. You should consider using a real database if you need more.
Mongfaux uses a simple string for the auto-generated "_id" field. I would suggest ignoring _id and creating your own "id" field using something like shortid. Alternatively you can manually set your own _id with require('mongodb').ObjectID.
Doesn't have your back with things like enforcing unique keys. Inserting two documents with the same "_id" will pass through no problem.
Does not produce very helpful errors
Very little consideration has been given to performance. Use mongoDB if that's a priority!
Callbacks didn't make the cut, use promises
Installation
npm install mongfaux
Usage
const db = require('mongfaux');
await db.collection('book').insertOne({author: 'foobar', title: 'Mongfaux'});
let books = await db.collection('book').find({}).toArray();
//.json files saved in process.cwd()/mongfaux/ by default
Multiple instances
const Mf = require('mongfaux');
const db1 = Mf.instance();
const db2 = Mf.instance();
db1.name('db1');
db2.name('db2');
//mongfaux/
//--db1/
//--db2/
Queries
let longJohnBooks = await db.collection('books').find({
'author.firstName': 'John',
'pages': {
'$gte': 300
}
}).toArray();
- Understands nested queries like
{'author.firstName': 'John'}
- Accepts the following operators: $ne $gt $gte $lt $lte $in $nin $eq $exists
- Can query values in a document array.
{tags: 'blue'}
will select{_id: '', tags: ['blue', green]}
, but does not understand array query operators ($all, $elemMatch, $size)
Updates
await db.collection('book').insertOne({
title: 'Good Book',
author: 'John Doe',
reviews: 'positive'
})
await db.collection('book').findOneAndUpdate(
{title: 'Good Book'},
{
$rename: {title: 'lametitle'},
$set: {author: 'Jane Doe'},
$unset: {reviews: true}
}
);
/*{
lametitle: 'Good Book',
author: 'Jane Doe'
}*/
- Doesn't accept shorthand updates like {author: 'Jane Doe'}
- Accepts the following operators: $inc $min $max $mul $set $unset $rename $currentDate $addToSet $pop $push
Projections
let firstNames = await db.collection('book').find({}).project({
{'author.firstName': 1, _id: 0, title: 1}
}).toArray();
/*[
{author: {firtName: 'John'}, title: 'My Book'},
{author: {firstName: 'Jane'}, title: 'Hello World'}
]*/
- Accepts nested keys
- Doesn't accept operators
API
Db
.collection(name)
Returns the collection. If the collection doesn't exist, a new collection and .json file will be created
.parentDirectory( directory )
Sets the parent directory of the "mongfaux" folder where .json files are saved. Directory is process.cwd() by default.
.db( dbname )
Sets the database name. Also uses "mongfaux/dbname" folder for .json files. Has "name()" alias.
Collection
.createIndex()
Does nothing
.insertOne( document ) -> Promise
Autogenerates _id key and inserts document into the collection
- Returns e.g.
{insertedCount: 1, ops: [document], insertedId: id}
.insertMany( documents ) -> Promise
Autogenerated _id key for each document and inserts each document.
- Returns e.g.
{insertedCount: 1, ops: documents, insertedIds: []}
.find( query, options ) -> Cursor
Finds one or more documents in the collection.
- Requires .toArray() to return a promise.
- Option object accepts: sort, skip, limit, and projection
.findOne( query ) -> Promise
Finds a single document.
- Does not require .toArray()
- Options object accepts: sort, skip, and projection options
.findOneAndUpdate( query, update, options ) -> Promise
Finds and updates a single document.
- Returns e.g.
{value: document, ok: 1}
. - Options object accepts: sort, upsert, returnOriginal, projection
- returnOriginal is
true
by default
.updateOne( query, update, options ) -> Promise
Updates a single document without returning any documents.
- Returns e.g.
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
.updateMany( query, update, options ) -> Promise
Updates many documents without returning any documents.
- Returns e.g
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
.findOneAndDelete ( query, options ) -> Promise
Finds, returns, and deletes a single document.
- Returns e.g.
{ok: 1, value: document}
- Options object accepts: sort, projection
.deleteOne ( query ) -> Promise
Deletes a single document without returning any documents.
- Returns e.g.
{deletedCount: 1}
.deleteMany ( query ) -> Promise
Delete multiple documents without returning any documents.
- Returns e.g.
{deletedCount: 1}
.findOneAndReplace ( query, document, options ) -> Promise
Finds a single document and replaces it.
- Returns e.g.
{value: }
- Options object accepts: sort, upsert, returnOriginal
- returnOriginal is
true
by default
.replaceOne ( query, document, options ) -> Promise
Replaces a document without returning any documents.
- Returns e.g.
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
Cursor
.project ( object ) -> Cursor
Sets a field projection for the query result
.skip ( amount ) -> Cursor
Skips over a number of documents at the beginning of the query result
.limit ( amount ) -> Cursor
Specifies the maximum number of documents to return
.max ( indexBounds ) -> Cursor
Specifies the exclusive upper bound for a specific index
- e.g
{countingid: 10000}
.min ( indexBounds ) -> Cursor
Specifies the inclusive lower bound for a specific index
- e.g
{countingid: 10}
.sort ( object ) -> Cursor
- e.g
{date: -1}
for sorting by date in a descending order
.toArray () -> Promise
- Finishes query and returns the result.
.count () -> Promise
- Finishes query and returns the number of documents.