Skip to content

Commit

Permalink
added bin and lib
Browse files Browse the repository at this point in the history
  • Loading branch information
caylazabel committed Jan 19, 2017
1 parent 1d5c4ba commit d80040b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 28 deletions.
35 changes: 35 additions & 0 deletions bin/loadarticles
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

const db = require('../lib/loadDB')
const fail = () => process.exit(1)
const success = () => process.exit(0)
const help = () => {
console.log(`
LoadArticles v1.0.0
A simple node script which takes a JSON file of articles and populates them
inside of a postgres database.
Usage: loadarticles
Loads from a default JSON file, located in the /data directory
Program exits successfully if database records are created without Error.
If Error is present program exits failure.
`)
}

function main () {
db.createTableAuthor()
.then(db.createTableArticle)
.then(() => db.loadAuthors('hackerIpsum.json'))
.then(() => db.loadArticles('hackerIpsum.json'))
.then(() => success())
.catch(err => {
help()
fail()
console.error(err)
})
}

main()
116 changes: 116 additions & 0 deletions lib/loadDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict'

const Promise = require('bluebird')
const fsProm = Promise.promisifyAll(require('fs'))
const pg = require('pg')
const Pool = pg.Pool
const ops = module.exports = {}

const pool = new Pool({
user: process.env.USER,
password: '',
host: 'localhost',
database: process.env.USER,
max: 10,
idleTimeoutMillis: 1000
})

pool.on('error', e => console.error(e))

function SQL(parts, ...values) {
return {
text: parts.reduce((prev, curr, i) => `${prev}$${i}${curr}`),
values
};
}

const getAuthorId = function(record) {
return new Promise((res, rej) => {
res(
pool.query(SQL`SELECT author_id FROM authors WHERE author=${record.author}`)
.then(id => {
record.id = id.rows[0].author_id
return record
})
)
.catch(err => rej(err))
})
}

const loadRecordArticle = function(record) {
return new Promise((res, rej) => {
res(pool.query(SQL`INSERT INTO
articles(author_id, title, category, "publishedOn", body)
VALUES(${record.id}, ${record.title}, ${record.category}, ${record.publishedOn}, ${record.body})`))
.catch(err => rej(err))
})
}

const loadRecordAuthor = function(record) {
return new Promise((res, rej) => {
res(pool.query(SQL`INSERT INTO
authors(author, "authorUrl")
VALUES(${record.author}, ${record.authorUrl})
ON CONFLICT DO NOTHING;`))
.catch(err => rej(err))
})
}

ops.createTableArticle = function() {
return new Promise((res, rej) => {
const sqlCreate = `
CREATE TABLE IF NOT EXISTS
articles (
article_id SERIAL PRIMARY KEY,
author_id INTEGER NOT NULL REFERENCES authors(author_id),
title VARCHAR(255) NOT NULL,
category VARCHAR(20),
"publishedOn" DATE,
body TEXT NOT NULL
);`

res(
pool.query(sqlCreate)
.then(() => console.log('create articles success'))
.catch(err => rej(err))
)
})
}

ops.createTableAuthor = function() {
return new Promise((res, rej) => {
const sqlCreate = `
CREATE TABLE IF NOT EXISTS
authors (
author_id SERIAL PRIMARY KEY,
author VARCHAR(255) UNIQUE NOT NULL,
"authorUrl" VARCHAR (255)
);`

res(
pool.query(sqlCreate)
.then(() => console.log('create authors success'))
.catch(err => rej(err))
)
})
}

ops.loadAuthors = (file) => {
return fsProm.readFileAsync(`${__dirname}/../public/data/${file}`)
.then(data => JSON.parse(data.toString().trim()))
.then(fd => fd.map(loadRecordAuthor))
.then(proms => Promise.all(proms))
.then(() => console.log('authors loaded successfully'))
.catch(err => console.error(err))
}

ops.loadArticles = (file) => {
return fsProm.readFileAsync(`${__dirname}/../public/data/${file}`)
.then(data => JSON.parse(data.toString().trim()))
.then(records => records.map(getAuthorId))
.then(proms => Promise.all(proms))
.then(records => records.map(loadRecordArticle))
.then(proms => Promise.all(proms))
.then(() => console.log('articles loaded successfully'))
.catch(err => console.error(err))
}
28 changes: 0 additions & 28 deletions public/Scripts/arrayArticles.js

This file was deleted.

0 comments on commit d80040b

Please sign in to comment.