8000 GitHub - SiswoHandoko/metapals-backend: This Repo Contain Backend Code API for https://www.nparks.gov.sg/florafaunaweb/species-search
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

SiswoHandoko/metapals-backend

Repository files navigation

Description

I implemented this service using Nest i am use this for POC for handle performance issue on NPARKS web and I applied this service with several design patterns

  • Dependency Injection and decorators
  • Versioning API for scalable Development on Future
  • Concurrent/Pararel Process on code for make better response time
  • CQRS (Command Query Responsibility Segregation) is a design pattern that separates write operations (commands) from read operations (queries) in applications instead of use cache like redis i prefer use cqrs for this issue.
  • Rate Limiter for maintain server performance (i set 1000 request per 1 minutes)
  • Database Indexing for all Query (I asume the issue is from query locking actually so i put some indexing for POC test)
  • Stress Test Sample for Benchmarking My API
  • DockerFile for Production Mode Ready
  • Unit Testing for Production Mode Ready

Database Design

alt text

This Database Design is for POC only and replicate process from nparks web. Explanation :

  • Table field_categories and fields is for clasification dropdown on filter modal
  • Table species is a species table wich will contain plant list or animal list in future
  • Table native_habitats and family_names is table for relation 1 to many to species
  • Table preferred_climate_zones is table many to many to species
  • Column commonName is contain array value for handle dynamic value

so basiccaly i try to make any representative case for any table relation from 1-n, n-n, and even dynamic value on field for future proof

Installation

$ npm install

Create .env File (example only)

# ENV TYPE
NODE_ENV=development

# DIALECT OR TYPE OF DB CONNECTION
POSTGRE_DIALECT=postgres

# MASTER CONNECTION FOR CREATE/UPDATE/DELETE OPERATION QUERY
POSTGRE_PORT_MASTER=5432
POSTGRE_HOST_MASTER=localhost
POSTGRE_USER_MASTER=postgres
POSTGRE_PASS_MASTER=
POSTGRE_DB_MASTER=metapals

# SLAVE CONNECTION ONLY FOR SELECT OPERATION QUERY
POSTGRE_PORT_SLAVE_0=5432
POSTGRE_HOST_SLAVE_0=localhost
POSTGRE_USER_SLAVE_0=postgres
POSTGRE_PASS_SLAVE_0=
POSTGRE_DB_SLAVE_0=metapals

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

Runs the app in the development mode Open http://localhost:3000 to view it in your browser.

I put Postman File on documentation folder on root directory

Metapals Backend.postman_collection.json

Database Migration (PostgreSQL)

I create Migration and seeder for testing and POC the API performance you can check this command for running on local

# migration up all table
$ npx sequelize-cli db:migrate

# seed the data for login
$ npx sequelize-cli db:seed:all

instead of run migration on local you can import .sql too i put .sql file on folder documentation on root directory

metapals.sql

Benchmark API and Stress Test API

use this command if you want to benchmarking the /v1/species list api you will get request persecond and average response time but dont forget to run your app first before run this command

node .\stressTest.js

Unit Test Command

use this command if you want to show Unit Test Coverage for all module it will be show the report coverage test from the code

# unit tests
$ npm run test

# test show coverage
$ npm run test:cov

Docker Build Production Ready

I put Dockerfile too for deployment production ready on future you can build the app use this command

# Build
$ docker build -t metapals-app .

# Run Docker image
$ docker run -p 3000:3000 metapals-app

API Example Main Species List

I put API Example for this Repo for get species by advance filter Request Species List:

http://localhost:3000/v1/species?page=1&perPage=10&fieldId=1&value=&valueId=2&search=a&sortBy=name

Parameter
page:1 (mandatory)
perPage:10 (mandatory)
fieldId:1 (mandatory)
valueId:2 (mandatory if field Id is not 2)
value:keira (mandatory if field Id is 2)
search:a (Optional)
sortBy:name (Optional)

Response Species List:

{
    "page": "1",
    "perPage": "10",
    "meta": "",
    "total": 75,
    "data": [
        {
            "id": 420,
            "familyNameId": 2,
            "nativeHabitatId": 4,
            "name": "Alberta Johns",
            "commonName": "{Lamont,Nico,Volkman}",
            "tag": "plant",
            "image": "https://loremflickr.com/640/480?lock=4972014819344384",
            "createdAt": "2024-03-05T07:25:29.163Z",
            "updatedAt": "2024-03-05T07:25:29.163Z"
        }
    ]
}

API Example For Filter Dropdown on Modal

Request Field Categories:

http://localhost:3000/v1/field-categories

Response Field Categories:

{
    "data": [
        {
            "id": 1,
            "name": "Name",
            "createdAt": "2024-03-05T07:25:27.755Z",
            "updatedAt": "2024-03-05T07:25:27.755Z"
        },
        {
            "id": 2,
            "name": "Biogeography",
            "createdAt": "2024-03-05T07:25:27.755Z",
            "updatedAt": "2024-03-05T07:25:27.755Z"
        }
    ]
}

Request Fields by fieldCategoryID:

http://localhost:3000/v1/fields?fieldCategoryId=2

Response Fields by fieldCategoryID:

{
    "data": [
        {
            "id": 3,
            "fieldCategoryId": 2,
            "name": "Native Habitat",
            "createdAt": "2024-03-05T07:25:27.763Z",
            "updatedAt": "2024-03-05T07:25:27.763Z"
        },
        {
            "id": 4,
            "fieldCategoryId": 2,
            "name": "Preferred Climate Zones",
            "createdAt": "2024-03-05T07:25:27.763Z",
            "updatedAt": "2024-03-05T07:25:27.763Z"
        }
    ]
}

Request Values by fieldID:

http://localhost:3000/v1/values?fieldId=4

Response Values by fieldID:

{
    "data": [
        {
            "id": 1,
            "name": "Desert / Arid",
            "createdAt": "2024-03-05T07:25:27.781Z",
            "updatedAt": "2024-03-05T07:25:27.781Z"
        },
        {
            "id": 2,
            "name": "Highland / Montane",
            "createdAt": "2024-03-05T07:25:27.781Z",
            "updatedAt": "2024-03-05T07:25:27.781Z"
        },
        {
            "id": 3,
            "name": "Mediterranean",
            "createdAt": "2024-03-05T07:25:27.781Z",
            "updatedAt": "2024-03-05T07:25:27.781Z"
        }
    ]
}

Releases

No releases published

Packages

No packages published
0