Last active
April 5, 2020 13:18
-
-
Save cefn/8ef16aa522c208d4d2877f2d40359a45 to your computer and use it in GitHub Desktop.
Uploads design docs to couchdb through nano for Component testing within views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
Loader | |
} = require("./designdocs/loader") | |
class CouchdbConfigurator{ | |
constructor(dbHandle){ | |
if( //duck-type check for nano db handle | |
typeof dbHandle === "object" && | |
typeof dbHandle.config === "object" && | |
dbHandle.config.url && | |
dbHandle.config.db ){ | |
this.dbHandle = dbHandle | |
} | |
else{ | |
throw "Constructor expects couchdb handle created like: nano(url).use(dbName)" | |
} | |
} | |
getDbName(){ | |
return this.dbHandle.config.db | |
} | |
async createDb(){ | |
return await this.dbHandle.server.db.create(this.getDbName()) | |
} | |
async insertDesignDocs(){ | |
const loader = new Loader() | |
const docs = loader.loadDesignDocs() | |
for(const doc of docs){ | |
await this.dbHandle.insert(doc) | |
} | |
} | |
async destroyDb(){ | |
return await this.dbHandle.server.db.destroy(this.getDbName()) | |
} | |
} | |
module.exports = { | |
CouchdbConfigurator | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment