Code Monkey home page Code Monkey logo

monk's Introduction

Monk


A tiny layer that provides simple yet substantial usability improvements for MongoDB usage within Node.JS.

build status codecov Join the chat at https://gitter.im/Automattic/monk

note: monk 2.x drop the support for node < 0.12. If you are still using an earlier version, stick to monk 1.x

const db = require('monk')('localhost/mydb')
// or
// const db = require('monk')('user:pass@localhost:port/mydb')

const users = db.get('users')

users.index('name last')
users.insert({ name: 'Tobi', bigdata: {} })
users.find({ name: 'Loki' }, '-bigdata').then(function () {
  // exclude bigdata field
})
users.find({}, {sort: {name: 1}}).then(function () {
  // sorted by name field
})
users.remove({ name: 'Loki' })

db.close()

Features

  • Well-designed API signatures
  • Easy connections / configuration
  • Command buffering. You can start querying right away
  • Promises built-in for all queries. Easy interoperability with modules
  • Auto-casting of _id in queries
  • Allows to set global options or collection-level options for queries. (eg: castIds is true by default for all queries)

Middlewares

Most of the Monk's features are implemented as middleware.

There are a bunch of third-parties middlewares that add even more functionalities to Monk:

Created an nice middleware? Send a PR to add to the list!

How to use

Documentation

License

MIT

monk's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

monk's Issues

Connection Status

Is there any way to know if a call like:

var db = monk('myURL')

actually opened a connection or not?

e.g. if my Mongo instance is down, I'd like to know about it.

There doesn't seem to be any kind of 'connection' object, Better would be something like:

var db = monk('myURL')
db.on('success')...
db.on('err')....

Monk "autocasts" strings to ObjectId types where supplied.

I'm building an API using Express and Monk that connects to a database where wrote are mainly handled by a Meteor application.

I know that Meteor uses its own algorithm to generate IDs. So when I do something like that:

id = "aczXLTjzjjn3PchX6" // this is an ID generated by Meteor (not a valid MongoID)

Users.findOne({ _id: id }, function(err, doc) {
    console.log(doc);
});

Monk outputs:

Argument passed in must be a single String of 12 bytes or a string of 24 hex characters.

This way, it seems very tricky to me to design a solid and reliable REST API.

Read More: http://stackoverflow.com/questions/25889863/play-with-meteor-mongoid-in-monk

cache database connection in module space

I like this future from mongoose as you do not need to handle dependency injection.

var monk = require('monk');

// connection still the same
monk('localhost/test');

// however this will return collection
monk.get('users');

What do you think?

close connection after program terminates

Not really sure what's going on here, or if it's an issue with another layer.

But when running one-off test scripts, the connection stays open (so the node program won't terminate), unless you explicitly close it.

I think there could be a reasonable default to close the connection, like listen for one of the signals or something.

why no save method?

Just this quick question because update and save are different. Is it something to do with the language of an orm/odm? Create gets used on new models and update gets used on existing models which leaves save nowhere? The only problem with update is I cant reset the _id

Invalid Database Name Error for Valid Database Names

Apologies in advance if this isn't a monk issue, however in mongodb/lib/mongodb/db.js line 233, I am being told that my database name is invalid:

Error: database names cannot contain the character '.'

My database name looks pretty valid to me:

my-database-name,10.*.*.*:27017

(IP address masked to protect the innocent, but it's a real IP address)

Am I being obtuse for expecting an IP address or fully qualified domain name to work in this instance?

Controlling Connection timeouts?

Hi,

I am new to Monk but have found it very useful in writing a rest web service backed by mongodb. However, in writing some unit tests for my REST web service I realised that I do not have a way to control connections timeouts.

How would I go about specifying that requests should timeout after say 2 seconds?

Thanks,

David

Can't use generic string _ids

Monk always casts any _id parameter that's a string into a BSON ObjectID. This prevents the insertion of new records that use a string _id that doesn't conform to ObjectID spec and it prevents the retrieval of existing records.

Is this restriction by design? Sometimes it's nice to be able to assign your own unique string identifiers (e.g., for upserting stats data).

Not sure if or how you'd want to support this. Maybe a global option to not auto-cast these ids? Happy to work on a pull request if you're OK supporting the feature.

Timing of queries affects promise handler arguments

I seem to have an issue whereby the two concurrent queries cause promise return values to be inconsistent. For instance, take the following code:

  var db = require("monk")("localhost/trumps");
  var Games = db.get("games");
  var Cards = db.get("cards");
  var userPromise = Cards.findById("53bd19796adf5b842e7f2b4d");
  var aiPromise = Cards.findById("53bd19796adf5b842e7f2b5a");
    var userCard;
    var aiCard;
    userPromise.then(function(userCardDoc) {
      userCard = userCardDoc;
      return "First Promise Return"
    }).chain(aiPromise).then(function(aiCardDoc) {
      aiCard = aiCardDoc;
      return "Second Promise"
    }).onFulfill(function(){
      console.log("both fulfilled ", userCard, aiCard);
    });

results in the following output:

both fulfilled
{ _id: 53bd19796adf5b842e7f2b4d,
  image: 'images/austria/cards/teams_austria__0014_nakustealth.jpg',
  name: 'teams_austria__0014_nakustealth.jpg',
  attributes:
   [ { name: 'wingspan', value: 14, unit: 'meters' },
     { name: 'speed', value: 19, unit: 'km/s' },
     { name: 'weight', value: 74, unit: 'kgs' },
     { name: 'creativity', value: 0, unit: 'points' } ] }
First Promise Return

notice that the value of the argument passed into the second promise handler gets set to the return value of the first handler.

In contrast, when I set a timeout:

  var db = require("monk")("localhost/trumps");
  var Games = db.get("games");
  var Cards = db.get("cards");
  var userPromise = Cards.findById("53bd19796adf5b842e7f2b4d");
  var aiPromise = Cards.findById("53bd19796adf5b842e7f2b5a");


  setTimeout(function(){

    var userCard;
    var aiCard;
    userPromise.then(function(userCardDoc) {
      userCard = userCardDoc;
      return "First Promise Return"
    }).chain(aiPromise).then(function(aiCardDoc) {
      aiCard = aiCardDoc;
      return "Second Promise"
    }).onFulfill(function(){
      console.log("both fulfilled")
      console.log(userCard);
      console.log(aiCard);
    });

  }, 100);

I get the expected results:

both fulfilled
{ _id: 53bd19796adf5b842e7f2b4d,
  image: 'images/austria/cards/teams_austria__0014_nakustealth.jpg',
  name: 'teams_austria__0014_nakustealth.jpg'}
{ _id: 53bd19796adf5b842e7f2b5a,
  image: 'images/austria/cards/teams_austria__0027_feuerlöscherairaction.jpg',
  name: 'teams_austria__0027_feuerlöscherairaction.jpg' }

I have tested the similar code using different collections and the issue still remains. Also, the timeout needed to ensure consistent result seems to vary as well, meaning unless I use a reasonably high value, there is a chance of this occurring.

A work around for me at the moment is simply nesting the success callbacks, however this is resulting in some nasty looking code.

Why only return a single document after inserting multiple?

I'm inserting multiple documents at once, and Monk is explicitly returning a single document in this scenario. I looked at the code and it looks like this is intentional, but I just wanted to ask whether or not it was?

 this.col.insert(data, opts, function (err, docs) {
    process.nextTick(function () {
      promise.fulfill.call(promise, err, docs ? docs[0] : docs);
    });
  });

https://github.com/LearnBoost/monk/blob/master/lib/collection.js#L318

Thanks

Automatically converting String to BinData?

I know Monk is a wrapper around Mongoskin, which itself uses the -native API. I looked through the Monk code and did not see it explicitly doing this conversion, but I figured I'd ask here and work my way down the dependency list.

I have some code that stores a string, returned by one of the node crypto methods, into Mongo. With Monk, and Mongo 2.2.0, this seemed to automatically store that string as BinData in Mongo. Specifically, the string is a user password using the property name password. I guess I assumed that it was automatically being stored using BinData type, by one of the client modules. Today, I have tried out Mongo 2.2.1, and the string is no longer being stored as BinData, but instead is being stored in Mongo as a string.

Anyway, I doubt the conversion was being done based on the contents of the string, but most likely because of the property name being used, password. Either that, or the conversion was somehow being detected and done by MongoDB 2.2.0, and for whatever reason is not longer being done in 2.2.1.

Any ideas on whether or not this was being in Monk, or perhaps Mongoskin / the -native API?

Stats

Is there a way to return number of total records in collection with query response using monk?

Thanks.

Find and Modify

This is not an issue, more of a help request (I could not find any other places to ask for help online)

I am using the following code to modify a record, but it just won't modify the record. Obviously I am doing something wrong.

users.findAndModify({"userid": data.user_id}, { $set: {"usersong": data.song_title} });

Could anyone please direct me in the right way. Thanks!

Recursive process.nextTick detected

Hey!

Since the last node version, I have this error :

(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

for a simple :

find({...}, { stream: true }).each(fn)

Any idea to fix that?

Thanks,
Philippe

Auto Reconnect?

Is there a way to specify { auto_reconnect: true } during the construction of a monk connection?

Catch connection errors

Right now errors from mongoskin/native emitters are not being catched.
They should be re-emitted so that they can be captured by manager.on('error')

Can't compile bson when installing monk

../ext/bson.h:52:51: error: ‘Arguments’ does not name a type
../ext/bson.h:52:62: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.h:55:43: error: ‘Arguments’ does not name a type
../ext/bson.h:55:54: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.h:56:45: error: ‘Arguments’ does not name a type
../ext/bson.h:56:56: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.h:59:49: error: ‘Arguments’ does not name a type
../ext/bson.h:59:60: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.h:60:57: error: ‘Arguments’ does not name a type
../ext/bson.h:60:68: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.h:66:33: error: ‘Arguments’ does not name a type
../ext/bson.h:66:44: error: ISO C++ forbids declaration of ‘args’ with no type [-fpermissive]
../ext/bson.cc: In member function ‘void BSONSerializer<T>::SerializeValue(void*, const v8::Handle<v8::Value>&)’:
../ext/bson.cc:194:39: error: no matching function for call to ‘v8::Object::Has(v8::Persistent<v8::String, v8::NonCopyablePersistentTraits<v8::String> >&)’
../ext/bson.cc:194:39: note: candidates are:
/root/.node-gyp/0.11.12/deps/v8/include/v8.h:2086:8: note: bool v8::Object::Has(v8::Handle<v8::Value>)
/root/.node-gyp/0.11.12/deps/v8/include/v8.h:2086:8: note:   no known conversion for argument 1 from ‘v8::Persistent<v8::String, v8::NonCopyablePersistentTraits<v8::String> >’ to ‘v8::Handle<v8::Value>’
/root/.node-gyp/0.11.12/deps/v8/include/v8.h:2094:8: note: bool v8::Object::Has(uint32_t)
/root/.node-gyp/0.11.12/deps/v8/include/v8.h:2094:8: note:   no known conversion for argument 1 from ‘v8::Persistent<v8::String, v8::NonCopyablePersistentTraits<v8::String> >’ to ‘uint32_t {aka unsigned int}’
../ext/bson.cc:197:23: error: base operand of ‘->’ has non-pointer type ‘v8::Persistent<v8::String, v8::NonCopyablePersistentTraits<v8::String> >’

. . .

Maybe the dependency on mongoskin should be updated to v1.3.20? Node-gyp compiles bson fine when installing mongoskin directly.

Add removeById

Since there's findById and updateById, one might expect the existence of removeById.

Sorting

Hi!

Can't implement sorting.
Find function accept sort as a string, but can't accept object like sort={_id:-1}.

Thanks.

Don't cache collections

This allows to pass around instances safely from whose collections you can override options without altering other pieces of code.

.count doesn't take any options.

By looking at the code seems like count doesn't take any options argument as mongo db native driver does.

My use case was that I wanted to quickest to find out if my query matches any document or not, so I was trying this: collection.count(myQuery, { limit : 1 }, ...) which doesn't work with monk.

capped collection support

didn't see any sign of .createCollection() unless mongoskin is magically handling that but it would be rad to support db.get(name, { capped: true, size: n }), just doing a dumb .capped check for now

assumes all _id's are ObjectId

we use custom ids so this just explodes with "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" and renders it useless, is there any way to bypass the exploding?

dereference?

var db = require('monk')(params)
db.dereference(ref,callback) not working

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.