Skip to content

Commit

Permalink
Style improvements on MongoDB example (vercel#20515)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LauraBeatris authored Dec 27, 2020
1 parent 27bb24f commit bad1448
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/with-mongodb/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export default function Home({ isConnected }) {
export async function getServerSideProps(context) {
const { client } = await connectToDatabase()

const isConnected = await client.isConnected() // Returns true or false
const isConnected = await client.isConnected()

return {
props: { isConnected },
Expand Down
18 changes: 15 additions & 3 deletions examples/with-mongodb/util/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,42 @@ if (!MONGODB_DB) {

/**
* 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.
*/
let cached = global.mongo
if (!cached) cached = global.mongo = {}

if (!cached) {
cached = global.mongo = {}
}

export async function connectToDatabase() {
if (cached.conn) return cached.conn
if (cached.conn) {
return cached.conn
}

if (!cached.promise) {
const conn = {}

const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
}

cached.promise = MongoClient.connect(MONGODB_URI, opts)
.then((client) => {
conn.client = client

return client.db(MONGODB_DB)
})
.then((db) => {
conn.db = db

cached.conn = conn
})
}

await cached.promise

return cached.conn
}

0 comments on commit bad1448

Please sign in to comment.