CMS without DB – Running KeystoneJS without MongoDB

KeystoneJS is a great CMS based on Node.js and MongoDB. Unfortunately you don’t always have a MongoDB available, be it because of hardware restrictions or because you do not want to install it on a local developer machine or Raspberry PI.

For simple use cases that do not have to scale well it thus might be a good idea to have a more lightweight version running on your server (my house automation would be such a use case).

Here is where TingoDB comes in – a embedded JavaScript noSQL database that stores your files as JSON objects. Using the Tungus as a driver this lets us use KeystoneJS as if it had a MongoDB running. Since this database is not going to scale for lots of requests you can simply replace the driver with the normal Mongoose driver for MongoDB later without having to do any changes.

To get started with KeystoneJS, you can install generator-keystone and then run yo to build a skeleton for your app.

npm install generator-keystone -s
yo keystone

Then go into the keystone.js file and add the following in the very beginning.

global.TUNGUS_DB_OPTIONS = { nativeObjectID: true, searchInArray: true };
var tungus = require('tungus');
 var mongoose = require('mongoose');

This will first set the TingoDB to use 12-byte BSON type ObjectIDs instead of integers (which keystone currently has a problem with somewhere in the routes). Requiring Tungus then replaces the standard driver for Mongoose with TingoDB.

All you have to do now is to set the new database connection in KeystoneJS.

keystone.set('mongo', 'tingodb://'+__dirname+'/data');

Your app is now running on a local database (you have to create the data directory inside of your app folder first for this to work properly).

Note: This will certainly work for small application – I have not tested this for production use with a large scale of users. You may want to include a switch to only use this in development.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>