Thursday 28 April 2016

In your database service calls, do you usually just return the db promised call, or do you prune the data there?

I have routes that calls upon database service functions that return promises.. And on the router layer, I chain the output of the function calls togethers, to construct the proper data response for the end-user. My question is that the mongo native driver, oftentimes has the actual data saved in something like dbResultd['ops'][0]['_id'] (upon insertion). Or on find, i'll need to do .limit().next(). Is this something i'd want to do in my service or route? It just seems like all a bit of a pain, because i'm not sure if the promise will catch things like a null return.Here's an explicit example of a service call:export const insertCommentForFileID = (userID, fileMetaID, comment) => { const date = new Date(); fileMetaID = convert.strToMongoID(fileMetaID); return db.sharedInstance().collection('comments').insertOne({ fileMetaID: fileMetaID, author: userID, comment: comment }); } The actual saved data that i may want to shoot back to the user is ingested in the promise as: ['ops'][0]['_id']I can append .then((r) => return r['ops'][0]['_id']) to the service but this just seems like a lot of boilerplate for each db access call

Submitted April 28, 2016 at 11:59PM by awhoof

No comments:

Post a Comment