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

broken with knex@0.20.11 since it no longer includes Bluebird #61

Closed
kriscarle opened this issue Mar 6, 2020 · 12 comments
Closed

broken with knex@0.20.11 since it no longer includes Bluebird #61

kriscarle opened this issue Mar 6, 2020 · 12 comments

Comments

@kriscarle
Copy link

Error message is:

UnhandledPromiseRejectionWarning: TypeError: self.knex.select(...).from(...).where(...).andWhereRaw(...).then(...).asCallback is not a function
    at /node_modules/connect-session-knex/index.js:264:5

See knex/knex#3704

machineghost added a commit to machineghost/connect-session-knex that referenced this issue Mar 8, 2020
Since the problem was a reliance on Bluebird's asCallback, I just copied that library's implementation, along with a slight tweak to use "assert" instead of "./assert", and to not modify the prototype.
@machineghost
Copy link
Contributor

machineghost commented Mar 8, 2020

For anyone looking for a quick fix (not permanent) to this issue:

- "connect-session-knex": "^1.5.0",
+ "connect-session-knex": "git@github.com:machineghost/connect-session-knex.git",

Basically it just copy/pastes the missing Bluebird function in, to avoid adding a dependency on the whole library.

It's a dead end repo, so I'm only mentioning it as a temporary fix (please don't change your package.json permanently!) Hopefully the library maintainer will either accept it, or another fix soon.

machineghost added a commit to machineghost/connect-session-knex that referenced this issue Mar 11, 2020
@machineghost
Copy link
Contributor

machineghost commented Mar 11, 2020

I realized my attempt to avoid Bluebird was flawed, as a proper recreation of asCallback would require copying a lot more code.

Rather than do that I redid things to instead depend on Bluebird, which let me convert every foo.asCallback into a resolve(foo).asCallback (resolve is Bluebird-speak for "convert into a Bluebird promise").

P.S. Unfortunately I don't have MySQL setup so I can't run the tests.

@jehy
Copy link

jehy commented Mar 11, 2020

What's the reason for removing bluebird? It's still faster then native promises, and has rich syntax.
Things like #62 just look strange - you avoid the library for no reason, break things and then copy-paste parts of library back...

@kibertoad
Copy link

@kibertoad
Copy link

@jehy See https://blog.kuzzle.io/bluebird-vs-native-vs-async/await-state-of-promises-performances-in-2019. In modern Node (12+) there are basically no performance benefits to use Bluebird vs native async/await, and using non-standard solutions is always a pain, especially since your users are likely to get coupled to the non-standard part of it (as evidenced by amount of issues users are currently having when Bluebird is gone).
I understand the pains of migration, and I am sorry for them, but this is not a new or unexpected development - we stopped officially supporting Bluebird since 0.18.0 (https://github.com/knex/knex/blob/master/UPGRADING.md)

@jehy
Copy link

jehy commented Mar 11, 2020

@kibertoad yup, I saw that benchmark. Also I saw opinion that

In long term I hope every promise library would be obsolete.

But IMHO there are two wrong assumptions:

  1. Bluebird is used bacause it's faster.
  2. Bluebird is used as a pollyfill.

I think that's not the case for really complex applications, which go beyond async-await one liners.
Bluebird has many features that are absolutely necessary for complex async flow - like timeouts, custom error handling, mapping with concurrency, cancellation, reducing and so on. All those features can be implemented in native promises, but that's a lot of useless boilerplate.

Suggested "solution" with wrapping all calls with bluebird Promise.resolve sounds really bad, especially if you are dealing with a big service where you'll have to rewrite tons of code with this ugly wrapper.

Let's take a look at ioredis. There was also a talk about supporting native promises - and guys made a really good solution - they added an option to support any custom promise library and they use native promise by default. Why not? Setting a custom promise library is fast and doesn't require patching all application code.

@machineghost
Copy link
Contributor

machineghost commented Mar 11, 2020

Suggested "solution" with wrapping all calls with bluebird Promise.resolve sounds really bad, especially if you are dealing with a big service where you'll have to rewrite tons of code with this ugly wrapper.

I don't disagree in the slightest. My only counter would be that a slow (but working) library is superior to a non-working one (ie. the state for the past five days) ... but of course a fast working library would be best.

I have no familiarity with ioredis though, so someone else will have to PR an implementation based on it.

gx0r pushed a commit that referenced this issue Mar 11, 2020
#62)

* Slightly kludgy fix for #61
Since the problem was a reliance on Bluebird's asCallback, I just copied that library's implementation, along with a slight tweak to use "assert" instead of "./assert", and to not modify the prototype.

* Fixed reference to "self" to instead use "this" (in the only place in the code that didn't have a "self" variable)

* Revert "Fixed reference to "self" to instead use "this" (in the only place in the code that didn't have a "self" variable)"

This reverts commit 88d1a96.

* Revert "Slightly kludgy fix for #61"

This reverts commit 8d9cdfb.

* Undid previous approach and went with a Bluebird-dependent approach
@jehy
Copy link

jehy commented Mar 12, 2020

@machineghost Sorry, when I was talking about wrapping every call in Promise.resolve - I meant suggested workaround from knex itself, not your fix. Your fix is fine :)

@gx0r
Copy link
Owner

gx0r commented Mar 12, 2020

Thank you for the bug report and fix! I published a new version!

@gx0r gx0r closed this as completed Mar 12, 2020
@kibertoad
Copy link

kibertoad commented Mar 19, 2020

@llambda Are there any reasons why native callbackify (https://nodejs.org/api/util.html#util_util_callbackify_original) couldn't be used instead? It is natively supported in Node 8 and it's way less hacky than Bluebird wrapping.

You can see an example for how to convert Promise using that approach here: https://stackoverflow.com/questions/40640623/how-to-convert-promise-into-callback-in-node-js

@gx0r
Copy link
Owner

gx0r commented Mar 19, 2020

@llambda Are there any reasons why native callbackify (https://nodejs.org/api/util.html#util_util_callbackify_original) couldn't be used instead? It is natively supported in Node 8 and it's way less hacky than Bluebird wrapping.

You can see an example for how to convert Promise using that approach here: https://stackoverflow.com/questions/40640623/how-to-convert-promise-into-callback-in-node-js

I think I'd be open to that change, if someone were to put together a merge request, otherwise I may do it in my own time.

@gx0r gx0r reopened this Mar 19, 2020
@machineghost
Copy link
Contributor

FYI the reason I went with a Bluebird approach is that I wasn't sure if there was any "magic" in Bluebird that wasn't in Node's version, and my goal was just to get the library working again as quickly as possible without breaking anything.

I 100% agree that callbackify is a better approach ... it just requires that someone actually test and confirm that no "Bluebird magic" gets lost in the process (and since I don't have MySQL setup I also couldn't do that testing).

@gx0r gx0r closed this as completed Mar 24, 2020
gx0r pushed a commit that referenced this issue Jan 15, 2021
#62)

* Slightly kludgy fix for #61
Since the problem was a reliance on Bluebird's asCallback, I just copied that library's implementation, along with a slight tweak to use "assert" instead of "./assert", and to not modify the prototype.

* Fixed reference to "self" to instead use "this" (in the only place in the code that didn't have a "self" variable)

* Revert "Fixed reference to "self" to instead use "this" (in the only place in the code that didn't have a "self" variable)"

This reverts commit 88d1a96.

* Revert "Slightly kludgy fix for #61"

This reverts commit 8d9cdfb.

* Undid previous approach and went with a Bluebird-dependent approach
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

No branches or pull requests

5 participants