Skip to content

ameksike/ksmf-skeleton-rest

Repository files navigation

KsMf REST API Skeleton

This is a template to create REST API services in a simple and easy way. This project implements an example where three main entities are managed: Users, Tags and Comments. The data is managed using a relational model with support for different database management systems (MySQL, PostgreSQL, SQLite, etc.). This solution provides from its home page access to documentation that helps understand the services, including how to test it. Also, this project shows the use of many-to-many and one-to-many relationships between entities, parameter validators, management of duplicates entries, sort and filter data, unit and integration test based on TDD approach, and application of some design patterns and principles such as SOLID.

Install

Configuration

  • edit ./cfg/config.json and define database access options
  • npx sequelize-cli db:migrate
  • npx sequelize-cli db:seed:all
  • npm start

Docker

$ docker-compose up -d db
$ docker-compose stop db
$ docker-compose -f docker-compose.yml up -d api
$ docker-compose -f docker-compose.yml stop api
$ docker logs oauth-server-api-1 -f
$ docker-compose run -it api /bin/ash
$ docker-compose build --no-cache api

Demo run

Filters

The filters are defined based on an array data structure, each record define a query criterion as a list of 3 element [field, value, operator]. The filters are specified as one more variable within the url of the endopint list.

filter = [ 
    [
        field: STRING
        value: STRING 
        operator: STRING [OPTIONAL] default: eq, values: eq,ne,is,not,or,gt,lt,between,in,like,regexp 
    ],
    ...
]

For more information about the available operators, see: operators on filters.

Filters examples

Sort

It is possible to sort the data based on different criteria. The criteria are described as an array of elements. These elements are arrays in the form [column or field name, direction].

sort = [ 
    [
        field: STRING
        direction: STRING [OPTIONAL] default: ASC, values: ASC|DESC
    ],
    ...
]

The column or field name will be escaped correctly and the direction will be checked in a whitelist of valid directions (such as ASC, DESC, NULLS FIRST, etc).

Sort examples

Pagination

The page and size options allow you to work with limiting and pagination. The size option defines the limit of rows to get from the server and with page you can navigate through data for get better performance.

Demo query with filters and sorting

Test

  • npm run test

Create Data Base Migrations

  • npx sequelize-cli model:generate --name user --attributes name:string,age:integer,job:string,note:text

  • npx sequelize-cli model:generate --name comment --attributes comment:text,userId:integer,flightId:integer,tagId:integer

  • npx sequelize-cli model:generate --name tag --attributes name:string

  • npx sequelize-cli model:generate --name tagComment --attributes commentId:integer,tagId:integer

  • npm run db:model:generate service --attributes name:string

  • npx sequelize-cli db:migrate

Create Data Base Seeders

  • npx sequelize-cli seed:generate --name add-user
  • npx sequelize-cli seed:generate --name add-tag
  • npx sequelize-cli seed:generate --name add-comment
  • npx sequelize-cli seed:generate --name add-comment-tag
  • npx sequelize-cli db:seed:all