Generate Mongoose schemas from TypeScript interfaces? Generate Mongoose schemas from TypeScript interfaces? mongoose mongoose

Generate Mongoose schemas from TypeScript interfaces?


Easiest would be to use some easy-to-parse format, and generate the Typescript and Mongoose interfaces from that. Here is an example in JSON:

{ "name": "IThing",  "type": "interface",  "members": [      { "name": "SomeProperty",        "type": "String" },      { "name": "DoStuff",        "type": "function",        "arguments": [            { "name": "callback",              "type": "function",              "arguments": [],              "return": "Number" }        ] }  ] }

The structure, and even the markup language can change to what you need.

The above would produce something like this in TypeScript:

interface IThing {    SomeProperty: String;    DoStuff(callback: () => Number)}

and this in Mongoose:

var IThing = new Schema({    "SomeProperty": "String"});IThing.methods.DoStuff = function (callback) {    // TODO};


Little late, but we just built a tool to do exactly this, check out mongoose-tsgen. This CLI tool generates an index.d.ts file containing all your schema interfaces, and doesn't require you to rewrite your schemas. Any time you change your schema, just re-run the tool.