Last active
December 3, 2024 07:49
-
-
Save rashidmya/2c075330e636134f00ebe85fbb88fed8 to your computer and use it in GitHub Desktop.
nextjs-typescript-mongoose example
This file contains hidden or 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
import { Mongoose } from 'mongoose'; | |
/* eslint-disable no-var */ | |
declare global { | |
var mongoose: { | |
promise: Promise<Mongoose> | null; | |
conn: Mongoose | null; | |
}; | |
} |
This file contains hidden or 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
import { models, model, Schema } from 'mongoose'; | |
const UserSchema: Schema = new Schema({ | |
email: { | |
type: String, | |
required: true, | |
unique: true, | |
}, | |
password: { | |
type: String, | |
required: true, | |
}, | |
displayName: { | |
type: String, | |
required: true, | |
}, | |
role: { | |
type: String | |
} | |
}); | |
const UserModel = models.User || model('User', UserSchema); | |
export default UserModel |
This file contains hidden or 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
import dbConnect from '@/utils/mongodb'; | |
import UserModel from '@/models/user.model'; | |
// ---------------------------------------------------------------------- | |
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | |
try { | |
dbConnect(); | |
const users = UserModel; | |
const allUsers = await users.find({}); | |
res.status(200).json({ users: allUsers }); | |
} catch (error) { | |
console.error(error); | |
res.status(500).json({ message: 'Internal server error' }); | |
} | |
} |
This file contains hidden or 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
import mongoose from 'mongoose'; | |
const { MONGODB_URI, MONGODB_DB } = process.env; | |
if (!MONGODB_URI) throw new Error('MONGODB_URI not defined'); | |
if (!MONGODB_DB) throw new Error('MONGODB_DB not defined'); | |
let cached = global.mongoose | |
if (!cached) { | |
cached = global.mongoose = {conn: null, promise: null} | |
} | |
async function dbConnect() { | |
if (cached.conn) return cached.conn; | |
if (!cached.promise) { | |
cached.promise = mongoose.connect(`${MONGODB_URI}/${MONGODB_DB}`).then(mongoose => mongoose) | |
} | |
cached.conn = await cached.promise; | |
return cached.conn | |
} | |
export default dbConnect; |
i'm not getting typesafety / flowthrough from this
Great! Solve "OverwriteModelError" error in next.js with typescript~
Thank you so much for this!
Thank you for your effort
thanks
Thanks, It did worked for my project
Thanks !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for posting your code!
I was thinking you could slim this down a bit by using
await
in your connect function unless there is some reason you can't?As an example, you could do the following in your
mongodb.ts
file:Then when you declae your type you wouldn't need the promise:
Is there any reason you could not do this?