Example Express Node API written in Typescript with Postgres and Redis backend and RBAC / ABAC authorization
This project was created initially for research purposes, reviewing how various other
apps organize their Node/Express/Typescript repos and trying to pick the best
parts of each. It is now a reference / starter API project any startup could use to
start off on the right foot with key functionality needed for future scaling and support.
npm test
)locustfile.py
)npm install
./setenv.test.sh
.env
file, simply set vars in your CI provider or container managerdocker-compose up
./setup_es.sh
to create index mapping templates for Elasticsearch after startupdocker-compose down -v
npm run test
npm run dev
postgres
(network exposed by docker-compose)admin
(or whatever you set in ENV vars)This app includes automated tests using Supertest and Jest to test routes, etc.
npm test
or npm run coverage
__tests__
folders in application for test source codeEvery DAO method should emit
an event in an activity stream format as shown. For max flexibility,
like to disable writes and make the architecture CQRS, you can create a new handler in utils/activity.helper.ts
.
You can follow along the commit history relating to the issues (closed) and see how, but a general idea is:
src/services/
foldersrc/services/role/*
and find/replace to match new namessrc/config/data.test.ts
and add necessary test data and permissions for CRUDsrc/config/{provider}.ts
and make connections from process.env.{vars here}
setenv.test.sh
, src/utils/validation.helper.ts
, .env
src/config/openapi.json
and add routes to documentation (if REST implementation)src/config/event.ts
listener and be sure to emit
within Dao methods
See the /config/data.test.ts
file to see how permissions, roles, and users were added to the database
that fulfill the requirements below. The /util/{type}.helper.ts
files abstract the specific module implementation
as much as possible so we could change out solutions in future without modifying the code base.
guest
, I want to be able to register
or login
, so that I can access features within the appguest
, I want to confirm my valid email
address, so that I can gain access to the applicationguest
, I want to be able to submit my email
credentials, so I can still login
if my password is lostuser
, I want to be able to search
by city name, so I can view geo data about the cityuser
, I want to be able to view users
(without age or password), so I know users of the systemuser
, I want to be able to edit my own user
record, so I can keep my information currentuser
, I want to be able to view a list of roles
(without permissions) so I know what roles are availableuser
, I want to be able to logout
, so that my authentication session cannot be used by othersuser
, I want to be informed if attempts to gain access to my account occur, so I can help prevent unauthorized accessuser
, I want to be able to disable one or more devices (tokens), so I can prevent unauthorized accessuser
, I want the app to respond quickly, so I don’t have to wait for informationadmin
, I want to be able to search
by city name, so I can view geo data about the cityadmin
, I want to be able to view users
(with age but no password), so I know users of the system and their ageadmin
, I want to be able to create any user
record, so I can manage users in the systemadmin
, I want to be able to edit any user
record, so I can manage users in the systemadmin
, I want to be able to delete any user
record, so I can manage users and keep the system currentadmin
, I want to be able to view roles
(with permissions), so I know roles of the system and their permissionsadmin
, I want to be able to create any role
record, so I can manage roles in the systemadmin
, I want to be able to edit any role
record, so I can manage roles in the systemadmin
, I want to be able to delete any role
record, so I can manage roles and keep the system currentadmin
, I want to be able to view roles
for any user, so I know users of the system and their rolesadmin
, I want to be able to add any role
record to any user, so I can manage users and their roles in the systemadmin
, I want to be able to remove any role
record from any user, so I can manage users and keep the system currentadmin
, I want to be able to view permissions
for any role, so I know permissions of the system and their permissionsadmin
, I want to be able to add any permission
record to any role, so I can manage permissions in the systemadmin
, I want to be able to remove any permission
record from any role, so I can manage permissions and keep the system currentadmin
, I want to be able to deny any user or user device token, so I can manage user and device accesssysadmin
, I want to be able to automatically check app health, so I can automate scaling and recoverysysadmin
, I want the app to log all events, so that I can optionally add alerts if acceptable thresholds are exceededsysadmin
, I want to be able to deny protected access to any user or individual token, so I can prevent unauthorized accesssysadmin
, I want to be able to disable client features that may have issues, so I can maintain app stabilitysysadmin
, I want to identify actions a support user performs on behalf of users, so I can audit and explain datasupport user
, I want to be able to initiate a lost password request for a user, so I can take care of them in real timesupport user
, I want to be able to assume a users identity without password, so I can use the app as they would and help identify issuessupport user
, I want to be able to search for and view all users, so I can take care of them in real timesupport user
, I want to be able to edit a limited set of user data, so I can take care of them in real timetech support
, I want to view and filter user activity logs in real time, so I can troubleshoot issues before escalationtech support
, I want to be able to edit a limited set of user data, so I can test different configurations during troubleshootingproduct owner
, I want an API that supports various authorization levels, so we can support future revenue and feature modelsproduct owner
, I want all features of the app automatically tested using TDD, so we can keep customers happy with stabilityproduct owner
, I want to allow external authentication providers (IdP), so we can offload effort or meet compliance guidelinesproduct owner
, I want to test new features on a subset of users or geographies, so we can measure impact, refine, or revert as neededproduct owner
, I want to be able to track usage of toggle/flag features, so we can fine-tune before global deployment (or omit)product owner
, I want to ignore metrics performed by support users on behalf of users, so we can accurately measure metricsarchitect
, I want to centralize events/activity stream, so that I can easily add stream pipeline, queue, or bus to implment CQRS-ESarchitect
, I want the app to be ‘stateless’ with remote DB, so that I can easily scale to meet growth requirementsarchitect
, I want the app to be layered, so it’s extensible with minimal duplicate code and able to change providersarchitect
, I want to app to be able to run in containers, so it is isolated and can easily scale to meet growth requirementsarchitect
, I want to be able to change password hash solutions, so we can stay current as security standards evolvearchitect
, I want to be able to plug in security middleware, so we can stay current as security standards evolvearchitect
, I want to log performance metrics, so I can eliminate bottlenecks or calculate resource needs for scalingdeveloper
, I want to be able to toggle/flag new functionality, so we can safely build/deploy and test out new featuresAs I wanted to piece together RBAC/ABAC using popular stack choices, I found several good examples online. I’d
like to give credit and thanks to the following for their hard work and excellent articles and examples.
As this is just a research project, I don’t plan on maintaining LTS but if any
suggestions on improving the app, please write Issue or PR and I’ll consider. Thanks!