npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mainamiru/prisma-dart-model-generator

v1.0.3

Published

Prisma Dart Model Generator

Downloads

9

Readme

Prisma Dart Model Generator

Prisma

Prisma is Database ORM Library for Node.js, Typescript.

Prisma basically generate each models type definition defined in schema.prisma.

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

    yarn add --dev  @mainamiru/prisma-dart-model-generator
       
    npm install --save-dev @mainamiru/prisma-dart-model-generator
    
  2. Define Generator in schema.prisma

    generator prismaDartGenerator {
        provider = "prisma-dart-model-generator"
        modelSuffix = "Model"
        output = "../generated/models"
    }
       

    And finally generate your Prisma schema:

    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. | |

  1. 😎 done! Let's check out generated files.

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

    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. ( The generating path can be customized through output option. )

    // 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
      };
    }
    
    
    
    // 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...