-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONtoPostgreSQL.js
72 lines (58 loc) · 3.66 KB
/
JSONtoPostgreSQL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Import modules/files you may need to correctly run the script.
Make sure to save your DB's uri in the config file, then import it using `import` statements so that we can ensure we are using ES6 modules
*/
import { Sequelize, Model, DataTypes, QueryTypes, sql } from '@sequelize/core';
//imports dontenv module and allows us to access stored environment variables stored in .env file - See https://www.npmjs.com/package/dotenv
import 'dotenv/config';
//Import file system - Examples of how to use the file system module - fs - https://www.scaler.com/topics/nodejs/fs-module-in-node-js/
import * as fs from 'fs';
//imports the Listing Model we created in ListingModels.js
import { Listing } from './ListingModel.js';
/* Connect to your database
See: Sequalize Getting Started - Connecting to a database by passing a URI - Read: https://sequelize.org/docs/v6/getting-started/#connecting-to-a-database
Copy URI/URL string from ElephantSQL - under details.
Security Best Practice: Don't use the URL/URI string (postgres://username:password@hostname/databasename) directly in applciation code. Store the database URL as the API_URL environment variable within the .env file.
BAD Implementation - const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname') // Example for postgres
Good Implementation - const sequelize = new Sequelize(process.env.API_URL);
Read - artilce to learn more about environment variables - https://medium.com/the-node-js-collection/making-your-node-js-work-everywhere-with-environment-variables-2da8cdf6e786
*/
//ADD CODE HERE to connect to you database
const sequelize = new Sequelize(process.env.API_URL);
//Testing that the .env file is working - This should print out the port number
console.log(process.env.PORT); //Should print out 8080
console.log(process.env.API_Key); //Should print out "Key Not set - starter code only"
try {
// Setup table in the DB
// Read more about Model Synchronization - https://sequelize.org/docs/v6/core-concepts/model-basics/#model-synchronization
await Listing.sync({ force: true });
console.log("The table for the Listing model was just (re)created!");
/*
This callback function read the listings.json file into memory (data) and stores errors in (err).
Write code to save the data into the listingData variable and then save each entry into the database.
*/
fs.readFile('listings.json', 'utf8', function(err, data) {
// Errors - Check out this resource for an idea of the general format err objects and Throwing an existing object.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw#throwing_an_existing_object
if (err) throw err;
// console.log(data);
// Save and parse the data from the listings.json file into a variable, so that we can iterate through each instance - Similar to Bootcamp#1
// ADD CODE HERE
const listingData = JSON.parse(data);
// Use Sequelize create a new row in our database for each entry in our listings.json file using the Listing model we created in ListingModel.js
// to https://sequelize.org/docs/v6/core-concepts/model-instances/#creating-an-instance
// ADD CODE HERE
sequelize.sync()
.then(async() => {
for(let i = 0; i < listingData.entries.length; i++){
listingData.entries[i].coordinates = JSON.stringify(listingData.entries[i].coordinates);
await Listing.create(listingData.entries[i]);
}
})
});
} catch (error) {
console.error('Unable to connect to the database:', error);
}
/*
Once you've written + run the script, check out your ElephantSQL database to ensure that it saved everything correctly.
*/