co-mongo icon indicating copy to clipboard operation
co-mongo copied to clipboard

sync comongo.collection() or sync comongo.get()

Open bodokaiser opened this issue 11 years ago • 3 comments

Hey,

I am really glad I found this project and I think this has the potential to be the next mongodb package for node :)

This just as a small note beside. What I want to report is that I think we could make a better approach for setting up the connection and to replace the current 'connect()', 'get()', 'configure()' approach:

var comongo = require('co-mongo');

// configure as usual
comongo.configure('mongodb://localhost/test')

// this is possible SYNC !!
var users = comongo.collection('users')

// use in generator
app.use(function *(next) {
  this.body = yield users.findOne();
});

The reason why I am interested on this feature is that I would like to "get" the db or the collection in "non-generator" function to pass around. For example as you see in the above snippet I use a module variable users to cache the collection. This is unfortunately not possible as the module is not executed in a generator function.

What do you think any suggestions?

bodokaiser avatar Jun 08 '14 05:06 bodokaiser

I remember looking into this at the time, but I can't remember the justification for doing it like this...let me have another look into the mongodb node source and see if there is a better way

thomseddon avatar Jun 10 '14 09:06 thomseddon

Hi, thanks for the library ! I just had a problem with db.collection being a generator function. I worked from the native-driver page and I paste from examples : db.collection('test').insert( ... or db.collection('test').find( ...

Well, it took me some time to figure out the problem.

emallard avatar Jul 31 '14 21:07 emallard

Looking back at the mongo source, I think the reason for making it yield'able was so as to support "strict mode". In this mode it will return an error if the collection doesn't exist: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L470-L504

As this is a very common method, I'm tempted to look inside the options argument and only return a thunk if strict mode has been requested - sound reasonable?

This will require a major version bump

thomseddon avatar Aug 02 '14 22:08 thomseddon