Last active
December 30, 2017 20:53
-
-
Save jwhitehorn/0ff8929f130146a6d012ca6e29d47565 to your computer and use it in GitHub Desktop.
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
var dbOpen = require('./database.js'); | |
var async = require('async'); | |
var tmp = require('tmp'); | |
var fs = require('fs-extra'); | |
var sqlite = require('sqlite3').verbose(); | |
var tmpobj = tmp.fileSync(); //use a new, empty, temporary file | |
var sqliteDb = new sqlite.Database(tmpobj.name); | |
dbOpen(function(err, models, done){ | |
if(err){ | |
return console.log("An error has occured", err); | |
} | |
async.series([ | |
function(next){ | |
sqliteDb.run("create table campgrounds (id integer, name text, latitude real, longitude real, rating real, address text, city text, state text, zip_code text)", next); | |
}, | |
function(next){ | |
sqliteDb.run("create table campground_photos (id integer, photo_url text, campground_id integer)", next); | |
}, | |
function(next){ | |
sqliteDb.run("create table campground_reviews (id integer, headline text, body text, rating text, campground_id integer)", next); | |
}, | |
function(next){ | |
var sql = "insert into campgrounds (id, name, latitude, longitude, rating, address, city, state, zip_code) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
models.Campground.find(function(err, campgrounds){ | |
async.eachSeries(campgrounds, function(c, next){ | |
sqliteDb.run(sql, [c.id, c.name, c.latitude, c.longitude, c.rating, c.address, c.city, c.state, c.zip_code], next) | |
}, next); | |
}); | |
}, | |
function(next){ | |
var sql = "insert into campground_photos (id, photo_url, campground_id) values (?, ?, ?)"; | |
models.CampgroundPhoto.find(function(err, photos){ | |
async.eachSeries(photos, function(p, next){ | |
sqliteDb.run(sql, [p.id, p.photo_url, p.campground_id], next) | |
}, next); | |
}); | |
}, | |
function(next){ | |
var sql = "insert into campground_reviews (id, headline, body, rating, campground_id) values (?, ?, ?, ?, ?)"; | |
models.CampgroundReview.find(function(err, reviews){ | |
async.eachSeries(reviews, function(r, next){ | |
sqliteDb.run(sql, [r.id, r.headline, r.body, r.rating, r.campground_id], next) | |
}, next); | |
}); | |
} | |
], function(err){ | |
//DONE | |
sqliteDb.close(); | |
done(); | |
if(!err){ | |
console.log("DONE: database successfully generated") | |
fs.copySync(tmpobj.name, './generated/database.db'); | |
}else{ | |
console.log("ERROR: ", err); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment