npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-model-binder

v1.4.0

Published

Loopback Model Binder for ES6 classes

Downloads

33

Readme

loopback-model-bider

Loopback Model Binder for ES6

  • Helps to write clean code.
  • Developer can focus on application logic.
  • Can use ES5 or ES6 code.

Usage

Install with npm

npm install loopback-model-binder --save

Steps

  • Copy all folders from demo folder to your models folder
  • Create boot file for your demo api.
import { modelBootstrap } from 'loopback-model-binder';
import * as path from 'path';

module.exports = (app) => {
  // the path should be the root path of your model schema
  modelBootstrap(app, path.join(__dirname, '../models'));
};
  • Run your strongloop app.

Model schema [you-custom-name]-model.json

When creating model json file, it should have -model.json suffix.

  • mongodb.collections is an array of your collections in mongodb database.
  • should be in PascalCase
{
  "name": "Mongo",
  "base": "PersistedModel",
  "mongodb": {
    "collections": ["MaritalStatus", "Occupations"]
  },
  "http": {
    "path": "mongo"
  }
}

Extend Model

  • RestMongoData - child datasource (name of the datasource)
  • collections - list of mongodb collections
{
  "name": "RestMongo",
  "plural": "RestMongos",
  "base": "PersistedModel",
  "http": {
    "path": "rest-mongo"
  },
  "extends": {
    "RestMongoData": {
      "collections": ["SeveralArtist"]
    }
  }
}
  • Note: if you want to extend Rest datasource no need to add "extends" property, just configure you datasource as child.
  • Please see rest-mongo-model.json

Datasources schema [your-custom-name]-datasources.json

  • When creating datasources json file, it should have -datasources.json suffix.
{
  "MongoData": {
    "connector": "mongodb",
    "debug": "true",
    "host": "localhost",
    "database": "sampledb",
    "port": 27017,
    "username": "root",
    "password": "*****"
  }
}
  • Attach multiple datasources to the model
    • Separate into multiple files. (should be 1 datasource in each file)
      • [model-name]-datasources.js - this is the parent datasource.
      • [custom-datasource-name]-datasources.json - child datasource
    • In one(1) datasource file.
      • MongoRestData - parent/main datasource
      • mongoSpotifyRest - child datasource
  {
    "MongoRestData": {
      "connector": "mongodb",
      "debug": "true",
      "host": "localhost",
      "database": "mongorest",
      "port": 27017,
      "username": "root",
      "password": ""
    },
    "mongoSpotifyRest": {
      "connector": "rest",
      "debug": "true",
      "options": {
        "headers": {
          "content-type": "application/json"
        },
        "strictSSL": false
      },
      "operations": [
        { 
          "template": {
            "method": "GET",
            "url": "https://api.spotify.com/v1/artists?ids={ids}"
          },
          "functions": {
            "getSpotifySeveralArtists": ["ids"]
          }
        }
      ]
    }
  }  

Override configs in you custom boot file.

Please see in folder demo/mongo-data/mongo-boot.js

  /// By default, it will read binder.config.js file, if exist.
  /// else loopback-model-binder has default configs.
  /// Please see in folder src/model-boot.js 
  get configs(){
    /// create a copy of the base configs
    let _configs = JSON.parse(JSON.stringify(super.configs));
    /// set the seed.isSeed value to false
    /// setting to false will not execute the seed
    _configs.seed.isSeed = false;

    /// return the new configs
    return _configs;
  }

Function or Class

import { modelBootstrap, ModelBoot, EntityBase, Model, ModelSeed } 'loopback-model-binder';

modelBootstrap(app, bootRootDir, isEnable)

  • app {Object} strongloop/loopback app object.
  • bootRootDir {String} root Directory of your custom boot file.
  • isEnable {Boolean} Enable or Disable remoteMethods by default = false

ModelBoot

  • Base class to create custom boot file. Please see
  • onInit {Observable} initialize the datasources and Model
  • configs {Object} configurations of model binder

EntityBase

  • Base class to create entity/functionality
  • onInit {void} Attach custom functionalities to Model from your entity.

Model

  • Model.instance.create {void} create a model
  • Model.instance.[YourModel] {Object} you access the Loopback Model functionalities

ModelSeed

  • Base class to create Seed data in MongoDB
  • execute {void} execute the seeding, must be implement in the child class.
  • migrate {void} migrate each object from array data.
    • data {Object} on each array.
    • filter {Object} check if data is exist
    • options {Object} default null
      • modelName {String} name of the Model
      • length {number} length of the data array