You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe. insert, update, delete methods from class Database are not very useful without returning an auto-generated ID. I am using mostly query method and writing SQL queries by myself with RETURNING clause.
Describe the solution you'd like
Add an opportunity to use RETURNING clause with insert, update, delete methods.
Describe alternatives you've considered
Maybe add support to other useful clauses too. For example, ON CONFLICT for 'INSERT' operation.
Additional context
Implementation may look something like that:
returning(fields,sql){if(fields.length>0){constdata=fields.toString();sql+=` RETURNING ${data}`;}return;};insert(table,record,output=[]){constkeys=Object.keys(record);constnums=newArray(keys.length);constdata=newArray(keys.length);leti=0;for(constkeyofkeys){data[i]=record[key];nums[i]=`$${++i}`;}constfields='"'+keys.join('", "')+'"';constparams=nums.join(', ');letsql=`INSERT INTO "${table}" (${fields}) VALUES (${params})`;returning(output,sql);returnthis.query(sql,data);};
There is a small problem with this approach. pg.pool.query will return not just fields, that are given with clause RETURNING. It will return an object from pg. Data is available by data.rows. It will be better to simplify returning object and return just data.rows
Returning object looks like this:
Is your feature request related to a problem? Please describe.
insert
,update
,delete
methods from classDatabase
are not very useful without returning an auto-generated ID. I am using mostlyquery
method and writing SQL queries by myself withRETURNING
clause.Describe the solution you'd like
Add an opportunity to use
RETURNING
clause withinsert
,update
,delete
methods.Describe alternatives you've considered
Maybe add support to other useful clauses too. For example,
ON CONFLICT
for 'INSERT' operation.Additional context
Implementation may look something like that:
There is a small problem with this approach.
pg.pool.query
will return not just fields, that are given with clauseRETURNING
. It will return an object from pg. Data is available bydata.rows
. It will be better to simplify returning object and return justdata.rows
Returning object looks like this:
The text was updated successfully, but these errors were encountered: