elersong / fireorm24

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

Support for Referenced Properties #14

Open elersong opened 3 months ago

elersong commented 3 months ago

Description

Fireorm currently does not support creating entities with referenced properties directly. Users need a way to reference other collections when creating new documents, such as referencing a Produtor from within a ProdutorDadosAdicionais entity.

Steps to Reproduce

  1. Create a Produtor class and a ProdutorDadosAdicionais class, with ProdutorDadosAdicionais having a property referencing Produtor.
  2. Attempt to create a new ProdutorDadosAdicionais document with a reference to an existing Produtor document.

Expected Behavior

Ability to create documents with referenced properties that correctly store document references in Firestore.

Actual Behavior

Currently, there is no straightforward way to set referenced properties, leading to workarounds or potential bugs with deserialization/serialization of Firestore entities.

Acceptance Criteria

Additional Context

Original

elersong commented 3 months ago

Additional Context

// Creating a reference with an object const artist = new Artist(); artist.id = 'new-artist'; artist.name = 'New Artist';

getRepository(Band).create({ id: 'fireorm rocks!', artist: setReference(artist), });

### Proposed API Changes

1. Reference Type and Decorator:

 - Introduce a generic Reference<T> type to handle document references.
 - Use @DocumentReference() decorator to mark properties as references.
```typescript
@Collection()
class Band {
  id: string;
  @DocumentReference()
  artist: Reference<Artist>;
}
  1. Handling Reference Creation:

    • Allow properties to be set either as a reference path (string) or as an object.
    • Validate reference paths to ensure they are well-formed.
    • Provide mechanisms to handle cases where the referenced document does not exist, such as:
    • Throwing an error.
    • Automatically creating the referenced document within a transaction.
    • Using a helper function (setReference) to manage reference creation.
      
      // Example with reference path as string
      getRepository(Band).create({
      id: 'fireorm rocks!',
      artist: '/artists/wovalle'
      });

// Example with reference object const artist = new Artist(); artist.id = 'new-artist'; artist.name = 'New Artist';

getRepository(Band).create({ id: 'fireorm rocks!', artist: setReference(artist), });


3. Adding Path Information to Entities:

 - Introduce a getPath method to entities for easy access to their document paths.
 - Ensure that the path is stored and accessible within the entity's instance.

4. Reference Validation:

 - Implement validation logic for reference paths based on Firebase's guidelines, ensuring paths are non-empty strings without double slashes.

5. TypeScript Compatibility:

 - Ensure the proposed changes are compatible with TypeScript's type checking and autocomplete features.
 - Use a generic approach to allow properties to be of type T or string while maintaining type safety.