Created
October 30, 2017 05:10
-
-
Save JeffML/bb799870f3cf8b4a27b68f22df648a98 to your computer and use it in GitHub Desktop.
Adding casual-generated data to sql database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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