budibase/packages/server/scripts/replicateApp.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

/**
* Script to replicate your PouchDb (in your home directory) to a remote CouchDB
* USAGE...
* node scripts/replicateApp <app_name> <remote_url>
* e.g. node scripts/replicateApp Mike http://admin:password@127.0.0.1:5984
*/
const CouchDB = require("../src/db")
2021-05-16 22:25:37 +02:00
const { DocumentTypes } = require("../src/db/utils")
const appName = process.argv[2].toLowerCase()
const remoteUrl = process.argv[3]
console.log(`Replicating from ${appName} to ${remoteUrl}/${appName}`)
const run = async () => {
const allDbs = await CouchDB.allDbs()
2021-05-04 12:32:22 +02:00
const appDbNames = allDbs.filter(dbName => dbName.startsWith("inst_app"))
let apps = []
for (let dbName of appDbNames) {
const db = new CouchDB(dbName)
2021-05-16 22:25:37 +02:00
apps.push(db.get(DocumentTypes.APP_METADATA))
}
apps = await Promise.all(apps)
const app = apps.find(
2021-05-04 12:32:22 +02:00
a => a.name === appName || a.name.toLowerCase() === appName
)
if (!app) {
console.log(
2021-05-04 12:32:22 +02:00
`Could not find app... apps: ${apps.map(app => app.name).join(", ")}`
)
return
}
2021-05-16 22:25:37 +02:00
const instanceDb = new CouchDB(app.appId)
const remoteDb = new CouchDB(`${remoteUrl}/${appName}`)
instanceDb.replicate
.to(remoteDb)
2021-05-03 09:31:09 +02:00
.on("complete", function () {
console.log("SUCCESS!")
})
2021-05-03 09:31:09 +02:00
.on("error", function (err) {
console.log(`FAILED: ${err}`)
})
}
run()