2

I need to run PostgreSQL script right after lifting my application. For example, I need to execute this command: psql -d DOGHOUZ -a -f script.sql from the app. Is there a way how to do that?

dim0_0n
  • 2,404
  • 5
  • 27
  • 46

1 Answers1

6

It depends how you lift your app. If you use:

node app.js

you can add

sails.on('lifted', function yourEventHandler () {
    console.log('lifted')
});

in your app.js file before line sails.lift(rc('sails'));

Otherwise you need to add that into configuration. Best approach would be creating new file in /config like /config/eventhooks.js with content:

module.exports.eventhooks = function(cb) {
    sails.on('lifted', function yourEventHandler () {
        console.log('lifted')
    });
}

You can read more here:

Edit 1

To execute CLI commands just do:

var exec = require('child_process').exec;
var cmd = 'psql -d DOGHOUZ -a -f script.sql';
exec(cmd, function(error, stdout, stderr){
    // command output is in stdout
});

More about executing commands in CLI you can read here

Community
  • 1
  • 1
Bonanza
  • 1,601
  • 14
  • 23