Skip to content

Instantly share code, notes, and snippets.

@pmlopes
Created September 27, 2018 09:11
Show Gist options
  • Save pmlopes/ce9becabcb4f147e0df3168a596acaa3 to your computer and use it in GitHub Desktop.
Save pmlopes/ce9becabcb4f147e0df3168a596acaa3 to your computer and use it in GitHub Desktop.
/// <reference types="@vertx/core/runtime" />
// @ts-check
const util = require('util');
import {Router} from '@vertx/web';
import {PgClient, Tuple} from '@reactiverse/reactive-pg-client';
import {PgPoolOptions} from '@reactiverse/reactive-pg-client/options';
const SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
const app = Router.router(vertx);
let client = PgClient.pool(
vertx,
new PgPoolOptions()
.setUser('benchmarkdbuser')
.setPassword('benchmarkdbpass')
.setDatabase('hello_world'));
app.get('/').handler(
// // Step #1
// ctx => {
// client.preparedQuery(SELECT_WORLD, Tuple.of(1), res => {
// if (res.succeeded()) {
// let resultSet = res.result().iterator();
//
// if (!resultSet.hasNext()) {
// // no result found
// ctx.fail(404);
// return;
// } else {
// let row = resultSet.next();
//
// ctx.response()
// .putHeader("Content-Type", "application/json")
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)}));
// }
// } else {
// ctx.fail(res.cause());
// }
// });
// }
// // Step #2
// ctx => {
// // vert.x promise
// let postgres = util.promisify(client);
//
// postgres
// .preparedQuery(SELECT_WORLD, Tuple.of(1))
// .then(rs => {
// let resultSet = rs.iterator();
//
// if (!resultSet.hasNext()) {
// ctx.fail(404);
// } else {
// let row = resultSet.next();
// ctx.response()
// .putHeader("Content-Type", "application/json")
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)}));
// }
// })
// .catch(e => ctx.fail(e));
// }
// // Step #3
// ctx => {
// (async function () {
// // vert.x promise
// let postgres = util.promisify(client);
//
// try {
// let rs = await postgres.preparedQuery(SELECT_WORLD, Tuple.of(1));
// let resultSet = rs.iterator();
//
// if (!resultSet.hasNext()) {
// ctx.fail(404);
// } else {
// let row = resultSet.next();
// ctx.response()
// .putHeader("Content-Type", "application/json")
// .end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)}));
// }
// } catch (e) {
// ctx.fail(e);
// }
// })();
// }
// Step #4
async ctx => {
// vert.x promise
let postgres = util.promisify(client);
try {
let rs = await postgres.preparedQuery(SELECT_WORLD, Tuple.of(1));
let resultSet = rs.iterator();
if (!resultSet.hasNext()) {
ctx.fail(404);
} else {
let row = resultSet.next();
ctx.response()
.putHeader("Content-Type", "application/json")
.end(JSON.stringify({id: row.getInteger(0), randomNumber: row.getInteger(1)}));
}
} catch (e) {
ctx.fail(e);
}
}
);
vertx
.createHttpServer()
.requestHandler(req => app.accept(req))
.listen(8080);
console.log('Server listening at: http://localhost:8080/');
{
"name": "async-project",
"version": "1.0.0",
"private": true,
"description": "Example of an async application",
"main": "index.js",
"scripts": {
"postinstall": "vertx-scripts init",
"start": "vertx-scripts launcher run",
"package": "vertx-scripts package",
"repl": "vertx-scripts repl"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vertx-scripts": "latest"
},
"dependencies": {
"@vertx/core": "latest",
"@vertx/web": "latest",
"@reactiverse/reactive-pg-client": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment