Skip to content

Instantly share code, notes, and snippets.

Jed Watson JedWatson

View GitHub Profile
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 / 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',
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

To implement API authentication in KeystoneJS, you need the following:

For key based authentication

  • Middleware that validates the key in the request body or a header

For session based authentication

  • An endpoint that handles signin
  • An endpoint that handles signout
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 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 / 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 / UserStore.js
Created Mar 26, 2015
Simple store-prototype example for a User Store + View
View UserStore.js
var Store = require('store-prototype');
var UserStore = new Store();
var users = {};
var loaded = false;
loadUsers: function() {
loadUsersSomehow(function(err, results) {
users = results;
JedWatson / localfile.js
Created Apr 11, 2016
LocalFile Contents Scratch
View localfile.js
{ fieldname: 'file',
originalname: 'Advanced',
name: '',
encoding: '7bit',
mimetype: 'text/markdown',
path: '/var/folders/ck/y02pffhs31xd_k4xc9xmn3hw0000gn/T/',
extension: 'md',
size: 7703,
truncated: false,
buffer: null } }
You can’t perform that action at this time.