dresende / node-orm2

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

Make a request using "app" and not "req" with express #524

Open nicolasroger17 opened 10 years ago

nicolasroger17 commented 10 years ago

I am trying to make a timer server that perform a request with the orm.

I defined my models using express :

app = express();

app.use(orm.express(conf, {
   define: function (db, models) {
    // les utilisateurs
      models.user = db.define("user", {
         lastName     : String,
         firstName    : String,
         emailAddress : String,
         address         : String,
         city         : String,
         zipCode      : Number,
         phone        : String,
         password     : String
      },
      [...]

I can use the models using the req object :

app.get("/", function (req, res) {
    // req.models is a reference to models used above in define()
    req.models.person.find(...);
});

,but I don't know how to make a request when it's not a request from the client, so without the variable "req".

I have access to app so I would like to do something like :

app.models.person.find();

but it doesn't work.

If someone knows the relation between "app" and the models I defined, this would be very helpful.

Thank you.

dxg commented 10 years ago

I approach this problem without using the built in orm.express middleware. I have a general 'connection' file I can require from anywhere. See here. It is used without req here and mixed into req here.

jwhitehorn commented 10 years ago

I have also used the pattern @dxg is recommending, and so far it's worked great!