budibase/packages/server/src/db/client.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

const PouchDB = require("pouchdb")
2020-09-25 15:47:42 +02:00
const replicationStream = require("pouchdb-replication-stream")
2020-04-25 09:35:34 +02:00
const allDbs = require("pouchdb-all-dbs")
2021-02-01 22:02:54 +01:00
const find = require("pouchdb-find")
2020-05-14 16:12:30 +02:00
const env = require("../environment")
2020-04-07 16:12:08 +02:00
2021-03-22 16:43:26 +01:00
const COUCH_DB_URL = env.COUCH_DB_URL || "http://localhost:10000/db/"
2020-05-14 16:12:30 +02:00
const isInMemory = env.NODE_ENV === "jest"
2020-09-25 15:47:42 +02:00
PouchDB.plugin(replicationStream.plugin)
2021-02-01 22:02:54 +01:00
PouchDB.plugin(find)
2020-09-25 15:47:42 +02:00
PouchDB.adapter("writableStream", replicationStream.adapters.writableStream)
2020-05-18 11:28:38 +02:00
let POUCH_DB_DEFAULTS = {
2020-05-18 12:01:09 +02:00
prefix: COUCH_DB_URL,
skip_setup: !!env.CLOUD,
2020-05-18 12:01:09 +02:00
}
2020-05-18 11:28:38 +02:00
if (isInMemory) {
PouchDB.plugin(require("pouchdb-adapter-memory"))
POUCH_DB_DEFAULTS = {
prefix: undefined,
2020-05-18 12:01:09 +02:00
adapter: "memory",
}
2020-05-18 11:28:38 +02:00
}
const Pouch = PouchDB.defaults(POUCH_DB_DEFAULTS)
2020-05-07 11:53:34 +02:00
allDbs(Pouch)
// replicate your local levelDB pouch to a running HTTP compliant couch or pouchdb server.
/* istanbul ignore next */
// eslint-disable-next-line no-unused-vars
function replicateLocal() {
Pouch.allDbs().then(dbs => {
for (let db of dbs) {
new Pouch(db).sync(
new PouchDB(`http://127.0.0.1:5984/${db}`, { live: true })
)
}
})
}
2020-11-12 11:41:49 +01:00
// replicateLocal()
module.exports = Pouch