Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
support for rethinkdb. closes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
petecoop committed Feb 28, 2015
1 parent 583ce4d commit c466f60
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ An Expressjs generator for Yeoman, based on the express command line tool.
- MongoDB
- MySQL
- PostgreSQL
- RethinkDB

## Getting started

Expand Down
18 changes: 13 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ module.exports = generators.Base.extend({
'None',
'MongoDB',
'MySQL',
'PostgreSQL'
'PostgreSQL',
'RethinkDB'
],
store: true
}];
Expand Down Expand Up @@ -176,14 +177,21 @@ module.exports = generators.Base.extend({
this.sourceRoot(path.join(__dirname, 'templates', 'css', stylesheets));
this.directory('.', 'public/css');

// grunt/gulp
var buildFile = this.options.buildTool === 'grunt' ? 'Gruntfile.js' : 'gulpfile.js';
this.copy(path.join(__dirname, 'templates', 'extras', name + '-shared', buildFile), buildFile);

// sequelize extra stuff
if (this.options.database === 'mysql' || this.options.database === 'postgresql') {
this.copy(path.join(__dirname, 'templates', 'extras', name + suffix, 'model-index.' + this.filetype), 'app/models/index.' + this.filetype);
this.copy(path.join(__dirname, 'templates', 'extras', name + suffix, 'sequelize-model-index.' + this.filetype), 'app/models/index.' + this.filetype);
}

//thinky extra stuff
if (this.options.database === 'rethinkdb') {
this.copy(path.join(__dirname, 'templates', 'extras', name + suffix, 'thinky-model-index.' + this.filetype), 'app/models/index.' + this.filetype);
this.copy(path.join(__dirname, 'templates', 'extras', name + suffix, 'thinky-config.' + this.filetype), 'config/thinky.' + this.filetype);
}

// grunt/gulp
var buildFile = this.options.buildTool === 'grunt' ? 'Gruntfile.js' : 'gulpfile.js';
this.copy(path.join(__dirname, 'templates', 'extras', name + '-shared', buildFile), buildFile);
},
assetsDirs: function () {
this.mkdir('public');
Expand Down
5 changes: 5 additions & 0 deletions app/templates/extras/mvc-coffee/thinky-config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config = require('./config')

thinky = require('thinky')(config.db)

module.exports = thinky
13 changes: 13 additions & 0 deletions app/templates/extras/mvc-coffee/thinky-model-index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
config = require('../../config/config')
glob = require('glob')
thinky = require('../../config/thinky')

models =
r: thinky.r

files = glob.sync config.root + '/app/models/!(index)*.js'
files.forEach (file) ->
model = require file
models[model.getTableName()] = model;

module.exports = models;
5 changes: 5 additions & 0 deletions app/templates/extras/mvc/thinky-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var config = require('./config');

var thinky = require('thinky')(config.db);

module.exports = thinky;
14 changes: 14 additions & 0 deletions app/templates/extras/mvc/thinky-model-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var config = require('../../config/config'),
glob = require('glob'),
thinky = require('../../config/thinky');

var models = {
r: thinky.r
};
var files = glob.sync(config.root + '/app/models/!(index)*.js');
files.forEach(function (file) {
var model = require(file);
models[model.getTableName()] = model;
});

module.exports = models;
14 changes: 8 additions & 6 deletions app/templates/mvc-coffee/app/controllers/home.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ router = express.Router()<% if(options.viewEngine == 'marko'){ %>
marko = require 'marko'<% } %><% if(options.database == 'mongodb'){ %>
mongoose = require 'mongoose'
Article = mongoose.model 'Article'<% } %><% if(options.database == 'mysql' || options.database == 'postgresql'){ %>
db = require '../models'<% } %>
db = require '../models'<% } %><% if(options.database == 'rethinkdb'){ %>
models = require('../models')
Article = models.Article<% } %>

module.exports = (app) ->
app.use '/', router
<% if(options.viewEngine == 'marko'){ %>
indexTemplate = marko.load require.resolve '../views/index.marko'<% } %>
router.get '/', (req, res, next) ->
<% if(options.database == 'mongodb'){ %>
router.get '/', (req, res, next) -><% if(options.database == 'mongodb'){ %>
Article.find (err, articles) ->
return next(err) if err<% } %><% if(options.database == 'mysql' || options.database == 'postgresql'){ %>
db.Article.findAll().success (articles) -><% } %><% if(options.viewEngine == 'marko'){ %>
db.Article.findAll().success (articles) -><% } %><% if(options.database == 'rethinkdb'){ %>
Article.run().then (articles) -><% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render
title: 'Generator-Express MVC',
$global: locals: req.app.locals
title: 'Generator-Express MVC'
articles: articles
, res<% } else { %>
res.render 'index',
$global: locals: req.app.locals
title: 'Generator-Express MVC'
articles: articles<% } %>
15 changes: 15 additions & 0 deletions app/templates/mvc-coffee/app/models/article.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ module.exports = (sequelize, DataTypes) ->
associate (models) ->
# example on how to add relations
# Article.hasMany models.Comments
<% } %><% if(options.database == 'rethinkdb'%>
thinky = require('../../config/thinky')
r = thinky.r
type = thinky.type

Article = thinky.createModel 'Article',
title: String
url: String
text: String

module.exports = Article

# example on hour to add relations
# Comment = require('./comment')
# Article.hasMany(Comment, 'comments', 'id', 'article_id')
<% } %>
9 changes: 6 additions & 3 deletions app/templates/mvc-coffee/config/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ config =
port: 3000<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-development'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'rethinkdb'){ %>
db: db: '<%= _.slugify(appname) %>_development'<% } %>

test:
root: rootPath
Expand All @@ -19,7 +20,8 @@ config =
port: 3000<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-test'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'rethinkdb'){ %>
db: db: '<%= _.slugify(appname) %>_test'<% } %>

production:
root: rootPath
Expand All @@ -28,6 +30,7 @@ config =
port: 3000<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-production'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'rethinkdb'){ %>
db: db: '<%= _.slugify(appname) %>_production'<% } %>

module.exports = config[env]
8 changes: 4 additions & 4 deletions app/templates/mvc-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"method-override": "~2.3.0",
"glob": "~4.4.1"<% if(options.database == 'mongodb'){ %>,
"mongoose": "~3.8.20"<% } %><% if(options.database == 'mysql'){ %>,
"sequelize": "~2.0.0-rc7",
"sequelize": "~2.0.3",
"mysql": "~2.5.3"<% } %><% if(options.database == 'postgresql'){ %>,
"sequelize": "~2.0.0-rc7",
"lodash": "~2.4.1",
"pg": "~4.3.0"<% } %><% if(options.viewEngine == 'jade'){ %>,
"sequelize": "~2.0.3",
"pg": "~4.3.0"<% } %><% if(options.database == 'rethinkdb'){ %>,
"thinky": "~1.16.6"<% } %><% if(options.viewEngine == 'jade'){ %>,
"jade": "~1.9.1"<% } %><% if(options.viewEngine == 'ejs'){ %>,
"ejs": "~2.3.1"<% } %><% if(options.viewEngine == 'swig'){ %>,
"swig": "~1.4.2"<% } %><% if(options.viewEngine == 'handlebars'){ %>,
Expand Down
12 changes: 7 additions & 5 deletions app/templates/mvc/app/controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ var express = require('express'),
mongoose = require('mongoose'),
Article = mongoose.model('Article');<% } %><% if(options.database == 'mysql' || options.database == 'postgresql'){ %>
db = require('../models');<% } %><% if(options.database == 'none'){ %>
Article = require('../models/article');<% } %>
Article = require('../models/article');<% } %><% if(options.database == 'rethinkdb'){ %>
models = require('../models'),
Article = models.Article;<% } %>
module.exports = function (app) {
app.use('/', router);
};
<% if(options.viewEngine == 'marko'){ %>
var indexTemplate = marko.load(require.resolve('../views/index.marko'));<% } %>
router.get('/', function (req, res, next) {
<% if(options.database == 'mongodb'){ %>
router.get('/', function (req, res, next) {<% if(options.database == 'mongodb'){ %>
Article.find(function (err, articles) {
if (err) return next(err);<% } %><% if(options.database == 'mysql' || options.database == 'postgresql'){ %>
db.Article.findAll().success(function (articles) {<% } %><% if(options.database == 'none'){ %>
db.Article.findAll().success(function (articles) {<% } %><% if(options.database == 'rethinkdb'){ %>
Article.run().then(function (articles) {<% } %><% if(options.database == 'none'){ %>
var articles = [new Article(), new Article()];<% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render({
$global: {locals: req.app.locals},
title: 'Generator-Express MVC',
articles: articles
}, res);<% } else { %>
res.render('index', {
$global: {locals: req.app.locals},
title: 'Generator-Express MVC',
articles: articles
});<% } %><% if(options.database !== 'none'){ %>
Expand Down
16 changes: 16 additions & 0 deletions app/templates/mvc/app/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ function Article (opts) {
}

module.exports = Article;
<% } %><% if(options.database == 'rethinkdb'){%>
var thinky = require('../../config/thinky'),
r = thinky.r,
type = thinky.type;

var Article = thinky.createModel('Article', {
title: String,
url: String,
text: String
});

module.exports = Article;

// example on hour to add relations
// var Comment = require('./comment');
// Article.hasMany(Comment, 'comments', 'id', 'article_id');
<% } %>
9 changes: 6 additions & 3 deletions app/templates/mvc/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var config = {
port: 3000,<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-development'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-development'<% } %><% if(options.database == 'rethinkdb'){ %>
db: {db: '<%= _.slugify(appname) %>_development'}<% } %>
},

test: {
Expand All @@ -22,7 +23,8 @@ var config = {
port: 3000,<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-test'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-test'<% } %><% if(options.database == 'rethinkdb'){ %>
db: {db: '<%= _.slugify(appname) %>_test'}<% } %>
},

production: {
Expand All @@ -33,7 +35,8 @@ var config = {
port: 3000,<% if(options.database == 'mongodb'){ %>
db: 'mongodb://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'mysql'){ %>
db: 'mysql://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'postgresql'){ %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-production'<% } %>
db: 'postgres://localhost/<%= _.slugify(appname) %>-production'<% } %><% if(options.database == 'rethinkdb'){ %>
db: {db: '<%= _.slugify(appname) %>_production'}<% } %>
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-express",
"version": "2.5.9",
"version": "2.5.10",
"description": "A nodejs express generator for Yeoman",
"keywords": [
"yeoman-generator",
Expand Down
15 changes: 15 additions & 0 deletions test/test-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,19 @@ describe('Express generator', function () {
runGenerationTest.call(this, expected, 'mvc', 'jade', 'less', false, 'mysql', 'grunt', done);
});
});

describe('MVC generator with RethinkDB', function () {
var expected = [
'app/models/index.js',
'config/thinky.js'
];

it('creates expected files', function (done) {
runGenerationTest.call(this, expected, 'mvc', 'jade', 'none', false, 'rethinkdb', 'grunt', done);
});

it('works with coffee', function (done) {
runGenerationTest.call(this, expected, 'mvc', 'jade', 'none', true, 'rethinkdb', 'grunt', done);
});
});
});

0 comments on commit c466f60

Please sign in to comment.