cloudant-database-generator
Generate (or destroy) cloudant databases using a simple on disk file structure.
cloudant-database-generator provides both a module and a cli, thus enabling you to use the generator directly within your app or from the command line.
Install
npm install cloudant-database-generator
Global installation is convenient when using the CLI
npm install cloudant-database-generator -g
Usage
CLI
create dbs:
cloudant-database-generator.js create --url <YOUR-CLOUDANT-URL> --path <RESOURCE_PATH>
destroy dbs:
cloudant-database-generator.js destroy --url <YOUR-CLOUDANT-URL> --path <RESOURCE_PATH>
Note: RESOURCE_PATH
may be absolute or relative to the current working directy
Module
const Generator = ;const path = ; // Create a custom progress visitorconst progressVisitor = { if elevel >= 30 console;} // Define the cloudant url // You may use an alternate initialization object (see initialization section)const url = '<CLOUDANT-URL>'; // Define the directory that contains our db assets e.g. ./db-resourcesconst assetPath = path url progressVisitor ; // pass false to ignore all docs, but design docs
Data Layout
How it works...
In in the resource directory (RESOURCE_PATH
), create a folder for each cloudant db. Each folder's name should be the name of a database to create.
Within each db folder, create a _design
folder. The _design
folder should contain a list of json files containing each design document.
Also, within each db folder, create a json document to host any documents to add at generation time. This should follow the bulk document format.
See examples/db-resources to review a sample layout
To try it, you can run the example:
- clone this repo
cd examples
- edit exmaples.js and set
<CLOUDANT-URL>
to your cloudant url node example
- once complete, the on disk structure will be live in your cloudant database
Sample layout
See examples/db-resources for a complete exmaple
resource-folder
database-1
_design
designdoc1-1.json
designdoc1-2.json
bulkdocs1-1.json
bulkdocs1-2.json
database-2
_design
designdoc2-1.json
designdoc2-2.json
bulkdocs2-1.json
...
You may use any name for files and folders. Two things to note:
- the name of the database folder becomes the name is the database
- the
_design
is a system name and is used to store design docs- If you choose you can add design docs using the bulk format. However, it is not recommended. Design documents are immportant and thus should be managed and versioned independently.
Note that both the _design
folder and the all *.json
documents are optional. If your db does not require docs to be created at generation time, you don't need to represented them on disk.
For example, below we represent a db with database-1
which is empty and db with name database-2
which contains a single design doc.
resource-folder
database-1
database-2
_design
designdoc1.json
...
Design doc representation
Its a standard cloudant design doc.
e.g. designdoc1-1.json
above
Doc representation
It's a standard bulk doc e.g. bulkdocs1-1.json
above
Module Initialization
var cloudant = Cloudant({account:me, password:password}); If you would prefer, you can also initialize Cloudant with a URL:
Cloudant Url
const url = "https://MYUSERNAME:MYPASSWORD@MYACCOUNT.cloudant.com"url
Bluemix
Running on Bluemix? You can initialize Cloudant directly from the VCAP_SERVICES environment variable:
instanceName: 'foo' vcapServices: JSON;
Account
const url = "https://MYUSERNAME:MYPASSWORD@MYACCOUNT.cloudant.com"account:me password:password
CLI Usage
Currently, the CLI only support a Cloudant URL.
> cloudant-database-generator Usage: cloudant-database-generator [command] [options] Commands: create destroy Options: -h, --help output usage information -V, --version output the version number -u --url <url> Cloudant database url -p --path <path> Resource path. Default ./cloudant-database -b --verbose Verbose logs -d --designonly Import design docs only. Do no import other docs