balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.82k stars 1.95k forks source link

Requiring a model #2985

Closed bwestergard closed 9 years ago

bwestergard commented 9 years ago

Let's say I have a module, x.js, whose functionality I'd like to define in terms of a sails model (created with sails generate api cats). How do I include the fully hydrated Cats model object (e.g. Cats.findOne()) in x.js?

bwestergard commented 9 years ago

I suppose one workaround is, when calling x.js from (e.g.) a controller, to pass the model in to a function that x.js exports. This obviously isn't ideal. I guess what I'm looking for is a function I can call like:

var Cats = require('sails').HydrateModel(require('../api/models/Cats.js'));

This function looks close, but doesn't get exported.

nikhiljohn10 commented 9 years ago

With that bash command, you create a Cats.js in models. In sails, Models are global. Create a function inside /api/models/Cats.js

module.exports = {

  attributes: {
     // Model attributes here
  },

  feedCat : function(catID, cb ){
      Cats.findOne({ id : catID }).exec(cb);
  }
};

here is your /api/controllers/CatsController.js

var x = require('x');
module.exports = {
  index : function(req, res){
    var catID = x.getID('kitty');
    Cats.feedCat(catID, function(err, cat){
      if(err) console.log("Bad cat.");
      else console("Feeding cat ",cat.name);
    });
  }
};
bwestergard commented 9 years ago

Thanks for the prompt reply. Let me rephrase.

Let's say the module is /lib/x.js, that is, a plain old nodejs module that isn't loaded through the sails process (so the fact that Cats is "global" in that sails-specific sense doesn't help here). How do I include Cats?

I'd like to do this because all of the static code analysis tools (linting, dependency charting, etc) and testing libraries (e.g. proxyquire) are built around the assumption that code is factored into commonjs modules that require other commonjs modules.

nikhiljohn10 commented 9 years ago

try npm link /lib/x.js then it will install the module locally in sails. Then u can use require as normal.

Ref: http://stackoverflow.com/questions/8088795/installing-a-local-module-using-npm

bwestergard commented 9 years ago

I want to require the model in x.js.

bwestergard commented 9 years ago

@tjwebb To phrase it another way: am I to understand that sails users are encouraged to structure their code by relying on "the sails process" exclusively?

bwestergard commented 9 years ago

After reading through some of the core code, I've decided what I really want to do is convert the sailsjs app I've inherited into one that just uses waterline. Sails is just a bit too monolithic for me.

nikhiljohn10 commented 9 years ago

I was able to call Cats.feed() inside x.js. But x.js must be inside the project folder. var x = require('./../../lib/x.js'); So u can use the above code to access the lib.

All you need to do is keep the x.js somewhere inside project folder.

kevinburkeshyp commented 9 years ago

Not actually a crazy requirement... we run into this a ton when trying to run tests. Sometimes you don't want/need to load the entire world. I think what @bwestergard is getting at is, what is the function that changes a model definition (object with list of keys, values) to add on the helpers (findOne, find, update, etc).