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

Handle Undefined Values by Setting Them as Null #19

Open
elersong opened this issue Jul 13, 2024 · 0 comments
Open

Handle Undefined Values by Setting Them as Null #19

elersong opened this issue Jul 13, 2024 · 0 comments
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented good first issue Good for newcomers

Comments

@elersong
Copy link
Owner

elersong commented Jul 13, 2024

Description

To improve query capabilities and data consistency in Fireorm, undefined values in a collection should be set to null. This change would allow querying those values using Where clauses. Additionally, the IFirestoreVal type should be extended to allow null values.

Steps to Reproduce

  1. Create a Fireorm entity with optional fields.
  2. Attempt to query documents where these fields are undefined.

Expected Behavior

Undefined fields should be stored as null in Firestore, allowing queries to include conditions on these null values.

Actual Behavior

Undefined fields are not stored, making it impossible to query for documents based on undefined values.

Acceptance Criteria

  • Modify the serializeEntity function to set undefined values as null before writing data to Firestore.
  • Extend the IFirestoreVal type to include null.
  • Ensure that querying for null values using Where clauses works as expected.

Additional Context

  • June 25, 2020: Initial issue raised about setting undefined values to null.
  • July 7, 2020: Agreement on the idea and labeling the issue as a good first issue.
  • October 18, 2020: Discussion about running integration tests and setting up environment variables for contributing.

Proposed API Changes

  1. Modify serializeEntity Function:

    • Update the serializeEntity function to convert undefined values to null before saving to Firestore.
    function serializeEntity(entity: any): any {
      const serializedEntity = { ...entity };
      Object.keys(serializedEntity).forEach(key => {
        if (serializedEntity[key] === undefined) {
          serializedEntity[key] = null;
        }
      });
      return serializedEntity;
    }
  2. Extend IFirestoreVal Type:

    • Update the IFirestoreVal type def to include null
    export type IFirestoreVal = string | number | boolean | null | ...; // other existing types
  3. Querying Null Values:

    • Ensure that the Where query functionality supports conditions on null values
    // Example query
    getRepository(MyEntity).where('myField', '==', null).find();    

Example Usage

// Define an entity with optional fields
@Collection()
class MyEntity {
  id: string;
  optionalField?: string;
}

// Save an entity with undefined optionalField, which will be stored as null
const entity = new MyEntity();
entity.id = '123';
await getRepository(MyEntity).create(entity);

// Query for entities where optionalField is null
const results = await getRepository(MyEntity).where('optionalField', '==', null).find();

Original Issue

@elersong elersong added enhancement New feature or request good first issue Good for newcomers from_willyovale An issue described in original project, but never implemented labels Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant