Skip to content

Instantly share code, notes, and snippets.

@JeffML
Created October 30, 2017 05:10
Show Gist options
  • Save JeffML/bb799870f3cf8b4a27b68f22df648a98 to your computer and use it in GitHub Desktop.
Save JeffML/bb799870f3cf8b4a27b68f22df648a98 to your computer and use it in GitHub Desktop.
Adding casual-generated data to sql database
const addEntities = (dataset) => {
dataset.forEach(d => {
d.title = d.title.replace(/'/, "''")
const stmt =
`
IF NOT EXISTS (
select * from author
where first_name = '${d.first_name}'
and last_name = '${d.last_name}')
INSERT INTO author (first_name, last_name) VALUES('${d.first_name}', '${d.last_name}');
IF NOT EXISTS (
select * from book
where title = '${d.title}'
and category = '${d.category}')
INSERT INTO book (title, category, author) VALUES('${d.title}', '${d.category}',
(select id from author where first_name ='${d.first_name}' and last_name = '${d.last_name}'))
`
try {
alasql(stmt)
} catch (e) {
console.error(stmt);
throw (e);
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment