-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
84 lines (77 loc) · 2.46 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
/*********************************************************************
*
* include some packages
*
********************************************************************/
const express = require('express')
const app = express()
const port = 3000
const { Faker } = require('fakergem');
/*********************************************************************
*
* database config
*
********************************************************************/
const knex = require('knex')({
client: 'pg',
version: '7.2',
connection: {
host : 'some.postgresql.com',
user : 'postgres',
password : 'password',
database : 'knex_postgresql_poc'
}
});
/*********************************************************************
*
* list table data
*
********************************************************************/
app.get('/list', async (request, response) => {
try{
const result = await knex.select().table('demo');
response.status(200).json(result);
}catch(error){
response.status(500).json({error: 'oops, something went wrong' });
}
});
/*********************************************************************
*
* push some data into the database
*
********************************************************************/
app.get('/push', async (request, response) => {
let title = Faker.Book.title();
let description = Faker.ChuckNorris.fact();
try{
const result = await knex('demo').insert({title: title, description: description, createdAt: knex.fn.now()})
response.status(200).json({ success: 'inserted some data',
data:{
title: title,
description: description
} });
}catch(error){
response.status(500).json({error: 'oops, something went wrong', msg: error });
}
});
/*********************************************************************
*
* entrypoint root url
*
********************************************************************/
app.get('/', (req, res) => {
res.send("<p><b>choose wisely</b>\
<ul>\
<li><a href='/list'>list</a> - show the current table entries</li>\
<li><a href='/push'>push</a> - create a random entry</li>\
</ul></p>\
")
})
/*********************************************************************
*
* some cli info
*
********************************************************************/
app.listen(port, () => {
console.log(`KNEX poc is listening at http://localhost:${port}`)
})