npm package: @mainamiru/prisma-dart-model-generator
Description: Prisma Dart Model Generator
Author: LUCKY BABU
Latest version: 1.0.11

# Prisma Dart Model Generator

## **Prisma**

> [Prisma](https://www.prisma.io/) is Database ORM Library for Node.js, Typescript.

Prisma basically generate each models type definition defined in [`schema.prisma`](https://www.prisma.io/docs/concepts/components/prisma-schema).

This is Prisma's basic way of doing things, and I love this approach.


The Prisma JS Client returns objects that does not contain the model's relational fields. The Generator can create two separate files per model, one that matches the Prisma Js Client's interfaces, and one that contains only the relational fields.

### **Usage**

1. **Install**

    ```shell
    yarn add --dev  @mainamiru/prisma-dart-model-generator
    
    npm install --save-dev @mainamiru/prisma-dart-model-generator

    ```

2. **Define Generator in `schema.prisma`**
    ```prisma
    generator prismaDartGenerator {
        provider = "prisma-dart-model-generator"
        modelSuffix = "Model"
        output = "../generated/models"
    }
    
    ```
      And finally generate your Prisma schema:

    ```shell
    npx prisma generate
    ```
  By default that will output the Typescript interface definitions to a file called   generated/models in your prisma folder, this can be changed by specifying the output option.   As mentioned above, by default the generated types will be type compatible with the   Prisma client types. If you instead want to generate types matching the JSON. stringify-ed versions of your models, you will need to change some of the options.

## Options

| **Option**        |                        **Type**                        |                                **Default**                                 | **Description**                                                                                                                                                                                        |
| ----------------- | :----------------------------------------------------: | :------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| output            |                        `string`                        |                             `"generated/models"`                              | The output location for the generated Typescript                                                                          |                                                                                                           
| modelSuffix       |                        `string`                        |                                    `"Model"`                                    | Suffix to add to model types.              |                                                                 |

3. **😎 done! Let's check out generated files.**

      if this models were defined in your prisma.schema file.

    ```prisma
    model User {
      id         String   @id @default(cuid())
      name       String   @db.VarChar(30)
      image      String?  @db.VarChar(200)
      gender     Gender   @default(MALE)
      email      String   @unique @db.VarChar(50)
      mobile     String   @unique @db.VarChar(15)
      role       UserRole @default(USER)
      password   String   @db.VarChar(200)
      created_at DateTime @default(now())
      updated_at DateTime @updatedAt
      posts Post[]

     @@map("users")
    }

    model Post {
       id         String   @id @default(cuid())
       title      String   @db.VarChar(100)
       image      String?  @db.VarChar(200)
       content    String   @db.VarChar(1000)
       published  Boolean  @default(true)
       authorId   String   @db.VarChar(100)
       created_at DateTime @default(now())
       updated_at DateTime @updatedAt

       author User? @relation(fields: [authorId], references: [id])
     @@map("posts")
    }

    enum Gender {
      MALE
      FEMALE
    }

    enum UserRole {
      USER
      ADMIN
      AUTHOR
    }

    ```

      then this types is generated in <PROJECT_PATH>/generated. <br>
      ( The generating path can be customized through _output_ option. )

    ```dart
    // user.dart
    import '../enums.dart';
    import 'post.dart';

    class UserModel {
      final String id;
      final String name;
      final String? image;
      final Gender gender;
      final String email;
      final String mobile;
      final UserRole role;
      final String password;
      final String createdAt;
      final String updatedAt;
      final List<PostModel>? posts;

      UserModel({
        required this.id,
        required this.name,
        this.image,
        required this.gender,
        required this.email,
        required this.mobile,
        required this.role,
        required this.password,
        required this.createdAt,
        required this.updatedAt,
        required this.posts,
      });

      factory UserModel.fromJson(Map<String, dynamic> json){
        return UserModel(
          id: json['id'],
          name: json['name'],
          image: json['image'],
          gender: json['gender'],
          email: json['email'],
          mobile: json['mobile'],
          role: json['role'],
          password: json['password'],
          createdAt: json['created_at'],
          updatedAt: json['updated_at'],
          posts: json['posts']?.map((value)=> PostModel.fromJson(value)),
        );
      }

      Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'image': image,
        'gender': gender,
        'email': email,
        'mobile': mobile,
        'role': role,
        'created_at': createdAt,
        'updated_at': updatedAt,
        'posts': posts
      };
    }
 


    ```

    ```dart
    // post.dart
    import 'user.dart';
        
    class PostModel {
      final String id;
      final String title;
      final String? image;
      final String content;
      final bool published;
      final String authorId;
      final String createdAt;
      final String updatedAt;
      final UserModel? author;

      PostModel({
        required this.id,
        required this.title,
        this.image,
        required this.content,
        required this.published,
        required this.authorId,
        required this.createdAt,
        required this.updatedAt,
        this.author,
      });

      factory PostModel.fromJson(Map<String, dynamic> json){
        return PostModel(
          id: json['id'],
          title: json['title'],
          image: json['image'],
          content: json['content'],
          published: json['published'],
          authorId: json['authorId'],
          createdAt: json['created_at'],
          updatedAt: json['updated_at'],
          author: json['author'] is Map ? UserModel.fromJson(json['author']) : null,
        );
      }

      Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'image': image,
        'content': content,
        'published': published,
        'authorId': authorId,
        'created_at': createdAt,
        'updated_at': updatedAt,
        'author': author
      };
    }

    ```

### **How it works?**

Prisma internally defines metadata as a dmmf object.

It is defined as an additional generator in the `schema.prisma` file and will operate in the `prisma generate` process.

### **Feature**

-   generate dart model from prisma model definition
-   Support Basic Type and Relation

### **FAQ**

**1. It's works with all javascript/typescript libary**

You can use for all API payload response in React, React Native, Nextjs etc...