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

hans-sequelize-api

v1.0.28

Published

rest-api form sequelize-express stack

Downloads

11

Readme

hans-sequelize-api

rest-api form sequelize-express stack

Functional of the package is setting 5 main methods bound database model:

  • get on / (returns {data: Entities[], meta: {page: number, pageSize: number, pageCount: number, total: number}});
  • get on /:id (returns Entity);
  • post on / (creates instance and returns Entity);
  • put on /:id (updates instance and returns Entity);
  • delete on /:id (deletes instance and returns Entity);

All methods allow query (hans-sequelize-qs)

Table of contents

Installing Add the package to your project

npm i hans-sequelize-api

using yarn

yarn add hans-sequelize-api

Example

Export SequelizeAPI from hans-sequelize-api

const SequelizeAPI = require('hans-sequelize-api')

using TypeScript

import SequelizeAPI from 'hans-sequelize-api'

In init api file

type PostgreModelName = 'User' | 'Post'

const User = ... //sequelize model
const Post = ... //sequelize model

const sequelizeModels = { User, Post } 

const sequelizeAPI = new SequelizeAPI<PostgreModelName>(sequelizeModels)

const setAPI = sequelizeAPI.initializeAPI({
    authMiddleware: auth as HandlerType, // express middleware
    adminMiddleware: admin as HandlerType, // express middleware
    validationMiddleware: validator as (rules: ValidationRules) => HandlerType // function that's returns express middleware
})

export default setAPI

In routes file (for example users.routes.ts)

import setAPI from 'any-init-api-file'


const addUserRules = {
    name: 'string|required',
    email: 'email|required'
}

const updateUserRules = {
    name: 'string',
    email: 'email'
}

module.exports = setAPI('User', router, {
    possibleMethods: ['gets', 'post', 'put', 'delete'],
    auth: ['post', 'put'],
    admin: ['delete'],
    validation: {post: addUserRules, put: updateUserRules},
    additionalMiddlewares: [
        {middleware: anyMiddleware, method: 'post'}
    ]
})

Query parameters

Query string general form is:

http://example-url/any-path?filters[<field-name>][operator]=<any-value>&page=<page-number>&pageSize=<items-count>

Pagination

To specify page number you should use parameter page:

?page=5

To specify page size (items count by page) you should use parameter pageSize:

?pageSize=25

Full pagination query string turns to:

?page=5&pageSize=25

Default value of page is 1, pageSize is 10

Fields

If we want to get items only with specified object fields we should use fields operator and provide an array:

?fields[0]=title&fields[1]=description

Let us have three items in database:

[
  {
    "id": "1",
    "title": "The first element",
    "description": "The description of the first element",
    "publicVisible": true,
    "count": 4,
    "Items": [
      {
        "id": "101",
        "itemTitle": "Any 101 item title"
      }
    ]
  },
  {
    "id": "2",
    "title": "The second element",
    "description": "The description of the second element",
    "publicVisible": false,
    "count": 20,
    "Items": [
      {
        "id": "102",
        "itemTitle": "Any 102 item title"
      },
      {
        "id": "103",
        "itemTitle": "Any 103 item title"
      }
    ]
  },
  {
    "id": "3",
    "title": "Third element",
    "description": "The description of the third element",
    "publicVisible": true,
    "count": 50,
    "Items": [
      {
        "id": "101",
        "itemTitle": "Any 101 item title"
      },
      {
        "id": "102",
        "itemTitle": "Any 102 item title"
      },
      {
        "id": "103",
        "itemTitle": "Any 103 item title"
      }
    ]
  }
]

So we get:

{
  "data": [
    {
      "title": "The first element",
      "description": "The description of the first element"
    },
    {
      "title": "The second element",
      "description": "The description of the second element"
    },
    {
      "title": "The third element",
      "description": "The description of the third element"
    }
  ],
  "meta": {
    "page": 1,
    "pageSize": 10,
    "total": 3,
    "pageCount": 1
  }
}

By default we get all object fields in items.

Filtering

In this version we can use only first-level filtering.

And we want to get only elements with publicVisible=true and title starting with "The". So we should provide following query parameters (using operator and):

?filters[and][0][title][startsWith]=The&filters[or][1][publicVisible][eq]=true

Then we get following response:

{
  "data": [
    {
      "id": "1",
      "title": "The first element",
      "description": "The description of the first element",
      "publicVisible": true,
      "count": 4
    }
  ],
  "meta": {
    "page": 1,
    "pageSize": 10,
    "total": 1,
    "pageCount": 1
  }
}

Sort

To sort our items by id descending we should use query operator sort:

?sort=id:desc

Then we get:

{
  "data": [
    {
      "id": "3",
      "title": "Third element",
      "description": "The description of the third element",
      "publicVisible": true,
      "count": 50
    },
    {
      "id": "2",
      "title": "The second element",
      "description": "The description of the second element",
      "publicVisible": false,
      "count": 20
    },
    {
      "id": "1",
      "title": "The first element",
      "description": "The description of the first element",
      "publicVisible": true,
      "count": 4
    }
  ],
  "meta": {
    "page": 1,
    "pageSize": 10,
    "total": 3,
    "pageCount": 1
  }
}

Relations

To get relations we can use operator relations and provide an array:

?relations[0]=Item

Note that we have to use relation name in single form. So then get:

{
  "data": [
    {
      "id": "1",
      "title": "The first element",
      "description": "The description of the first element",
      "publicVisible": true,
      "count": 4,
      "Items": [
        {
          "id": "101",
          "itemTitle": "Any 101 item title"
        }
      ]
    },
    {
      "id": "2",
      "title": "The second element",
      "description": "The description of the second element",
      "publicVisible": false,
      "count": 20,
      "Items": [
        {
          "id": "102",
          "itemTitle": "Any 102 item title"
        },
        {
          "id": "103",
          "itemTitle": "Any 103 item title"
        }
      ]
    },
    {
      "id": "3",
      "title": "Third element",
      "description": "The description of the third element",
      "publicVisible": true,
      "count": 50,
      "Items": [
        {
          "id": "101",
          "itemTitle": "Any 101 item title"
        },
        {
          "id": "102",
          "itemTitle": "Any 102 item title"
        },
        {
          "id": "103",
          "itemTitle": "Any 103 item title"
        }
      ]
    }
  ],
  "meta": {
    "page": 1,
    "pageSize": 10,
    "total": 3,
    "pageCount": 1
  }
}

By default we get items without any relation

Relation fields

To define object fields in relations we use relationFields operator:

?relations[0]=Item&relationFields[Item][0]=itemTitle

We get:

{
  "data": [
    {
      "id": "1",
      "title": "The first element",
      "description": "The description of the first element",
      "publicVisible": true,
      "count": 4,
      "Items": [
        {
          "itemTitle": "Any 101 item title"
        }
      ]
    },
    {
      "id": "2",
      "title": "The second element",
      "description": "The description of the second element",
      "publicVisible": false,
      "count": 20,
      "Items": [
        {
          "itemTitle": "Any 102 item title"
        },
        {
          "itemTitle": "Any 103 item title"
        }
      ]
    },
    {
      "id": "3",
      "title": "Third element",
      "description": "The description of the third element",
      "publicVisible": true,
      "count": 50,
      "Items": [
        {
          "itemTitle": "Any 101 item title"
        },
        {
          "itemTitle": "Any 102 item title"
        },
        {
          "itemTitle": "Any 103 item title"
        }
      ]
    }
  ],
  "meta": {
    "page": 1,
    "pageSize": 10,
    "total": 3,
    "pageCount": 1
  }
}

By default we get relation items with all object fields.