francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
103 stars 24 forks source link

Add support for more SchemaTypes #133

Closed dmchoiboi closed 9 months ago

dmchoiboi commented 9 months ago

Parser is throwing errors when parsing schemas that use mongoose.Schema.Types.* as a types.

This change updates the parser to handle schemas defined like

const addressSchema = new mongoose.Schema(
  {
    city: {
      type: mongoose.Schema.Types.String,
      required: true,
    },
    coordinates: [{ lat: Number, long: Number }]
  },
  { _id: false }
);

const anotherSchema = new mongoose.Schema(
  {
    info: {
      type: mongoose.Schema.Types.String,
      required: true,
    },
    creator: String,
    time: Number
  }
);

const User2Schema: User2Schema = new mongoose.Schema(
  {
    _id: {
      type: mongoose.Schema.Types.Number,
      required: true,
    },
    address: {
      type: addressSchema,
      // this test ensures the required property here is projected properly by `processChild` in `src/helpers/parser.ts`
      required: true,
    },
    lastOnlineAt: Date,

    // Testing https://github.com/francescov1/mongoose-tsgen/issues/114
    anArrayOfSchemasWithArrayDocuments: [addressSchema],

    // Testing schema maps
    aMapOfSchemas: {
      type: Map,
      of: anotherSchema,
      required: true
    },
    aMapOfSchemaArrays: {
      type: Map,
      of: [anotherSchema]
    },
    anArrayOfSchemaMaps: [
      {
        type: Map,
        of: anotherSchema
      }
    ]
  },
  {
    timestamps: true,
  }
);
francescov1 commented 9 months ago

Great add, thanks for the PR @dmchoiboi!

francescov1 commented 9 months ago

Live in version 9.2.10 🚀