This is Cli tool to Create nexus type for Prisma projects. When you try to upgrade from Prisma 1 to Prisma 2 and need to write nexus types for your models this tool will create this types for you from your schema.prisma
file
yarn add -D create-nexus-type
or
npm i create-nexus-type --save-dev
--schema To add schema file path if you not run command in root of project
--outDir Created files output dir default src/types
-mq add this option to create Queries and Mutations for models
-m add this option to create Mutations
-q add this option to create Queries
-f add this option to add {filtering: true} option to Queries
-o add this option to add {ordering: true} option to Queries
--js create javascript version
--mjs create es modules version
// schema.prisma
generator photonjs {
provider = "photonjs"
}
model User {
id String @id @default(cuid())
email String @unique
birthDate DateTime
posts Post[]
}
model Post {
id String @id @default(cuid())
author User[]
}
run
npx cnt
OutPut
// User.ts
import { objectType } from 'nexus';
export const User = objectType({
name: 'User',
definition(t) {
t.model.id();
t.model.email();
t.model.birthDate();
t.model.posts();
}
});
// Post.ts
import { objectType } from 'nexus';
export const Post = objectType({
name: 'Post',
definition(t) {
t.model.id();
t.model.author();
}
});
// index.ts
export * from './User';
export * from './Post';
run
npx cnt --mq -f -o
OutPut
import { objectType, extendType } from 'nexus';
export const User = objectType({
name: 'User',
definition(t) {
t.model.id();
t.model.email();
t.model.birthDate();
t.model.posts();
}
});
export const userQuery = extendType({
type: 'Query',
definition(t) {
t.crud.user();
t.crud.users({ filtering: true, ordering: true });
}
});
export const userMutation = extendType({
type: 'Mutation',
definition(t) {
t.crud.createOneUser();
t.crud.updateOneUser();
t.crud.upsertOneUser();
t.crud.deleteOneUser();
t.crud.updateManyUser();
t.crud.deleteManyUser();
}
});
And have another option to create TypeScript types to use for your work
usage: create-types (Create TypeScript types from Prisma schema)
--schema To add schema file path if you not run command in root of project
--outDir Created files output dir default src/generated
// schema.prisma
generator photonjs {
provider = "photonjs"
}
model User {
id String @id @default(cuid())
email String @unique
birthDate DateTime?
role UserRole
posts Post[]
}
model Post {
id String @id @default(cuid())
author User[]
}
enum UserRole {
USER
ADMIN
}
run
npx create-types
OutPut
// types.ts
export interface User {
id: string;
email: string;
birthDate: Date | null;
role: UserRole;
posts: Post[];
}
export interface Post {
id: string;
author: User[];
}
enum UserRole {
USER = 'USER',
ADMIN = 'ADMIN'
}
Didn't find something here? Look through the issues or simply drop us a line at ahmed.elywa@icloud.com.