Skip to content

Instantly share code, notes, and snippets.

View Jeff-Lewis's full-sized avatar

Jeff Lewis Jeff-Lewis

  • StreetConnect & SchoolBee
  • NYC
View GitHub Profile
@Jeff-Lewis
Jeff-Lewis / gist:60e0a5311fa37d1c8090
Created January 27, 2016 14:28 — forked from aaaristo/newer.js
a "ThreadLocal"-like context for asynchronous callback domains
function isIterator(obj)
{
return obj&&obj.next&&obj.throw;
}
function isRunWithContext(obj)
{
return obj&&typeof obj=='function'&&obj.ctx;
}
@Jeff-Lewis
Jeff-Lewis / 1-proposal.md
Created March 25, 2016 22:02 — forked from JedWatson/1-proposal.md
Proposal: adding reverse-relationship population to Mongoose (as implemented in KeystoneJS)

I've developed a useful feature in KeystoneJS that lets you populate a relationship from either side, while only storing the data on one side, and am looking for feedback on whether it is something that could / should be brought back into mongoose itself. (It might be possible to add as a separate package but I suspect there'd be too much rewriting of mongoose internals for that to be a good idea).

I've added this as an issue in mongoose for consideration: #1888 but am leaving this gist in place because the examples are easier to read.

I've used Posts and Categories as a basic, contrived example to demonstrate what I'm talking about here; in reality you'd rarely load all the posts for a category but there are other real world cases where it's less unreasonable you'd want to do this, and Posts + Categories is an easy way to demo it.

The problem

The built-in population feature is really useful; not just for

@Jeff-Lewis
Jeff-Lewis / mongoose-soft-remove.js
Created April 29, 2016 14:48 — forked from RobertWHurst/mongoose-soft-remove.js
Mongoose Soft Remove Plugin
var Model = require('mongoose').Model;
function softRemove(schema) {
if (!schema.path('isRemoved')) {
schema.add({ isRemoved : { type: Boolean, index: true, default: false } });
}
if (!schema.path('removedAt')) {
@Jeff-Lewis
Jeff-Lewis / date-util.js
Created June 3, 2016 14:24 — forked from cbumgard/date-util.js
Formatting dates/times in node.js using Olson timezones and moment.js. Converts server-side timestamps to the browser's timezone.
var moment = require('moment');
var tz = require('timezone/loaded');
var time = require('time');
module.exports = function() {
var strftime_format = '%F %T %z'; // used to convert a date into a normalized strftime format with timezone
var moment_format = 'YYYY-MM-DD HH:mm:ss zz'; // moment.js LDML format for parsing date strings
/**
* Convert a Javascript Date into node-time wrapper with the appropriate timezone.
'use strict';
const asyncWrap = process.binding('async_wrap');
const fs = require('fs');
//
// Track
//
// 1. Setup a global variable to track the context of the async_hook_init_function
let callbackContext = 'root';
process.nextTick(function () {
"use strict";
var clsModule = require("continuation-local-storage");
const superagent = require("superagent");
const assert = require("assert");
var http = require("http");
var keepAlive = process.env.KEEP_ALIVE !== "0";
var httpAgent = new http.Agent({
keepAlive: keepAlive,
@Jeff-Lewis
Jeff-Lewis / output.md
Created July 22, 2016 15:31 — forked from brycebaril/output.md
process.nextTick vs setImmediate

@mafintosh asks: "Does anyone have a good code example of when to use setImmediate instead of nextTick?"

https://twitter.com/mafintosh/status/624590818125352960

The answer is "generally anywhere outside of core".

process.nextTick is barely asynchronous. Flow-wise it is asynchronous, but it will trigger before any other asynchronous events can (timers, io, etc.) and thus can starve the event loop.

In this script I show a starved event loop where I just synchronously block, use nextTick and setImmediate

@Jeff-Lewis
Jeff-Lewis / domain-postmortem.md
Created August 8, 2016 18:41
node domain postmortem

domain Module Postmortem

Usability Issues

Implicit Behavior

It's possible for a developer to create a new domain and then simply run domain.enter(). Which then acts as a catch-all for any exception in the future that couldn't be observed by the thrower. Allowing a module author to intercept the exceptions of unrelated code in a different module. Preventing

@Jeff-Lewis
Jeff-Lewis / mongooseUniquenessValidation.js
Created August 25, 2016 22:28 — forked from edinella/mongooseUniquenessValidation.js
Mongoose uniqueness validator, case-sensitive or not
// context
var mongoose = require('mongoose');
mongoose.connect('mongodb://...');
/**
* Generates Mongoose uniqueness validator
*
* @param string modelName
* @param string field
* @param boolean caseSensitive