How to use the loopback.remoteMethod function in loopback

To help you get started, we’ve selected a few loopback examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-workspace / common / models / workspace.js View on Github external
options = undefined;
      }

      // clone options so that we don't modify input arguments
      options = extend({}, options);

      options = extend(options, {
        root: true,
        name: name,
        template: templateName,
      });

      Workspace.addComponent(options, cb);
    };

    loopback.remoteMethod(Workspace.createFromTemplate, {
      http: {verb: 'post', path: '/'},
      accepts: [
        {arg: 'templateName', type: 'string'},
        {arg: 'name', type: 'string'},
      ],
    });

    /**
     * @typedef {{name, description,supportedByStrongLoop}} ConnectorMeta
     */

    /**
     * @type {Array.}
     * @internal
     */
    const staticConnectorList = require('../../available-connectors');
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
*/
    Workspace.listAvailableConnectors = function(cb) {
      PackageDefinition.findOne(function(err, pkg) {
        if (err) return cb(err);
        async.map(staticConnectorList, function(connector, cb) {
          isDependency(connector, pkg, function(err, isDep) {
            if (err) return cb(err);

            connector.installed = isDep;
            cb(null, connector);
          });
        }, cb);
      });
    };

    loopback.remoteMethod(Workspace.listAvailableConnectors, {
      http: { verb: 'get', path: '/connectors' },
      returns: { arg: 'connectors', type: 'array', root: true },
    });

    /**
     * Check if the project is a valid directory.
     * The callback is called with no arguments when the project is valid.
     * @param {function(Error=)} cb
     */
    Workspace.isValidDir = function(cb) {
      // Every call of `Model.find()` triggers reload from the filesystem
      // This allows us to catch basic errors in config files
      Facet.find(function(err, list) {
        if (err) {
          cb(err);
        } else if (!list.length) {
github strongloop / loopback-workspace / common / models / data-source-definition.js View on Github external
});

  /**
   * Update existing tables / collections.
   *
   * @param {string} modelName
   * @callback {Function} callback
   * @param {Error} err
   * @param {boolean} success
   */

  DataSourceDefinition.prototype.autoupdate = function(modelName, cb) {
    this.invokeMethodInWorkspace('autoupdate', modelName, cb);
  };

  loopback.remoteMethod(DataSourceDefinition.prototype.autoupdate, {
    accepts: {arg: 'modelName', type: 'string'},
    returns: {arg: 'success', type: 'boolean'},
    http: {verb: 'POST'},
  });

  DataSourceDefinition.prototype.invokeMethodInWorkspace = function(methodName) {
    // TODO(bajtos) We should ensure there is never more than one instance
    // of this code running at any given time.
    let isDone = false;
    const self = this;
    const args = Array.prototype.slice.call(arguments, 0);
    let cb;
    const stdErrs = [];
    const invokePath = require.resolve('../../bin/datasource-invoke');

    // remove method name
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
* @param {String} dest
     * @callback {Function} cb
     */
    Workspace.copyGitignore = function(dest, cb) {
      if (arguments.length === 3) {
        // support the old signature copyGitignore(templateDir, dest, cb)
        dest = arguments[2];
        cb = arguments[3];
      }

      var gitignore = require.resolve('../../templates/gitignore');
      var dotGitignore = path.resolve(dest, '.gitignore');
      Workspace.copyRecursive(gitignore, dotGitignore, cb);
    };

    loopback.remoteMethod(Workspace.addComponent, {
      http: { verb: 'post', path: '/component' },
      accepts: { arg: 'options', type: 'object', http: { source: 'body' }},
    });

    function createFacet(name, template, cb) {
      var steps = [];

      steps.push(function(cb) {
        var facet = template.facet || {};
        facet.name = name;
        Facet.create(facet, cb);
      });

      if (template.config) {
        setFacetName(template.config);
        steps.push(function(next) {
github strongloop / loopback-workspace / common / models / data-source-definition.js View on Github external
if (err.origin === 'invoke') {
        // report `ping` errors as a 200 result with error details, not a 500
        cb(null, false, {
          message: err.message,
          code: err.code,
          details: err.details,
          stack: err.stack,
        });
      } else {
        cb(err);
      }
    });
  };

  loopback.remoteMethod(DataSourceDefinition.prototype.testConnection, {
    returns: [
      {arg: 'status', type: 'boolean'},
      {arg: 'error', type: 'object'},
    ],
  });

  /**
   * Test the datasource connection (static version).
   *
   * @deprecated Use the prototype version.
   *
   * @param {Object} data DataSourceDefinition
   * @callback {Function} callback
   * @param {Error} err A connection or other error
   * @param {Boolean} success `true` if the connection was established
   */
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
function fetchServerHostPort(cb) {
      FacetSetting.find(
        { where: { facetName: 'server' }},
        function extractHostPortFromFacetSettings(err, list) {
          if (err) return cb(err);
          var config = {};
          list.forEach(function(it) {
            config[it.name] = it.value;
          });

          cb(null, config.host, config.port);
        });
    }

    loopback.remoteMethod(Workspace.start, {
      http: { verb: 'post', path: '/start' },
      returns: {
        arg: 'data',
        type: { pid: Number, host: String, port: Number },
        root: true,
      },
    });

    process.once('exit', function killWorkspaceChild() {
      if (Workspace._child)
        Workspace._child.kill();
    });

    /**
     * Stop the project (app) in the workspace started by {@link start}.
     * @param {function(Error=,Object=)} cb callback
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
};
      cb(null, availableLBVersions);
    };

    Workspace.availableLBVersions = function(cb) {
      Workspace.getAvailableLBVersions(function(err, data) {
        const lbVersions = [];
        Object.keys(data).forEach(function(key) {
          const version = data[key];
          lbVersions.push({value: key, description: version.description});
        });
        cb(null, lbVersions);
      });
    };

    loopback.remoteMethod(Workspace.availableLBVersions, {
      http: {verb: 'get', path: '/loopback-versions'},
      returns: {arg: 'versions', type: 'array'},
    });

    /**
     * Get an array of available template names.
     *
     * @callback {Function} callback
     * @param {Error} err
     * @param {String[]} templateNames
     */

    Workspace.getAvailableTemplates = function(cb) {
      fs.readdir(TEMPLATE_DIR, function(err, files) {
        cb(err, err ? undefined : files.filter(dirFilter));
      });
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
http: {verb: 'post', path: '/stop'},
      returns: {arg: 'data', type: 'Object', root: true},
    });

    /**
     * Restart the project (app) in the workspace.
     * @param {function(Error=,Object=)} cb callback
     */
    Workspace.restart = function(cb) {
      Workspace.stop(function(err) {
        if (err) return cb(err);
        Workspace.start(cb);
      });
    };

    loopback.remoteMethod(Workspace.restart, {
      http: {verb: 'post', path: '/restart'},
      returns: {arg: 'data', type: 'Object', root: true},
    });

    /**
     * Return run status of the app.
     * @param {function(Error=,Object=)} cb callback
     */
    Workspace.isRunning = function(cb) {
      const result = Workspace._child ?
        {running: true, pid: Workspace._child.pid} :
        {running: false};

      process.nextTick(function() {
        cb(null, result);
      });
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
function fetchServerHostPort(cb) {
      FacetSetting.find(
        {where: {facetName: 'server'}},
        function extractHostPortFromFacetSettings(err, list) {
          if (err) return cb(err);
          const config = {};
          list.forEach(function(it) {
            config[it.name] = it.value;
          });

          cb(null, config.host, config.port);
        },
      );
    }

    loopback.remoteMethod(Workspace.start, {
      http: {verb: 'post', path: '/start'},
      returns: {
        arg: 'data',
        type: {pid: Number, host: String, port: Number},
        root: true,
      },
    });

    process.once('exit', function killWorkspaceChild() {
      if (Workspace._child)
        Workspace._child.kill();
    });

    /**
     * Stop the project (app) in the workspace started by {@link start}.
     * @param {function(Error=,Object=)} cb callback
github strongloop / loopback-workspace / common / models / workspace.js View on Github external
/**
     * Return run status of the app.
     * @param {function(Error=,Object=)} cb callback
     */
    Workspace.isRunning = function(cb) {
      var result = Workspace._child ?
      { running: true, pid: Workspace._child.pid } :
      { running: false };

      process.nextTick(function() {
        cb(null, result);
      });
    };

    loopback.remoteMethod(Workspace.isRunning, {
      http: { verb: 'get', path: '/is-running' },
      returns: { arg: 'data', type: 'Object', root: true },
    });

    Workspace.getWorkspace = function(cb) {
      cb(null, process.env.WORKSPACE_DIR);
    };

    loopback.remoteMethod(Workspace.getWorkspace, {
      http: { verb: 'get', path: '/get-workspace' },
      returns: { arg: 'path', type: 'string' },
    });

    Workspace.loadWorkspace = function(path, cb) {
      app.dataSources.db.connector.saveToFile(null, function() {
        process.env.WORKSPACE_DIR = path;