2022-03-29 17:03:44 +02:00
|
|
|
const pouch = require("./pouch")
|
2022-04-21 15:56:14 +02:00
|
|
|
const env = require("../environment")
|
2021-04-20 18:17:44 +02:00
|
|
|
|
2022-03-29 17:03:44 +02:00
|
|
|
let PouchDB
|
|
|
|
let initialised = false
|
2022-05-20 19:29:37 +02:00
|
|
|
const dbList = new Set()
|
2022-03-29 17:03:44 +02:00
|
|
|
|
|
|
|
const put =
|
|
|
|
dbPut =>
|
|
|
|
async (doc, options = {}) => {
|
2022-03-30 15:24:04 +02:00
|
|
|
if (!doc.createdAt) {
|
|
|
|
doc.createdAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
doc.updatedAt = new Date().toISOString()
|
|
|
|
return dbPut(doc, options)
|
2022-03-29 17:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const checkInitialised = () => {
|
|
|
|
if (!initialised) {
|
|
|
|
throw new Error("init has not been called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.init = opts => {
|
|
|
|
PouchDB = pouch.getPouch(opts)
|
|
|
|
initialised = true
|
2021-04-15 17:45:21 +02:00
|
|
|
}
|
2021-04-15 17:57:01 +02:00
|
|
|
|
2022-04-19 20:42:52 +02:00
|
|
|
// NOTE: THIS IS A DANGEROUS FUNCTION - USE WITH CAUTION
|
|
|
|
// this function is prone to leaks, should only be used
|
|
|
|
// in situations that using the function doWithDB does not work
|
|
|
|
exports.dangerousGetDB = (dbName, opts) => {
|
2022-03-29 17:03:44 +02:00
|
|
|
checkInitialised()
|
2022-05-20 19:29:37 +02:00
|
|
|
if (env.isTest()) {
|
|
|
|
dbList.add(dbName)
|
|
|
|
}
|
2022-03-29 17:03:44 +02:00
|
|
|
const db = new PouchDB(dbName, opts)
|
|
|
|
const dbPut = db.put
|
|
|
|
db.put = put(dbPut)
|
|
|
|
return db
|
2021-04-20 18:17:44 +02:00
|
|
|
}
|
2021-05-14 17:31:07 +02:00
|
|
|
|
2022-04-20 18:33:42 +02:00
|
|
|
// use this function if you have called dangerousGetDB - close
|
|
|
|
// the databases you've opened once finished
|
|
|
|
exports.closeDB = async db => {
|
2022-04-21 15:56:14 +02:00
|
|
|
if (!db || env.isTest()) {
|
2022-04-20 18:33:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
2022-04-27 16:58:55 +02:00
|
|
|
// specifically await so that if there is an error, it can be ignored
|
|
|
|
return await db.close()
|
2022-04-20 18:33:42 +02:00
|
|
|
} catch (err) {
|
|
|
|
// ignore error, already closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:42:52 +02:00
|
|
|
// we have to use a callback for this so that we can close
|
|
|
|
// the DB when we're done, without this manual requests would
|
|
|
|
// need to close the database when done with it to avoid memory leaks
|
2022-06-09 13:33:10 +02:00
|
|
|
exports.doWithDB = async (dbName, cb, opts = {}) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
const db = exports.dangerousGetDB(dbName, opts)
|
|
|
|
// need this to be async so that we can correctly close DB after all
|
|
|
|
// async operations have been completed
|
2022-04-21 00:10:39 +02:00
|
|
|
try {
|
|
|
|
return await cb(db)
|
|
|
|
} finally {
|
|
|
|
await exports.closeDB(db)
|
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
|
|
|
|
2022-03-29 17:03:44 +02:00
|
|
|
exports.allDbs = () => {
|
2022-05-20 19:29:37 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
throw new Error("Cannot be used outside test environment.")
|
|
|
|
}
|
2022-03-29 17:03:44 +02:00
|
|
|
checkInitialised()
|
2022-05-20 19:29:37 +02:00
|
|
|
return [...dbList]
|
2021-05-14 17:31:07 +02:00
|
|
|
}
|