Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var cradle = require('cradle');
- var readline = require('readline');
- var Step = require('step');
- if (!module.parent) {
- if(process.argv.length != 8) {
- console.log("usage: node csv2couchdb csvfilename couchhost couchport couchusername couchpassword couchdbname");
- console.log("example: node csv2couchdb testdata/short.csv localhost 5984 admin password relaxedaccountant");
- console.log("\n~~~ Make sure that your couchDB instance is running ~~~\n");
- }
- else {
- var csvfile = process.argv[2];
- console.log("path to csvfile:: "+csvfile);
- var config = { "couchDB": {} };
- config.couchDB.host = process.argv[3];
- config.couchDB.port = process.argv[4];
- console.log("host:port:: "+config.couchDB.host+":"+config.couchDB.port);
- var username = process.argv[5];
- console.log("username:: "+username);
- var password = process.argv[6];
- console.log("password length:: "+password.length);
- config.couchDB.name = process.argv[7];
- console.log("couchdbname:: "+config.couchDB.name);
- try {
- csvToCouch(connectToCouch(config, username, password), csvfile);
- } catch(err) {
- console.dir(err);
- }
- }
- }
- function connectToCouch(config, username, password) {
- var db = new(cradle.Connection)(
- config.couchDB.host,
- config.couchDB.port,
- {
- secure: false,
- auth: {
- username: username,
- password: password
- }
- }
- ).database(config.couchDB.name);
- ensureDbExists(db, function(err) {
- if (err) {
- throw err;
- }
- initializeEntries(db);
- });
- return db;
- }
- function ensureDbExists(db, next) {
- db.exists(function(err, res) {
- if (err) {
- next(err);
- }
- if (res) {
- console.log('Found database.');
- next(null);
- } else {
- console.log('Creating database.');
- db.create(next);
- }
- });
- }
- function initializeEntries(db) {
- console.log('Creating entries design document');
- db.save('_design/entries', {
- all: {
- map: function(doc) {
- if (doc.docType === 'entry') {
- emit([doc.startDate, doc.endDate], doc.description);
- }
- }
- }
- });
- }
- function csvToCouch(db,filename) {
- var csv = require('csv');
- csv()
- .fromPath(__dirname+"/"+filename, {
- columns: true
- })
- .on('data',function(data){
- try {
- saveToDb(db,data);
- } catch(err) {
- console.dir(err);
- }
- })
- .on('end',function(count){
- console.log('Number of lines: '+count);
- })
- .on('error',function(error){
- console.log(error.message);
- });
- }
- function saveToDb(db, doc) {
- db.save(doc, function(err, res) {
- if (err) {
- throw err;
- }
- console.log('Saved ' + doc.docType + ' to DB.');
- });
- }
- // function initializeWorklogs(db) {
- // console.log('Creating worklogs design document...');
- // db.save('_design/worklogs', {
- // all: {
- // map: function(doc) {
- // if (doc.docType === 'worklog') {
- // emit([doc.startDate, doc.endDate], doc.description);
- // }
- // }
- // }
- // }, function(err, res) {
- // if (err) {
- // console.dir(err);
- // throw err;
- // }
- // db.view('worklogs/all', function(err, res) {
- // if (err) {
- // throw err;
- // }
- // if (res.length == 0) {
- // console.log('Inserting example worklogs.');
- // for (var i = 1; i <= 2; i++) {
- // saveToDb(db, {
- // docType: 'worklog',
- // startDate: '2011-04-13',
- // endDate: '2011-04-14',
- // hours: 8.3,
- // description: 'Worklog ' + i
- // });
- // }
- // //Put a fake worklog for the dashboard
- // var fakeworklog = {
- // docType: 'worklog',
- // startDate: '2011-04-13',
- // endDate: '2011-04-14',
- // hours: 8.3,
- // description: 'This is to test the dashboard.'
- // };
- // db.save('dashboard-test', fakeworklog, function(err, res) {
- // if (err) {
- // throw err;
- // }
- // console.log('Saved dashboard fake worklog to DB.');
- // });
- // } else {
- // console.log('Found existing worklogs.');
- // }
- // db.merge('_design/worklogs', {
- // filters: {
- // "worklogs": "function(doc, req) { return (doc.docType === 'worklog'); }"
- // }
- // }, function (err, res) {
- // if (err) { console.log('could not add filters'); }
- // });
- // });
- // });
- // }
- // function initializeUsers(db) {
- // console.log('Creating users design document...');
- // db.save('_design/users', {
- // all: {
- // map: function(doc) {
- // if (doc.docType === 'user') {
- // emit(doc.email, doc.sha1Password);
- // }
- // }
- // }
- // }, function(err, res) {
- // if (err) {
- // throw err;
- // }
- // db.view('users/all', function(err, res) {
- // if (err) {
- // throw err;
- // }
- // if (res.length == 0) {
- // console.log('Inserting example users.');
- // var user = User.create({
- // docType: 'user',
- // email: 'danny@antportal.com',
- // password: 'ant1'
- // });
- // saveToDb(db, user);
- // console.log('Created user: '+user.email);
- // var user = User.create({
- // docType: 'user',
- // email: 'hasan@antportal.com',
- // password: 'ant1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'arlolra@antportal.com',
- // password: 'ant1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'nebu@antportal.com',
- // password: 'ant1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'leah@antportal.com',
- // password: 'ant1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'claude@bpa.com',
- // password: 'bpa1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'marc@bpa.com',
- // password: 'bpa1'
- // });
- // saveToDb(db, user);
- // var user = User.create({
- // docType: 'user',
- // email: 'jonathan@bpa.com',
- // password: 'bpa1'
- // });
- // saveToDb(db, user);
- // // for (var i = 1; i <= 2; i++) {
- // // var password = 'test' + i;
- // // var email = password + '@example.com';
- // // console.log('Creating user with email: "'+email+'", password "'+password+'"');
- // // var user = User.create({
- // // docType: 'user',
- // // email: email
- // // });
- // // user.password = password;
- // // saveToDb(db, user);
- // // }
- // } else {
- // console.log('Found existing users.');
- // }
- // });
- // });
- // }
- // function initializeUtilities(db) {
- // console.log('Creating utility design document...');
- // db.save('_design/util', {
- // allWithEditors: {
- // map: function(doc) {
- // if (doc.editors && doc.editors.length > 0) {
- // emit(doc._id, doc.editors);
- // }
- // }
- // }
- // }, function (err, _) {
- // if (err) {
- // console.log('Error creating utility design document.');
- // throw err;
- // }
- // });
- // }
Add Comment
Please, Sign In to add comment