-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
48 lines (39 loc) · 1.31 KB
/
index.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
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
// This is the file where we will use our instrumented datastore.
const SimpleDatastore = require('./simple-datastore')
const fastify = require('fastify')({ logger: true })
const { PORT: port = 3000, HOST: host = '127.0.0.1' } = process.env
const datastore = new SimpleDatastore(host, port, "DUMMY_DATABASE")
// We will be using the fastify web framework to host the
// datastore, but any host (Docker, web framework, etc.) can be used
fastify.post('/query', async (request, reply) => {
// Execute a single query
const query = 'SELECT * FROM users'
const result = await datastore.execute(query)
return reply.send({ result })
})
fastify.post('/batch', async (request, reply) => {
// Execute a batch of queries.
const queries = ['SELECT * FROM orders', 'SELECT * FROM products']
const result = await datastore.batch(queries)
return reply.send({ result })
})
fastify.addHook('onClose', async (instance) => {
// Close the connection to the datastore
datastore.close()
})
// Start the server where the database will be hosted
const start = async () => {
try {
datastore.connect()
fastify.listen({ port, host })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()