Skip to content

Commit 2833764

Browse files
authored
chore: added mongoose example app (#362)
1 parent 07e9de6 commit 2833764

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
const CONN_STRING = 'mongodb://localhost:27019'
7+
8+
module.exports = {
9+
CONN_STRING
10+
}

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
const fastify = require('fastify')({ logger: true })
7+
const { PORT: port = 3000, HOST: host = '127.0.0.1' } = process.env
8+
const model = require('./model')
9+
const mongoose = require('mongoose')
10+
const { CONN_STRING } = require('./constants')
11+
12+
fastify.listen({ host, port }, function (err, address) {
13+
if (err) {
14+
fastify.log.error(err)
15+
process.exit(1)
16+
}
17+
})
18+
19+
fastify.get('/mongoose', async (request, reply) => {
20+
await mongoose.connect(CONN_STRING)
21+
const data = await model.find({})
22+
const names = data.map((datum) => `${datum.author} ${datum.title}`)
23+
return reply.send({ names })
24+
})

mongoose/model.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
const mongoose = require('mongoose')
7+
const { Schema } = mongoose
8+
const blogSchema = new Schema({
9+
title: String, // String is shorthand for {type: String}
10+
author: String,
11+
body: String,
12+
comments: [{ body: String, date: Date }],
13+
date: { type: Date, default: Date.now },
14+
hidden: Boolean,
15+
meta: {
16+
votes: Number,
17+
favs: Number
18+
}
19+
})
20+
const model = mongoose.model('Blog', blogSchema)
21+
22+
module.exports = model

mongoose/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
"@faker-js/faker": "^10.2.0",
17+
"fastify": "^5.7.4",
18+
"mongoose": "^9.1.5",
19+
"newrelic": "^13.12.0"
20+
}
21+
}

mongoose/seed.js

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

0 commit comments

Comments
 (0)