19

I'd like to replace waterline with mongoose in my sails.js application. I'm looking for the correct way to do this, but I don't see how in the documentation. Can anyone explain how to do this?

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Vadorequest
  • 16,593
  • 24
  • 118
  • 215

1 Answers1

27

Defining overrides via .sailsrc

You could do this via config overrides, to be defined via .sailsrc in your project root. Basically you have to prevent the entire Waterline initialization, currently tagged as orm hook. In .sailsrc:

{
  "hooks": {
    "orm": false,
    "pubsub": false
  }
}

You'll have to disable the pubsub hook as well - it depends on the orm hook. Relevant lines in the source: v0.10, v0.9.8.

This will switch off the orm hook for the following start commands:

  • sails lift
  • sails console
  • node app.js (since commit 862c053a66), see "Making app.js use .sailsrc" for older versions

Concerning the stability of this in future versions of Sails you should be aware of the fact that the hook system currently is tagged as unstable and disabling hooks is advised against:

// Allow disabling of hooks by setting them to "false"
// Mostly useful for testing, and may cause instability in production!

Additional information can be found here:

Making app.js use .sailsrc

Note: This is baked into Sails by default since the discussed PR was merged for bleeding edge git checkouts.

For Sails 0.10.x

To make .sailsrc apply to app.js you could replace line 37 in app.js with this:

// app.js, following line 36
var fs = require('fs');
var sailsRc = __dirname + '/.sailsrc';
var config = {};

fs.exists(sailsRc, function(exists){
   if (!exists) return sails.lift();

   fs.readFile(sailsRc, 'utf8', function(err, data){
     if (err) {
       console.warn('Error while reading .sailsrc:' + err);
     }

     try {
       config = JSON.parse(data);
     } catch(e) {
       console.warn('Error while parsing .sailsrc:' + err);
     }

     sails.lift(config);
   });
});

For Sails 0.9.x

Replace app.js with this:

// Start sails and pass it command line arguments
var fs = require('fs'),
    optimist = require('optimist'),
    sails = require('sails');

var sailsRc = __dirname + '/.sailsrc';
var config = optimist.argv;

fs.exists(sailsRc, function(exists){
  if (!exists) return sails.lift(config);

  fs.readFile(sailsRc, 'utf8', function(err, data){
    if (err) {
      console.warn('Error while reading .sailsrc:' + err);
    }

    try {
      config = sails.util.merge(config, JSON.parse(data));
    } catch(e) {
      console.warn('Error while parsing .sailsrc:' + err);
    }

    sails.lift(config);
 });
});
marionebl
  • 3,342
  • 20
  • 34
  • I use the version 0.9.8 and my app.js is almsot empty excpt `require('sails').lift(require('optimist').argv);` but I found a way to start sails lift instead of app.js https://docs.google.com/file/d/0ByzbHcAxmCyvT0VITW0wSHlnMG8 – Vadorequest Feb 06 '14 at 19:34
  • 1
    Ah, I should have mentioned this applies to the v0.10 and the current master branch. Updated my answer accordingly and looking into the options for this on 0.9.x. – marionebl Feb 06 '14 at 19:38
  • By the way, I couldn't make it work with nodemon (unable to run sails lift from nodemon, reference issue). ANd there should not be any comment into the `.sailsrc` file. – Vadorequest Feb 06 '14 at 19:44
  • I use comments in the first line in code blocks to jot down the path this file should reside in. Anyway, I removed the comment for the .sailsrc block. Concerning your nodemon issue I'd say this is a topic for a seperate question. – marionebl Feb 07 '14 at 10:16
  • @Vadorequest Any luck with the code block under "For Sails 0.9.x"? – marionebl Feb 11 '14 at 10:16
  • Works. If you could add a link about `pubsub` doc or something, would be great. Thanks. – Vadorequest Feb 11 '14 at 15:53
  • 2
    Added links to the relevant lines in the sails source. – marionebl Feb 11 '14 at 16:09
  • 2
    fantastic overview- more info on disabling hooks here https://github.com/balderdashy/sails/issues/1077#issuecomment-28074836 – mikermcneil Mar 02 '14 at 03:28
  • 1
    Thanks @mikermcneil. I updated the answer to reflect the the landed PR and added the link to Sails issue 1077. – marionebl Mar 02 '14 at 10:12