mongodb-json-schema-to-typescript

compile mongodb json schema to typescript typings

Usage no npm install needed!

<script type="module">
  import mongodbJsonSchemaToTypescript from 'https://cdn.skypack.dev/mongodb-json-schema-to-typescript';
</script>

README

mongodb-json-schema-to-typescript

Transform MongoDB-compliant JSON Schema files to TypeScript.

On top of json-schema-to-typescript, it supports bsonType values, including date.

This fork also supports some bsonType types: Date or fallback to usual values for type.

Example

Input:

{
  "title": "Example Schema",
  "bsonType": "object",
  "properties": {
    "name": {
      "bsonType": "string"
    },
    "subscriptionDate": {
      "bsonType": "date"
    },
    "age": {
      "description": "Age in years",
      "bsonType": "integer",
      "minimum": 0
    },
    "hairColor": {
      "enum": ["black", "brown", "blue"],
      "bsonType": "string"
    }
  },
  "additionalProperties": false,
  "required": ["name", "subscriptionDate"]
}

Output:

export interface ExampleSchema {
  name: string;
  subscriptionDate: Date;
  /**
   * Age in years
   */
  age?: number;
  hairColor?: "black" | "brown" | "blue";
}

CLI

A CLI utility is provided with this package.

npx mongodb-json-schema-to-typescript foo.json > foo.d.ts

Tests

npm test

Further Reading