Conduit API

October 28, 2021 · View on GitHub

API for Real World App (Conduit) using Node.js + TypeScript with Functional Programming

Required global dependencies

  • Node.js v16
  • Yarn v1

Up & Running

  1. Install local dependencies:
yarn
  1. Tests (watch mode):
yarn test:watch
  1. Get the server and database up:
yarn dev

To stop the server watch, just press Ctrl + c.

  1. Stop docker after get server down:
yarn docker:down
  1. Destroy all data from DB:
yarn docker:destroy

Scripts

ScriptDescription
yarn startRun production server
yarn devGet database up and run dev server
yarn serverRun only dev server
yarn docker:upGet docker configuration up
yarn docker:downGet docker configuration down
yarn docker:destroyDestroy database and all docker data
yarn migrate <name>Run Prisma migration (You must provide a name)
yarn migrationRun existent Prisma migrations
yarn migration:prodRun existent Prisma migrations on production
yarn testRun unit and integration tests once (great to be used in CI)
yarn test:unitRun unit tests once
yarn test:watchRun unit tests in watch mode
yarn test:integrationRun integration tests once
yarn test:integration:watchRun integration tests in watch mode
yarn lintRun linter
yarn lint:fixFix lint errors
yarn type-checkTS typechecking
yarn prepareNot suposed to be manually used. It's just to configure husky
yarn buildGenerates production build
yarn ciRun lint, typechecking and tests (meant to be used in a CI)
yarn update-depsUpdate both dependencies and devDependencies

Tree structure

This project uses Hexagonal Architecture (Ports & Adapters) with Functional Programming.

.
├── src
│   ├── config
│   │   ├── tests
│   │   │   └── fixtures
│   │   └── module-alias.ts
│   ├── core
│   │   ├── <domain / entity>
│   │   │   ├── use-cases
│   │   │   │   ├── <use-case>-adapter.ts
│   │   │   │   ├── <use-case>.test.ts
│   │   │   │   ├── <use-case>.ts
│   │   │   └── types.ts
│   │   └── types
│   │       ├── <type>.test.ts
│   │       └── <type>.ts
│   ├── ports
│   │   ├── adapters
│   │   │   └── <port-adapter>
│   │   └── <port>
│   ├── app.ts
│   └── index.ts
├── .env.example
├── jest.config.integration.js
└── jest.config.js

Directory / FileDescription
srcAll source code write in TypeScript must be in this directory.
src/index.tsMain entry point for initial configuration of the project. Do not edit this file. Start with src/app.ts.
src/app.tsProject entry point. You can call your first port here and boot your providers that should start before your server.
src/configAll configurations can live here.
src/config/tests/fixturesHelpers for using in tests.
src/config/module-alias.tsModule configurations for using @/ instead of ../../ on dev, tests and production environments.
src/corePure domain implementations. core files must not know any port or adapter, nor anything ouside core directory.
src/core/<domain/entity>Inside the core directory, you can organize your files by domain or entity.
src/core/<domain/entity>/typesStart point for modelling your domain / entity with TypeScript types.
src/core/<domain/entity>/use-casesHere you can put your functions with business rules for this specific domain / entity, and the adapters that ports will use.
src/core/typesHere you can put the types that are not related with any of your domains or entities.
src/helpersHere you can put your global helpers.
src/portsAnything with external world contact. When you need to access something on core, you must use an adapter.
src/ports/adaptersAdapters for ports. For example: You can have a database adapter that can use Postgres or MariaDB. An http adapter that can consume express or fastify.
src/ports/<port>Here you can create your raw ports with real implementation: express / fastify as http server, postgres / mariadb as databases, etc.
.env.exampleList of Environment Variables. Please, copy this file and create a new .env file to use Env Vars.
jest.config.integration.jsJest configuration file for integration tests.
jest.config.jsMain Jest configuration file.

Important usage information

Environment Variables

You can use env vars by copying the .env.example file to a new .env file on the root of the project. To document all used env vars, and get autocomplete when use the function env('YOU_VAR'), just put all your env vars on file src/helpers/env.ts.

Global import

All files and dirs inside src directory can be imported using @/. Prefer using this way over local import (../../).

License

MIT