Skip to content

links

corneliu edited this page Jul 23, 2013 · 5 revisions

Links or kernel links are methods attached to this.kernel linking adaptors or custom methods. They are configurable through the main config file and are placed depending on their role in a run level.

Take a quick look at config.js file before continuing.

boot

As the name suggest, this run level is used to prepare objects before the application comes online. Adaptors should initialize and test their connections, if you have application logic that requires prepopulated objects, do that here and pretty much anything you need before starting to receive requests.

run

If we got here, boot was successful. Daemons should start and the application should go online.

shutdown

A shutdown will be initiated if a fatal error is caught, the sources change and the application is started with --watch-changes, a kill signal was received or the kernel.shutdown method invoked from somewhere in your application logic.

During this runlevel, all links will have their .unload methods called.

define a link in config.js

A link can be defined as :

path to a custom module

[...]
,boot : {
    /**
     * Dummy link, check appdir/links/dummy.js
     * for more info
     */
    dum : 'dummy'
[...]

one of the builtin links

/**
 * Mysql configuration with master / slave
 */
 db : [
    'mysql'
    , { host : 'localhost', user : 'root', pass : '123', port : '3306' } //master
    , { host : 'localhost', user : 'root', pass : '123', port : '3306' } //slave
    , 'testdb' //defaultdb
    ,  { slow : 5 } //log queries that take longer
 ]

use the link

// lets use the db link created above
// we're in an example model (included via this.instance.model from a controller)
function SomeModel() {
    this.kernel.db('testdb', function(err, conn) {
        if (!err) {
            conn.query(
                'SELECT something FROM somewhere',
                [],
                function(err, rows) {
                   if (!err) {
                        console.log(rows);
                    }
                }
            )
        }
    });
 }
Something went wrong with that request. Please try again.