Generator Classes

Version Downloads/total License

Here is what we use in our CLI to generate fiels

CONTENT

Backend Generator

Generate CRUD system for your Prisma GraphQL server

findUnique | findFirst | findMany | findCount | aggregate | createOne | updateOne | upsertOne | deleteOne | updateMany | deleteMany


ARGUMENTSType
generator{ name: one of nexus , sdl , graphql-modules, nexus-plugin-prisma, schemaPath: prisma schema path }
optionsYou can add Options object to customize the generator

import { Generator } from '@paljs/generator';

new Generator({ name: 'nexus', schemaPath: './prisma/schema.prisma' }, {}).run();

output

Generated code depending on the type of generator

UIGenerator

  • admin settings schema uses in our PrismaTable
  • admin pages for build pages with our Prismatable
  • graphql (queries, mutations) documents to use in your customize code
import { UIGenerator } from '@paljs/generator';

// accept one arg for schema.prisma path
const uiGenerator = new UIGenerator('prisma/schema.prisma');

// accept one arg for output of generated settings schema
uiGenerator.buildSettingsSchema('adminSettings.json');

// accept `AdminPagesOptions` object
uiGenerator.generateAdminPages({});

// accept `Options` Object
uiGenerator.generateGraphql(options);

AdminPagesOptions

interface AdminPagesOptions {
  // add array of model to just generate pages for them by `pal g`
  models?: string[];
  // customize page content
  pageContent?: string;
  // output dir
  outPut?: string;
}

pageContent it's your generated page content as string. default:

import React from 'react';
import PrismaTable from 'components/PrismaTable';

const #{id}: React.FC = () => {
  return <PrismaTable model="#{id}" />;
};

export default #{id};

will replace #{id} to model name.

Client Queries And Mutations

Ok now I have tables to manage my db what if I need to add more logic functions and need to build query body.

like this

query findManyUser(
  $where: UserWhereInput
  $orderBy: UserOrderByInput
  $cursor: UserWhereUniqueInput
  $skip: Int
  $take: Int
) {
  findManyUser(where: $where, orderBy: $orderBy, cursor: $cursor, skip: $skip, take: $take) {
    ...User
  }
}

NOTE: I create query string in .graphql files because I use graphql-codegen CLI to auto generate @apollo/client react hook.

customize output with adding Options to uiGenerator.generateGraphql(options);

Output User model result

fragment UserFields on User {
  id
  email
  name
  password
  createdAt
  groupId
}

fragment User on User {
  ...UserFields
  group {
    ...GroupFields
  }
}

query findUniqueUser($where: UserWhereUniqueInput!) {
  findUniqueUser(where: $where) {
    ...User
  }
}

query findManyUser(
  $where: UserWhereInput
  $orderBy: UserOrderByInput
  $cursor: UserWhereUniqueInput
  $skip: Int
  $take: Int
) {
  findManyUser(where: $where, orderBy: $orderBy, cursor: $cursor, skip: $skip, take: $take) {
    ...User
  }
}

query findManyUserCount(
  $where: UserWhereInput
  $orderBy: UserOrderByInput
  $cursor: UserWhereUniqueInput
  $skip: Int
  $take: Int
) {
  findManyUserCount(where: $where, orderBy: $orderBy, cursor: $cursor, skip: $skip, take: $take)
}

mutation createOneUser($data: UserCreateInput!) {
  createOneUser(data: $data) {
    ...User
  }
}

mutation updateOneUser($where: UserWhereUniqueInput!, $data: UserUpdateInput!) {
  updateOneUser(where: $where, data: $data) {
    ...User
  }
}

mutation deleteOneUser($where: UserWhereUniqueInput!) {
  deleteOneUser(where: $where) {
    id
  }
}

mutation deleteManyUser($where: UserWhereInput) {
  deleteManyUser(where: $where) {
    count
  }
}

mutation updateManyUser($where: UserWhereInput, $data: UserUpdateManyMutationInput!) {
  updateManyUser(where: $where, data: $data) {
    count
  }
}

options type definition

interface Options {
  // prisma props name inside the graphql resolvers context. "default: prisma" maybe can be db or you can use with multi schema project to be prisma1, prisma2
  prismaName: string;  // add array of model to just generate them by `pal g`
  models?: string[];  // models output path
  output: string;  // generate javaScript files
  javaScript?: boolean;  // add onDelete.cascade() function on deleteOne and deleteMany mutations default false
  onDelete?: boolean;  // exclude fields from all models
  excludeFields: string[];  // exclude queries or mutations for one or more models
  excludeModels: { name: string; queries?: boolean; mutations?: boolean }[];  // disable all queries for all models
  disableQueries?: boolean;  // disable all mutations for all models
  disableMutations?: boolean;  // exclude fields from one or more models will merge it with general excludeFields
  excludeFieldsByModel: { [modelName: string]: string[] };  // exclude queries and mutations for one or more model it's object with key : model name value array of QueriesAndMutations type  excludeQueriesAndMutationsByModel: {    [modelName: string]: QueriesAndMutations[];
  };  // exclude queries and mutations for all models array of QueriesAndMutations type
  excludeQueriesAndMutations: QueriesAndMutations[];
}

type QueriesAndMutations =
  | 'findUnique'
  | 'findMany'
  | 'findCount'
  | 'aggregate'
  | 'createOne'
  | 'updateOne'
  | 'upsertOne'
  | 'deleteOne'
  | 'updateMany'
  | 'deleteMany';