Skip to content

Commit 6935094

Browse files
committed
chore: added mongoose example app
1 parent 3d0a5ed commit 6935094

File tree

8 files changed

+129
-0
lines changed

8 files changed

+129
-0
lines changed

mongoose/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Mongoose App
2+
3+
This application demonstrates mongoose with the Node.js agent.
4+
5+
## Setup
6+
```sh
7+
docker compose up -d
8+
npm install
9+
# Seed some data
10+
npm run seed
11+
12+
cp env.sample .env
13+
# Fill out out `NEW_RELIC_LICENSE_KEY` with your ingest key
14+
npm start
15+
```
16+
17+
## Make requests
18+
```sh
19+
curl http://localhost:3000/mongoose
20+
```

mongoose/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const CONN_STRING = 'mongodb://localhost:27019'
2+
3+
module.exports = {
4+
CONN_STRING
5+
}

mongoose/docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
mongodb:
3+
container_name: nr_test_mongodb
4+
image: library/mongo:5
5+
ports:
6+
- "27019:27017"
7+
healthcheck:
8+
test: ["CMD", "mongo", "--quiet"]
9+
interval: 1s
10+
timeout: 10s
11+
retries: 30
12+

mongoose/env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NEW_RELIC_LICENSE_KEY=<your-license-key>
2+
NEW_RELIC_APP_NAME=mongoose-example-app
3+
# Uncomment for NR employees
4+
#NEW_RELIC_HOST=staging-collector.newrelic.com

mongoose/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fastify = require('fastify')({ logger: true })
2+
const { PORT: port = 3000, HOST: host = '127.0.0.1' } = process.env
3+
const model = require('./model')
4+
const mongoose = require('mongoose')
5+
const { CONN_STRING } = require('./constants')
6+
7+
fastify.listen({ host, port }, function (err, address) {
8+
if (err) {
9+
fastify.log.error(err)
10+
process.exit(1)
11+
}
12+
})
13+
14+
fastify.get('/mongoose', async (request, reply ) => {
15+
await mongoose.connect(CONN_STRING)
16+
const data = await model.find({})
17+
const names = data.map((datum) => `${datum.author} ${datum.title}`)
18+
return reply.send({ names })
19+
})

mongoose/model.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const mongoose = require('mongoose')
2+
const { Schema } = mongoose
3+
const blogSchema = new Schema({
4+
title: String, // String is shorthand for {type: String}
5+
author: String,
6+
body: String,
7+
comments: [{ body: String, date: Date }],
8+
date: { type: Date, default: Date.now },
9+
hidden: Boolean,
10+
meta: {
11+
votes: Number,
12+
favs: Number
13+
}
14+
});
15+
const model = mongoose.model('Blog', blogSchema)
16+
17+
module.exports = model

mongoose/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "mongoose-app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"type": "commonjs",
8+
"main": "index.js",
9+
"scripts": {
10+
"seed": "node seed.js",
11+
"start": "node --env-file .env -r newrelic index.js",
12+
"start:debug": "node --inspect-brk --env-file .env -r newrelic index.js",
13+
"start:no-agent": "node index.js"
14+
},
15+
"dependencies": {
16+
"fastify": "^5.7.4",
17+
"mongoose": "^9.1.5",
18+
"newrelic": "^13.12.0"
19+
},
20+
"devDependencies": {
21+
"@faker-js/faker": "^10.2.0"
22+
}
23+
}

mongoose/seed.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const model = require('./model')
2+
const mongoose = require('mongoose')
3+
const { CONN_STRING } = require('./constants')
4+
const { faker } = require('@faker-js/faker')
5+
6+
7+
async function main() {
8+
const promises = []
9+
await mongoose.connect(CONN_STRING)
10+
for (let i = 0; i < 100; i++) {
11+
const prom = model.create({
12+
title: faker.book.title(),
13+
author: faker.person.firstName() + ' ' + faker.person.lastName(),
14+
body: faker.lorem.paragraphs(5),
15+
comments: [{ body: faker.lorem.sentence(), date: Date.now() }],
16+
hidden: false,
17+
meta: {
18+
votes: 1,
19+
favs: 1
20+
}
21+
})
22+
promises.push(prom)
23+
}
24+
await Promise.all(promises)
25+
console.log('done seeding db')
26+
process.exit(0)
27+
}
28+
29+
main()

0 commit comments

Comments
 (0)