Skip to content

Instantly share code, notes, and snippets.

@JedWatson
JedWatson / KeystoneApiExample.md
Last active July 26, 2021 11:29
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 https://github.com/JedWatson/generator-keystone)

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
JedWatson / keystone.js
Created February 24, 2014 16:00
Example of how to integrate node-i18n with a KeystoneJS app (using yo keystone generated site as a basis) - see https://github.com/mashpie/i18n-node for more docs.
// Simulate config options from your production environment by
// customising the .env file in your project's root folder.
require('dotenv')().load();
// Require keystone and i18n
var keystone = require('keystone'),
i18n= require('i18n');
// Initialise Keystone with your project's configuration.
// See http://keystonejs.com/guide/config for available options
@JedWatson
JedWatson / 1-proposal.md
Last active January 2, 2024 17:59
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
JedWatson / notes.md
Last active February 20, 2020 13:01
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
JedWatson / async.js
Last active January 3, 2016 18:29 — 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*
var transform = function(contents, doneTransform) {
var authExpiry = 60 * 60 * 1000,
authId = uuid.v4(),
authKey = 'file:auth:' + authId;
redis.client.set(authKey, JSON.stringify({
userId: req.user.id
}));
redis.client.expire(authKey, authExpiry);
@JedWatson
JedWatson / async.js
Last active March 1, 2018 11:29
Example usage of async.map to update an array against a redis cache
// 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
JedWatson / async.js
Last active January 3, 2016 14:19 — forked from getify/gist:8459026
Comparison between (my) old callback-structured code, @getify's asynquence refactor, and my async refactor
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.
async.parallel({
// functions are passed a callback with the standard argument pattern of fn(err, result)
count: count.exec,
query: query.exec
}, function(err, results) {