How to use the loopback.findModel 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 EdgeVerve / loopback-connector-nodes-for-Node-RED / rule-engine-decision-table / decision-table.js View on Github external
RED.httpAdmin.get("/getDecisionTables", RED.auth.needsPermission(''), function(req,res) {
        var decisionTableModel = loopback.findModel('DecisionTable');
        if(decisionTableModel) {
            // Firing the Find query to get results from DB.
            // TODO: Query to retrieve only property 'name' from DB.
            decisionTableModel.find({}, req.callContext, function(err, data){
                var result = [];
                if(data.length > 0){
                    data.forEach(function(elem){
                        // Retrieving only 'name' from the DB result.
                        result.push({name:elem.name});
                    })
                }
                // Sending result JSON.
                res.json(result);
            });
        } else {
            console.error("Unable to find Model DecisionTable.")
github EdgeVerve / oe-cloud / common / models / framework / model-definition.js View on Github external
var itemType = propDetails.type[0];
          if (typeof itemType === 'function') {
            /* Array of another model */
            if (itemType.name === 'ModelConstructor') {
              associations.push(itemType);
              propDetails.itemtype = 'model';
              propDetails.modeltype = itemType.definition.name;
            } else {
              /* Array of primitive */
              propDetails.itemtype = itemType.name.toLowerCase();
            }
          }
          propDetails.type = 'array';
        }
        if (propDetails.refcodetype) {
          associations.push(loopback.findModel(propDetails.refcodetype, options));
        }
        if (propDetails.enumtype) {
          var enumModel = model.app.models[propDetails.enumtype];
          if (enumModel) {
            // enumtype is pointing to model
            propDetails.listdata = enumModel.settings.enumList;
          } else {
            // enumtype is not pointing to model
            log.error(options, 'error finding enumtype ', propDetails.enumtype);
          }
        }
        properties[propName] = propDetails;
      }
    });
github EdgeVerve / oe-cloud / server / boot / data-acl.js View on Github external
function attachBeforeRemoteHookToModel(modelName, options) {
  var model = loopback.findModel(modelName, options);
  // Checking the flag that DataACL exists and attaching the hook
  if (!model.settings._dataACLExists) {
    model.settings._dataACLExists = true;
    // Attaching beforeRemote hook to model to do the DataACL applyFilter
    model.beforeRemote('**', function (ctx, modelInstance, next) {
      var proxyKey = appinstance.get('evproxyInternalKey') || '97b62fa8-2a77-458b-87dd-ef64ff67f847';
      if (ctx.req && ctx.req.headers && proxyKey) {
        if (ctx.req.headers['x-evproxy-internal-key'] === proxyKey) {
          return next();
        }
      }
      dataACLModel.applyFilter(ctx, next);
    });
  }
}
github strongloop / loopback-workspace / server / connector.js View on Github external
function getOrCreateLoopBackConfigModel() {
      let LoopBackConfigFile = loopback.findModel('LoopBackConfigFile');
      if (LoopBackConfigFile) return LoopBackConfigFile;

      LoopBackConfigFile = ConfigFile.extend('LoopBackConfigFile');

      // Override `getWorkspaceDir` to return node_modules/loopback
      LoopBackConfigFile.getWorkspaceDir = function() {
        const workspaceDir = LoopBackConfigFile.base.getWorkspaceDir();
        return path.join(workspaceDir, 'node_modules', 'loopback');
      };

      // Override `isReadOnly` to be always `true`
      Object.defineProperty(LoopBackConfigFile.prototype, 'isReadOnly', {
        value: true,
      });

      return LoopBackConfigFile;
github EdgeVerve / oe-cloud / test / z-z-z-actor-pattern-aqcuire-db-lock-test-mongo.js View on Github external
before('create test models', function createModels(done) {
    var modelDefinition = loopback.findModel('ModelDefinition');
    var data = {
      'name': 'TestAccount',
      'base': 'BaseActorEntity',
      'options': {
        stateThreshold: 1
      }
    };

    modelDefinition.create(data, bootstrap.defaultContext, createTransferModel);

    function createTransferModel() {
      var data = {
        'name': 'TestTransfer',
        'base': 'BaseJournalEntity'
      };
      modelDefinition.create(data, bootstrap.defaultContext, addAllFunctions);
github EdgeVerve / loopback-connector-nodes-for-Node-RED / find-data / find-data.js View on Github external
this.on('input', function (msg) {
			var filter;
			node.status({});
			var modelName = config.modelname || msg.modelName;
			if (config.filter && typeof config.filter === 'string') filter = JSON.parse(config.filter);
			if (config.filter && typeof config.filter === 'object') filter = config.filter;
			if (!filter) filter = msg.filter;
			if (!filter) filter = {};

			var Model;

			if (modelName && modelName.trim().length > 0) {
                Model = loopback.findModel(modelName, node.callContext);
				if (Model) {
					Model.find(filter, msg.callContext, function (err, response) {
						if (err) {
							console.log(err);
							node.status({
								"fill": "red",
								"shape": "dot",
								"text": "An error occurred"
							});
							msg.payload = err;
							node.send([null, msg]);
						} else {
							node.status({
								"fill": "green",
								"shape": "dot",
								"text": "Found " + response.length + " records"
github EdgeVerve / oe-cloud / server / boot / node-red.js View on Github external
redNodes.setFlows(flowArray).then(function setFlowsFn() {
              callback();
            }).otherwise(function setFlowsOtherwiseFn(err) {
              console.log('node red error');
              callback(err);
            });
          }
        });
      });
    } else {
      console.log(' *** NODE-RED : RELOADING FLOWS *** ');
      var flowArray = [];
      var options = {};
      options.ignoreAutoScope = true;
      options.fetchAllScopes = true;
      var flowModel = loopback.findModel('NodeRedFlow');
      flowModel.find({}, options, function findCb(err, results) {
        if (err) {
          callback(err);
        }
        results.forEach(function resultsForEach(r) {
          r.flow.forEach(function prepareFlowArrayFn(f) {
            flowArray.push(f);
          });
        });
        if (flowArray.length > 0) {
          redNodes.setFlows(flowArray).then(function setFlowsFn() {
            callback();
          }).otherwise(function setFlowsOtherwiseFn(err) {
            console.log('node red error');
            callback(err);
          });
github EdgeVerve / oe-cloud / server / boot / oe-studio.js View on Github external
return new Promise(function (resolve, reject) {
    var model = loopback.findModel(modelName, options);
    model.findOne({ where: data }, options, function (err, res) {
      if (err) {
        reject(err);
      } else if (!res) {
        model.create(data, options, function (err, res) {
          if (!err) {
            resolve(res);
          } else {
            reject(err);
          }
        });
      } else {
        resolve(true);
      }
    });
  });
github EdgeVerve / oe-cloud / lib / db-migrate-helper.js View on Github external
function doMigrate(appinstance, option, cb) {
  var appVer = buildAppVersion(appinstance, option);
  var SystemConfig = loopback.findModel('SystemConfig');
  SystemConfig.find({
    where: {
      key: 'version'
    }
  }, util.bootContext(), function systemConfigFindCbFn(err, data) {
    var versionMisMatch = true;
    if (err) {
      cb(err);
    }
    if (data.length) {
      curDbVer = data[0].value;
      curSysVerData = data[0];
      versionMisMatch = appVerGreaterThanDBVer(appVer, curDbVer);
    }
    if (versionMisMatch) {
      var dbMig = getListOfMigrations(appinstance, option);
github EdgeVerve / oe-cloud / server / boot / 05_mark-as-cache-able.js View on Github external
messaging.subscribe('evictCache', function (msg) {
  var modelName = msg.modelName;
  var evictCtx = msg.evictCtx;
  var model = loopback.findModel(modelName);
  if (model) {
    model.evictCache(true, evictCtx);
  }
});
/**