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
Create a Fireorm entity with optional fields.
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
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;
}
Extend IFirestoreVal Type:
Update the IFirestoreVal type def to include null
export type IFirestoreVal = string | number | boolean | null | ...; // other existing types
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();
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, theIFirestoreVal
type should be extended to allow null values.Steps to Reproduce
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
serializeEntity
function to set undefined values as null before writing data to Firestore.IFirestoreVal
type to include null.Where
clauses works as expected.Additional Context
Proposed API Changes
Modify serializeEntity Function:
serializeEntity
function to convert undefined values to null before saving to Firestore.Extend IFirestoreVal Type:
IFirestoreVal
type def to include nullQuerying Null Values:
Example Usage
Original Issue