Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb support #85

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
language: node_js
node_js:
- 0.10
25 changes: 25 additions & 0 deletions examples/mongoSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var
express = require('express'),
app = express();

var poet = require('../lib/poet')(app, {
storageType: 'database',
mongoUri: 'mongodb://poet-demo:[email protected]:10072/poet_mongo_demo',
dbRefreshInterval: 60000,
});

poet.watch(function () {
// watcher reloaded
}).init().then(function () {
// Ready to go!
});

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(app.router);

app.get('/', function (req, res) { res.render('index'); });

app.listen(3000);

13 changes: 12 additions & 1 deletion lib/poet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var
routes = require('./poet/routes'),
methods = require('./poet/methods'),
utils = require('./poet/utils'),
method = utils.method;
method = utils.method,
mongoose = require('mongoose');

function Poet (app, options) {
this.app = app;
Expand All @@ -30,6 +31,16 @@ function Poet (app, options) {

// Bind routes to express app based off of options
routes.bindRoutes(this);

// Open Database Connection if posts source is database
if (this.options.storageType == 'database') {
mongoose.connect(options.mongoUri);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('database connection is established');
});
};
}

module.exports = function (app, options) {
Expand Down
3 changes: 3 additions & 0 deletions lib/poet/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function createDefaults () {
metaFormat: 'json',
readMoreLink: readMoreLink,
readMoreTag: '<!--more-->',
storageType: 'filesystem',
mongoUri: '',
dbRefreshInterval: 60000, // 60 seconds
routes: {
'/post/:post': 'post',
'/page/:page': 'page',
Expand Down
99 changes: 88 additions & 11 deletions lib/poet/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ var
fs = require('fs'),
_ = require('underscore'),
path = require('path'),
when = require('when'),
defer = require('when').defer,
all = require('when').all,
nodefn = require('when/node/function'),
fsThen = require('fs-then'),
utils = require('./utils');
utils = require('./utils'),
mongoose = require('mongoose'),
postModel;

/**
* Adds `data.fn` as a template formatter for all files with
Expand Down Expand Up @@ -44,8 +47,51 @@ exports.addTemplate = addTemplate;
function init (poet, callback) {
var options = poet.options;

clearPosts(poet);

var promise;
if (options.storageType == 'database')
promise = loadFromDatabase(poet);
else
promise = loadFromFileSystem(poet);

if (callback)
promise.then(callback.bind(null, null), callback.bind(null));

return promise;
}
exports.init = init;

/**
* Load posts from database
* @param poet
* @returns {Promise}
*/
function loadFromDatabase (poet) {
return when(getPostModel().find({}, function (err, posts) {
posts.forEach(function(post, index) {
var convertedPost = utils.convertPost(post, poet.options);
poet.posts[convertedPost.slug] = convertedPost;
});
})).then(function() {
// Clear out the cached sorted posts, tags, categories, as this point they
// could have changed from new posts
clearCache(poet);
return poet;
});
}
exports.loadFromDatabase = loadFromDatabase;

/**
* Load posts from filesystem
* @param poet
* @returns {Promise}
*/
function loadFromFileSystem (poet) {
var options = poet.options;

// Get list of files in `options.posts` directory
var promise = utils.getPostPaths(options.posts).then(function (files) {
return utils.getPostPaths(options.posts).then(function (files) {
// Generate a collection of promises that resolve
// to post objects
var collection = files.reduce(function (coll, file) {
Expand Down Expand Up @@ -91,13 +137,8 @@ function init (poet, callback) {
clearCache(poet);
return poet;
});

if (callback)
promise.then(callback.bind(null, null), callback.bind(null));

return promise;
}
exports.init = init;
exports.loadFromFileSystem = loadFromFileSystem;

/**
* Clears the `poet` instance's 'cache' object -- useful when modifying
Expand All @@ -112,6 +153,17 @@ function clearCache (poet) {
}
exports.clearCache = clearCache;

/**
* Clears posts during init
* @param {Object} poet
* @returns {Poet}
*/
function clearPosts (poet) {
poet.posts = {};
return poet;
}
exports.clearPosts = clearPosts;

/**
* Sets up the `poet` instance to watch the posts directory for any changes
* and calls the callback whenever a change is made
Expand All @@ -122,9 +174,34 @@ exports.clearCache = clearCache;
*/

function watch (poet, callback) {
fs.watch(poet.options.posts, function (event, filename) {
poet.init().then(callback);
});
if (poet.options.storageType == 'database') {
setInterval(function() {
poet.init().then(function () {
})
}, poet.options.dbRefreshInterval);
}
else {
fs.watch(poet.options.posts, function (event, filename) {
poet.init().then(callback);
});
}
return poet;
}
exports.watch = watch;

/**
* Define schema for post object for mongodb
* @returns {Schema}
*/
function getPostModel() {
if (postModel == null) {
postModel = mongoose.model('Post', new mongoose.Schema({
date: Date,
title: String,
content: String,
tags: Array,
categories : Array
}));
}
return postModel;
}
16 changes: 16 additions & 0 deletions lib/poet/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ function createPost (filePath, options) {
}
exports.createPost = createPost;

/**
* Convert database post records to displayable version
* Generate url, slug, preview etc.
* @param post
* @param options
* @returns post
*/
function convertPost (post, options) {
post.date = new Date(post.date);
post.slug = convertStringToSlug(post.slug || post.title);
post.url = createURL(getRoute(options.routes, 'post'), post.slug);
post.preview = getPreview(post, post.content, options);
return post;
}
exports.convertPost = convertPost;

/**
* Takes an array `posts` of post objects and returns a
* new, sorted version based off of date
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"underscore": "1.4.x",
"front-matter": ">= 0.1.x",
"when": "2.1.x",
"fs-then": "0.1.x"
"fs-then": "0.1.x",
"mongoose": "1.3.x"
},
"devDependencies": {
"mocha": ">= 1.3.2",
Expand Down