These are chat archives for Automattic/mongoose

17th
Sep 2018
Faran Ali
@Faran52_twitter
Sep 17 2018 06:23
How can I alter mongoose document, like in post queries like post findOne
Kev
@lineus
Sep 17 2018 11:07
@mbellemo_twitter it feels a bit hacky, but this gist shows a way to do it in a post hook. @Faran52_twitter this also answers your question, as it shows mutating the document returned from the query ( which is the first arg to the callback ). TWO FOR ONE!!!!
Maurizio Bellemo
@mbellemo_twitter
Sep 17 2018 12:21
@lineus not sure if I get it... the only thing I wanted to do is to show an up-to-date aggregation for the FE. The BE is already behaving perfectly :)
Kev
@lineus
Sep 17 2018 12:30
@mbellemo_twitter if you want to return current data from the backend in the hook, you have to use an update method that returns the current data in it's callback/promise - hence the change to findOneAndUpdate. You could also acheive the same thing with doc.save(). The important bit is adding the aggregate property to the return doc from the hook, which the example does ( but not until the doc is actually updated in the db )
the unmarkModified() call is important too, as the path in the doc returned from the findOneAndUpdate will be "modified" and thus calling save on it will re-save the same path to the db.
Maurizio Bellemo
@mbellemo_twitter
Sep 17 2018 12:33
@lineus I see... for me was more the problem to do the aggregation before so that it can be served to the FE together with the initial update. I guess the only way to do is during the first query in the then clause...
Kev
@lineus
Sep 17 2018 12:33
honestly, this is a lot to accomplish in a hook, unless it was something I wanted to implement in every single findOneAndUpdate call on that model, I would implement it in a static method.
Maurizio Bellemo
@mbellemo_twitter
Sep 17 2018 12:33
yep... probably the best way
stherrienaspnet
@stherrienaspnet
Sep 17 2018 13:22
thanks @lineus
Kev
@lineus
Sep 17 2018 13:32
@stherrienaspnet you're welcome :smile:
stherrienaspnet
@stherrienaspnet
Sep 17 2018 13:47
@lineus i did not found schema translation way using mongoose and i18next, maybe there is one... but found this https://www.npmjs.com/package/mongoose-intl
Kev
@lineus
Sep 17 2018 13:52
@mbellemo_twitter just for fun, I created a test instance of express/mongoose with my earlier example to see if it would work the way I expected, passing the value to the FE. I created a repo for it here and when you post to the route, it returns the aggregate path in the response. There's a test for the route in test/ :smile: but curl works too:
express>: curl -X POST -s -H 'Content-type: application/json' --data '{"name": "Francis"}' 'http://localhost:3000/mbell' | json{
  "success": true,
  "msg": {
    "_id": "5b9fb1077491030ef44deccd",
    "first": "Francis",
    "last": "Clooney",
    "__v": 0,
    "aggregate": "Francis Clooney"
  }
}
express>:
@stherrienaspnet oh, that's cool! Thanks for sharing it :+1:
stherrienaspnet
@stherrienaspnet
Sep 17 2018 17:32
Hello anyone using service + repository pattern with mongoose?
stherrienaspnet
@stherrienaspnet
Sep 17 2018 19:39
Hello everyone what are you using to do migrations?