felixfbecker / node-sql-template-strings

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

Allow to set custom attribute on query object #133

Open EmrysMyrddin opened 4 years ago

EmrysMyrddin commented 4 years ago

It could be great to allow user to add custom attribute to the returned query.

My use-case is the usage of a custom Postgres client which enables some debuging utilities :

client.query({
  text: `SELECT * FROM table WHERE column = $1`,
  values: [value],
  explain: true
}

In this example, my client will also log the query plan of the request.

Today, I have to write it like this using your module, and it's not very handy:

const query = SQL`SELECT * FROM table WHERE column = ${value}`;
query.explain = true;
client.query(query);

I have tried with some destructuring but it doesn't play nicely with classes...

client.query({
  ...SQL`SELECT * FROM table WHERE column = ${value}`, // This doesn't work since the prototype is lost
  explain: true
});

My proposition is to allow the addition of custom attributes in the same way we add a name:

client.query(
  SQL`SELECT * FROM table WHERE column = ${value}`.set("explain", true)
);

This way it is really simple for me to add/remove my little debugging trick !

What do you think about this ?

felixfbecker commented 4 years ago

Any reason this wouldn't work?

client.query(Object.assign(sql`select ...`, { explain: true }));
EmrysMyrddin commented 4 years ago

Yes it probably works but it's really difficult to read for me.

EmrysMyrddin commented 1 year ago

@felixfbecker Hey ! Do you thing this can have some interest or you want to close it ?