redtree
redtree

Reputation: 295

How to pass a variable into a Kraken.js model?

I have a variable that holds my Sequelize connection, like so:

var sequelize = new Sequelize('database', 'username');

With Kraken.js, how do I pass this into a module? I don't see where I could add this in the index.js configuration file...

Thanks!

(I can't tag this as krakenjs because I don't have enough karma.)

Upvotes: 2

Views: 1281

Answers (2)

Struggler
Struggler

Reputation: 682

You can add the connection in separate file and export it (http://openmymind.net/2012/2/3/Node-Require-and-Exports/). I store such files in a dir called lib/

Then in the index.js (the starting point of the app) import the file using

require("./lib/.."). 

And then invoke it in index.js as

app.configure = function configure(nconf, next) {...};

(http://krakenjs.com/#structure_of_a_project)

A good sample app is https://github.com/lmarkus/Kraken_Example_Shopping_Cart

Upvotes: 1

Lenny Markus
Lenny Markus

Reputation: 3418

It's a pretty similar setup to what's shown in this guide:

http://sequelizejs.com/articles/express

You would create the model file, (under ./models). You'd need to register this model using the associate function (See previous link).

And to actually use the model, you could replicate the same usage as in the Kraken Shopping cart example https://github.com/lmarkus/Kraken_Example_Shopping_Cart/

eg: (After you've registered your models)

'use strict';
var Product = require('../models/productModel');

module.exports = function (server) {

 /**
* Display a list of the products.
*/
    server.get('/', function (req, res) {

        Product.find(function (err, prods) {

            res.render('index', products);    
        });   
    });
};

Upvotes: 2

Related Questions