npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-to-mongoose-schema

v1.0.10

Published

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

Downloads

5

Readme

mongo-to-mongoose-schema

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

Installation

npm install mongo-to-mongoose-schema

Usage

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:

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.

License

This project is licensed under the MIT License.