googleapis / nodejs-firestore

Node.js client for Google Cloud Firestore: a NoSQL document database built for automatic scaling, high performance, and ease of application development.
https://cloud.google.com/firestore/
Apache License 2.0
636 stars 147 forks source link

[Firestore] `where` should use a "type safe" approach #2020

Open cedvdb opened 3 months ago

cedvdb commented 3 months ago

You can currently use any string in the where clause of firestore. When using with converter the fieldPath should be "string safe"

Current implementation:

    where(
      fieldPath: string | FieldPath,
      opStr: WhereFilterOp,
      value: any
    ): Query<T>;

Proposed implementation (when using with converter) :

    where(
      fieldPath: Path<T>,
      opStr: WhereFilterOp,
      value: any
    ): Query<T>;

where Path:

// Allows dot separated properties of <T>
// for firebase updates
export type UpdatePath<T, K extends keyof T> =
  K extends string
  ? T[K] extends Record<string, any>
  ? T[K] extends ArrayLike<any>
  ? K | `${K}.${UpdatePath<T[K], Exclude<keyof T[K], keyof any[]>>}`
  : K | `${K}.${UpdatePath<T[K], keyof T[K]>}`
  : K
  : never;
export type Path<T> = UpdatePath<T, keyof T> | keyof T;
ehsannas commented 3 months ago

Thanks for reporting @cedvdb . I'll bring up the proposed API with the team