The challenge lies in titling this particular question, but demonstrating it is quite straightforward. My goal is to include multiple value sets in an SQL insert statement like the following:
var sqlInsertString = `INSERT INTO events (url) VALUES`
var sqlInsertValuesString = `('${event.url ? event.url : null}', null, 1, 2, 3)`
pg_client.query(sqlInsertString + sqlInsertValuesString)
This process occurs within a loop, hence the need for separating the insert string and values string. What I aim for is to have the URL inserted as 'https://www.example.com'
with quotes if event.url contains a URL, and to insert null if event.url is empty.
In my current code snippet above, 'null' is inserted as a string with quotes instead of an actual null value in the database. Removing the quotes would lead to errors due to the presence of ':' in the URL.
How can I modify the sqlInsertValuesString
to either provide a valid URL string or a genuine null
without quotation marks using this approach?