-
Notifications
You must be signed in to change notification settings - Fork 22
/
simple-datastore.js
63 lines (55 loc) · 1.54 KB
/
simple-datastore.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
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
// This is our example datastore. It has common methods that
// we would expect to see in a datastore, such as connect, close,
// execute query, and batch queries. It is worth noting that
// this datastore is assumed to be SQL-like.
class SimpleDatastore {
constructor (host, port, database=null) {
this.client = null
this.connected = false
this.host = host
this.port = port
this.database = database
}
connect () {
this.connected = true
console.log('Connected to simple-datastore')
}
close () {
this.connected = false
console.log('Closed connection to simple-datastore')
}
// These are our datastore query methods
execute (query) {
return new Promise((resolve, reject) => {
if (!this.connected) {
return reject(Error('Not connected'))
}
// Here we will simulate executing a query
// by logging and returning a result.
console.log(query)
const result = { attribute: 'value' }
resolve(result)
})
}
batch (queries) {
return new Promise((resolve, reject) => {
if (!this.connected) {
return reject(Error('Not connected'))
}
const results = []
for (const query of queries) {
// Simulate waiting for each query to execute
console.log(query)
const result = { attribute: 'value' }
results.push(result)
}
resolve(results)
})
}
}
module.exports = SimpleDatastore