Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created July 9, 2012 09:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswomack/733e4de4b328f4fd88dc to your computer and use it in GitHub Desktop.
Save jameswomack/733e4de4b328f4fd88dc to your computer and use it in GitHub Desktop.
Starting couchdb if not running and creating a db named isd if it doesn't exist in Node.js
request = require 'request'
db = new((require 'cradle').Connection)().database('isd')
startCouchDB = =>
childproc = require "child_process"
couchdb = childproc.spawn "couchdb"
couchdb.stdout.setEncoding "utf8"
buffer = ""
couchdb.stdout.on "data", (data) =>
lines = (buffer + data).split(/\r?\n/)
buffer = lines.pop()
lines.forEach (line, index) ->
if line.match('Apache CouchDB has started on')
insert()
console.log line
couchdb.stdout.on "end", ->
if buffer.length > 0
console.log buffer
buffer = ""
console.log 'process ended'
insert = ->
request uri: "http://127.0.0.1:5984/", (e, r, b) =>
if e? and e.code is "ECONNREFUSED"
console.log "couchdb is not running, attempting to start"
startCouchDB()
else
console.log b
await db.exists defer e, exists
await db.create defer {} unless exists
#more code here...
insert()
// Generated by CoffeeScript 1.3.3
(function() {
var db, insert, request, startCouchDB,
_this = this;
request = require('request');
db = new (require('cradle')).Connection().database('isd');
startCouchDB = function() {
var buffer, childproc, couchdb;
childproc = require("child_process");
couchdb = childproc.spawn("couchdb");
couchdb.stdout.setEncoding("utf8");
buffer = "";
couchdb.stdout.on("data", function(data) {
var lines;
lines = (buffer + data).split(/\r?\n/);
buffer = lines.pop();
return lines.forEach(function(line, index) {
if (line.match('Apache CouchDB has started on')) {
insert();
}
return console.log(line);
});
});
return couchdb.stdout.on("end", function() {
if (buffer.length > 0) {
console.log(buffer);
buffer = "";
}
return console.log('process ended');
});
};
insert = function() {
var _this = this;
return request({
uri: "http://127.0.0.1:5984/"
}, function(e, r, b) {
if ((e != null) && e.code === "ECONNREFUSED") {
console.log("couchdb is not running, attempting to start");
return startCouchDB();
} else {
console.log(b);
return db.exists(function(e, exists) {
if (!exists) {
return db.create(function(_arg) {
_arg;
});
}
});
}
});
};
insert();
}).call(this);
// Generated by IcedCoffeeScript 1.3.1b
(function() {
var db, iced, insert, request, startCouchDB, __iced_k, __iced_k_noop,
_this = this;
iced = require('iced-coffee-script').iced;
__iced_k = __iced_k_noop = function() {};
request = require('request');
db = new (require('cradle')).Connection().database('isd');
startCouchDB = function() {
var buffer, childproc, couchdb;
childproc = require("child_process");
couchdb = childproc.spawn("couchdb");
couchdb.stdout.setEncoding("utf8");
buffer = "";
couchdb.stdout.on("data", function(data) {
var lines;
lines = (buffer + data).split(/\r?\n/);
buffer = lines.pop();
return lines.forEach(function(line, index) {
if (line.match('Apache CouchDB has started on')) insert();
return console.log(line);
});
});
return couchdb.stdout.on("end", function() {
if (buffer.length > 0) {
console.log(buffer);
buffer = "";
}
return console.log('process ended');
});
};
insert = function() {
var _this = this;
return request({
uri: "http://127.0.0.1:5984/"
}, function(e, r, b) {
var e, exists, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
if ((e != null) && e.code === "ECONNREFUSED") {
console.log("couchdb is not running, attempting to start");
return __iced_k(startCouchDB());
} else {
console.log(b);
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "./forgiving-couchdb-code.iced"
});
db.exists(__iced_deferrals.defer({
assign_fn: (function() {
return function() {
e = arguments[0];
return exists = arguments[1];
};
})(),
lineno: 31
}));
__iced_deferrals._fulfill();
})(function() {
(function(__iced_k) {
if (!exists) {
(function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "./forgiving-couchdb-code.iced"
});
db.create(__iced_deferrals.defer({
lineno: 31
}));
__iced_deferrals._fulfill();
})(__iced_k);
} else {
return __iced_k();
}
})(__iced_k);
});
}
});
};
insert();
}).call(this);
@jameswomack
Copy link
Author

Node Packages

Major Packages 👅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment