Skip to content

Instantly share code, notes, and snippets.

View garbados's full-sized avatar

DFB garbados

View GitHub Profile
@garbados
garbados / relations.js
Last active December 24, 2015 16:19
Example of entity relations in a Cloudant design document.
{
// name of the design doc
_id: '_design/relations',
views: {
lib: couchapp.loadFiles(__dirname + '/../lib'),
// collate entity relations using
collate: {
map: function (doc) {
var deref = require('views/lib/index').deref,
relation = deref[doc.type];
@garbados
garbados / app.js
Last active December 24, 2015 00:49
Very basic [node.couchapp.js](https://github.com/mikeal/node.couchapp.js) config files
var couchapp = require('couchapp'),
path = require('path');
ddoc = {
_id: '_design/app',
rewrites: [{
from: '',
to: '/index.html'
},{
from: '*',
@garbados
garbados / couchdb-tunnel.pl
Created September 7, 2013 21:17
Copy of the Linode script for creating an SSH tunnel to a CouchDB instance on a Linode server. Source: https://library.linode.com/databases/couchdb/ssh-tunnel#sph_create-a-tunnel-with-couchdb-tunnel-on-mac-os-x-or-linux
#!/usr/bin/perl
# CouchDB Tunnel Tool for MacOS X and Linux
# Copyright (c) 2010 Linode, LLC
# Author: Philip C. Paradis <[email protected]>
# Modifications: Sam Kleinman <[email protected]>
# Usage: couchdb-tunnel.pl [start|stop]
# Access a CouchDB database instance by way of an SSH tunnel.
##
@garbados
garbados / list-format.js
Last active December 21, 2015 23:29
Example map function with sample output and list function.
function(head, req){
var i,
x,
results = {},
headers = {},
csv = [];
while(x = getRow()){
(function(row){
var result = results[JSON.stringify(row.key)] || {};
for(i in row.value){
@garbados
garbados / custom.js
Last active January 25, 2018 07:58
Sample CouchDB map functions for working with dates without a library.
function(doc){
// handle a custom format using methods in the date object
var format = "{getFullYear}/{getMonth}/{getDate}",
date = new Date(doc.created_at),
// use a regular expression to replace parts of our format string
formatted_date = format.replace(/\{(\w+)\}/g, function(str, func){
var time = date[func]();
// if `func` is `getMonth` we must offset by one
if(func === 'getMonth'){
time += 1;
@garbados
garbados / gist:6105137
Created July 29, 2013 15:26
example of using commonjs to separate transformation logic from the map function itself
{
views: {
gender: {
map: function(doc){
var lib = require('/views/lib/transform')
, key_value = lib.gender(doc);
emit(key_value[0], key_value[1]);
}
},
@garbados
garbados / counter.js
Last active December 20, 2015 04:39
JavaScript "Counter" object for iterating over asynchronous tasks, so you can do something once they're done even if they don't complete in any known order.
// calls `cb` after `next()` meets `end`
// useful for asynchronous iteration
var Counter = function(end, cb){
this._count = 0;
this.next = function(){
this._count++;
if(this._count === end){
cb();
}
}

Windows Azure "ResourceProvider" Design

This doc outlines how to create a ResourceProvider to integrate Cloudant into the Microsoft Azure markertplace.

What?

Microsoft requires us to have a ResourceProvider (RP) that responds to HTTP requests, in order to mediate requests around Subscriptions and Resources.

Subscriptions are roughly analogous to a Cloudant account, while Resources equivocate to databases. The ResourceProvider must provide CRUD operations against both of these entity types. Various gotchas complicate this:

@garbados
garbados / gist:5995785
Created July 14, 2013 20:17
`yo angular` Gruntfile.js modified for Couchapps.
// Generated on 2013-07-14 using generator-angular 0.3.0
'use strict';
var LIVERELOAD_PORT = 35729;
var lrSnippet = require('connect-livereload')({ port: LIVERELOAD_PORT });
var mountFolder = function (connect, dir) {
return connect.static(require('path').resolve(dir));
};
// # Globbing
// for performance reasons we're only matching one level down:
@garbados
garbados / gist:5995080
Created July 14, 2013 17:51
Except from the Yeoman template [generator-couchapp](https://github.com/garbados/generator-couchapp) that recursively copies everything in the template directory accordingly to some simple rules. Cheap time-saver.
CouchappGenerator.prototype.app = function app() {
// copy everything in `templates` based on rules:
// - if dir, this.directory
// - if filename starts with '__', ignore
// - if filename starts with '_', this.template
// - else, this.copy
var files = fs.readdirSync(__dirname + "/templates");
for(var i in files){
var stat = fs.statSync(__dirname + "/templates/" + files[i]);
if(stat.isDirectory()){