Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hooks to support mongo-db transaction #276

Merged

Conversation

jaiyashree
Copy link
Contributor

@jaiyashree jaiyashree commented Nov 29, 2018

Summary

To directly add api-request level hooks and service level hooks to implement mongo-db-transaction

  • Is this PR dependent on PRs in other repos? No

Other Information

The create and remove methods of service.js were modified to pass the params.mongoose to the mongoose dependency to carry the database session and to commit or rollback at the end of api-request or the service call.
Note: This is left optional to ensure the codes already written without this param still works fine.

@daffl
Copy link
Member

daffl commented Nov 29, 2018

This looks really interesting, thank you! For new features like that we do need some tests though. A good start would be to show how the example would work and then go from there.

@DaddyWarbucks
Copy link

I think that the skipPath argument should be optional. Right now, if you do not pass skipPath the function will error. And if you don't want to skip any paths you must pass an empty array. That is not ideal IMHO. This seems like it was written to be used in an app.hooks scenario instead of a service.hooks scenario. Feathers already has a mechanism for determining which services/paths a hook should be run, which is specifying the hook per service. Unless I am missing something?
I would suggest making skipPath optional. Something like

if (!skipPath || skipPath.indexOf(context.path) === -1) {

This would allow the hook to be used on a per service basis, instead of specifying at app.hooks and skipping paths, but also still allow it to be used that way.
That is the only "critical" issue I see here. If you guys don't mind, I would be happy to amend a few other things that I think could be improved.

Great work!

@jaiyashree
Copy link
Contributor Author

jaiyashree commented Nov 30, 2018

I think that the skipPath argument should be optional. Right now, if you do not pass skipPath the function will error. And if you don't want to skip any paths you must pass an empty array. That is not ideal IMHO. This seems like it was written to be used in an app.hooks scenario instead of a service.hooks scenario. Feathers already has a mechanism for determining which services/paths a hook should be run, which is specifying the hook per service. Unless I am missing something?
I would suggest making skipPath optional. Something like

if (!skipPath || skipPath.indexOf(context.path) === -1) {

This would allow the hook to be used on a per service basis, instead of specifying at app.hooks and skipping paths, but also still allow it to be used that way.
That is the only "critical" issue I see here. If you guys don't mind, I would be happy to amend a few other things that I think could be improved.

Great work!

Thanks for reviewing. I've modified the skipPath param to hold an empty array if not assigned a value from calling statement. And the app.hooks.js is just a sample and this can be used in any service hooks too. I'm proceeding with the required changes and will amend it asap.

@jaiyashree jaiyashree force-pushed the feature/mongoose-transaction branch 7 times, most recently from 3dd2ea9 to 272133d Compare December 3, 2018 12:55
@jaiyashree
Copy link
Contributor Author

jaiyashree commented Dec 3, 2018

This looks really interesting, thank you! For new features like that we do need some tests though. A good start would be to show how the example would work and then go from there.

Hi. I've added test cases as working-sample and updated README.md for prerequisite. Please review for additional changes to be added.

@jaiyashree
Copy link
Contributor Author

@daffl
Copy link
Member

daffl commented Dec 3, 2018

This is really tricky in Travis CI. Looks like the best shot is the run-rs package. If it works this would also allow local testing without needing a MongoDB server running.

@jaiyashree
Copy link
Contributor Author

This is really tricky in Travis CI. Looks like the best shot is the run-rs package. If it works this would also allow local testing without needing a MongoDB server running.

Thanks daffl 👍 I will also try to figure a way out .

@jaiyashree jaiyashree force-pushed the feature/mongoose-transaction branch 12 times, most recently from c368e33 to fe3ba71 Compare December 6, 2018 12:25
@jaiyashree
Copy link
Contributor Author

Hi @daffl ,
Thanks for giving idea on how to use run-rs in background using '&', it worked 👍 . The problem was that the test cases started to run even before the run-rs is connected. A time wait was enough to run it.
Also, I've updated the node version support to v7.6.0 from v6. This is because async-await is supported from >=v7.6.0.
Note: The node version support for feathers-generator-plus is >= v8 already.

Awaiting your suggestion/review.

@jaiyashree jaiyashree force-pushed the feature/mongoose-transaction branch 2 times, most recently from fa35a18 to 02d80da Compare December 7, 2018 12:59
@daffl
Copy link
Member

daffl commented Dec 7, 2018

Dropping supported Node versions would be a breaking change which planned for the next major release. We'd either have to wait merging this until then or have a promise based version for now.

@jaiyashree
Copy link
Contributor Author

Hi @daffl
I'll change them to the node 6 standard then by using promise.

@daffl
Copy link
Member

daffl commented Dec 8, 2018

Sounds good, thanks!

@daffl
Copy link
Member

daffl commented Dec 8, 2018

Mind you, I am planning to make a major release with some other updates in the next couple of weeks that would drop Node 6 and use async/await.

@daffl
Copy link
Member

daffl commented Dec 18, 2018

Ok, I just merged the updates I mentioned. If you merge your changes with that version we can get it released 😄

@jaiyashree
Copy link
Contributor Author

Sure @daffl 👍

@jaiyashree jaiyashree force-pushed the feature/mongoose-transaction branch 2 times, most recently from dd85090 to 2685be7 Compare December 18, 2018 15:15
@jaiyashree
Copy link
Contributor Author

@daffl awaiting your review 👍

lib/service.js Outdated
return result.map(item => (item.toObject ? item.toObject() : item));
if (params.mongoose && params.mongoose.session && typeof data === 'object') {
const createOptions = Object.assign({}, params.mongoose);
return model.create([data], createOptions)
Copy link
Member

@daffl daffl Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even when you are in a session you could still create array data right? Why not just pass createOptions into the model.create(data) that we already have in https://github.com/feathersjs-ecosystem/feathers-mongoose/pull/276/files#diff-6ce8873d3951d1d3483af360fa1f4a93R155

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @daffl ... I've passed it as a option to create model...
Note: When a user try creating a single record 'service.create(obj, mongooseOptions)' we need to internally convert it into 'service.create([obj], mongooseOptions)' and them return result[0] because the 'mongoose' dependency does not accept options for a single object creation as it supports a spread of object.

@daffl daffl force-pushed the feature/mongoose-transaction branch from d645702 to 2acd002 Compare December 29, 2018 01:47
@daffl daffl merged commit cd80f1e into feathersjs-ecosystem:master Dec 29, 2018
@daffl
Copy link
Member

daffl commented Dec 29, 2018

Sweet, made some minor tweaks and released as v7.1.0, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants