Skip to content

Instantly share code, notes, and snippets.

View prestonp's full-sized avatar
😍
you had me at hello world

Preston Pham prestonp

😍
you had me at hello world
View GitHub Profile
@prestonp
prestonp / quicksort.md
Last active August 29, 2015 13:56
underscore quicksort

Save and run as sort.js with a list of integers as arguments

node sort.js 1 3 2 9 3 22

output

[ 1, 2, 3, 3, 9, 22 ]
@prestonp
prestonp / gist:9535819
Last active August 29, 2015 13:57
unexepected timestamp handling
var models = require('./models');
var utils = require('./utils');
var config = require('./config');

models.Order.find({}, function(err, results) {
  console.log('models.Order.find type of datetime',results[0].attributes.datetime);
  console.log('type of datetime', typeof results[0].attributes.datetime);
});
@prestonp
prestonp / gist:9842745
Last active August 29, 2015 13:57
aggregate arrays in postgresql

Tables

Table restaurants
        Column         |           Type           
-----------------------+--------------------------
 id                    | integer                  
 
Table contacts
    Column     |            Type             
@prestonp
prestonp / example.md
Created April 14, 2014 17:27
scheduler example

Declare actions.js

var scheduler = require('../lib/scheduler');
scheduler.registerAction('make-call', function(job, done) {
  twilio.makeCall(job.data, function(error) {
    if (error) {
      logger.error('Could not place call for job #' + job.id, error);
      done('failed');
 } else {
@prestonp
prestonp / heapdump.js
Last active August 29, 2015 14:01
heapdump
// Step 1: npm install heapdump
// Step 2: Paste this snippet in your main
// app to create heap snapshots that you
// can view in the chrome profiler
require('heapdump');
setInterval(function() {
process.kill(process.pid, 'SIGUSR2');
}, 15000);
@prestonp
prestonp / readme.md
Last active August 29, 2015 14:01
stampy

Usage

Stampy is used to log time differences. You could use it to profile time-consuming operations.

Note that the first .log() begins the starting timestamp. Consecutive .log() just prints the diff from the last timestamp.

var stampy = require('stampy');
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility;
line-height: 1.5em;
@prestonp
prestonp / dirac-plugins.js
Last active August 29, 2015 14:03
dirac plugins
// monkey patches dirac with ./plugins folder
var dirac = require('dirac');
modules.exports = function(options) {
options = options || {};
// plugins folder contains table folders, each containing plugin definitions
fs.readDir(options.path || './plugins');
// pseudocode
@prestonp
prestonp / doc.md
Last active August 29, 2015 14:03
transaction example

Transactions API

To perform transactions use the dirac.tx api. Dirac defaults to using client pooling per request. So a single client must be used over the lifespan of a transaction. The tx will fetch one for you and release it upon .commit().

A dirac tx object has access to the same DALs in addition to transactional commands.