Skip to content

Generate TypeScript Interfaces and Enums from Mongoose Schemas

Notifications You must be signed in to change notification settings

gatsbylabs/malt

Repository files navigation

Malt

Mongoose Type Generator

Generate TypeScript Interfaces and Enums from Mongoose Schemas.

Installation

With yarn:

yarn install -D malt

With npm:

npm install --save-dev malt

Usage

Malt can take a directory, list of files, or a combination of both as an input. It will recursively search the provided source files for Mongoose schemas.

malt src/
malt src/models/myModel.ts
malt src/models src/otherModels src/myModel.ts

Having a schema that looks like this:

import mongoose from "mongoose";

const mySchema = new mongoose.Schema({
  name: String,
  binary: Buffer,
  living: Boolean,
  updated: { type: Date, default: Date.now },
  age: { type: Number, min: 18, max: 65 },
  mixed: Schema.Types.Mixed,
  _someId: Schema.Types.ObjectId,
  decimal: Schema.Types.Decimal128,
  array: [],
  ofString: [String],
  ofNumber: [Number],
  ofDates: [Date],
  ofBuffer: [Buffer],
  ofBoolean: [Boolean],
  ofMixed: [Schema.Types.Mixed],
  ofObjectId: [Schema.Types.ObjectId],
  ofArrays: [[]],
  ofArrayOfNumbers: [[Number]],
  nested: {
    stuff: { type: String, lowercase: true, trim: true },
  },
  map: Map,
  mapOfString: {
    type: Map,
    of: String,
  },
  required: { type: String, required: true },
  enums: { type: String, enum: ["A", "B"] },
});

Will generate a types file that looks like this:

// eslint-disable
import mongoose from "mongoose";

export interface MySchema {
  _id?: mongoose.Types.ObjectId | null | undefined;
  name?: string | null | undefined;
  binary?: Buffer | null | undefined;
  living?: boolean | null | undefined;
  updated?: Date | null | undefined;
  age?: number | null | undefined;
  mixed?: any | null | undefined;
  _someId?: mongoose.Types.ObjectId | null | undefined;
  decimal?: mongoose.Types.Decimal128 | null | undefined;
  array?: any[] | null | undefined;
  ofString?: string[] | null | undefined;
  ofNumber?: number[] | null | undefined;
  ofDates?: Date[] | null | undefined;
  ofBuffer?: Buffer[] | null | undefined;
  ofBoolean?: boolean[] | null | undefined;
  ofMixed?: any[] | null | undefined;
  ofObjectId?: mongoose.Types.ObjectId[] | null | undefined;
  ofArrays?: any[][] | null | undefined;
  ofArrayOfNumbers?: number[][] | null | undefined;
  nested?: Nested | null | undefined;
  map?: Map<string, any> | null | undefined;
  mapOfString?: Map<string, string> | null | undefined;
  required: string;
  enums?: Enums | null | undefined;
}
export interface Nested {
  _id?: mongoose.Types.ObjectId | null | undefined;
  stuff?: string | null | undefined;
}
export enum Enums {
  A = "A",
  B = "B",
}

Checklist

  • basic schema types
  • nested schema types
  • array schema types
  • schema type options (reqired, enums)
  • schema options (timestamp, id, etc.)
  • support variable traversal for nodes
    • support aliased imports of schemas and types
  • support string union option for enums
  • discriminated unions
  • add comment with text of original schema field configuration
  • options on null, undefined, or optional generation

Created and maintained by Enoch Chau and the engineers at Gatsby Labs.

About

Generate TypeScript Interfaces and Enums from Mongoose Schemas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published