-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d03f5
commit d814311
Showing
11 changed files
with
323 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
NEXT_PUBLIC_GRAPHQL_URI="http://localhost:3001" | ||
NEXT_PUBLIC_GRAPHQL_URI="http://localhost:3001" | ||
MONGO_URI="mongodb://localhost:27017/vns" | ||
APOLLO_SERVER_CORS_WHITELIST="http://localhost:3000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
describe("connect to mongo", () => { | ||
it("get the best restaurants in town", () => { | ||
cy.visit("/vns/debug/mongo"); | ||
cy.get("li").should("have.length", 5); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* This middleware relies on Express | ||
* | ||
* @see https://developer.mongodb.com/how-to/nextjs-building-modern-applications | ||
* @see https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/ | ||
* @see https://github.com/vercel/next.js/discussions/12229 | ||
*/ | ||
|
||
import { Request, Response } from "express"; | ||
import mongoose from "mongoose"; | ||
// Import mongoose models here | ||
import "~/api/mongoose/models"; | ||
|
||
/* | ||
async function closeDbConnection() { | ||
try { | ||
await mongoose.connection.close(); | ||
} catch (err) { | ||
// Catch locally error | ||
} | ||
} | ||
*/ | ||
|
||
import debug from "debug"; | ||
const debugMongo = debug("vns:mongo"); | ||
|
||
// trigger the initial connection on app startup | ||
export const connectToDb = async () => { | ||
if (![1, 2].includes(mongoose.connection.readyState)) { | ||
debugMongo("Call mongoose connect"); | ||
return await mongoose.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
}); | ||
} | ||
debugMongo("Ran connectToDb, already connected or connecting to Mongo"); | ||
}; | ||
|
||
const mongoConnectionMiddleware = () => { | ||
// init the first database connection on server startup | ||
connectToDb(); | ||
// mongoose.set("useFindAndModify", false); | ||
|
||
// then return a middleware that checks the connection on every call | ||
return async (req: Request, res: Response, next) => { | ||
try { | ||
// To debug the number of connections in Mongo client: db.serverStatus().connections | ||
await connectToDb(); | ||
|
||
// Do not forget to close connection on finish and close events | ||
// NOTE: actually we don't need this. Db connection close should happen on lambda destruction instead. | ||
// res.on("finish", closeDbConnection); | ||
// res.on("close", closeDbConnection); | ||
next(); | ||
} catch (err) { | ||
res.status(500); | ||
res.send("Could not connect to db"); | ||
} | ||
}; | ||
}; | ||
|
||
export default mongoConnectionMiddleware; | ||
|
||
// We need to add a converter between Mongoose ID and Apollo Server | ||
// @see https://github.com/apollographql/apollo-server/issues/1633 | ||
const ObjectId = require("mongoose").Types.ObjectId; | ||
ObjectId.prototype.valueOf = function () { | ||
return this.toString(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Add your models here | ||
import "mongoose"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useQuery } from "@apollo/react-hooks"; | ||
import gql from "graphql-tag"; | ||
import { withApollo } from "@vulcan/next-apollo"; | ||
const MongoDebugPage = () => { | ||
const { data, loading, error } = useQuery( | ||
gql` | ||
query restaurants { | ||
restaurants { | ||
_id | ||
name | ||
} | ||
} | ||
` | ||
); | ||
if (loading) return <>Thinking about restaurants...</>; // NOTE: in TypeScript with React we can't return just a string | ||
if (error) | ||
return ( | ||
<div> | ||
I could not remember any restaurant, sorry... Error: {error.message} | ||
</div> | ||
); | ||
|
||
const { restaurants } = data; | ||
|
||
return ( | ||
<div> | ||
<h2>First 5 restaurants that comes to my mind:</h2> | ||
<ul> | ||
{restaurants.map(({ _id, name }) => ( | ||
<li key={_id}>{name}</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
export default withApollo(MongoDebugPage, { | ||
graphqlUri: "http://localhost:3000/api/graphql", | ||
}); |
Oops, something went wrong.