# mongoose-ts-converter

> A library that converts Mongoose models from Node.js codebase to TypeScript schema

Latest version **1.0.18** (published 2024-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-ts-converter
pnpm add mongoose-ts-converter
yarn add mongoose-ts-converter
bun add mongoose-ts-converter
```

Provides the command `mongoose-ts-converter`.

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.18 |
| Published | 2024-11-05 |
| First published | 2024-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 23.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Franklyn Edekobi |
| Maintainers | qobi |
| Keywords | mongoose, typescript, schema, converter |

## Links

- npm: https://www.npmjs.com/package/mongoose-ts-converter
- npm.io page: https://npm.io/package/mongoose-ts-converter

## Dependencies (6)

- [express](https://npm.io/package/express.md) ^4.21.1
- [mongoose](https://npm.io/package/mongoose.md) ^8.7.2
- [commander](https://npm.io/package/commander.md) ^10.0.0
- [supertest](https://npm.io/package/supertest.md) ^7.0.0
- [@types/mongoose](https://npm.io/package/@types/mongoose.md) ^5.11.96
- [json-schema-to-typescript](https://npm.io/package/json-schema-to-typescript.md) ^15.0.2

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.18 (latest) — 2024-11-05
- 1.0.13 — 2024-10-30
- 1.0.12 — 2024-10-30
- 1.0.11 — 2024-10-30
- 1.0.10 — 2024-10-29
- 1.0.9 — 2024-10-29
- 1.0.8 — 2024-10-29
- 1.0.7 — 2024-10-29
- 1.0.6 — 2024-10-29
- 1.0.5 — 2024-10-29
- 1.0.4 — 2024-10-29
- 1.0.3 — 2024-10-29

## README

# Mongoose TypeScript Converter

[![1.0.0](https://badge.fury.io/js/mongoose-ts-converter.svg)](https://badge.fury.io/js/mongoose-ts-converter)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

## Description

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.

## Features

- Converts Mongoose schemas to TypeScript interfaces
- Supports complex data types, including nested objects and arrays
- CLI support for easy integration in projects
- Option to start a documentation server to view generated types
- Middleware support for serving documentation in an existing Express app

## Installation

Install the package via npm:

```bash
npm install mongoose-ts-converter
```

Or with yarn:

```bash
yarn add mongoose-ts-converter
```

## Usage

Programmatic API

```bash
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)
})
```

## CLI Usage

Run the following command to generate TypeScript interfaces from Mongoose models in your project:

```bash
npx mongoose-ts-converter --models ./path/to/models --output ./path/to/output
```

## CLI Options

```bash
-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)
```

## Example

Given a Mongoose model in the models directory:

```bash
// 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:

```bash
// output/User.d.ts
export interface User {
  username: string
  email: string
}
```

## Starting the Documentation Server

To start a documentation server that serves the generated TypeScript schemas, add the --serve option:

```bash
npx mongoose-ts-converter --models ./path/to/models --output ./path/to/output --serve
```

Specify a custom port for the documentation server (default is 3000):

```bash
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:

```bash
http://localhost:<specified-port>
```

## Use as Middleware

If you want to serve the documentation for generated TypeScript schemas as part of an existing Express application, you can use the docsRouter middleware:

### Setup

1. First, generate the TypeScript schemas using either the CLI or convertToTS function and specify the output directory.
2. Include docsRouter from mongoose-ts-converter in your Express app, specifying the directory where the generated TypeScript files are stored.

Example

```bash
// 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

- Replace ./path/to/output with the actual path to the directory where TypeScript files are generated.
- Access your documentation at http://localhost:3000/api-docs.

## JSON API for Schema Documentation

The createDocsRouter middleware also provides a JSON endpoint, which can be useful for frontend applications that need to access schema documentation programmatically.

### JSON Endpoint

Once configured, the following endpoint will be available:

- GET /api-docs/json: Returns a JSON array of objects, each representing a TypeScript schema file with its filename (without extension) and content.

Example JSON Response

```bash
{
  "schemas": [
    {
      "file": "User",
      "content": "export interface User { username: string; email: string; }"
    },
    {
      "file": "Product",
      "content": "export interface Product { name: string; price: number; }"
    }
  ]
}
```

### Frontend Integration

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.

## License

This project is licensed under the MIT License.

## Contact

For issues, questions, or contributions, please reach out to edekobifrank@gmail.com

---
_Source: https://npm.io/package/mongoose-ts-converter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
