Code Monkey home page Code Monkey logo

sails-hook-blueprint-utils's Introduction

sails-hook-blueprint-utils

npm version

Adds utility methods to Sails API blueprints.

Installation

In Sails.js v0.11+ installed hooks are run automatically. Therefore, simply install the hook via npm:

npm install --save sails-hook-blueprint-utils

Create config/installedHooks.js

module.exports.installedHooks = {
   "sails-hook-blueprint-utils": {
      "name": "blueprintUtils",
      "configKey": "blueprintUtils"
   }
}

If you want to use a policy, add it to your sails.config:

    ...

    blueprintUtils: {
        policy: 'isAllowed'
    }

    ...

Right now only one policy is supported.

Ingredients

  • Count

Returns total record count of the specified model.

"where" parameter is optional. If it's used it's used in the same way like you use it in default blueprint API .find() method

GET /users/count?where={:CRITERIA}

{ 
    count: 42000 
}

GET /users/5694f97cc48a107117e19c2d/products/

{ 
    count: 42000 
}

  • Associations

Returns related tables/collections associated with the model

GET /users/associations

{ 
    associations: [
        {
            "alias": "endpoints",
            "type": "collection",
            "collection": "endpoint",
            "via": "users"
        },
        {
            "alias": "clients",
            "type": "collection",
            "collection": "client",
            "via": "user"
        }
    ]
}
  • Schema

Returns the data model / schema of the specified model

GET /users/schema 

{ 
    schema:
    {
      "name": {
         "type": "string",
         "required": true,
         "minLength": 1,
         "maxLength": 50
       },
       "email": {
         "type": "email",
         "required": true,
         "unique": true,
         "isValidEmail": true
       },
       "password": {
         "type": "string",
         "required": true,
         "minLength": 3,
         "maxLength": 15,
         "protected": true
       }
    }
}
  • Filters

Returns the filterable fields of the specified model to use in frontend

Filters are defined in model itself as fieldname: field text

Protected values are excluded from filters.

module.exports = {

    schema: true,

    filters: {
        "name": 'Name',
        "createdAt": 'Created At',
        "updatedAt": 'Updated At'
    },
    
    attributes: {
      "name": {
         "type": "string",
         "required": true,
         "minLength": 1,
         "maxLength": 50
       },
       "email": {
         "type": "email",
         "required": true,
         "unique": true,
         "isValidEmail": true
       },
       "password": {
         "type": "string",
         "required": true,
         "minLength": 3,
         "maxLength": 15,
         "protected": true
       }
    }
}
GET /users/filters 

{
  "filters": [
    {
      "name": "username",
      "text": "Username",
      "type": "string"
    },
    {
      "name": "createdAt",
      "text": "Created At",
      "type": "datetime"
    },
    {
      "name": "updatedAt",
      "text": "Updated At",
      "type": "datetime"
    }
  ]
}

Credits

sails-hook-blueprint-utils's People

Contributors

buraktt avatar dependabot[bot] avatar sercanov avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

buraktt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.