elersong / fireorm24

ORM for Firebase Firestore 🔥 updated for 2024
MIT License
1 stars 0 forks source link

Exclude Reserved Fields from Firestore Documents #26

Open elersong opened 2 months ago

elersong commented 2 months ago

Description

Creating or updating documents in Fireorm adds the id field to the actual document. Certain reserved fields in Firestore, such as exists, id, and hasPendingWrites, should not be stored within documents. The presence of these fields can trigger warnings from packages like @nandorojo/swr-firestore.

Steps to Reproduce

  1. Create or update a document using Fireorm.
  2. Check the stored document and observe the presence of the id field.
  3. Use a package like @nandorojo/swr-firestore and observe warnings about reserved fields.

Expected Behavior

Firestore documents should not store reserved fields such as id, exists, and hasPendingWrites.

Actual Behavior

Firestore documents store the id field, leading to warnings and potential inconsistencies.

Acceptance Criteria

Additional Context

Proposed API Changes

  1. Update serializeEntity Function:

    • Modify the serializeEntity function to exclude reserved fields from being stored in Firestore documents.
    function serializeEntity(entity: any): any {
     const serializedEntity = { ...entity };
     ['id', 'exists', 'hasPendingWrites'].forEach(field => {
       delete serializedEntity[field];
     });
     return serializedEntity;
    }
  2. Provide Configuration Option:

    • Add a configuration option to control whether the id field should be excluded from documents.
    // Example configuration
    initialize({ excludeReservedFields: true });
  3. Add Warning Mechanism:

    • Implement a warning mechanism that alerts developers if reserved fields are detected within documents.
    function checkForReservedFields(entity: any): void {
     ['exists', 'hasPendingWrites'].forEach(field => {
       if (entity.hasOwnProperty(field)) {
         console.warn(`Warning: Reserved field "${field}" detected in document.`);
       }
     });
    }

Example Implementation

@Collection()
class User {
  id: string;
  display_name: string;
  username: string;
}

// Initialize Fireorm with configuration to exclude reserved fields
initialize({ excludeReservedFields: true });

// Repository instance
const repo = getRepository(User);

// Create or update user document
const user = new User();
user.id = 'user123';
user.display_name = 'John Doe';
user.username = 'johndoe';

repo.create(user); // The `id` field will not be stored in the document

Original Issue