Skip to content

Commit

Permalink
Setup all tables before working the feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico B committed Aug 7, 2017
1 parent dfc80f0 commit 213f5e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
27 changes: 15 additions & 12 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
const r = require('rethinkdb');
const data = require('../data');
const types = require('../data/types');
const config = require('./config');
const { toTableName } = require('../utils');
const { toTableName, valueSeq } = require('../utils');

function logChanges(err, result) {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
}

const connection = r.connect(config);

// Main setup function
async function setupAllTables(conn) {
const availableTables = await r.tableList().run(conn);
const uncreatedTables = valueSeq(types).map(toTableName)
.filter(table => !availableTables.includes(table));
uncreatedTables.forEach(table => setupTable(conn, table));
return conn;
}

// Tables need to have no dash
function setupTable(conn, table) {
return r.tableCreate(table, { primaryKey: 'uuid' })
.indexCreate('publishedAt')
.run(conn);
r.tableCreate(table, { primaryKey: 'uuid' }).run(conn);
r.table(table).indexCreate('publishedAt').run(conn);
}

// Create a union
Expand All @@ -37,13 +45,7 @@ function doTableUnion(...tables) {
async function updateTable(conn, feedType, documents) {
// Check if the table exists
const feedTable = toTableName(feedType);
const availableTables = await r.tableList().run(conn);

// If table doesn't exist yet, create it based on the type of the
// Feed object
if (!availableTables.includes(feedTable)) {
await setupTable(conn, feedTable).then(results => console.log(results));
}
const uuids = await Promise.all(promiseUUIDs(conn, documents));
const documentsToInsert = documents.map(mergeUUIDs(conn, uuids));
return r.table(feedTable).insert(documentsToInsert).run(conn);
Expand Down Expand Up @@ -72,10 +74,11 @@ function promiseUniqueIdentifier(conn, type, link, date) {
}

// connection.then(conn => readTables(conn, 'designernews'));
const connection = r.connect(config);

module.exports = {
connection,
setupTable,
setupAllTables,
updateTable,
readTables,
};
26 changes: 13 additions & 13 deletions src/feed-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const { valueSeq } = require('./utils');

const sources = valueSeq(data);

function updateAllFeeds(sources, db) {
sources.forEach(feed => updateFeed(feed, db));
function updateAllFeeds(conn, sources) {
sources.forEach(feed => updateFeed(conn, feed));
}

function updateFeed(feed, db) {
return db.connection.then(async conn => {
try {
const docs = await feed.listing;
return db.updateTable(conn, feed.type, docs)
.then(results => console.log(results.inserted, 'Inserted', feed.type));
} catch(e) {
throw e;
}
});
async function updateFeed(conn, feed) {
try {
const docs = await feed.listing;
return db.updateTable(conn, feed.type, docs)
.then(results => console.log(results.inserted, 'Inserted', feed.type));
} catch(e) {
throw e;
}
}

updateAllFeeds(sources, db);
db.connection
.then(conn => db.setupAllTables(conn))
.then(conn => updateAllFeeds(conn, sources));

0 comments on commit 213f5e3

Please sign in to comment.