# mongo-to-mongoose-schema

> A TypeScript package that generates Nest.js/Mongoose schemas from existing MongoDB collections.

Latest version **1.0.10** (published 2024-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongo-to-mongoose-schema
pnpm add mongo-to-mongoose-schema
yarn add mongo-to-mongoose-schema
bun add mongo-to-mongoose-schema
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2024-03-19 |
| First published | 2024-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | S0T12 |
| Maintainers | s0t12 |
| Keywords | mongodb, mongoose, nest.js, schema, codegen, typescript |

## Links

- npm: https://www.npmjs.com/package/mongo-to-mongoose-schema
- Repository: https://github.com/S0T12/mongo-to-mongoose-schema
- Homepage: https://github.com/S0T12/mongo-to-mongoose-schema#readme
- Issues: https://github.com/S0T12/mongo-to-mongoose-schema/issues
- npm.io page: https://npm.io/package/mongo-to-mongoose-schema

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^6.8.3

## 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.10 (latest) — 2024-03-19
- 1.0.9 — 2024-03-19
- 1.0.8 — 2024-03-18
- 1.0.7 — 2024-03-16
- 1.0.6 — 2024-03-14
- 1.0.5 — 2024-03-14
- 1.0.4 — 2024-03-14
- 1.0.3 — 2024-03-13
- 1.0.2 — 2024-03-13
- 1.0.1 — 2024-03-13
- 1.0.0 — 2024-03-13

## README

# mongo-to-mongoose-schema

A TypeScript package that generates Nest.js/Mongoose schemas from existing MongoDB collections.

## Installation

```bash
npm install mongo-to-mongoose-schema
```

## Usage

```typescript
import { generateMongooseSchemas } from 'mongo-to-mongoose-schema';

generateMongooseSchemas('mongodb://localhost/your-database', './schemas');
```

This will generate TypeScript schema files for all non-empty collections in the `your-database` database and place them in the `./schemas` directory.

## How it works

1. The package connects to the provided MongoDB database URI.
2. It retrieves a list of all collections, excluding system collections.
3. For each non-empty collection:
   - It fetches a sample document from the collection.
   - It infers the schema structure based on the sample document's properties and data types.
   - It generates a TypeScript schema file for the Nest.js/Mongoose model, using the inferred schema structure.

## Generated Schema File

The generated schema file will have the following structure:

```typescript
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';

@Schema({ collection: 'your_collection_name' })
export class YourCollectionName extends Document {
  @Prop()
  stringProperty: string;

  @Prop()
  numberProperty: number;

  @Prop()
  booleanProperty: boolean;

  @Prop()
  dateProperty: Date;

  @Prop()
  objectIdProperty: Types.ObjectId;

  @Prop()
  arrayProperty: [{ nestedProperty: string }];

  @Prop()
  nestedObjectProperty: { nestedProperty: string };

  // ... other properties
}

export const YourCollectionNameSchema = SchemaFactory.createForClass(YourCollectionName);
```

The package infers the appropriate types for properties based on the sample document's data. It supports common types like `string`, `number`, `boolean`, `Date`, `Types.ObjectId`, arrays, and nested objects up to a maximum depth of 5 levels.

You can then import and use the generated schema in your Nest.js application.

## Configuration

The `generateMongooseSchemas` function accepts two arguments:

1. `databaseUri` (string): The MongoDB connection URI.
2. `outputDir` (string): The output directory where the schema files will be generated.

## Limitations

- The package generates schemas based on a single sample document from each collection. If the documents in a collection have varying structures, the generated schema may not accurately represent all possible data shapes.
- Nested objects and arrays are supported up to a maximum depth of 5 levels to avoid potential circular references. This depth can be adjusted in the `utils.ts` file by modifying the `MAX_DEPTH` constant.

## Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests on the [GitHub repository](https://github.com/S0T12/mongo-to-mongoose-schema).

## License

This project is licensed under the [MIT License](LICENSE).

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