Using Waterline model outside SailsJS api
Asked Answered
G

1

13

Is it possible to use models defined within [app-name]/api/models outside api folder?

I have created separate folder in application root, where I placed cron job that should fill my database every hour. I would like to reuse Models defined inside my api folder, but not sure how to do it.

I see that I can do sails.lift inside that cron script but that doesn't seem very nice to me.

Did someone experience something similar? I'm new to node.js, so maybe I'm missing something pretty obvious.

Giorgi answered 9/6, 2014 at 15:3 Comment(0)
W
17

If your concern with using sails.lift is that it starts an actual HTTP server that listens for requests, you can use sails.load instead. It will do everything that lift does--including loading hooks and models--except for starting the server.

var Sails = require('sails');
Sails.load(function(err, sails) {
   // At this point you have access to all your models, services, etc.
});
Whippletree answered 9/6, 2014 at 16:12 Comment(5)
Looks like this is what I was looking for! I'll give it a try and let you know!Giorgi
Yes, that's exactly the thing I was looking for! Thanks a lot!Giorgi
I was needing this for working with my models from a node.js script, thank youLangobard
Use process.exit() to shut down the script when complete. Unless there is a sails method.Collings
@gotmikhail yep, works for me, just wrote a sails script for Heroku scheduler using the snippet aboveForwardlooking

© 2022 - 2024 — McMap. All rights reserved.