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

[Docs] Update mongodb example #39658

Merged
merged 2 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { MongoClient } from 'mongodb'

if (!process.env.MONGODB_URI) {
throw new Error('Invalid environment variable: "MONGODB_URI"')
}

const uri = process.env.MONGODB_URI
const options = {}

Expand Down
11 changes: 8 additions & 3 deletions examples/with-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
"start": "next start"
},
"dependencies": {
"mongodb": "^4.1.3",
"mongodb": "^4.8.1",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.5",
"@types/react": "16.9.17",
"typescript": "4.6.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import Head from 'next/head'
import clientPromise from '../lib/mongodb'
import { InferGetServerSidePropsType } from 'next'

export default function Home({ isConnected }) {
export async function getServerSideProps(context) {
try {
await clientPromise
// `await clientPromise` will use the default database passed in the MONGODB_URI
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
//
// `const client = await clientPromise`
// `const db = client.db("myDatabase")`
//
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands

return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}

export default function Home({
isConnected,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return (
<div className="container">
<Head>
Expand Down Expand Up @@ -221,26 +247,3 @@ export default function Home({ isConnected }) {
</div>
)
}

export async function getServerSideProps(context) {
try {
await clientPromise
// `await clientPromise` will use the default database passed in the MONGODB_URI
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
//
// `const client = await clientPromise`
// `const db = client.db("myDatabase")`
//
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands

return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}
20 changes: 20 additions & 0 deletions examples/with-mongodb/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions examples/with-mongodb/types/mongodb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MongoClient } from 'mongodb'

declare global {
var _mongoClientPromise: Promise<MongoClient>
}