'SQL insert array of string - issues with .join(', ')
Currently I create a string with JavaScript that is used as a SQL query. It reads a number of rows from an Excel sheet and then creates the SQL query by combining all those datapoints with .join(','). I want to add added a column that is a list of tags.
It’s a comma separated array. When my string gets .joined(',')ed it just becomes a string.
Technically its still an array because its being added to my SQL query string as ARRAY['list, of, things'] but I need it to be in my SQL query string as ARRAY['list', 'of', 'things']. Any way to prevent this?
Snip it of code. company[3] is passed in as an array of strings:
const tagsPresent = company[3] !== null;
return format(
`(%L, %L, ${
tagsPresent ? `'{${company[3]}}'` : 'NULL::text[]'
// I've also tried 'ARRAY[${company[3]}]' and ARRAY['+ company[3] +']
})`,
company[1],
company[2]
);
})
.join(', ');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
