Skip to content

Instantly share code, notes, and snippets.

@joewagner
joewagner / gist:5314301
Last active December 15, 2015 19:49
mongoose.js failing tests
You're not testing shards!
Please set the MONGOOSE_SHARD_TEST_URI env variable.
e.g: `mongodb://localhost:27017/database
Sharding must already be enabled on your database
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
You're not testing replica sets!
Please set the MONGOOSE_SET_TEST_URI env variable.
@joewagner
joewagner / catchVersionErr.js
Last active December 22, 2015 11:48
Helper function to catch version errors when trying to update an Array field in a mongoose Model
// --- Dependancies ---
var _ = require('underscore');
var mongoose = require('mongoose');
// Any function that makes an update to a mongoose model that has an Array field can
// use this to catch version errors, and retry the update at most two times
var catchVersionErr = function () {
var updateFunc, callback, context, args = _.toArray(arguments);
context = args.shift();
@joewagner
joewagner / gist:11326170
Created April 26, 2014 17:36
A class that runs a function on an interval. An instance of this class can be passed to many controllers, but only one interval will ever be set.
var RingOfPower = function (action, freq) {
if (typeof action === "undefined" || typeof freq === "undefined") {
throw new Error("must provide a function and an interval");
}
this.action = action;
this.frequency = freq;
};
RingOfPower.prototype.wield = function () {
clearInterval(this.interval);
@joewagner
joewagner / Sharded mongodb environment on localhost
Last active May 7, 2024 13:33
Bash shell script that sets up a sharded mongodb cluster on a single machine. Handy for testing or development when a sharded deployment is required. Notice that this will remove everything in the data/config and data/shard directories. If you are using those for something else, you may want to edit this...
# clean everything up
echo "killing mongod and mongos"
killall mongod
killall mongos
echo "removing data files"
rm -rf data/config
rm -rf data/shard*
# For mac make sure rlimits are high enough to open all necessary connections
ulimit -n 2048
@joewagner
joewagner / gist:611c31b49dbe17d49168
Last active August 29, 2015 14:01
Simple class that may be used to create a pool of mongoose connections with unique MongoURIs
var mongoose = require('mongoose');
var UserSchema = require(__dirname + '/user-schema');
var DatabasePool = function () {
this.dbs = {};
};
// gets a connection to a specific mongo uri string, or
// creates it if it doesn't exist, or isn't connected
// Then attaches "User" Model to the connection
∆ ∆ ∫
˚Ú˚ ÒÒÒ
¯¯¯ | |
@joewagner
joewagner / optimization-status.js
Created February 2, 2016 20:06
Demonstrates v8 optimization for reassigning vs. not reassigning `arguments` object
// reassigning an argument
function argReassign1(a) {
a = a || {};
}
// not reassigning an argument
function argReassign2(a) {
if (a === void 0) var a = {};
}
const express = require("express");
const session = require("express-session");
const cors = require("cors");
const app = express();
app.use(cors({
origin: ["http://localhost:3000"], credentials: true
}
));
@joewagner
joewagner / brute force limit folding a paper
Created December 28, 2019 05:10
copy paste this into a javascript shell and you'll get 33.33333333333333
var lim = function (num) {
if (num === void 0) num = 100;
var series = [100, 50, 25];
var next = function () {
var lastTerm = series[series.length - 1];
var secondToLastTerm = series[series.length - 2];
// notice that, since we started above with the first three terms, that the fliping back and forth between adding
// and subtracting is handled here ↓, i.e. `secondToLastTerm - lastTerm` is alternating negative and positive
@joewagner
joewagner / Filecoin vanilla block inspector
Last active October 28, 2021 23:29
Filecoin vanilla block inspector
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chain Height</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<!--
Make sure your Docker daemon is running