question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

db.load vs orm.express

See original GitHub issue

I have a single models.js module that I’d like to load in both Express request handlers and non-Express code. It would be nice if the function definitions for these two cases could be similar so that the models.exports function could be written once well.

Express expects a function with db and models as an argument:

define: function (db, models) {
    models.person = db.define("person", { ... });
}

On the other hand, db.load wants to pass just a callback:

db.load("./models", function (err) {
    // loaded!
    var Person = db.models.person;
    var Pet    = db.models.pet;
});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ptnplanetcommented, Aug 24, 2013

I just want to share my solution:

File stack.js

require defineModels = require('./model').define;
app.use(orm.express('sqlite://sqlite3.s3dbt', { define: defineModels }));

File model.js

var async = require('async'),
    definedModels;
module.exports.define = function (db, models, next) {
    definedModels = models;
    var curriedLoad = function (file) { return function (cb) { db.load(file, cb); }; };

    async.waterfall([
            curriedLoad('.model/user'),
            curriedLoad('.model/posting'),
            // ...
    ], function (err) {
        db.models.user.hasMany(db.models.posting);
        db.sync(next);
    });
};

// I use passport.js and need models without being able to access them through the request object.
module.exports.model = function (name) {
    return definedModels[name];
}
0reactions
dresendecommented, Aug 26, 2013

This will pass to the readme or the wiki as soon as we release a new version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Learn all about Different ORM in Node JS || 2021 - Medium
ORMs provide the concept of Database Abstraction which makes switching databases easier and creates a consistent code base for your application.
Read more >
Node.js ORMs: Why you shouldn't use them - LogRocket Blog
ORM is a powerful tool, but it adds a layer of complexity that can cause some hiccups. Here's why you may want to...
Read more >
Understanding Node.js Sequelize ORM Models - Section.io
This article will help you understand Sequelize models, from model definition to model usage. Sequelize works with all the SQL-based databases. ...
Read more >
The Ultimate Guide To Get Started With Sequelize ORM
Sequelize is a popular ORM for Node.js which allows managing the SQL databases easily. It supports various databases like — Postgres, MySQL, ...
Read more >
Top 11 Node.js ORMs, query builders & database libraries in ...
Choosing an ORM or query builder for your Node.js app can be daunting. ... Eager and Lazy loading of relations; Synchronizing database based ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found