Monday, 22 December 2014

how to use keystonejs build an admin site for your application

If you have a website or application which is using mongodb. Usually you need a admin site to manage data, including insert init data and update data or delete some bad data. A simple CRUD (create read update delete) application is which you need. However, to make it your self from the scratch is tough.

keystonejs is designed for you. It is a nodejs+mongodb application. it is a blogger system with admin page. It also offer security and authorisation.  We can customise the admin page to implement what we want.


Two key files you should look into:

1. keystone.js at the root folder of your project.
2. models/User.js

1, keystone.js is the entrance of the project, it is also the main configuration place of this project.
2, User.js is an example of how to manage a database table in this project. If you want to manage your table, you should copy and customise this file.


I will spend time on explaining the keystone.js first:

the main settings of this project:


'name': 'newxxx1',// this is database name
'brand': 'ddddxxxx', //appname which displays on the webpage

'less': 'public',

More details can be found here: (it is very simple and straight forward)


Very important: this application can automatically create the table for you in mongodb if the table is not exist.

How to manage your table:

1, you need copy an existing model js file from /models folder.

2, define the table:

var PostComment = new keystone.List('PostCommentXxx -- this the table name in camel case format', {
label: 'Commentsaaa',
singular: 'Commentbbbb' // this is the name of this object on the webpage, just for display.

PostCommentXxx this is the table name in camel case, it will automaticlly create a table in mongoDB and the table name is postcommentxxxs.  (!!!! it automaticall add s at end of the name)
it will also generate the nav link name which is : "post-comment-xxx"

So if your table's name is not end with "s" you will have problem. then you should do:

var PeterTestxx = new keystone.List('Day', {
label: 'day Table',
path: 'petertesttablepash',
singular: 'Peter Test Table',
schema :  { collection: 'mytable' }   // this is your table's name. it follow's mongnose's syntax. however keystonejs's document note it as a string instade of object.  

3, define columns:

so you should able to see this in keystone.js file:

keystone.set('nav', {
'posts': ['posts', 'post-categories'],
'galleries': 'galleries',
'enquiries': 'enquiries',
'users': 'users',
'ddd': 'users',
'field-tests': 'things',
'xxxx-tests': 'post-comment-xxxs'  // xxxx-tests is the name display on admin's navigation.


No comments:

Post a comment