Last active
November 11, 2025 08:24
-
-
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; |
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 so much for this!