123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Connects to Postgres using the sequelize driver
- // Handles related routes
- const Promise = require('bluebird');
- const h = require('../helper');
- const Sequelize = require('sequelize');
- const sequelize = new Sequelize('hello_world', 'benchmarkdbuser', 'benchmarkdbpass', {
- host: 'TFB-database',
- dialect: 'postgres',
- logging: false
- });
- const Worlds = sequelize.define('world', {
- id: {
- type: 'Sequelize.INTEGER',
- primaryKey: true
- },
- randomnumber: { type: 'Sequelize.INTEGER' }
- }, {
- timestamps: false,
- freezeTableName: true
- });
- const Fortunes = sequelize.define('Fortune', {
- id: {
- type: 'Sequelize.INTEGER',
- primaryKey: true
- },
- message: { type: 'Sequelize.STRING' }
- }, {
- timestamps: false,
- freezeTableName: true
- });
- const randomWorldPromise = () =>
- Worlds.findOne({ where: { id: h.randomTfbNumber() } })
- .then((results) => results)
- .catch((err) => process.exit(1));
- module.exports = {
- SingleQuery: (ctx, next) =>
- randomWorldPromise().then((world) => {
- ctx.set('Server', 'Koa');
- ctx.type = 'application/json';
- ctx.body = world;
- return next();
- }),
- MultipleQueries: (ctx, next) => {
- const queries = h.getQueries(ctx),
- worldPromises = [];
- for (let i = 0; i < queries; i++) {
- worldPromises.push(randomWorldPromise());
- }
- return Promise.all(worldPromises)
- .then((worlds) => {
- ctx.set('Server', 'Koa');
- ctx.type = 'application/json';
- ctx.body = worlds;
- return next();
- });
- },
- Fortunes: (ctx, next) => {
- return Fortunes.findAll().then((fortunes) => {
- fortunes.push(h.additionalFortune());
- fortunes.sort((a, b) => a.message.localeCompare(b.message));
- ctx.set('Server', 'Koa');
- ctx.type = 'text/html';
- return ctx.render('fortunes', { fortunes });
- }).catch((err) => process.exit(1));
- },
- Updates: (ctx, next) => {
- const queries = h.getQueries(ctx),
- worldPromises = [];
- for (let i = 0; i < queries; i++) {
- worldPromises.push(randomWorldPromise());
- }
- const worldUpdate = (world) => {
- world.randomNumber = h.randomTfbNumber();
- return Worlds.update(
- {randomNumber: world.randomNumber},
- {where: {id: world.id}}
- )
- .then((results) => world)
- .catch((err) => process.exit(1));
- };
- return Promise
- .all(worldPromises)
- .map((world) => worldUpdate(world))
- .then((updated) => {
- ctx.set('Server', 'Koa');
- ctx.type = 'application/json';
- ctx.body = updated;
- return next();
- })
- .catch((err) => process.exit(1));
- }
- };
|