Skip to content

Commit 795bf62

Browse files
committed
expecting schema object instead of mongoose schema
1 parent ee442cc commit 795bf62

File tree

5 files changed

+21
-35
lines changed

5 files changed

+21
-35
lines changed

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To add the MCP server to Claude Desktop:
3434
],
3535
"env": {
3636
"MONGODB_URI": "",
37-
"SCHEMA_PATH" : "schemas>"
37+
"SCHEMA_PATH" : "schema objects>"
3838
}
3939
}
4040
}
@@ -89,25 +89,19 @@ npx @modelcontextprotocol/inspector node dist/index.js
8989

9090
### Creating Mongoose Schemas
9191

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
9494

9595
```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 {
10098
name: { type: String, required: true },
10199
email: { type: String, required: true, unique: true },
102100
age: Number,
103101
createdAt: { type: Date, default: Date.now },
104102
isDeleted: { type: Boolean, default: false },
105103
deletedAt: Date
106-
});
107-
108-
const User = mongoose.model('User', userSchema);
109-
110-
export default User;
104+
};
111105
```
112106

113107
## How It Works
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import mongoose from 'mongoose';
2-
3-
const productSchema = new mongoose.Schema({
1+
export default {
42
name: { type: String, required: true },
53
price: { type: Number, required: true },
64
description: String,
@@ -9,9 +7,4 @@ const productSchema = new mongoose.Schema({
97
createdAt: { type: Date, default: Date.now },
108
isDeleted: { type: Boolean, default: false },
119
deletedAt: Date
12-
});
13-
14-
15-
const Product = mongoose.model('product', productSchema);
16-
17-
export default Product;
10+
};
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import mongoose from 'mongoose';
2-
3-
const userSchema = new mongoose.Schema({
1+
export default {
42
name: { type: String, required: true },
53
email: { type: String, required: true, unique: true },
64
age: Number,
75
createdAt: { type: Date, default: Date.now },
86
isDeleted: { type: Boolean, default: false },
97
deletedAt: Date
10-
});
11-
12-
const User = mongoose.model('user', userSchema);
13-
14-
export default User;
8+
};

src/clients/mongodb-client.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ class MongoDBClient {
111111
try {
112112
console.error(`Attempting to load schema file: ${file}`);
113113
const moduleImport = require(file);
114-
const schema = moduleImport.default;
114+
const schemaObject = moduleImport.default;
115+
const fileName = path.basename(file, path.extname(file));
115116

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}`);
119121
} else {
120122
console.error(`Schema file ${file} does not export a valid model`);
121123
}

src/mongoose/manager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mongoose from "mongoose";
1+
import mongoose, { model, Schema } from "mongoose";
22
import { glob } from "glob";
33
import path from "path";
44

@@ -58,6 +58,7 @@ export async function loadSchemas(schemaPath: string) {
5858
// Format the file path for dynamic import
5959
// For files on disk, we need to use the file:// protocol
6060
const fileUrl = `file://${file}`;
61+
const fileNameWithoutExtension = path.basename(file, path.extname(file));
6162

6263
// Dynamically import the schema file
6364
const module = await import(fileUrl).catch(err => {
@@ -68,7 +69,9 @@ export async function loadSchemas(schemaPath: string) {
6869
if (!module) continue;
6970

7071
// 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)
7275

7376
// Add some debug info
7477
console.error(`Module loaded from ${file}:`, {

0 commit comments

Comments
 (0)