Last active
March 25, 2021 16:15
-
-
Save lvngd/b2447c8cf3ea25d59d768170ad334428 to your computer and use it in GitHub Desktop.
Code to insert darklyrics JSON data into postgres using Node + Knex
This file contains 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 knex = require('knex')({ | |
client: 'pg', | |
connection: 'postgresql://darklyricsuser:darklyricspassword@localhost/darklyrics', | |
}); | |
const fs = require('fs'); | |
const data = JSON.parse(fs.readFileSync('/Users/christina/Downloads/results(1).json', 'utf8')); | |
async function insertBand(band) { | |
return await knex('bands') | |
.insert({name: band.bandName, url: band.bandUrl}) //insert bands into db | |
//return band primary key id for albums foreign key | |
.returning('id')//needed for postgres | |
.then((response) => { | |
//next insert nested albums and get their ids | |
let band_id = response[0]; | |
let albumParams = band['bandAlbums']; | |
let albumList = []; | |
for(alb in albumParams){ | |
let album = albumParams[alb]; | |
albumList.push({ | |
name: album.albumName, | |
type: album.albumType, | |
year: album.albumYear, | |
band_id: band_id //foreign key to band | |
}); | |
} | |
return albumList; | |
}) | |
.catch()//end then bands | |
.then(async (albumList) => { | |
return await knex('albums') | |
.insert(albumList)//insert albums into db | |
//return album primary key id to be used for songs foreign key album_id | |
.returning('id')//needed for postgres | |
.then((response) => { | |
let songList = []; | |
//these are the | |
for(i in response){ | |
let album_id = response[i]; | |
let albumInfo = band.bandAlbums[i]; | |
let albumSongs = band.bandAlbums[i].albumSongs; | |
for(s in albumSongs){ | |
songList.push({ | |
name: albumSongs[s].songName, | |
url: albumSongs[s].songUrl, | |
album_id: album_id //foreign key to album | |
//TODO - need translation data | |
});//end push | |
}//end songList loop | |
}//end looping through album ids | |
return songList; | |
}) | |
.catch((err) => {console.log(`album error ${err}`)}) | |
}) | |
.catch((err) => console.log(`album error ${err}`)) | |
.then(async (songList) => { | |
await knex('songs').insert(songList) | |
.then() | |
.catch((err) => {console.log(`song error ${err}`)});//insert songs into db | |
}); | |
}//end insertBand | |
async function insertData(){ | |
for(d in data){ | |
let band = data[d]; | |
await insertBand(band); | |
} | |
knex.destroy(); | |
return; | |
} | |
insertData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment