All Projects → valentinpalkovic → prisma-json-schema-generator

valentinpalkovic / prisma-json-schema-generator

Licence: MIT license
A generator for Prisma 2 to generate a valid JSON Schema (v7)

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to prisma-json-schema-generator

blitz
⚡️The Fullstack React Framework — built on Next.js
Stars: ✭ 11,558 (+7871.03%)
Mutual labels:  prisma, prisma2
database-schema-examples
Database Schema Examples we strive to support in Prisma
Stars: ✭ 94 (-35.17%)
Mutual labels:  prisma, prisma2
prisma-nestjs-graphql
Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module
Stars: ✭ 276 (+90.34%)
Mutual labels:  prisma, prisma-generator
nestjs-prisma-docker
Dockerizing a NestJS app with Prisma and PostgreSQL
Stars: ✭ 42 (-71.03%)
Mutual labels:  prisma, prisma2
nest-prisma-rest-api-boilerplate
Nest + Prisma + TypeScript | REST API Starter repository
Stars: ✭ 24 (-83.45%)
Mutual labels:  prisma, prisma2
goprisma
A Go wrapper for prisma to turn databases into GraphQL APIs using Go.
Stars: ✭ 54 (-62.76%)
Mutual labels:  prisma, prisma2
nestjs-prisma-starter
Starter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker
Stars: ✭ 1,107 (+663.45%)
Mutual labels:  prisma, prisma2
prisma-pg-jest
Example showcasing how to use Prisma + Postgres + Jest, where each test has its own unique DB context
Stars: ✭ 103 (-28.97%)
Mutual labels:  prisma, prisma2
express-objection-starter
an opinionated, production-ready, isomorphic express/knex/objection starter with centralized configuration
Stars: ✭ 19 (-86.9%)
Mutual labels:  json-schema
helm-schema-gen
So that you don't have to write values.schema.json by hand from scratch for your Helm 3 charts. [CURRENTLY NOT MAINTAINED]
Stars: ✭ 104 (-28.28%)
Mutual labels:  json-schema
prismabuilder.io
Build your Prisma schema visually in this easy-to-use web based tool
Stars: ✭ 161 (+11.03%)
Mutual labels:  prisma
nikolovlazar.com
My personal site's repo built using Next.js, Chakra UI, MDX, Prisma, PlanetScale.
Stars: ✭ 126 (-13.1%)
Mutual labels:  prisma
Rockets
REST and websockets C++ library
Stars: ✭ 39 (-73.1%)
Mutual labels:  json-schema
mrapi
A framework for rapid development of API or DAL applications.
Stars: ✭ 20 (-86.21%)
Mutual labels:  prisma
fastify-graphql-nexus-prisma
GraphQL Server with Fastify, Mercurius, Prisma, and Nexus
Stars: ✭ 125 (-13.79%)
Mutual labels:  prisma
api-data
Static JSON data from the API, plus a JSON Schema
Stars: ✭ 88 (-39.31%)
Mutual labels:  json-schema
nest-typescript-starter
Nest framework TypeScript starter (node.js)
Stars: ✭ 35 (-75.86%)
Mutual labels:  prisma2
babl
JSON templating on steroids
Stars: ✭ 29 (-80%)
Mutual labels:  json-schema
typescript-to-json-schema
Generate JSON schema from your Typescript sources
Stars: ✭ 54 (-62.76%)
Mutual labels:  json-schema
json-validator
A json validator in PHP
Stars: ✭ 52 (-64.14%)
Mutual labels:  json-schema

Actions Status Code QL npm GitHub license semantic-release Open Source? Yes!

Prisma JSON Schema Generator

A generator, which takes a Prisma 2 schema.prisma and generates a JSON Schema in version 7 of the specification (https://json-schema.org/).

Getting Started

1. Install

npm:

npm install prisma-json-schema-generator --save-dev

yarn:

yarn add -D prisma-json-schema-generator

2. Add the generator to the schema

generator jsonSchema {
  provider = "prisma-json-schema-generator"
}

With a custom output path (default=./json-schema)

generator jsonSchema {
  provider = "prisma-json-schema-generator"
  output = "custom-output-path"
}

Additional options

generator jsonSchema {
  provider = "prisma-json-schema-generator"
  keepRelationScalarFields = "true"
  schemaId = "some-schema-id"
  includeRequiredFields = "true"
  persistOriginalType = "true"
}

The generator currently supports a few options

Key Default Value Description
keepRelationScalarFields "false" By default, the JSON Schema that's generated will output only objects for related model records. If set to "true", this will cause the generator to also output foreign key fields for related records
schemaId undefined Add an id to the generated schema. All references will include the schema id
includeRequiredFields "false" If this flag is "true" all required scalar prisma fields that do not have a default value, will be added to the required properties field for that schema definition.
persistOriginalType "false" If this flag is "true" the original type will be outputed under the property key "originalType"

3. Run generation

prisma:

prisma generate

nexus with prisma plugin:

nexus build

Supported Node Versions

Node Version Support
(Maintenance LTS) 14 ✔️
(Active) 16 ✔️
(Current) 18 ✔️

Examples

PostgreSQL

This generator converts a prisma schema like this:

datasource db {
	provider = "postgresql"
	url      = env("DATABASE_URL")
}

model User {
    id                  Int      @id @default(autoincrement())
    // Double Slash Comment: It will NOT show up in JSON schema
    createdAt           DateTime @default(now())
    /// Triple Slash Comment: It will show up in JSON schema [EMAIL]
    email               String   @unique
    weight              Float?
    is18                Boolean?
    name                String?
    number              BigInt   @default(34534535435353)
    favouriteDecimal    Decimal
    bytes               Bytes /// Triple Slash Inline Comment: It will show up in JSON schema [BYTES]
    successorId         Int?     @unique
    successor           User?    @relation("BlogOwnerHistory", fields: [successorId], references: [id])
    predecessor         User?    @relation("BlogOwnerHistory")
    role                Role     @default(USER)
    posts               Post[]
    keywords            String[]
    biography           Json
}

model Post {
    id     Int   @id @default(autoincrement())
    user   User? @relation(fields: [userId], references: [id])
    userId Int?
}

enum Role {
    USER
    ADMIN
}

into:

{
    $schema: 'http://json-schema.org/draft-07/schema#',
    definitions: {
        Post: {
            properties: {
                id: { type: 'integer' },
                user: {
                    anyOf: [
                        { $ref: '#/definitions/User' },
                        { type: 'null' },
                    ],
                },
            },
            type: 'object',
        },
        User: {
            properties: {
                biography: {
                    type: [
                        'number',
                        'string',
                        'boolean',
                        'object',
                        'array',
                        'null'
                    ],
                },
                createdAt: { format: 'date-time', type: 'string' },
                email: {
                    description: 'Triple Slash Comment: Will show up in JSON schema [EMAIL]',
                    type: 'string'
                },
                id: { type: 'integer' },
                is18: { type: ['boolean', 'null'] },
                keywords: { items: { type: 'string' }, type: 'array' },
                name: { type: ['string', 'null'] },
                number: { type: 'integer', default: '34534535435353' },
                bytes: {
                    description: 'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',
                    type: 'string'
                },
                favouriteDecimal: { type: 'number' },
                posts: {
                    items: { $ref: '#/definitions/Post' },
                    type: 'array',
                },
                predecessor: {
                    anyOf: [
                        { $ref: '#/definitions/User' },
                        { type: 'null' },
                    ],
                },
                role: { enum: ['USER', 'ADMIN'], type: 'string', default: 'USER' },
                successor: {
                    anyOf: [
                        { $ref: '#/definitions/User' },
                        { type: 'null' },
                    ],
                },
                weight: { type: ['integer', 'null'] },
            },
            type: 'object',
        },
    },
    properties: {
        post: { $ref: '#/definitions/Post' },
        user: { $ref: '#/definitions/User' },
    },
    type: 'object',
}

So the following input will correctly be validated:

{
    post: {
        id: 0,
        user: {
            id: 100,
        },
    },
    user: {
        id: 10,
        createdAt: '1997-07-16T19:20:30.45+01:00',
        email: '[email protected]',
        biography: {
            bornIn: 'Scharnow',
        },
        is18: true,
        keywords: ['prisma2', 'json-schema', 'generator'],
        name: null,
        posts: [
            {
                id: 4,
            },
            {
                id: 20,
            },
        ],
        predecessor: {
            id: 10,
            email: '[email protected]',
        },
        successor: null,
        role: 'USER',
        weight: 10.14,
    },
}

MongoDB

The generator also takes care of composite types in MongoDB:

datasource db {
    provider = "mongodb"
    url      = env("DATABASE_URL")
}

model User {
    id      String @id @default(auto()) @map("_id") @db.ObjectId
    photos  Photo[]
}

type Photo {
    height Int      @default(200)
    width  Int      @default(100)
    url    String
}

Output:

{
    $schema: 'http://json-schema.org/draft-07/schema#',
    definitions: {
        User: {
            properties: {
                id: { type: 'string' },
                photos: {
                    items: { $ref: '#/definitions/Photo' },
                    type: 'array',
                },
            },
            type: 'object',
        },
        Photo: {
            properties: {
                height: {
                    type: 'integer',
                    default: 200,
                },
                width: {
                    type: 'integer',
                    default: 100,
                },
                url: {
                    type: 'string',
                },
            },
            type: 'object',
        },
    },
    properties: {
        user: { $ref: '#/definitions/User' },
    },
    type: 'object',
}

License: MIT

Copyright (c) 2020 Valentin Palkovič

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].