@mainamiru/prisma-dart-model-generator
v1.0.3
Published
Prisma Dart Model Generator
Downloads
1
Maintainers
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
Install
yarn add --dev @mainamiru/prisma-dart-model-generator npm install --save-dev @mainamiru/prisma-dart-model-generator
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. | |
😎 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...