felixfbecker / node-sql-template-strings

ES6 tagged template strings for prepared SQL statements 📋
ISC License
608 stars 40 forks source link

Immutable queries? #71

Open cassiozen opened 6 years ago

cassiozen commented 6 years ago

Currently query.append mutates the original query, which makes cumbersome to reuse a base query in multiple places.

For example, this will not work - Every time the route handler gets called, a new WHERE segment will be appended to the original baseQuery:

const baseQuery = SQL`SELECT * FROM books`;

app.get("/", (req, res) => {
  const query = baseQuery.append(SQL` WHERE id = ${req.params.id}`);
  // Some response
});

Is there any immutable way of appending?

felixfbecker commented 6 years ago

No, the idea was to always use queries inline for readability.

cassiozen commented 6 years ago

Would you consider adding such a feature, under a different method name? (PR #72)

slavik-m commented 6 years ago

@cassiozen +1 I need it!

migueloller commented 6 years ago

No, the idea was to always use queries inline for readability.

That seems like a very bad excuse. When building a data access layer it is very common to share queries, especially CTEs. Adding something like #72 also doesn't remove the ability to use queries inline.

samholmes commented 5 years ago

I suggest .concat() as a method name.