Skip to content

Instantly share code, notes, and snippets.

Jed Watson JedWatson

Block or report user

Report or block JedWatson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
JedWatson / async.js
Last active Jan 3, 2016 — forked from getify/gist:8459026
Comparison between (my) old callback-structured code, @getify's asynquence refactor, and my async refactor
View async.js
var doQuery = function() {
// passing an object to async.parallel will cause it to collect their results
// and provide them to the callback in an object with the same keys.
// functions are passed a callback with the standard argument pattern of fn(err, result)
count: count.exec,
query: query.exec
}, function(err, results) {
JedWatson / async.js
Last active Mar 1, 2018
Example usage of to update an array against a redis cache
View async.js
// This function takes a contents object with a files array.
// The goal is to cache each file details in redis then replace
// the file's path with a secure, obscured url.
// Finally the contents object is returned with the updated array
// in place of the old one.
// async = require('async')
// uuid = require('node-uuid')
// redis.client = require('node-redis').createClient()
JedWatson / async.js
Last active Jan 3, 2016 — forked from getify/async.js
Another refactor of a refactor, this time w/o all original comments but w/ proposals. Not currently possible with asynquence, but maybe? *nudge*
View async.js
var transform = function(contents, doneTransform) {
var authExpiry = 60 * 60 * 1000,
authId = uuid.v4(),
authKey = 'file:auth:' + authId;
redis.client.set(authKey, JSON.stringify({
redis.client.expire(authKey, authExpiry);
JedWatson /
Last active Feb 20, 2020
Notes on how to create a new field type for Keystone

Creating new Field Types for KeystoneJS

We're currently working on making it easier to add new field types to KeystoneJS as plugins.

In the meantime, if you'd like to work on your own field type, hopefully this guide will point you in the right direction.

Keystone fields require the following:

  • a {fieldType}.js file in ./lib/fieldTypes that controls the field and encapsulates options support, underscore functions, validation and updating
  • the {fieldType}.js file needs to be included by ./lib/fieldTypes/index.js
JedWatson /
Last active May 12, 2020
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

JedWatson / keystone.js
Created Feb 24, 2014
Example of how to integrate node-i18n with a KeystoneJS app (using yo keystone generated site as a basis) - see for more docs.
View keystone.js
// Simulate config options from your production environment by
// customising the .env file in your project's root folder.
// Require keystone and i18n
var keystone = require('keystone'),
i18n= require('i18n');
// Initialise Keystone with your project's configuration.
// See for available options
JedWatson /
Last active Apr 8, 2020
Example of how to scaffold API endpoints for Posts in a Keystone project (based on the yo keystone example).

This is an example of how to scaffold API endpoints to list / get / create / update / delete Posts in a Keystone website.

It's a modification of the default project created with the yo keystone generator (see

Gists don't let you specify full paths, so in the project structure the files would be:

routes-index.js        -->    /routes/index.js         // modified to add the api endpoints
routes-api-posts.js    -->    /routes/api/posts.js     // new file containing the Post API route controllers
JedWatson /
Last active Sep 17, 2019
Examples of how to use Keystone's createItems functionality

Populating Data in KeystoneJS

Keystone's createItems function is a simple but powerful way to populate your database with data.

It can be used to create test fixtures or initialise your database with default content / users / etc.

There's also a shorthand syntax that can be used within update files; if you are using the auto updates feature, any file that exports a create object will automatically be wrapped and the data will be created.

createItems takes two passes at the data it is passed, first creating the items and retaining references by key (if provided) that can be used to populate relationships in the second pass. This makes it easy to create related data without asynchronous nesting (which for data creation sometimes ends up in knots).

JedWatson / Countries.js
Last active Apr 18, 2020
Example of how to use the filters option for Relationship fields
View Countries.js
// A global file to provide the countries and cities
exports.countries = [{
name: 'Australia',
cities: ['Melbourne', 'Sydney', 'Canberra']
}, {
name: 'España',
cities: ['Madrid', 'Barcelona', 'Sevilla']
}, {
name: 'Italia',

Keybase proof

I hereby claim:

  • I am JedWatson on github.
  • I am jedwatson ( on keybase.
  • I have a public key whose fingerprint is 75DB 2419 8E41 BF37 4E39 88F9 5D4F 4D18 EF37 8B10

To claim this, I am signing this object:

You can’t perform that action at this time.