Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Allow module path as configurable document property.
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffano committed Mar 22, 2012
1 parent f908c66 commit b6bffc1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -21,8 +21,8 @@ Set up databases and documents:

Programmatically:

var couchpenter = new Couchpenter();
couchpenter.setUp('http://user:pass@localhost:5984', './couchpenter.json', function (err) {
var couchpenter = new require('couchpenter').Couchpenter();
couchpenter.setUp('http://user:pass@localhost:5984', './couchpenter.json', process.cwd(), function (err) {
// do something
});

Expand All @@ -36,19 +36,23 @@ Couchpenter setup file is a just a simple JSON file:
"documents": {
"db1": [
{ "_id": "doc1", "foo": "bar" },
{ "_id": "doc2", "foox": "barx" },
{ "_id": "doc2", "foo": "bar" },
"path/to/doc3file.json"
],
"db2": [
{ "_id": "doc4", "fooy": "bary" }
{ "_id": "doc4", "foo": "bar" },
"path/to/modulename"
]
}
}

Databases property specifies the name of the databases that should exist in CouchDB. If the database does not exist, then it will be created.

Documents property specifies a mapping between the database and the documents that should exist in that database. If the document does not exist, then it will be created. If it already exists, then it will be updated.
The document value can either be an object, or a file path string containing the JSON document.
The document value can be:
* an object
* a file path string containing a JSON document, file name must end with .json
* a module path string, path must be relative to current directory if it's used from command-line, or relative to setUp() moduleDir if it's used programmatically

Colophon
--------
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Expand Up @@ -43,7 +43,7 @@ function exec() {
args.file = args.file || './couchpenter.json';

console.log('Couchpenter is setting up CouchDB ' + args.url);
new Couchpenter().setUp(args.url, args.file, function (err) {
new Couchpenter().setUp(args.url, args.file, process.cwd(), function (err) {
if (err) {
console.error('An error has occured. ' + err.message);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/couchpenter.js
Expand Up @@ -10,7 +10,7 @@ function Couchpenter() {
config.write(dir, cb);
}

function setUp(dbUrl, configFile, cb) {
function setUp(dbUrl, configFile, moduleDir, cb) {
var conf = config.read(configFile),
db = new Db(dbUrl),
tasks = [];
Expand All @@ -23,7 +23,7 @@ function Couchpenter() {

if (conf.documents) {
tasks.push(function (cb) {
db.setDocuments(conf.documents, cb);
db.setDocuments(conf.documents, moduleDir, cb);
});
}

Expand Down
12 changes: 8 additions & 4 deletions lib/db.js
@@ -1,7 +1,8 @@
var _ = require('underscore'),
async = require('async'),
fs = require('fs'),
nano = require('nano');
nano = require('nano'),
p = require('path');

function Db(url) {

Expand Down Expand Up @@ -50,7 +51,7 @@ function Db(url) {
// set up CouchDB documents
// - create new documents
// - update existing documents (revision number will be changed afterward)
function setDocuments(dbDocs, cb) {
function setDocuments(dbDocs, moduleDir, cb) {
console.log('Preparing documents');

// prepare only the valid documents
Expand All @@ -59,9 +60,12 @@ function Db(url) {

arr.forEach(function (item) {
if (typeof item === 'string') {
// TODO construct document either with require or json parse
try {
docs.push(JSON.parse(fs.readFileSync(item)));
if (item.match(/\.json$/)) {
docs.push(JSON.parse(fs.readFileSync(item)));
} else {
docs.push(require(p.join(moduleDir, item)));
}
} catch (e) {
console.error('- ignoring invalid document file ' + item + ', error: ' + e.message);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"async": "0.1.16",
"nano": "1.3.8",
"nano": "http://nodeload.github.com/cliffano/nano/tarball/master",
"ncp": "0.2.5",
"nomnom": "1.5.1",
"underscore": "1.3.1",
Expand Down

0 comments on commit b6bffc1

Please sign in to comment.