-
Notifications
You must be signed in to change notification settings - Fork 50
How to handle arrays as records for INSERTS and UPDATES #22
Comments
Arrays are expanded without any surrounding parentheses so that you can use const format = require('pg-format');
var sql = format('INSERT INTO my_table (a, b) VALUES (%L, ARRAY[%L])', 'abc', [ 1, 2, 3 ]);
console.log(sql); // INSERT INTO my_table (a, b) VALUES ('abc', ARRAY['1','2','3'])' |
I'm currently using this form
It's a dynamic data situation so I don't know the number of columns before runtime. I suspect the best solution is to dynamically generate a format string like this based on the number of properties and if a value has an array datatype?
Thanks for prompt reply. |
I have same characteristic problem if we doing batch insert. My objective on my query : INSERT INTO vitamin(name,composition ) VALUES ('Multi Vitamin', [{"option":"vitamin A", "miligram":8mg},{"option":"vitamin B", "miligram":8mg}, {"option":"vitamin D", "miligram":10mg}]), ('Fibre Blend', [{"option":"Protein", "miligram":8mg},{"option":"Fat", "miligram":8mg}, {"option":"insoluble fibre", "miligram":10mg}]); The solution from comment above cannot applied on batch insert like my objective query. If I do looping the code , like this const format = require('pg-format');
var dataVitamin = []
for(let data of vitaminList)
{
var name = data.name;
var nutritions = data.nutritions
var params = [name, nutritions];
dataVitamin.push(params);
}
const query = format('INSERT INTO vitamin(name, composition) VALUES %L', dataVitamin);
console.log(query); The INSERT INTO vitamin(name,composition ) VALUES
('Multi Vitamin', {"option":"vitamin A", "miligram":8mg}::jsonb,{"option":"vitamin B", "miligram":8mg}::jsonb, {"option":"vitamin D", "miligram":10mg}::jsonb),
('Fibre Blend', {"option":"Protein", "miligram":8mg}::jsonb,{"option":"Fat", "miligram":8mg}::jsonb, {"option":"insoluble fibre", "miligram":10mg}::jsonb) when we run the query on my code, error come up : if i convert my code above become like this const format = require('pg-format');
// this type of code, get my objective
for(let data of vitaminList)
{
var name = data.name;
var nutritions = data.nutritions
const query = format('INSERT INTO vitamin(name, composition) VALUES (%L, ARRAY[%L])', name, nutritions);
console.log(query);
} the converted code serve the purpose of the objective , but if we do loop for 10.000 rows data on single run. Any solution for this issue ? Thank you |
@KarmitoHerry , Facing the same, can anyone help? |
In cases where
|
How would I best use pg-format to handle the insertion of arrays into columns with an array type.
Imagine a table with a column 'roles' of type text[].
To insert into that table the statement would look something like
INSERT INTO table (name, roles) VALUES ("Josh", ARRAY["role1", "role2"])
or
INSERT INTO table(name, roles) VALUES ("Josh", {"role1", "role2"})
However, when pg-format encounters an array it formats as a literal like this:
INSERT INTO table(name, roles) VALUES ("Josh", ("role1", "role2")) which is invalid. That format ("role1", "role2") works great for IN statements so clearly is the correct handling for arrays in some circumstances.
Recommendations for UPDATES and INSERTS where the value being written is an Array?
The text was updated successfully, but these errors were encountered: