Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated Getting-Started.md - added more detail making the process easier to understand #2352

Merged
merged 19 commits into from
Jun 28, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 70 additions & 38 deletions docs/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,59 +131,91 @@ npm install --save fastify-plugin

**server.js**
```js
const fastify = require('fastify')({
logger: true
})
'use strict'

fastify.register(require('./our-db-connector'), {
url: 'mongodb://localhost:27017/'
})
fastify.register(require('./our-first-route'))
const fastify = require('fastify')({logger: true})
const database = require('./database')
const routes = require('./routes')

fastify.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})
const start = async () => {
fastify.register(database)
fastify.register(routes)

try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}

start()
```

**our-db-connector.js**
**database.js**
```js
'use strict'

/*
- In a terminal start up mongodb

- create the test database
> use test_database

- create collection
> db.createCollection('test_collection')

- insert a row
db.test_collection.insert({name: 'John Smith'})

- show the newly inserted row and copy the _id string
> db.test_collection.find().pretty()

- use the _id string in the url example localhost:3000/search/5ef583456f8921125bb83b20
*/

const fastifyPlugin = require('fastify-plugin')
const MongoClient = require('mongodb').MongoClient

async function dbConnector (fastify, options) {
const url = options.url
delete options.url
async function dbConnector(fastify, options) {
sojohnnysaid marked this conversation as resolved.
Show resolved Hide resolved
const { ObjectId, MongoClient } = require('mongodb')
sojohnnysaid marked this conversation as resolved.
Show resolved Hide resolved
const assert = require('assert')
const url = 'mongodb://localhost:27017'

const dbName = 'test_database'

const db = await MongoClient.connect(url, options)
fastify.decorate('mongo', db)
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
})

const db = client.db(dbName)
fastify.decorate('mongo', db)
fastify.decorate('ObjectId', ObjectId)
}

// Wrapping a plugin function with fastify-plugin exposes the decorators
// and hooks, declared inside the plugin to the parent scope.
module.exports = fastifyPlugin(dbConnector)

```

**our-first-route.js**
**routes.js**
```js
async function routes (fastify, options) {
const database = fastify.mongo.db('db')
const collection = database.collection('test')

fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})
'use strict'

fastify.get('/search/:id', async (request, reply) => {
const result = await collection.findOne({ id: request.params.id })
if (result.value === null) {
throw new Error('Invalid value')
}
return result.value
})
async function routes(fastify, options) {
const collection = fastify.mongo.collection('test_collection')
fastify.route({
method: 'GET',
url: '/search/:id',
handler: async (request, reply) => {
try {
const _id = fastify.ObjectId(request.params.id)
const result = await collection.findOne({ _id })
return result
} catch (error) {
throw new Error('Invalid value')
}
}
})
}

module.exports = routes
Expand Down