Unverified Commit bad14484 authored by Laura Beatris's avatar Laura Beatris Committed by GitHub

Style improvements on MongoDB example (#20515)

I've looked at the example code and saw some consistent issues related to code style. The changes applied to this PR fixes the following points:

- Differences of line breaks styles between multiple files
- Differences of if statements styles
- Unnecessary comment
- A typo on a JSDocs

---

There were line breaks between statements on `pages/index.js`
````
export async function getServerSideProps(context) {
  const { client } = await connectToDatabase()

  const isConnected = await client.isConnected() 

  return {
    props: { isConnected },
  }
}
```` 

And this wasn't being applied to the MongoDB utility:

````
export async function connectToDatabase() {
  if (cached.conn) return cached.conn
  if (!cached.promise) {
    const conn = {}
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
{...}
````

And also, as shown in the snippet above, there are different styles of if statements being used. 

With that being said, the reason I made this PR is because I think that this kind of inconsistent arises questions when a contributor looks to the codebase, even if this is a simple example. 
parent 27bb24fe
...@@ -225,7 +225,7 @@ export default function Home({ isConnected }) { ...@@ -225,7 +225,7 @@ export default function Home({ isConnected }) {
export async function getServerSideProps(context) { export async function getServerSideProps(context) {
const { client } = await connectToDatabase() const { client } = await connectToDatabase()
const isConnected = await client.isConnected() // Returns true or false const isConnected = await client.isConnected()
return { return {
props: { isConnected }, props: { isConnected },
......
...@@ -16,30 +16,42 @@ if (!MONGODB_DB) { ...@@ -16,30 +16,42 @@ if (!MONGODB_DB) {
/** /**
* Global is used here to maintain a cached connection across hot reloads * Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentiatlly * in development. This prevents connections growing exponentially
* during API Route usage. * during API Route usage.
*/ */
let cached = global.mongo let cached = global.mongo
if (!cached) cached = global.mongo = {}
if (!cached) {
cached = global.mongo = {}
}
export async function connectToDatabase() { export async function connectToDatabase() {
if (cached.conn) return cached.conn if (cached.conn) {
return cached.conn
}
if (!cached.promise) { if (!cached.promise) {
const conn = {} const conn = {}
const opts = { const opts = {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
} }
cached.promise = MongoClient.connect(MONGODB_URI, opts) cached.promise = MongoClient.connect(MONGODB_URI, opts)
.then((client) => { .then((client) => {
conn.client = client conn.client = client
return client.db(MONGODB_DB) return client.db(MONGODB_DB)
}) })
.then((db) => { .then((db) => {
conn.db = db conn.db = db
cached.conn = conn cached.conn = conn
}) })
} }
await cached.promise await cached.promise
return cached.conn return cached.conn
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment