Integrate keystone module to existing express and mongoose application.

898 views
Skip to first unread message

naray...@gmail.com

unread,
Jan 24, 2014, 6:08:14 AM1/24/14
to keyst...@googlegroups.com
hi, i created node js application with express and mongoose and it's running fine, now i want to use keystone in my application to build admin ui based on mongoose model. How can i integrate keystone to build admin model based on existing mongoose model. is it possible? any help is appreciated. thanks, P. Chinna Satyanarayana.

j...@keystonejs.com

unread,
Jan 24, 2014, 9:47:51 AM1/24/14
to keyst...@googlegroups.com
Hi Chinna,

It should be fairly straightforward.

The main changes you'll have to make are:

1. Either bind the KeystoneJS middleware into your express configuration, or replace it with Keystone.start()

Depending on how custom your express config is, it's probably simplest to replace your express config with Keystone.start(). Check out the getting started guide for how to set up Keystone options and init the server. All the route middleware is exactly the same (since Keystone wraps express).

Integrating it into your existing middleware stack is more complicated, let me know if you need to do this and I'll try and give you more pointers.

For a complete picture of how keystone.start() works, or what you'd need to emulate if integrating it piecemeal, check out the start method in keystone/index.js

2. Change your Mongoose Schema definitions into Keystone List 

This is really straight-forward. It's just a syntax change really; instead of

var MySchema = new mongoose.Schema({ path: String });
mongoose.model('MyModel', MySchema);

... etc, do it like this:

var MyList = new Keystone.List({ /* options */ });
MyList.add({ path: String });
MyList.register();

The only difference in syntax is that Keystone Lists take an options object when initialising, while mongoose Schemas accept an object of paths to add to the Schema.

Simply use the add method on a List to add your object of paths on the subsequent line.

To access the schema of a list (where you can define virtuals, middleware, etc. - anything mongoose supports) access MyList.schema

And after you have registered a list, access the model like MyList.model

3. Update your queries to look at the Keystone Lists instead of the Mongoose Models

This is optional, but recommended.

Instead of getting the model from mongoose like this:

mongoose.model('MyModel').find() // ...

Access it through the Keystone List:

keystone.list('MyList').model.find() // ...


Hope this helps, let me know how you go and if you need any other pointers.

Cheers,
Jed.

Reply all
Reply to author
Forward
0 new messages