node-sql-template-strings icon indicating copy to clipboard operation
node-sql-template-strings copied to clipboard

Allow to set custom attribute on query object

Open EmrysMyrddin opened this issue 5 years ago • 3 comments

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 ?

EmrysMyrddin avatar May 04 '20 16:05 EmrysMyrddin

Any reason this wouldn't work?

client.query(Object.assign(sql`select ...`, { explain: true }));

felixfbecker avatar May 04 '20 16:05 felixfbecker

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

EmrysMyrddin avatar May 04 '20 16:05 EmrysMyrddin

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

EmrysMyrddin avatar Sep 08 '23 15:09 EmrysMyrddin