Skip to content

Latest commit

 

History

History
71 lines (43 loc) · 3.41 KB

README.MD

File metadata and controls

71 lines (43 loc) · 3.41 KB

Monorepo Demo

This monorepo repository is a demonstration of how to achieve end-to-end type safety across a Node.js backend and any number and kind of JavaScript frontend. This is achieved using Turborepo and NPM Workspaces.

The project is two Next.js applications and a shared Node.js API. Use the company-app to create a company, then use the user-app to create users. Any user with an address that is within 50 miles of a company will be matched to that company and viewable from the company dashboard.

Project Structure

This repository is broken into two main parts: the apps and packages directories. The apps directory contains the applications that will be deployed and used, while packages contains the code that is shared between them.

Apps

api

A Node.js API built using Fastify and TypeORM to interface with a PostgreSQL database. Highlights include

  • Request and response validation via Zod and api-schemas package
  • Autogenerated OpenAPI documentation exposed at /docs
  • Usage of Mapbox API
  • Usage of PostGIS for indexed geospatial queries
  • Event-based data processing using EventEmitter

When a user or company is created, an event is emitted to get the latitude and longitude of the given address. This is stored as a geography type in the indexed database column location. Once this process is done for a user, another event is emitted to match the user to the closest three companies in a 50-mile radius.

user-app

A Next.js application to create users in the database. Highlights include

  • Usage of shared component library components
  • Usage of shared api-sdk library to make type-safe requests to the API
  • Styled with Tailwind CSS.

company-app

A Next.js application to create companies in the database. After your company is created, you are redirected to its dashboard, where the company matches will be displayed.

Packages

api-schemas

This package uses Zod to define the API interface, including the entities returned by the API and the endpoint contracts (parameters and payloads). The API uses these definitions to validate requests and responses. The Zod definitions are then exposed as TypeScript types that can be used to ensure type safety in any other app or package.

api-sdk

This package is a simple wrapper around the native fetch interface that exposes type-safe functions to make calls to each API endpoint. It can be used in any environment where fetch is supported, including browsers, Node.js, and serverless.

components

This package contains the shared React components used by both Next.js applications. They are styled with Tailwind CSS.

mono

This package is a Node.js CLI tool for using the monorepo. It lets you run select apps in dev mode or build select apps with their dependencies. Run npm run mono from the root of the repository to start it.

Local development

  1. From the root directory, run npm i
  2. Create a .env file in the apps/api directory with the following variables
PGHOST=
PGDATABASE=
PGUSER=
PGPASSWORD=
MAPBOX_TOKEN=

I recommend Neon for the PostgreSQL database. You can create a free Mapbox account here.

  1. From the root of the directory, run npm run mono and run the apps in dev mode.