dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 377 forks source link

Creating schema at application start #539

Open shtpavel opened 10 years ago

shtpavel commented 10 years ago

Hello, I've got some question. I want ot sync database schema on app start. I've got app.js file like so:

------------------------#####DB-MIDDLEWARE####--------------------
app.use(orm.express("mysql://user:password@localhost/database", {
    define: function(db, models, next) {
        db.load('./models/index', function(err) {
            if (err)
                throw err;

            console.log('Model loaded successfuly.');
            db.sync();
        });
        next();
    }
}));

and models index file like so:

var checkError = function(cb, err) {
    if (err)
        return cb(err);
    return cb();
};

module.exports = function(db, cb) {
    db.load('./one.js',function(err)  { checkError(cb, err); });
    db.load('./two.js', function(err)  { checkError(cb, err); });
    db.load('./three.js', function(err)  { checkError(cb, err); });
    db.load('./four.js', function(err)  { checkError(cb, err); });
    db.load('./five.js', function(err)  { checkError(cb, err); });

    var One= db.models.one;
    var Two= db.models.two;
    var Three= db.models.three;
    var Four= db.models.four;
    var Five= db.models.five;

    console.log('Defining references')
    One.hasOne('two', Two, {field: 'twoId', reverse:'ones'});

    return cb();
}

I expect that it will sync all model at once, but actual result is that it loads _only one_ model per application run and I can't see any ForeigKeys in my db-schema (in some mysql-client). What I'm doing wrong? Thanks!