
Security News
Lovable’s OJ Rewrites Vite’s Dev Server in Rust as AI Lowers the Cost of Forking Open Source
Lovable’s OJ rewrites Vite’s dev server in Rust, reducing memory use and preview times as AI lowers the cost of open source reimplementation.
mongoose-ts-converter
Advanced tools
A library that converts Mongoose models from Node.js codebase to TypeScript schema
Mongoose TypeScript Converter is a lightweight Node.js library designed to simplify Mongoose-to-TypeScript schema conversions. It allows developers to automatically generate TypeScript interfaces from Mongoose schemas, ensuring type safety and saving development time.
Install the package via npm:
npm install mongoose-ts-converter
Or with yarn:
yarn add mongoose-ts-converter
Programmatic API
const { convertToTS } = require("mongoose-ts-converter")
const mongoose = require("mongoose")
const schema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
})
convertToTS(schema, "User").then((tsInterface) => {
console.log(tsInterface)
})
Run the following command to generate TypeScript interfaces from Mongoose models in your project:
npx mongoose-ts-converter --models ./path/to/models --output ./path/to/output
-m, --models <dir> - Directory containing your Mongoose model files
-o, --output <dir> - Directory to output the generated TypeScript files
--serve Start a documentation server after generating schemas
--port <number> Specify the port for the documentation server (default: 3000)
Given a Mongoose model in the models directory:
// models/User.js
const mongoose = require("mongoose")
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
})
module.exports = mongoose.model("User", userSchema)
Running the CLI will generate a TypeScript file in the output directory:
// output/User.d.ts
export interface User {
username: string
email: string
}
To start a documentation server that serves the generated TypeScript schemas, add the --serve option:
npx mongoose-ts-converter --models ./path/to/models --output ./path/to/output --serve
Specify a custom port for the documentation server (default is 3000):
npx mongoose-ts-converter --models ./path/to/models --output ./path/to/output --serve --port 4000
After running the command, open your browser and go to:
http://localhost:<specified-port>
If you want to serve the documentation for generated TypeScript schemas as part of an existing Express application, you can use the docsRouter middleware:
Example
// app.js
const express = require("express");
const { docsRouter } = require("mongoose-ts-converter");
const app = express();
const PORT = 3000;
// Serve TypeScript documentation
app.use("/api-docs", docsRouter("./path/to/output"));
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
In this setup
The createDocsRouter middleware also provides a JSON endpoint, which can be useful for frontend applications that need to access schema documentation programmatically.
Once configured, the following endpoint will be available:
Example JSON Response
{
"schemas": [
{
"file": "User",
"content": "export interface User { username: string; email: string; }"
},
{
"file": "Product",
"content": "export interface Product { name: string; price: number; }"
}
]
}
With this JSON API, you can easily fetch and display the generated TypeScript schemas on a frontend application or another service, enabling seamless access to your documentation.
This project is licensed under the MIT License.
For issues, questions, or contributions, please reach out to edekobifrank@gmail.com
FAQs
A library that converts Mongoose models from Node.js codebase to TypeScript schema
We found that mongoose-ts-converter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.

Security News
Lovable’s OJ rewrites Vite’s dev server in Rust, reducing memory use and preview times as AI lowers the cost of open source reimplementation.

Security News
It has been one year since Shai-Hulud made its first appearance on npm.

Research
/Security News
Operators behind PolinRider used a compromised GitHub account to plant malware in four development versions of a Packagist package with 700,000+ downloads.