2022-03-29 17:03:44 +02:00
|
|
|
const pouch = require("./pouch")
|
2021-04-20 18:17:44 +02:00
|
|
|
|
2022-03-29 17:03:44 +02:00
|
|
|
let PouchDB
|
|
|
|
let initialised = false
|
|
|
|
|
|
|
|
const put =
|
|
|
|
dbPut =>
|
|
|
|
async (doc, options = {}) => {
|
|
|
|
const response = await dbPut(doc, options)
|
|
|
|
// TODO: add created / updated
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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-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
|
|
|
|
exports.doWithDB = async (dbName, cb, opts) => {
|
|
|
|
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
|
|
|
|
const resp = await cb(db)
|
|
|
|
try {
|
|
|
|
await db.close()
|
|
|
|
} catch (err) {
|
|
|
|
// ignore error - it may have not opened database/is closed already
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:03:44 +02:00
|
|
|
exports.allDbs = () => {
|
|
|
|
checkInitialised()
|
|
|
|
return PouchDB.allDbs()
|
2021-05-14 17:31:07 +02:00
|
|
|
}
|