Skip to content

Commit

Permalink
Add database configuration for riak
Browse files Browse the repository at this point in the history
Add controller templates for riak
Allow people to specify primary key to use for riak
  • Loading branch information
taliesins committed Aug 9, 2011
1 parent 7df4867 commit 12828eb
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/datamapper/riak.js
Expand Up @@ -22,7 +22,7 @@ exports.mixPersistMethods = function (Model, description) { // model_name, prope
// TODO: underscorize
var model_name = description.className,
model_name_lowercase = model_name.toLowerCase(),
primary_key = 'key',
primary_key = description.primaryKey || 'id',
table_name = description.tableName,
properties = description.properties,
associations = description.associations,
Expand Down
14 changes: 0 additions & 14 deletions lib/generators.js
Expand Up @@ -171,8 +171,6 @@ exports.list = function () {

if (driver == 'mongoose') {
attrs.push(property + ': { type: ' + type + ' }');
} else if (driver == 'riak'){
attrs.push(property + ': ' + type);
}
else {
attrs.push(' property("' + property + '", ' + type + ');');
Expand All @@ -192,18 +190,6 @@ exports.list = function () {

code = '';
}
else if (driver == 'riak'){
schema = '\n\n/**\n * ' + Model + '\n */\n';
schema += 'var ' + Model + ' = Model.create({\n';
schema += 'schema: { ' + attrs.join(',\n ') + '\n},\n';
schema += 'primaryKey: "' + camelize(Model, true) + 'Id",\n';
schema += 'bucket: "' + camelize(pluralize(Model)) + '"\n';
schema += '});\n\n';
schema += 'module.exports["' + Model + '"] = ' + Model + ';';
appendToFile('db/schema.js', schema);

code = '';
}
else {
code = 'var ' + Model + ' = describe("' + Model + '", function () {\n' +
attrs.join('\n') + '\n});';
Expand Down
25 changes: 25 additions & 0 deletions templates/config/database_riak.json
@@ -0,0 +1,25 @@
{ "development":
{ "driver": "riak"
, "host": "localhost"
, "port": "8098"
, "database": "APPNAME-dev"
}
, "test":
{ "driver": "riak"
, "host": "localhost"
, "port": "8098"
, "database": "APPNAME-test"
}
, "staging":
{ "driver": "riak"
, "host": "localhost"
, "port": "8098"
, "database": "APPNAME-staging"
}
, "production":
{ "driver": "riak"
, "host": "localhost"
, "port": "8098"
, "database": "APPNAME-production"
}
}
57 changes: 57 additions & 0 deletions templates/crud_controller_riak.coffee
@@ -0,0 +1,57 @@
before ->
Model.findById req.params['id'], (error, model) =>
if error
redirect path_to.models
else
@model = model
next()
, only: ['show', 'edit', 'update', 'destroy']

action 'new', ->
@model = new Model
@title = 'New model'
render()

action 'create', ->
Model.create req.body, (error, model) =>
if error
flash 'error', 'Model can not be created'
@model = model
@title = 'New model'
render 'new'
else
flash 'info', 'Model created'
redirect path_to.models

action 'index', ->
Model.allInstances (error, models) =>
@models = models
@title = 'Models index'
render()

action 'show', ->
@title = 'Model show'
render()

action 'edit', ->
@title = 'Model edit'
render()

action 'update', ->
@model.save req.body, (error, model) =>
if error
flash 'error', 'Model can not be updated'
@title = 'Edit model details'
render 'edit'
else
flash 'info', 'Model updated'
redirect path_to.model(@model)

action 'destroy', ->
@model.destroy (error, success) ->
if error
flash 'error', 'Can not destroy model'
else
flash 'info', 'Model successfully removed'
send "'" + path_to.models + "'"

76 changes: 76 additions & 0 deletions templates/crud_controller_riak.js
@@ -0,0 +1,76 @@
before(loadModel, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
this.title = 'New model';
this.model = new Model;
render();
});

action('create', function () {
Model.create(req.body, function (error, model) {
if (error) {
flash('error', 'Model can not be created');
render('new', {
model: model,
title: 'New model'
});
} else {
flash('info', 'Model created');
redirect(path_to.models);
}
});
});

action('index', function () {
this.title = 'Models index';
Model.allInstances(function (error, models) {
render({
models: models,
});
});
});

action('show', function () {
this.title = 'Model show';
render();
});

action('edit', function () {
this.title = 'Model edit';
render();
});

action('update', function () {
this.model.save(req.body, function (error, model) {
if (error) {
flash('error', 'Model can not be updated');
this.title = 'Edit model details';
render('edit');
} else {
flash('info', 'Model updated');
redirect(path_to.model(this.model));
}
}.bind(this));
});

action('destroy', function () {
this.model.destroy(function (error, success) {
if (error) {
flash('error', 'Can not destroy model');
} else {
flash('info', 'Model successfully removed');
}
send("'" + path_to.models + "'");
});
});

function loadModel () {
Model.findById(req.params['id'], function (error, model) {
if (error) {
redirect(path_to.models);
} else {
this.model = model;
next();
}
}.bind(this));
}

0 comments on commit 12828eb

Please sign in to comment.