Welcome to Moony
.
├── dist # Compiled files (alternatively build
)
├── src # Source files (alternatively lib
or app
)
├── src/docs # Documentation files like swagger
├── src/db # All db related code like migrations goes here
├── src/config # App related config files like express app goes here
├── src/routes # All public or private end points configured here
├── src/handlers # All controllers or handlers for the end points defined here
├── src/utils # All utility functions goes here
├── test # Unit tests or integration tests (alternatively spec
or tests
)
├── .prettierrc # Prettier settings defined here
├── .eslintrc.json # Eslint settings and rules defined here
├── .gitignore
├── package.json
└── tsconfig.json # Typescript configuration
├── LICENSE
└── README.md
This project uses node and yarn. Go check them out if you don't have them locally installed. Dont mix yarn and npm
npm install -g npx yarn typescript
Go to the project root folder and run following command
yarn
App needs following environment variables
- DATABASE_URL: # url for the database (refer database connection format in case of doubts)
- SERVER_PORT: # port on server to run
- CORS_ORIGINS: # domain names separated with comma
- TOKEN_SECRET: # secret to generate token
- SHOW_API_DOCS: value should be yes, variable to show api docs(optional, use this variable only in development mode)
ORM is used to query database, prisma is the orm used for this project. Code first approach is followed using migrations, please follow below steps.
-
When new model added or existing model is changed , use below commands to apply changes to the database in respective environments
npx prisma migrate dev
-
To apply pending migrations to development, staging, or testing environments, run the migrate deploy command as part of your CI/CD pipeline:
npx prisma migrate deploy
-
When new model is added or existing model changed, use below command to generate client used to query db, which will reflect database models.
npx prisma generate
-
Please refer Prisma Guides for further reading
-
Seeding data
npx prisma db seed
Create helloWorld.ts
in src/routes/controllers
and add following GET endpoint code
import { Request, Response, NextFunction } from 'express';
const helloWorldGet = (req: Request, res: Response, next: NextFunction) => {
var newDate: any = new Date();
res.status(200).json({message: 'Hello World'});
}
export {helloWorldGet}
If it is private url register in src/routes/private.ts
else register in src/routes/public.ts
following will create a public route(src/routes/public.ts
).
import {Router} from 'express';
import {helloWorldGet} from '../controllers';
const router = Router();
router.get("/hello-world", helloWorldGet);
export default router;
Run server and navigate to /hello-world
in browser and now you can see following output.
{'message':'Hello World'}
This section describes all the available scripts that the contributors can use during the development.
yarn run dev
Use following command for live reload.
yarn run dev:watch
Use following command to build and generate build files.
yarn run build
After build run following command.
yarn run start
The merge flow is dev -> staging -> uat -> prod. When we need to deploy to staging, a PR from dev -> staging will be created, reviewed, and merged. Merging into staging will kick off a staging build similarly.
Similar for merging into uat and prod.
Use following command to check lint errors.
yarn run lint
Use following command to run tests with coverage details.
yarn run test-coverage
Use following command to run tests whenever a test is changed without restarting tests again.
- Don't import directly from files , import using index.ts.
do:
import {helloWorldGet} from '../controllers'
don't do:
import {helloWorldGet} from '../controllers/helloWorldController'
- Before commit if there are any changes in code
run test
andrun lint
, so that can avoid unnecessary commit history.