Search code examples
keystonejs

How to add new fields to a list in KeystoneJS


I want to extend the Blog in KeystoneJS by adding a boolean "frontPage" field to the Post schema, which I want to use to show selected posts on the homepage.

I came up with this code that I put in the updates folder:

var keystone = require('keystone');
var async = require('async');

exports = module.exports = function (done) {
    let post = keystone.list('Post');

    post.add({
        frontPage: Boolean
    });
    done();
};

it seems to work, but the change does not persist when I restart the server. All docs describe the process of creating new Lists, but none tells how to modify an existing one. Also tried to add a post.register()at the end but no luck.

Is there a function to persist the new schema, or I should write a shell script outside Keystone for that?

Thank you


Solution

  • It seems to work, but the change does not persist when I restart the server.

    Scripts in the application updates folder are intended for data import or migration, and intentionally are only applied once to a given deployment.

    All docs describe the process of creating new Lists, but none tells how to modify an existing one.

    To add, remove, or change fields in your model you should modify the file in your Keystone project (eg: models/Post.js) and then restart your application to pick up the changes.

    There generally is no need to create a corresponding update script unless you wanted to include associated data changes (for example, setting values for the existing documents).