Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to programmatically check if a field is nullable #24022

Open
wilker7ribeiro opened this issue Apr 30, 2024 · 0 comments
Open

Add a way to programmatically check if a field is nullable #24022

wilker7ribeiro opened this issue Apr 30, 2024 · 0 comments
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. topic: dmmf topic: schema metadata Metadata to allow advanced commenting or protection of certain parts of a schemas

Comments

@wilker7ribeiro
Copy link

Problem

I want to programmatically check if a field is nullable or not.

I want to integrate a third party filter with a query using an dynamic order by, something like:

private prepareOrderBy(filter: ThirdPartyFilter): Prisma.MyEntityOrderByWithRelationInput {
  if (filter.sortBy) {
    return filter.sortBy.reduce((orderByAcm, sortByField) => {
      orderByAcm[sortByField.field] = {
        sort: sortByField.desc ? Prisma.SortOrder.desc : Prisma.SortOrder.asc,
        nulls: sortByField.nullsLast ? Prisma.NullsOrder.last : Prisma.NullsOrder.first,
      };
      return orderByAcm;
    }, {} as Prisma.MyEntityOrderByWithRelationInput);
  }
  return { createdAt: Prisma.SortOrder.desc };
}

The problem is that the order by typing depends if the field is nullable or not (so that we can include de null last or first thing):

export type MyEntityOrderByWithRelationInput = {
  nonNullableField?: SortOrder
  nullableField?: SortOrderInput | SortOrder
}

And there is no way to know if the field is nullable or not programmatically, so I cant make it 100% dynamic.

Suggested solution

Maybe add on the FieldRef interface a field to check if the field is required:

/**
 * A reference to a specific field of a specific model
 */
export declare interface FieldRef<Model, FieldType> {
    readonly modelName: Model;
    readonly name: string;
    readonly typeName: FieldType;
    readonly isList: boolean;
    readonly isNullable: boolean; // <-- new field
}

That way I could check with prismaClient.myEntity.fields[sortByField.field].isNullable

@SevInf SevInf added kind/improvement An improvement to existing feature and code. topic: dmmf team/client Issue for team Client. topic: schema metadata Metadata to allow advanced commenting or protection of certain parts of a schemas labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. topic: dmmf topic: schema metadata Metadata to allow advanced commenting or protection of certain parts of a schemas
Projects
None yet
Development

No branches or pull requests

2 participants