-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
40 lines (33 loc) · 1.18 KB
/
database.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
module.exports = (db) => {
const findTownCode = async code => {
const town = await db.oneOrNone("SELECT * FROM towns WHERE code = $1", [code])
return town
}
const addRegNumber = async (regNumber, town_code) => {
db.none("INSERT INTO regNumbers (regNumber, town_code) VALUES ($1, $2)", [regNumber, town_code])
}
const getRegNumbers = async () => {
const regNumbers = await db.manyOrNone("SELECT * FROM regNumbers")
return regNumbers
}
const findRegNumber = async regNumber => {
const results = await db.oneOrNone("SELECT * FROM regNumbers WHERE regNumber = $1", [regNumber])
return results
}
const getAllTowns = async () => {
const results = await db.manyOrNone("SELECT * FROM towns")
return results
}
const getRegNumbersForTown = async (town) => {
const results = await db.manyOrNone("SELECT * FROM regNumbers JOIN towns ON towns.id = regNumbers.town_code WHERE towns.code = $1", [town])
return results
}
return {
findTownCode,
addRegNumber,
getRegNumbers,
findRegNumber,
getAllTowns,
getRegNumbersForTown
}
}