File tree Expand file tree Collapse file tree 5 files changed +21
-35
lines changed Expand file tree Collapse file tree 5 files changed +21
-35
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ To add the MCP server to Claude Desktop:
34
34
],
35
35
"env" : {
36
36
"MONGODB_URI" : " " ,
37
- "SCHEMA_PATH" :
" schemas >
"
37
+ "SCHEMA_PATH" :
" schema objects >
"
38
38
}
39
39
}
40
40
}
@@ -89,25 +89,19 @@ npx @modelcontextprotocol/inspector node dist/index.js
89
89
90
90
### Creating Mongoose Schemas
91
91
92
- Place your Mongoose schema files in the a directory and specify that path in SCHEMA_PATH var
93
- Make sure mongoose npm package is installed globally or within that path
92
+ Place your Mongoose schema object files in the a directory and specify that path in SCHEMA_PATH var
93
+ Make sure file names reflect the collection name
94
94
95
95
``` javascript
96
- // models/user.js
97
- import mongoose from ' mongoose' ;
98
-
99
- const userSchema = new mongoose.Schema ({
96
+ // models/users.js (for users collection)
97
+ export default {
100
98
name: { type: String , required: true },
101
99
email: { type: String , required: true , unique: true },
102
100
age: Number ,
103
101
createdAt: { type: Date , default: Date .now },
104
102
isDeleted: { type: Boolean , default: false },
105
103
deletedAt: Date
106
- });
107
-
108
- const User = mongoose .model (' User' , userSchema);
109
-
110
- export default User ;
104
+ };
111
105
```
112
106
113
107
## How It Works
Original file line number Diff line number Diff line change 1
- import mongoose from 'mongoose' ;
2
-
3
- const productSchema = new mongoose . Schema ( {
1
+ export default {
4
2
name : { type : String , required : true } ,
5
3
price : { type : Number , required : true } ,
6
4
description : String ,
@@ -9,9 +7,4 @@ const productSchema = new mongoose.Schema({
9
7
createdAt : { type : Date , default : Date . now } ,
10
8
isDeleted : { type : Boolean , default : false } ,
11
9
deletedAt : Date
12
- } ) ;
13
-
14
-
15
- const Product = mongoose . model ( 'product' , productSchema ) ;
16
-
17
- export default Product ;
10
+ } ;
Original file line number Diff line number Diff line change 1
- import mongoose from 'mongoose' ;
2
-
3
- const userSchema = new mongoose . Schema ( {
1
+ export default {
4
2
name : { type : String , required : true } ,
5
3
email : { type : String , required : true , unique : true } ,
6
4
age : Number ,
7
5
createdAt : { type : Date , default : Date . now } ,
8
6
isDeleted : { type : Boolean , default : false } ,
9
7
deletedAt : Date
10
- } ) ;
11
-
12
- const User = mongoose . model ( 'user' , userSchema ) ;
13
-
14
- export default User ;
8
+ } ;
Original file line number Diff line number Diff line change @@ -111,11 +111,13 @@ class MongoDBClient {
111
111
try {
112
112
console . error ( `Attempting to load schema file: ${ file } ` ) ;
113
113
const moduleImport = require ( file ) ;
114
- const schema = moduleImport . default ;
114
+ const schemaObject = moduleImport . default ;
115
+ const fileName = path . basename ( file , path . extname ( file ) ) ;
115
116
116
- if ( schema && schema . modelName ) {
117
- this . models [ schema . modelName . toLowerCase ( ) ] = schema ;
118
- console . error ( `Successfully loaded schema for: ${ schema . modelName } ` ) ;
117
+ if ( schemaObject ) {
118
+ const schema = new mongoose . Schema ( schemaObject ) ;
119
+ this . models [ fileName . toLowerCase ( ) ] = mongoose . model ( fileName , schema ) ;
120
+ console . error ( `Successfully loaded schema for: ${ fileName } ` ) ;
119
121
} else {
120
122
console . error ( `Schema file ${ file } does not export a valid model` ) ;
121
123
}
Original file line number Diff line number Diff line change 1
- import mongoose from "mongoose" ;
1
+ import mongoose , { model , Schema } from "mongoose" ;
2
2
import { glob } from "glob" ;
3
3
import path from "path" ;
4
4
@@ -58,6 +58,7 @@ export async function loadSchemas(schemaPath: string) {
58
58
// Format the file path for dynamic import
59
59
// For files on disk, we need to use the file:// protocol
60
60
const fileUrl = `file://${ file } ` ;
61
+ const fileNameWithoutExtension = path . basename ( file , path . extname ( file ) ) ;
61
62
62
63
// Dynamically import the schema file
63
64
const module = await import ( fileUrl ) . catch ( err => {
@@ -68,7 +69,9 @@ export async function loadSchemas(schemaPath: string) {
68
69
if ( ! module ) continue ;
69
70
70
71
// Get the model (either default export or module itself)
71
- const model = module . default || module ;
72
+ const schemaObject = module . default || module ;
73
+ const schema = new Schema ( schemaObject )
74
+ const model = mongoose . model ( fileNameWithoutExtension , schema )
72
75
73
76
// Add some debug info
74
77
console . error ( `Module loaded from ${ file } :` , {
You can’t perform that action at this time.
0 commit comments