Skip to content

Instantly share code, notes, and snippets.

@Jlevyd15
Last active November 15, 2017 17:37
Show Gist options
  • Save Jlevyd15/be8d6145a55ee02cbeefb90460e874b6 to your computer and use it in GitHub Desktop.
Save Jlevyd15/be8d6145a55ee02cbeefb90460e874b6 to your computer and use it in GitHub Desktop.

keystonejs notes

Creating custom keystone Lists (models)

  • keystone lists or models basically map back to mongodb collections
  • each property you add into the model object will become a field in the keystone admin UI. The model itself will become a mongo collection in the db and the fields will also be stored under the collection.
  • each property you define in the model needs to have a type. Types are defined by the keystone api. There are many types that are built into keystone a list of them can be found Here

How to create a new list

in the models/ directory create a file called Tickets.js

var keystone = require('keystone');
var Types = keystone.Field.Types;;

var Ticket = new keystone.List('Ticket', {
    autokey: { from: 'title', path: 'slug', unique: true },
});

Ticket.add({
    title: { type: String, initial: true, default: '', required: true },
    description: { type: Types.Textarea },
    priority: { type: Types.Select, options: 'Low, Medium, High', default: 'Low' },
    category: {
        type: Types.Select,
        options: 'Bug, Feature, Enhancement',
        default: 'Bug'
    },
    status: { type: Types.Select, options: 'New, In Progress, Open, On Hold, Declined, Closed', default: 'New' },
    createdBy: {
        type: Types.Relationship,
        ref: 'User',
        index: true,
        many: false
    },
    assignedTo: {
        type: Types.Relationship,
        ref: 'User',
        index: true,
        many: false
    },
    createdAt: { type: Types.Datetime, default: Date.now },
    updatedAt: { type: Types.Datetime, default: Date.now }
});

//default sort
Ticket.defaultSort = '-createdAt';

//custom table columns for our custom list in admin ui
Ticket.defaultColumns = 'title|20%, status|15%, createdBy, assignedTo, createdAt3 ';

Ticket.register();
  • ⭐ keystone takes the name you passed into keystone.List Var Ticket = new keystone.List('Ticket', {... and uses that to construct the mongodb collection name. In this example the collection name would be tickets it will be important to keep track of this collection name because you'll be using it later.

Update they admin dashboard UI with a custom list list name

  • the keystone.js file located at the root of the project directory contains config info. for the application
  • to change the name of the custom list edit the keystone.set method at the bottom of the file, it looks like this.
  • add a property to the object the key being the label and the value being the collection name.
keystone.set('nav', {
 	'users': 'users',
  'manageTickets': 'tickets'
 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment