Can I still use BlueBird calls on Sequelize V6 in a transaction

263 Views Asked by At

For a project I have migrated from sequelize v5 --> v6 As mentioned here, cls-hooked should now be used instead of continuation-local-storage. so I am now using cls-hooked. Another breaking change is that BlueBird has been replaced by native Promises. However, I have a lot code that wraps the Sequelize calls (within the lifetime of the transaction) that still uses BlueBird, because it has so many convenient helper methods, that native Promises lack.

However, now sometimes (but not consistently) weird errors like this are mentioned by Sequelize: Error, error stacktrace: Error: commit has been called on this transaction(83e3c09b-efd4-4a32-bc1e-2f009a20b9c0), you can no longer use it. (The rejected query is attached as the 'sql' property of this error). Logically this does not make sense to me, since the query should already have been done before the transaction is committed (and worked fine while using continuation-local-storage). If I for that specific call exchange BlueBird with Promise I cannot reproduce the problem anymore.

When using sequelize V5 I used cls-bluebird to solve these similar problems, by patching the continuation-local-storage namespace:

import * as cls from 'continuation-local-storage'
....
const clsBluebird = require('cls-bluebird');
clsBluebird(nameSpace);

However, cls-bluebird is especially made for continuation-local-storage (see the readme of cls-bluebird):

Patch bluebird for continuation-local-storage support.

It is possible in Sequelize V6 to patch a cls-hooked namespace like this:

const cls = require('cls-hooked');
const nameSpace = cls.createNamespace("some-constant");
const clsBluebird = require('cls-bluebird');
clsBluebird(nameSpace);

Interestingly, that fixes my " commit has been called... " problem, but I doubt if it is reliable to let cls-bluebird patch the unsupported cls-hooked library. Could someone verify that?

If not, I guess my only option is to replace all of my BlueBird calls with native Promises?

1

There are 1 best solutions below

0
Han On

I found this issue in the cls-hooked repo, where the author seems to suggest that cls-bluebird does work with cls-hooked.

Quick answer: yes, you will loose context with an unpatched bluebird. Take a look at 'cls-bluebird' https://github.com/TimBeyer/cls-bluebird.