Skip to content

Instantly share code, notes, and snippets.

@TimBeyer
TimBeyer / content-type.json
Last active December 11, 2017 16:02
Generate a migration from a content type
{
"name": "What is a collection",
"description": null,
"displayField": "plainText",
"fields": [
{
"id": "plainText",
"name": "plainText",
"type": "Text",
"localized": false,
@TimBeyer
TimBeyer / index.js
Last active February 9, 2016 10:30
requirebin sketch
var contentful = require('contentful')
var util = require('util')
var client = contentful.createClient({
// This is the space ID. A space is like a project folder in Contentful terms
space: 'developer_bookshelf',
// This is the access token for this space. Normally you get both ID and the token in the Contentful web app
accessToken: '0b7f6x59a0'
});
// This API call will request an entry with the specified ID from the space defined at the top, using a space-specific access token.
@TimBeyer
TimBeyer / index.js
Created September 10, 2013 10:06 — forked from dazld/index.js
'use strict';
var assert = require('assert'),
director = require('director'),
EventEmitter = require('events').EventEmitter,
ns,// = require('continuation-local-storage').createNamespace('testing'),
express = require('express'),
app = express(),
domain = require('domain');
var uuid = require('node-uuid');
var _ = require('lodash');
var privates = {};
function MyClass () {
this.id = uuid.v4();
privates[this.id].foobar = 'somethingprivate';
};
var items = [{'price': 123, 'tax': 0.1}, {'price': 234, 'tax': 0.3}];
var taxCalculator = function (items) {
var taxedMapper = function (item) {
return item.price + item.price * item.tax;
};
var nonTaxedMapper = function (item) {
return item.price;

Working using domains

In this example we'd like to have a factory that works per request and allows to keep track of created Models We create a factory per request and create a domain to which we attach the factory That way the singleton is automatically shared with all parts of the application under process.domain.*

Working but complicated

In this example we'd like to have a factory that works per request and allows to keep track of created Models We create a factory per request and pass it around to the parts of the code that need access This can quickly lead to hard to understand, messy code

@TimBeyer
TimBeyer / broken_app.js
Last active December 21, 2015 01:49 — forked from dazld/broken_app.js
// In this example we'd like to have a factory that works per request and allows to keep track of created Models
// The singleton will however apply to all requests and thus keep growing
// For that reason we supply it with a reset function.
// This works fine until between receiving a request and sending a response there is no asynchronous code execution
// Once multiple requests come in in very short intervals, the asynchronous execution will lead to requests cross-polluting
// the singleton class
// factory.js
//
// Should be per-request so we can keep track
@TimBeyer
TimBeyer / index.js
Created October 27, 2015 10:51
requirebin sketch
var planout = require('planout');
function getOwnPropertyDescriptors (obj) {
var descriptors = {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
descriptors[prop] = Object.getOwnPropertyDescriptor(obj, prop);
}
}
return descriptors;
@TimBeyer
TimBeyer / index.js
Created October 27, 2015 10:22
requirebin sketch
var planout = require('planout');
function getOwnPropertyDescriptors (obj) {
var descriptors = {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
descriptors[prop] = Object.getOwnPropertyDescriptor(obj, prop);
}
}
return descriptors;