From 499a7da57221d7a80a1c3c0c664694cb4347bd70 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Fri, 17 Jul 2020 17:02:27 +0100 Subject: [PATCH] dev script to replicate local dev db to remote couchDb, by appname --- packages/server/scripts/replicateApp.js | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/server/scripts/replicateApp.js diff --git a/packages/server/scripts/replicateApp.js b/packages/server/scripts/replicateApp.js new file mode 100644 index 0000000000..3dba99a528 --- /dev/null +++ b/packages/server/scripts/replicateApp.js @@ -0,0 +1,56 @@ +/** + * Script to replicate your PouchDb (in your home directory) to a remote CouchDB + * USAGE... + * node scripts/replicateApp + * e.g. node scripts/replicateApp Mike http://admin:password@127.0.0.1:5984 + */ + +const { resolve, join } = require("path") +const { homedir } = require("os") +const budibaseDir = join(homedir(), ".budibase") +process.env.BUDIBASE_DIR = budibaseDir +require("dotenv").config({ path: resolve(budibaseDir, ".env") }) +const env = require("../src/environment") +const CouchDB = require("../src/db") +const clientDbName = require("../src/db/clientDb").name(env.CLIENT_ID) + +const appName = process.argv[2].toLowerCase() +const remoteUrl = process.argv[3] + +console.log(`Replicating from ${appName} to ${remoteUrl}/${appName}`) + +const run = async () => { + const clientDb = new CouchDB(clientDbName) + + const body = await clientDb.query("client/by_type", { + include_docs: true, + key: ["app"], + }) + + const app = body.rows + .map(r => r.doc) + .find(a => a.name == appName || a.name.toLowerCase() === appName) + + if (!app) { + console.log( + `Could not find app... apps: ${body.rows.map(r => r.doc.name).join(", ")}` + ) + return + } + + const devInstance = app.instances.find(i => i.name === `dev-${env.CLIENT_ID}`) + + const instanceDb = new CouchDB(devInstance._id) + const remoteDb = new CouchDB(`${remoteUrl}/${appName}`) + + instanceDb.replicate + .to(remoteDb) + .on("complete", function() { + console.log("SUCCESS!") + }) + .on("error", function(err) { + console.log(`FAILED: ${err}`) + }) +} + +run()