Search code examples
sails.js

Load Sails programmatically and work with models


I see that I can access models at sails.models after loading sails programmatically, but I can't figure out how to find and update models. I want to do something like this, is it possible?

const Sails = require('sails').constructor;
const sails = new Sails();
await sails.load();
const user = await sails.models.user.findOne({id:1});

I get an error that findOne does not exist. No methods exists on the models from sails.models, only the customToJSON.


Solution

  • My syntax was wrong, this is the right way:

    const Sails = require('sails').constructor;
    const sails = new Sails();
    
    sails.load({}, async () => {
      //console.log(sails.models.user);
      const u = await sails.models.user.findOne({id: 7});
      console.log(u);
    
      sails.lower( (err) => {
          if (err) {
            return console.log("Error occurred lowering Sails app: ", err);
          }
          console.log("Sails app lowered successfully!");
        }
      );
    });