Skip to content

Commit

Permalink
NEWRELIC-7888: (nestjs) add a Nest.js example app
Browse files Browse the repository at this point in the history
This is a basic Nest.js app using Prisma for the database and Winston
for logging. It exercises several features of Nest.js like
interceptors and guards. Like other examples here, it can be run in
Docker.

Optionally, via  NES_USE_FASTIFY envvar, it may use Fastify, but it
defaults to Express, as that's Nest.js's default.
  • Loading branch information
Jordi Gutiérrez Hermoso committed Apr 19, 2023
1 parent 32eff05 commit c5fe42f
Show file tree
Hide file tree
Showing 32 changed files with 10,111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ newrelic_agent.log
.idea
dist
**/*.log

# For custom environment
.env
11 changes: 11 additions & 0 deletions nestjs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:lts-slim
EXPOSE 3000

RUN apt-get update && apt-get install httpie jq -y

WORKDIR /app
COPY app .
COPY entrypoint.sh /entrypoint.sh
RUN npm ci

ENTRYPOINT ["/entrypoint.sh"]
7 changes: 7 additions & 0 deletions nestjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is an example of an instrumented Nest.js app, using Prisma and Winston.

Get it running:

1. Copy sample.env to .env
2. Edit .env to add your New Relic ingest key, and any other desired changes.
3. Start with `docker-compose up --build`.
25 changes: 25 additions & 0 deletions nestjs/app/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
28 changes: 28 additions & 0 deletions nestjs/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# compiled output
/dist
/node_modules

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
4 changes: 4 additions & 0 deletions nestjs/app/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
39 changes: 39 additions & 0 deletions nestjs/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.

## Installation

```bash
$ npm install
```

## Running the app

```bash
# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod
```

## Test

```bash
# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov
```

## License

Nest is [MIT licensed](LICENSE).
51 changes: 51 additions & 0 deletions nestjs/app/make-requests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
shopt -s expand_aliases

# https://stackoverflow.com/a/62087619/11352427
randomString() {
local length=$1
LC_ALL=C tr -dc A-Za-z0-9 </dev/urandom | head -c $length
}

# Make sure httpie can work in a script
alias http='http --print b --ignore-stdin'
beauty="CatsArePretty"

echo "Starting request loop"
while true; do
name=$(randomString 10)
email=${name}@example.com
echo "Creating a user"
http post http://localhost:3000/user name=${name} email=${email} | tee json | jq -C
user_id=$(jq .id json)

echo "Say hello"
http get http://localhost:3000/

echo "Show this user"
http get http://localhost:3000/user/${user_id} | jq -C

echo "Creating two cats"
http post http://localhost:3000/cat name=$(randomString 20) birthdate=2023-04-01 ownerEmail=${email} breed=${beauty} | tee json | jq -C
cat_id_1=$(jq .id json)
http post http://localhost:3000/cat name=$(randomString 20) birthdate=2023-04-01 ownerEmail=${email} breed=${beauty} | tee json | jq -C
cat_id_2=$(jq .id json)

echo "Find the cats"
http get http://localhost:3000/filtered-cats/${beauty} | jq -C

echo "Find the cats, but rudely, get denied access"
http get http://localhost:3000/filtered-cats/${beauty}?please=no | jq -C

echo "Find a cat but wrongly, trigger a Nest.js error"
http get http://localhost:3000/cat/felix | jq -C

echo "Delete the cats"
http delete http://localhost:3000/cat/${cat_id_1} | jq -C
http delete http://localhost:3000/cat/${cat_id_2} | jq -C
sleep 5

echo "Delete the user"
http delete http://localhost:3000/user/${user_id} | jq -C
sleep 5
done
8 changes: 8 additions & 0 deletions nestjs/app/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
Loading

0 comments on commit c5fe42f

Please sign in to comment.