-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
48 lines (45 loc) · 1.38 KB
/
db.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
const { MongoClient, ObjectID } = require('mongodb')
const user = {
name: process.env.DBUSER,
pass: process.env.DBPASS,
}
const server = `${process.env.DBURL}/outvoice?retryWrites=true`
const uri = `mongodb+srv://${user.name}:${user.pass}@${server}`
const db = MongoClient.connect(
uri,
{ useNewUrlParser: true },
).then(async client => {
process.on('SIGINT', () => client.close(() => process.exit(0)))
return client.db('outvoice').collection('invoices')
})
const replacer = (rgx, map) => (fn => s => s.replace(rgx, fn))(c => map[c])
const urlSafeEncode = replacer(/[+/=]/g, { '+': '-', '/': '_', '=': '.' })
const urlSafeDecode = replacer(/[-_.]/g, { '-': '+', _: '/', '.': '=' })
const toHex = n => n.toString(16).padStart(2, '0')
const fromHex = s => parseInt(s, 16)
const decodeId = id =>
[...Buffer.from(urlSafeDecode(id), 'base64')].map(toHex).join('')
const encodeId = id =>
urlSafeEncode(
Buffer.from(
String(id)
.match(/.{2}/g)
.map(fromHex),
).toString('base64'),
)
module.exports.invoices = {
set: async data => encodeId((await (await db).insertOne(data)).insertedId),
get: async id => {
const decoded = decodeId(id)
let obj
try {
obj = new ObjectID(decoded)
} catch (err) {
return null
}
const data = await (await db).findOne(obj)
if (!data) return null
data._id = undefined
return data
},
}