How to use the loopback.createModel 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-connector-rest / example / rest-crud-app.js View on Github external
var loopback = require('loopback');

// simplier way to describe model
var User = loopback.createModel('User', {
  name: String,
  bio: String,
  approved: Boolean,
  joinedAt: Date,
  age: Number
});

console.log(User.modelName);

var app = loopback();

app.set('port', process.env.PORT || 3000);
app.use(loopback.bodyParser.json({extended: false}));

var count = 2;
var users = [new User({id: 1, name: 'Ray'}), new User({id: 2, name: 'Joe'})]
github EdgeVerve / oe-cloud / lib / common / util.js View on Github external
var embeddedModelName = jsonifyModel.properties[p].type;
          if (!getApp().models[embeddedModelName]) {
            jsonifyModel.properties[p].type = jsonifyModel.properties[p].type + '-' + createContextString(autoscopeFields, actualContext.ctx);
          }
        } else if (typeList.indexOf(jsonifyModel.properties[p].type) === -1 && Array.isArray(jsonifyModel.properties[p].type)) {
          var embeddedType = jsonifyModel.properties[p].type[0];
          if (typeList.indexOf(embeddedType) === -1 && !getApp().models[embeddedType]) {
            jsonifyModel.properties[p].type[0] = jsonifyModel.properties[p].type[0] + '-' + createContextString(autoscopeFields, actualContext.ctx);
          }
        }
      });
    }
  }


  var model = loopback.createModel(jsonifyModel);
  model.updateId = uuidv4();
  model.variantOf = jsonifyModel.variantOf;
  model.clientPlural = jsonifyModel.clientPlural;
  model.clientModelName = jsonifyModel.clientModelName;
  attachOverrideModelFunction(model);

  // Setting dynamic model set to true
  model.settings = model.settings || {};
  model.settings._dynamicModel = true;

  // var model = loopback.createModel(JSON.parse(JSON.stringify(modeldefinition)));
  // console.log("************", jsonifyModel.clientModelName);
  getApp().locals.modelNames[model.clientModelName.toLowerCase()] = model.clientModelName;
  getApp().locals.modelNames[model.clientModelName] = model.clientModelName;
  // Attach it to the datasource
github saggiyogesh / nodeportal / services / ArticleService / ArticleBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-article.js");

var Article = loopback.createModel(config);

DAOExtras(Article);

//create finders form finders property
Article.getById = function getById(id, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"id":id}}, next);
};


Article.getByIdAndVersion = function getByIdAndVersion(id,version, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"id":id,"version":version}}, next);
};
github saggiyogesh / nodeportal / services / CommentService / CommentBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-comment.js");

var Comment = loopback.createModel(config);

DAOExtras(Comment);

//create finders form finders property
Comment.getByParentCommentId = function getByParentCommentId(parentCommentId, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.find({"where":{"parentCommentId":parentCommentId}}, next);
};


Comment.getByThreadId = function getByThreadId(threadId, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.find({"where":{"threadId":threadId},"sort":"createDate ASC"}, next);
};
github saggiyogesh / nodeportal / services / ArticleVersionService / ArticleVersionBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-article_version.js");

var ArticleVersion = loopback.createModel(config);

DAOExtras(ArticleVersion);

//create finders form finders property
ArticleVersion.getById = function getById(id, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.find({"where":{"id":id}}, next);
};


ArticleVersion.getByIdAndVersion = function getByIdAndVersion(id,version, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"id":id,"version":version}}, next);
};
github fullcube / loopback-component-mq / lib / index.js View on Github external
function setupRabbitMqModel(app, settings) {
  // Assign ACLs from the component configuration.
  modelDef.acls = _.get(settings, 'options.acls', [])

  // Create the model.
  const RabbitMQ = loopback.createModel(modelDef)

  // Apply model customizations.
  require('./models/rabbitmq')(RabbitMQ)

  // Register the model.
  app.model(RabbitMQ)

  return loopback.getModel('RabbitMQ')
}
github saggiyogesh / nodeportal / services / PluginInstanceService / PluginInstanceBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-plugin_instance.js");

var PluginInstance = loopback.createModel(config);

DAOExtras(PluginInstance);

//create finders form finders property
PluginInstance.getByPluginNamespaceAndPageId = function getByPluginNamespaceAndPageId(pluginNamespace,pageId, next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"pluginNamespace":pluginNamespace,"pageId":pageId}}, next);
};





//TODO build finders with paging, different ways for nosql & sql dbs
github saggiyogesh / nodeportal / services / PageService / PageBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-page.js");

var Page = loopback.createModel(config);

DAOExtras(Page);

//add before hooks

Page.beforeCreate = function (next) {
            var url = this.friendlyURL;
            if (url.charAt(0) != '/') {
                this.friendlyURL = "/" + url;
            }

            if (!this.data) {
                this.data = {};
            }
            next();
        };
github EdgeVerve / oe-cloud / common / mixins / history-mixin.js View on Github external
properties._modelId.type = model.definition.properties[idName].type;
        continue;
      }
      for (var key1 in model.definition.properties[key]) {
        if (key1 === 'type') {
          properties[key] = {};
          properties[key][key1] = model.definition.properties[key][key1];
        }
      }
    }
  }

  var dataSourceGroup = model.definition.settings.dataSourceGroup;

  // Create a new Model in loopback.
  var newModel = loopback.createModel(auditModelName, properties, {
    base: 'PersistedModel',
    mixins: {
      'SwitchDatasourceMixin': true
    },
    dataSourceGroup: dataSourceGroup
  });

  model._historyModel = newModel;
  // Attach it to datasource to which model is attached,
  // skipping Models that do not have a datasource attached

  if (model.dataSource) {
    model.dataSource.attach(newModel);
  }

  log.debug(log.defaultContext(), 'Created History Model ', auditModelName, ' for model ', model.modelName);
github saggiyogesh / nodeportal / services / LayoutService / LayoutBaseService.js View on Github external
//generated file
// not modified directly
var loopback = require("loopback"),
    DAOExtras = require(utils.getLibPath() + "/ServicesHelper/DAOExtras"),
    config = require(utils.getRootPath() + "/services/modelConf/np-model-layout.js");

var Layout = loopback.createModel(config);

DAOExtras(Layout);

//create finders form finders property
Layout.getDefault = function getDefault( next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"name":"2-col-70-30"}}, next);
};


Layout.getOneCol = function getOneCol( next) {   // arguments are defined in arguments property
    //find or findOne will have argument from query
    this.findOne({"where":{"name":"1-col"}}, next);
};