Skip to content

omar-dulaimi/json-to-prisma-schema-convertor

Repository files navigation

JSON to Prisma Schema Convertor

npm version npm HitCount npm

Convert your JSON schema to an approximate Prisma Schema.

Buy Me A Coffee

Table of Contents

Installation

Using npm:

 npm install json-to-prisma-schema-convertor

Using yarn:

 yarn add json-to-prisma-schema-convertor --dev

Usage

1- Star this repo 😉

2- Either use npx, or add an npm script like this:

{
  "scripts": {
    "json-to-prisma": "json-to-prisma-schema-convertor convert --inputPath='./prisma/schema.json' --outputPath='./prisma/schema.prisma'"
  }
}

2- Running npm run json-to-prisma or npx json-to-prisma-schema-convertor convert --inputPath='./prisma/schema.json' --outputPath='./prisma/schema.prisma' for the following JSON schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "User": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "email": {
          "type": "string"
        },
        "weight": {
          "type": ["number", "null"]
        },
        "is18": {
          "type": ["boolean", "null"]
        },
        "name": {
          "type": ["string", "null"]
        },
        "successor": {
          "anyOf": [
            {
              "$ref": "#/definitions/User"
            },
            {
              "type": "null"
            }
          ]
        },
        "predecessor": {
          "anyOf": [
            {
              "$ref": "#/definitions/User"
            },
            {
              "type": "null"
            }
          ]
        },
        "role": {
          "type": "string",
          "default": "USER",
          "enum": ["USER", "ADMIN"]
        },
        "posts": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Post"
          }
        },
        "keywords": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "biography": {
          "type": ["number", "string", "boolean", "object", "array", "null"]
        }
      }
    },
    "Post": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "user": {
          "anyOf": [
            {
              "$ref": "#/definitions/User"
            },
            {
              "type": "null"
            }
          ]
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "user": {
      "$ref": "#/definitions/User"
    },
    "post": {
      "$ref": "#/definitions/Post"
    }
  }
}

will generate the following Prisma schema

model Post {
  id   Int
  user User?
}

model User {
  id          Int
  createdAt   DateTime
  email       String
  weight      Int?
  is18        Boolean?
  name        String?
  successor   User?
  predecessor User?
  role        UserRole @default(USER)
  posts       Post[]
  keywords    String[]
  biography   Json?
}

enum UserRole {
  USER
  ADMIN
}

Available Options

  • outputPath: string - path of the Json schema to convert

    • alias: op
    • required
  • inputPath: string - path of the prisma schema to be generated

    • alias: ip
    • required