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

Nested sql tags accross modules #74

Open
petrzjunior opened this issue Aug 15, 2022 · 0 comments
Open

Nested sql tags accross modules #74

petrzjunior opened this issue Aug 15, 2022 · 0 comments

Comments

@petrzjunior
Copy link

petrzjunior commented Aug 15, 2022

I have a library which constructs SQL filters and I want to consume it in my main project.

Example

In library:

export const filter = sql`WHERE year = 2018`;

In the main project:

const {filter} = require('library');
sql`SELECT * FROM books ${filterThisYear ? filter : undefined}`

Expected behavior

I expect nested sql tags to merge and append the filter.

Actual behavior

Due to the object weirdness in JavaScript, class instances are compared by their prototype chain which depends on the way the class was imported. In this case, instanceof returns false. The SqlContainer class is the same, but imported from a different place. Finally this cause sql not to detect nested tag and treats is as an object parameter instead.

Suggested fix

I created a fork which adds a tag method to allow comparison across modules. This is not the cleanest code ever, but is successfully used in Moment.js for instance.

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

No branches or pull requests

1 participant