Code Monkey home page Code Monkey logo

prisma-schema-transformer's Introduction

prisma-schema-transformer

This is a temporary patch

Patch for support prisma 3

You can look for the source repository, maybe it has a new release.

Test on Prisma 3.6.0 only

Thank source repository contributors and Marc-Oros fork

Install

yarn add --dev https://github.com/allyusd/prisma-schema-transformer.git

Usage

yarn prisma-schema-transformer prisma/schema.prisma

More about my solution

Write a prisma/preSchema.prisma and add it to git

Add prisma/schema.prisma to .gitignore

Add package.json scripts

  "scripts": {
    "schema": "cp prisma/preSchema.prisma prisma/schema.prisma && yarn prisma-schema-transformer prisma/schema.prisma",
    "migrate": "yarn schema && yarn prisma migrate dev --name"
  }

transformer schema only

yarn schema

transformer schema and migrate

yarn migrate init

Example

write prisma/preSchema.prisma with snake_case

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

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

model user {
  id    Int     @default(autoincrement()) @id
  email String  @unique
  name  String?
  hello_world  String?
  posts post[]
}

model post {
  id        Int      @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean? @default(false)
  author    user?    @relation(fields: [author_id], references: [id])
  author_id  Int?
}

Source content

EXPERIMENTAL FOR PRISMA V3

This project utilizes the getDMMF method from @prisma/sdk to perform some post-processing work on generated Prisma schema, including the following.

  • Transform snake_case to camelCase,
  • Properly singularize or pluralize model and field name.
  • Add @updatedAt attribute to field in the event of column name is updated_at
  • Ignore models from the schema.

TODO

  • Auto generate the generator and datasource nodes.

Install

$ yarn global add prisma-schema-transformer

Usage

$ prisma-schema-transformer prisma/schema.prisma
Usage
  $ prisma-schema-transformer [options] [...args]

Specify a schema:
  $ prisma-schema-transformer ./schema.prisma

Instead of saving the result to the filesystem, you can also print it
  $ prisma-schema-transformer ./schema.prisma --print

Exclude some models from the output
  $ prisma-schema-transformer ./schema.prisma --deny knex_migrations --deny knex_migration_lock

Options:
  --print   Do not save
  --deny    Exlucde model from output
  --help    Help
  --version Version info

Motivation

Using snake_case in database and automatically transform generated Prisma schema to camelCase with @map and @@map as needed to map the new name back to the database.

There is a snippet provided by @TLadd, but I found regex a bit unreliable.

How does it work

getDMMF parses the Prisma schema file into dmmf(datamodel meta format), which we can use to do some post-processing on the Prisma internal data structure.

Deserializer

There does not seem to be a printer or a deserializer for DMMF, source. You can learn more about the implementation at deserializer.ts. It is responsible for converting a serialized Prisma schema, DMMF, back to plain text file.

It's hacky, but it works. Some test fixtures are taken from the @prisma/sdk repository for testing. We use the getDMMF method to compare the serialized structure of original and transformed Prisma schema, make sure they are identical.

Transformer

Manipulate the naming of Model and Field to follow the camelCase naming convention.

  • Model name is always singular.
  • Field name is singular by default with the execption of many-to-many relation.

License

This project is MIT licensed.

prisma-schema-transformer's People

Contributors

michaellzc avatar allyusd avatar emarcey avatar bathlamos avatar zyhou avatar tajnymag avatar yama-tomo avatar dependabot[bot] avatar itkach avatar reiv avatar

Forkers

nivelofi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.