Guest User

Untitled

a guest
Dec 23rd, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. var fs = require('fs');
  2. var cradle = require('cradle');
  3. var readline = require('readline');
  4. var Step = require('step');
  5.  
  6. if (!module.parent) {
  7. if(process.argv.length != 8) {
  8. console.log("usage: node csv2couchdb csvfilename couchhost couchport couchusername couchpassword couchdbname");
  9. console.log("example: node csv2couchdb testdata/short.csv localhost 5984 admin password relaxedaccountant");
  10. console.log("\n~~~ Make sure that your couchDB instance is running ~~~\n");
  11. }
  12. else {
  13. var csvfile = process.argv[2];
  14. console.log("path to csvfile:: "+csvfile);
  15. var config = { "couchDB": {} };
  16. config.couchDB.host = process.argv[3];
  17. config.couchDB.port = process.argv[4];
  18. console.log("host:port:: "+config.couchDB.host+":"+config.couchDB.port);
  19. var username = process.argv[5];
  20. console.log("username:: "+username);
  21. var password = process.argv[6];
  22. console.log("password length:: "+password.length);
  23. config.couchDB.name = process.argv[7];
  24. console.log("couchdbname:: "+config.couchDB.name);
  25.  
  26. try {
  27. csvToCouch(connectToCouch(config, username, password), csvfile);
  28. } catch(err) {
  29. console.dir(err);
  30. }
  31. }
  32. }
  33.  
  34. function connectToCouch(config, username, password) {
  35. var db = new(cradle.Connection)(
  36. config.couchDB.host,
  37. config.couchDB.port,
  38. {
  39. secure: false,
  40. auth: {
  41. username: username,
  42. password: password
  43. }
  44. }
  45. ).database(config.couchDB.name);
  46. ensureDbExists(db, function(err) {
  47. if (err) {
  48. throw err;
  49. }
  50. initializeEntries(db);
  51. });
  52. return db;
  53. }
  54.  
  55. function ensureDbExists(db, next) {
  56. db.exists(function(err, res) {
  57. if (err) {
  58. next(err);
  59. }
  60. if (res) {
  61. console.log('Found database.');
  62. next(null);
  63. } else {
  64. console.log('Creating database.');
  65. db.create(next);
  66. }
  67. });
  68. }
  69.  
  70. function initializeEntries(db) {
  71. console.log('Creating entries design document');
  72. db.save('_design/entries', {
  73. all: {
  74. map: function(doc) {
  75. if (doc.docType === 'entry') {
  76. emit([doc.startDate, doc.endDate], doc.description);
  77. }
  78. }
  79. }
  80. });
  81. }
  82.  
  83. function csvToCouch(db,filename) {
  84. var csv = require('csv');
  85.  
  86. csv()
  87. .fromPath(__dirname+"/"+filename, {
  88. columns: true
  89. })
  90. .on('data',function(data){
  91. try {
  92. saveToDb(db,data);
  93. } catch(err) {
  94. console.dir(err);
  95. }
  96. })
  97. .on('end',function(count){
  98. console.log('Number of lines: '+count);
  99. })
  100. .on('error',function(error){
  101. console.log(error.message);
  102. });
  103.  
  104. }
  105.  
  106. function saveToDb(db, doc) {
  107. db.save(doc, function(err, res) {
  108. if (err) {
  109. throw err;
  110. }
  111. console.log('Saved ' + doc.docType + ' to DB.');
  112. });
  113. }
  114.  
  115.  
  116.  
  117. // function initializeWorklogs(db) {
  118. // console.log('Creating worklogs design document...');
  119. // db.save('_design/worklogs', {
  120. // all: {
  121. // map: function(doc) {
  122. // if (doc.docType === 'worklog') {
  123. // emit([doc.startDate, doc.endDate], doc.description);
  124. // }
  125. // }
  126. // }
  127. // }, function(err, res) {
  128. // if (err) {
  129. // console.dir(err);
  130. // throw err;
  131. // }
  132. // db.view('worklogs/all', function(err, res) {
  133. // if (err) {
  134. // throw err;
  135. // }
  136. // if (res.length == 0) {
  137. // console.log('Inserting example worklogs.');
  138. // for (var i = 1; i <= 2; i++) {
  139. // saveToDb(db, {
  140. // docType: 'worklog',
  141. // startDate: '2011-04-13',
  142. // endDate: '2011-04-14',
  143. // hours: 8.3,
  144. // description: 'Worklog ' + i
  145. // });
  146. // }
  147. // //Put a fake worklog for the dashboard
  148. // var fakeworklog = {
  149. // docType: 'worklog',
  150. // startDate: '2011-04-13',
  151. // endDate: '2011-04-14',
  152. // hours: 8.3,
  153. // description: 'This is to test the dashboard.'
  154. // };
  155. // db.save('dashboard-test', fakeworklog, function(err, res) {
  156. // if (err) {
  157. // throw err;
  158. // }
  159. // console.log('Saved dashboard fake worklog to DB.');
  160. // });
  161. // } else {
  162. // console.log('Found existing worklogs.');
  163. // }
  164. // db.merge('_design/worklogs', {
  165. // filters: {
  166. // "worklogs": "function(doc, req) { return (doc.docType === 'worklog'); }"
  167. // }
  168. // }, function (err, res) {
  169. // if (err) { console.log('could not add filters'); }
  170. // });
  171. // });
  172. // });
  173. // }
  174.  
  175. // function initializeUsers(db) {
  176. // console.log('Creating users design document...');
  177. // db.save('_design/users', {
  178. // all: {
  179. // map: function(doc) {
  180. // if (doc.docType === 'user') {
  181. // emit(doc.email, doc.sha1Password);
  182. // }
  183. // }
  184. // }
  185. // }, function(err, res) {
  186. // if (err) {
  187. // throw err;
  188. // }
  189. // db.view('users/all', function(err, res) {
  190. // if (err) {
  191. // throw err;
  192. // }
  193. // if (res.length == 0) {
  194. // console.log('Inserting example users.');
  195. // var user = User.create({
  196. // docType: 'user',
  197. // email: 'danny@antportal.com',
  198. // password: 'ant1'
  199. // });
  200. // saveToDb(db, user);
  201. // console.log('Created user: '+user.email);
  202. // var user = User.create({
  203. // docType: 'user',
  204. // email: 'hasan@antportal.com',
  205. // password: 'ant1'
  206. // });
  207. // saveToDb(db, user);
  208. // var user = User.create({
  209. // docType: 'user',
  210. // email: 'arlolra@antportal.com',
  211. // password: 'ant1'
  212. // });
  213. // saveToDb(db, user);
  214. // var user = User.create({
  215. // docType: 'user',
  216. // email: 'nebu@antportal.com',
  217. // password: 'ant1'
  218. // });
  219. // saveToDb(db, user);
  220. // var user = User.create({
  221. // docType: 'user',
  222. // email: 'leah@antportal.com',
  223. // password: 'ant1'
  224. // });
  225. // saveToDb(db, user);
  226. // var user = User.create({
  227. // docType: 'user',
  228. // email: 'claude@bpa.com',
  229. // password: 'bpa1'
  230. // });
  231. // saveToDb(db, user);
  232. // var user = User.create({
  233. // docType: 'user',
  234. // email: 'marc@bpa.com',
  235. // password: 'bpa1'
  236. // });
  237. // saveToDb(db, user);
  238. // var user = User.create({
  239. // docType: 'user',
  240. // email: 'jonathan@bpa.com',
  241. // password: 'bpa1'
  242. // });
  243. // saveToDb(db, user);
  244.  
  245. // // for (var i = 1; i <= 2; i++) {
  246. // // var password = 'test' + i;
  247. // // var email = password + '@example.com';
  248. // // console.log('Creating user with email: "'+email+'", password "'+password+'"');
  249. // // var user = User.create({
  250. // // docType: 'user',
  251. // // email: email
  252. // // });
  253. // // user.password = password;
  254. // // saveToDb(db, user);
  255. // // }
  256. // } else {
  257. // console.log('Found existing users.');
  258. // }
  259. // });
  260. // });
  261. // }
  262.  
  263. // function initializeUtilities(db) {
  264. // console.log('Creating utility design document...');
  265. // db.save('_design/util', {
  266. // allWithEditors: {
  267. // map: function(doc) {
  268. // if (doc.editors && doc.editors.length > 0) {
  269. // emit(doc._id, doc.editors);
  270. // }
  271. // }
  272. // }
  273. // }, function (err, _) {
  274. // if (err) {
  275. // console.log('Error creating utility design document.');
  276. // throw err;
  277. // }
  278. // });
  279. // }
Add Comment
Please, Sign In to add comment