Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails js using models outside web server

I want to create a cli to create admin users I have the user model setup in api/models/User.js

and in cli on

var User, program;

program = require("commander");

User = require("../api/models/User");

program.version("0.0.1");

program.command("create_user").description("Create a user into database").action(function() {
  return console.log(User);
});

program.parse(process.argv);

User log is:

    User = {
      attributes: {
        username: "string",
        password: "string",
      }
    };

and no waterline methods available to use.

like image 527
carlitux Avatar asked May 24 '13 16:05

carlitux


People also ask

Is sails JS popular?

Sails is the most popular MVC framework for Node. js, designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture.

What is waterline in sails JS?

Waterline: SQL/noSQL Data Mapper (ORM/ODM) Sails comes installed with a powerful ORM/ODM called Waterline, a datastore-agnostic tool that dramatically simplifies interaction with one or more databases.

How do I run the sails app?

Create your app Empty · An empty Sails app, yours to configure (type "?" for help, or <CTRL+> to cancel) ? Type 1 (or press enter) to start with our "Web App" template: an opinionated starter project that includes essential features like login, password recovery, emails, and billing.


2 Answers

You can use sails run <command>.

Simply create <appPath>/commands/index.js with this content:

module.exports = {
    testcommand: function (done) {
        // .. your code
        console.log('this is my testcommand');
        done();
    }
}

now you can run sails run testcommand

like image 78
Edy Avatar answered Sep 22 '22 13:09

Edy


If any one needs this:

sails = require("sails");
sails.lift({
  log: {
    level: 'silent'
  }
}, function() {
  User.create(obj);
  process.stdin.destroy();
});
like image 41
carlitux Avatar answered Sep 20 '22 13:09

carlitux