-
Notifications
You must be signed in to change notification settings - Fork 17
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
[WIP] IntegratesQueryBuilder mixin #3
[WIP] IntegratesQueryBuilder mixin #3
Conversation
Do you have any ideas on how to cover that by tests? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi 👋
Thanks for this very cool PR.
I've written a few comments on your code. Regarding the testing, I'm not sure how to mock a node module. Maybe this article could help you get started.
I hope this helps.
src/IntegratesQueryBuilder.js
Outdated
{ | ||
beforeRequest(request) { | ||
if (superclass.hasOwnProperty('beforeRequest')) { | ||
superclass.beforeRequest(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we assume in the rest of the code that this mixin depends on MakesRequests
, there is no need to check for the existence of beforeRequest
. We can simply do:
request = super.beforeRequest(request)
src/IntegratesQueryBuilder.js
Outdated
let queryBuilderModule | ||
|
||
try { | ||
queryBuilderModule = require('js-query-builder') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, that seems to be the way to define optional dependencies. We just need to set it up in the externals
webpack options and according to this issue it should recognise this as an optional external.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it is necessary to define it as external dependency somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require()
works like a charm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay cool, so you don't have any errors when running npm run build
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tried npm run build
yet.
But I have no errors with npm run test
both with and without js-query-builder
being installed.
src/IntegratesQueryBuilder.js
Outdated
|
||
request.builder(builder) | ||
|
||
request.query = builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice integration of js-query-builder
but I feel that we could probably do better. Something more laravel-like.
let articles = Articles.query()
.filter('age', 20)
.sort('-created_at', 'name')
.get()
We'd simply need to create a static query
method that would return a new class QueryBuilder
. This class needs to keep track of the model and have a get
methods to send the accumulated request.
Note that both this approach and the current approach can be used together.
Anyway feel free to discard this if you'd rather leave things like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your idea 👍
Thanks for the comments! |
I've updated the code and made it more laravel-like. |
I really like your changes. Returning the builder directly with an extra |
Thanks! Sure, I'll write a few tests. |
I've added the tests. But, unfortunately, have a problem with optionality of |
|
Adding
But when I install |
Do you have any ideas on how to beat this problem? |
Hi @hivokas thanks for writing those tests. Unfortunately I am really struggling to find any literature regarding optional externals with webpack. Could you copy/paste the warning that you get when installing Javel to another project? |
|
@hivokas That is very annoying, I wrote a reply on an existing webpack issue to see if we're missing anything. Otherwise, I might have to consider using a different bundling solution. |
I have updated the pull request. What do you think about such workaround? |
Thanks for your work on this. There is something I'm not sure to understand.
Because if this is the case, then I think you work around makes sense but I'd like the "fake module registration" to belongs in the |
|
What do you mean by "fake module registration"? Let me explain my idea: If user wants to use
|
This way I'm avoiding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. Thank you for your explanation.
Now I understand why you've decided to mock your library in the tests.
I think it's a good idea but I just have a few comments regarding the implementation that I've written as a review.
package.json
Outdated
@@ -41,6 +41,7 @@ | |||
"babel-plugin-webpack-alias-7": "^0.1.1", | |||
"eslint": "^5.12.1", | |||
"eslint-loader": "^2.1.1", | |||
"mock-require": "^3.0.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this dependency anymore, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
src/IntegratesQueryBuilder.js
Outdated
@@ -0,0 +1,43 @@ | |||
import { Mixin } from 'mixwith' | |||
import Javel from '@/Javel' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather register the modules a little bit differently.
Instead of exposing a global Javel
class...
import { Javel } from 'javel'
Javel.registerOptionalModule(name, dependency)
let dependency = Javel.resolveOptionalModule(name)
... I'd rather expose the functions directly
import { registerModule, resolveModule } from 'javel'
registerModule(name, dependency)
let dependency = resolveModule(name)
That way we can even rename the Javel
class into something more specific like ModuleRegistration
.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your idea.
try { | ||
queryBuilderModule = Javel.resolveOptionalModule('js-query-builder') | ||
} catch (e) { | ||
throw new Error('IntegratesQueryBuilder mixin requires optional "js-query-builder" module to be registered.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the try/catch
here is necessary anymore since the resolveOptionalModule
method already has it's own try/catch
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've wrapped resolving of the module with try/catch
in order to make an exception message more sensible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay makes sense. 🙂
I've updated the pull request according to your review. |
Thank you very much this PR and for your work on finding out how to register optional modules! Before I create a new release, can you please confirm that this PR is completely transparent to users that do not use the Thanks again! 🍺 |
Do we need kinda readme for this mixin? |
Hi, yes I think we would need to update the documentation to explain this new module registration system. I see it as:
What do you think? |
@lorisleiva, yeah, I agree with you. Who will work on it: you or me? |
Hi, I'm more than happy to do it. I just think that once I've written the main structure of the second bulletpoint, maybe you could document some of the features of your I won't be able to do that just now but I'll open an issue to make sure it's in my pipeline. 👍 |
Deal 🕶️ |
This PR adds
IntegratesQueryBuilder
mixin which integrates https://github.com/coderello/js-query-builderExample of use: