Model Guides

598 views
Skip to first unread message

Jonathan M. Hethey

unread,
Dec 2, 2012, 10:28:39 AM12/2/12
to locomo...@googlegroups.com
I understand that locomotive.js does not force you to use any kind of database, but I would really like some examples for integration of various database systems, especially because I am fairly new to all kinds of node.js stuff and I'm just being a little nervous, that I write code that's considered bad practice or bad in this context, etc, etc.

Maybe I'm also looking in the wrong place, I mean there's a lot of ways to do things and I can probably write all this based on express or just vanilla node, so if you think I should look into [module] to do this, just tell me.

It could be very cool and beginner friendly to create some sample models and ship them in some kind of sample repository. I think it would get people started with using this cool framework a lot more than just saying: Yeah, we'll not decide this for you. You don't have to, I bet you can get people to hack sample models and connections to databases together in a blaze.

I imagine something like this:

a clonable repository with files like:

folder/
-> package name of database npm module

models/
-> mysql-node
-> mongoose
-> redis
-> couch
-> mssql (just kidding)

Grant Friedline

unread,
Dec 26, 2012, 4:04:45 PM12/26/12
to locomo...@googlegroups.com
I was looking for the same thing Jonathan.  I too am new to node.js and actually new to MVC so I was looking for models examples to make sure I'm doing it right.  I'm an old school PHP / MySQL functional coder but locomotive on node.js is helping me learn both node and MVC at the same time.

Grant

Robert Klep

unread,
Dec 27, 2012, 3:37:22 AM12/27/12
to locomo...@googlegroups.com
Locomotive is very database/ORM-agnostic, so within the Locomotive-scope I would say there are no bad practices per se.

Given that, I myself prefer to use an ORM-framework with which you can define proper models, and associations between them. At the moment, I'm using sequelize since one of the requirements for the project was MySQL.

This Locomotive boilerplate project was very helpful for me, even though it uses a different ORM (mongoose). With regard to Locomotive, the setup is pretty much similar with sequelize and all the fine details are usually not Locomotive-related, but ORM-related (and so not entirely on-topic here).

– robert

Robert Klep

unread,
Dec 27, 2012, 4:06:09 PM12/27/12
to locomo...@googlegroups.com
Okay, so I found some spare time and whipped up a Locomotive + Sequelize boilerplate project :-)

theoverl...@gmail.com

unread,
Nov 2, 2013, 9:33:48 PM11/2/13
to locomo...@googlegroups.com
Hi Robert.

I'm new to Locomotive (and, for that matter Node). I'm trying to set up a Locomotive project using MySQL.
Your Locomotive + Sequelize boilerplate project has been really useful, but I wondered if you could post any code that you used to get it working with MySQL? Strangely, I can't find any examples anywhere on the web...

Many thanks,

Jim

Robert Klep

unread,
Nov 3, 2013, 2:27:47 AM11/3/13
to locomo...@googlegroups.com
Hey Jim,

Since Sequelize also has MySQL support, it's just a matter of changing the Sequelize setup in config/initializers/10_sequelize.js so it will connect to a MySQL server instead.

If you don't want to use Sequelize, but use MySQL directly, you can create a new initializer that connects to MySQL and, for example, attaches the client object to the Locomotive app, so you can use it from your controllers.

// config/initializers/10_mysql.js
var mysql = require('mysql');

module.exports = function(done) {
  var app         = this;
  var connection  = mysql.createConnection(...);

  connection.connect(function(err) {
    // propagate error back to Locomotive
    if (err) return done(err);
    // store reference to MySQL client in app instance
    app.mysql = connection;
  });
};

// your controller
MyController.action = function() {
  var mysql = this.app.mysql;
  var self  = this;

  mysql.query('SELECT 1', function(err, results) {
    if (err) throw Error(err);
    self.results = results;
    self.render();
  });
};

The (untested) code above uses this MySQL driver.

– robert

Op zondag 3 november 2013 02:33:48 UTC+1 schreef theoverl...@gmail.com:

theoverl...@gmail.com

unread,
Nov 3, 2013, 5:51:20 AM11/3/13
to locomo...@googlegroups.com
Brilliant. Thanks Robert. Simple stuff, I know, but I'm just getting started...
Reply all
Reply to author
Forward
0 new messages