Taken as read that all this is work in progress, thanks for putting
your stuff out there!
Is the 'db' passed to the callback in 'open' the same thing that
'open' was called on? Given you're not doing anything with that
callback I'm wondering if after you create your wrapper and you go
straight on to one of your operations, could you get end up trying one
of your operations before the database has been opened? I didn't look
closely at that part of the driver, but you may be saved by there
being a queue that keeps those things in order. If it's safe to do
then it's a nice simplifying move.
The difference is philosophical I guess, you're going down the path of
an OO wrapper, and I'll be following it with interest. Day by day I'd
promote it as the type of interface that will be easiest to work with.
I'm trying to turn it into more strictly functional thing. No
variables, no state sitting anywhere but on the stack, no depending on
nested scopes, just parameters, no fear of messing things up by making
wrong assumptions about state. But in my experience, it takes a bit of
time to get your head around the style, which is why I don't do stuff
like that in the office. It's just playing games with style. The
context object I've added makes that possible.
(Ideally, I'd like a callback object which encapsulated state and an
explicitly listed sequence of callbacks. Vaguely inspired by monads,
and the whole notion of avoiding implicit sequence. That would be a
new and very different driver, a project for another year when I've
got less on my plate.)
Thinking about how I'd use your library. Say I wanted to read
something from one record then process it and modify another record.
That's two dataproviders, and one has to be called from inside the
other's callback, or else you need some sort message passing or
blocking. Are you imagining that they will both belong to some
containing object that will hold the state from the earlier calls to
make it available to the later calls?
Thanks,
Julian.
> I tend to wrap things if I want to reduce complexity etc
>
> Take a look at this:
>
>
http://github.com/listentorick/mongodataprovider/blob/master/dataprov...
>
> <
http://github.com/listentorick/mongodataprovider/blob/master/dataprov...>It
> works for me so feel free to submodule it. Note that it is a work in
> progress....
>
>
>
> On Sat, Sep 25, 2010 at 12:21 PM, jbk <
julian.b.kel...@gmail.com> wrote:
> > I'm working on a web application using node-mongodb-native and
> > express.js. There are a number of URLs that perform similar actions:
> > add a record to a collection if it is not present, get all records,
> > etc...
>
> > I had difficulty finding a pattern to reuse formulaic code (Detail
> > here:
> >
http://noisylines.blogspot.com/2010/09/first-days-of-nodejs-and-mongo...
> > )
>
> > Basically all the sample code I've found depends on things like db and
> > collection being in lexical scope, I want to pull out intermediate
> > layers for accessing my database and collection. Check out the
> > duplication here to see what I'm trying to avoid:
>
> >
http://github.com/scrawlings/DelphiMethod/blob/a25d23a9fd7339bfb9e5e7...
>
> > So I've modified the node mongodb driver in my app. My modification
> > pushes a context object around which allows me to carry data through
> > explicitly. (Sorry, I haven't got the whole git fork and reference
> > thing sorted out, still learning my way around it. Feel free to point
> > me to instructions on how to make my changes easier to review.) Check
> > out the use of callback_context to see what I've tried:
>
> >
http://github.com/scrawlings/DelphiMethod/blob/master/lib/mongodb/col...
>
> > To use it I basically end up tucking things into an extra parameter
> > like context in this snippet. It's optional except that if breaks the
> > two argument Collection.find(callback, options), (which is questioned
> > by someone before me):
>
> > function get_collection(err, db, context)
> > {
> > context.db =
> > db
> > db.collection(collection_name, handle_open_collection,
> > context);
> > }
>
> > Hacking the driver doesn't seem a good solution. Can anyone point me
> > to examples of a better pattern without hacking on the driver?
>
> > To have a look at the sort of things I've managed, which is pretty
> > much good enough for my purposes, please look at my database utilities
> > layer:
> >
http://github.com/scrawlings/DelphiMethod/blob/master/server/database...
>
> > That results in some simple URL services which can be seen here:
>
> >
http://github.com/scrawlings/DelphiMethod/blob/master/server/admin_se...