# Api

## Description

Backend API build with [Nest](https://github.com/nestjs/nest) Framework.

This project uses [@nestjsx/crud](https://github.com/nestjsx/crud) to simplify and standardize the REST API

## Built with

| Component      | Using                                                                                            | Descrption |
| -------------- | ------------------------------------------------------------------------------------------------ | ---------- |
| Base           | [NestJS](https://nestjs.com)                                                                     |            |
| ORM            | [TypeORM](http://typeorm.io/)                                                                    |            |
| CRUD           | [@nestjsx/crud](https://github.com/nestjsx/crud)                                                 |            |
| nest-router    | [@nestjsx/nest-router](https://github.com/nestjsx/nest-router)                                   |            |
| File Upload    | [Multer](https://github.com/expressjs/multer)                                                    |            |
| Validation     | [Class-Validator](https://github.com/typestack/class-validator)                                  |            |
| Documentation  | [Open API(Swagger)](https://swagger.io)                                                          |            |
| Authentication | [Passport.js](http://www.passportjs.org)                                                         |            |
| Testing        | [Jest](https://github.com/facebook/jest) & [SuperTest](https://github.com/visionmedia/supertest) |            |
| Code Style     | [Prettier](https://github.com/prettier/prettier) & [TS-Lint](https://palantir.github.io/tslint/) |            |

## Development

### Run

#### Postgres

> start Postgres

```bash
# start local postgres
docker-compose up postgres
# (or) start local postgres with data reset
docker-compose up -V postgres #remove volumes

# (optional) start local keycloak
docker-compose up keycloak

# stop local postgres before restart again
docker-compose down
# (or) stop local postgres with data reset
docker-compose down -v #remove volumes
```

> if error `The container name "/postgres" is already in use by container`, remove orphan container.

```bash
docker ps -a
docker rm 82be5234c94a
```

> connect Postgres running on local k8s

```bash
$ POD_NAME=$(kubectl get pods  -lapp=postgres -o jsonpath='{.items[0].metadata.name}')
$ kubectl port-forward $POD_NAME 5432:5432
```

#### Run Dev Mode

```bash
# start in watch mode
nx serve api
# to turn on logging for `request`
NODE_DEBUG=request nx serve api
DEBUG=typeorm:* nx serve api
# optionally you can run with prod env(environment.prod.ts) for tesrting! use this for testing only.
nx serve api --prod
```

#### Run Prod Mode

```bash
# clean dist first
npx rimraf dist
# then build
ng build api --prod
# then run
node dist/apps/api/main.js
```

**API URL:** <http://localhost:3000>

**Swagger Docs URL:** <http://localhost:3000/docs>

### Build

> build for production env

```bash
ng build api --prod
# or
yarn workspace @ngx-starter-kit/api build:prod
```

### Commands

```bash
# to run custom commands, `check api:typeorm in angular.json`
ng run api:typeorm
```

### Generate

> scaffolding nest artifacts \
> &#x20;For more details checkout [playbook](https://github.com/xmlking/ngx-starter-kit/tree/1e3789558d629f2004b2c6f2716ecc3c48fc0934/PLAYBOOK-NEST.md)

```bash
# check of nest installed
nest info

# scaffold auth module
nest g module app/auth  --dry-run
nest g controller auth app/auth --flat --dry-run
nest g service auth app/auth --flat --dry-run
nest g class user.entity app/auth --no-spec --dry-run
nest g class auth.exception app/auth --no-spec --dry-run
```

### Unit Test

> coverage will be generate in coverage/apps/api

```bash
# unit test only changed
ng test api --onlyChanged
# generate coverage
ng test api --codeCoverage
# test in CI env
ng test api --runInBand
```

### E2E Test

```bash
ng e2e api
# e2e test with watch mode
ng e2e api --watch
# e2e test in CI env
ng e2e api --forceExit --detectOpenHandles
```

### Health

> check API App health

#### local

<http://localhost:3000/health/live> <http://localhost:3000/health/ready>

#### prod

<https://api.kashmora.com/live> <https://api.kashmora.com/ready>

### Reference

* Awesome Nest
  * <https://github.com/juliandavidmr/awesome-nest>
* NestJS Samples
  * <https://github.com/nestjs/nest/tree/master/sample>
* TypeORL Samples
  * <https://github.com/typeorm/typeorm/tree/master/sample>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xmlking.gitbook.io/ngx-starter-kit/develop-3/modules/apps/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
