awesome-express-api
v0.1.10
Published
## Overview ### Features - -
Downloads
6
Readme
Awesome-Express-Api
Overview
Features
-
-
Table of contents
Installation
npm i awesome-express-api
Initialize
import Express from 'express';
import AwesomeExpressApi from 'TODO'
const app = Express();
const pocInstance = new AwesomeExpressApi(app, DatabaseLayer, options);
// Register resources
pocInstance.addResource('customer')
// Start server
pocInstance.startServer();
Options
interface OptionsInterface {
resetDatabase?: boolean,
listRoutes?: boolean,
port?: number,
filterQueryFormat?: string,
filterKey?: string,
searchKey?: string,
searchQueryFormat?: string,
sortKey?: string,
pageSizeKey?: string,
pageNumberKey?: string,
pageStartNumber?: number,
sort?: { [keyName: string]: string },
populateAfterPatchPost?: boolean,
responseBuilder?: ResponseBuilderInterface
}
Custom response format at get route
The standard response format for a get route is a array with result objects. If you want additional fields in the response, you can specify a response builder and customize the response.
For usage pass a function with the following format to the responseBuilder-option:
function responseBuilder(req, databaseResult, filter, search)
Example
async function responseBuilder(req, result, filter, search) {
const totalCount = await req.currentModel.model.countDocuments({...filter, ...search})
const notFilteredTotalCount = await req.currentModel.model.countDocuments({})
return {
items: result,
totalCount,
notFilteredTotalCount
}
}
const pocInstance = new AwesomeExpressApi(app, DatabaseLayer, {
responseBuilder: responseBuilder
});
Validators
The following validators are available at the moment:
isString
minLength:NUMBER
maxLength:NUMBER
notEmpty
isBoolean
Access in requests
DatabaseLayer
Docker
Login to github docker to build and push new images
docker login docker.pkg.github.com -u USERNAME -p PRIVATETOKEN
Build new Image
- docker build -t docker.pkg.github.com/bobo-le/awesome-express-api/aea:TAG .
- docker push docker.pkg.github.com/bobo-le/awesome-express-api/aea:TAG
Kubernetes
Create a deployment with pods and service inside
kubectl create -f deployment.yaml
Update running pods with new image
kubectl set image deployment/DEPLOYMENTNAME LABELNAME=docker.pkg.github.com/bobo-le/awesome-express-api/aea:TAG --record
Scale Pods
- kubectl scale --replicas=COUNT -f deployment.yaml
- COUNT=0 menas stopping all
Apply new config
kubectl apply -f deployment.yaml
Create secret to pull image from private registry
- kubectl create secret docker-registry regcred --docker-server=docker.pkg.github.com --docker-username=USERNAME --docker-password=PRIVATETOKEN --docker-email=EMAIL
- "regcred" is the name of your secret. Used in deployment.yaml
Get URL for running service
- minikube service SERVICENAME --url
- the api is under this url reachable
Helm
When using helm, kubecl -f FILES doesnt work anymore out of the box. Instead use the following
Optionally using kubectl -f FILES
- helm template REPONAME -x .\templates\deployment.yaml
- this renders the template file with the correct values
- result can be piped to -f with the following line
- helm template REPONAME -x .\templates\deployment.yaml | kubectl -f -
Installing environment with helm
- helm install --name REPONAME HELMDIRECTORY
- the repo is available under the REPONAME property
Upgrading with helm
- helm upgrade REPONAME HELMDIRECTORY
- it uses rolling update