Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

javascript - Is there a way to disable waterline and use a different ORM in sails.js?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
 });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...