wovalle / fireorm

ORM for firestore 🔥
https://fireorm.js.org
MIT License
558 stars 75 forks source link

Firestore Complex Types handling #58

Open wovalle opened 5 years ago

wovalle commented 5 years ago

In https://github.com/wovalle/fireorm/pull/56 we started re-exporting [class-transformer's @Type Decorator]() to be able to cast Firestore GeoPoint fields. That solution is far from final and was only meant as a temporary workaround.

The way Typescript works, types are not available at runtime. If you want to cast Firestore's complex types into your custom classes, you need to tell Typescript the class it should cast it to... via a Decorator.

This issue is more of an exploration in this topic. More information is needed to fully implement a flexible solution.

Acceptance criteria:

eric-ships commented 3 years ago

Hi @wovalle - we're running into this issue in validating complex data types using nested classes with fireorm. Is there an update on this issue? Is there anything that we can help with?

wovalle commented 3 years ago

Hey @ericliu121187, somehow I missed this notification.

Sadly there are no updates, I haven't had time to tackle this issue. What are you trying to accomplish?

eric-ships commented 3 years ago

Hi @wovalle , no worries. Let’s say we are creating a Post document via fireorm.

await getRepository(Post).create(
  Post.create({
    // ...data
  }),
);
@Collection(‘posts’)
export class Post {
  @Expose()
  @IsString()
  public readonly id!: string;
  /**
   * Metadata
   */
  @Expose()
  @IsObject()
  public readonly metadata!: Readonly<{
    author: Readonly<{
      name: string;
    }>;
    data: Readonly<{
      entities: Readonly<{
        hashtags?: readonly unknown[];
        urls?: readonly unknown[];
        tags?: readonly unknown[];
      }>;
      description: string;
    }>;
  }>;

  static create(
    data: Partial<Post>,
  ): Post {
    return plainToClass(Post, data);
  }
}

Am I correct in saying that the nested values in metadata would not be validated?

If not, do you have any suggestions in how we could validate it? Writing a custom validator isn't ideal here bc we want to work with classes to add custom methods to values + ideally, the validation would be recursive out-of-the-box.

What are your thoughts here?

wovalle commented 3 years ago

Not an expert with validation but I think they won't be validated. In my case I'd try to flatten more the structure to have simple fields that can be easily validated.

hondem commented 3 years ago

Hello guys. Is there any progress with this one?

eric-ships commented 3 years ago

@hondem Not on my end. We decided to punt on validating nested data. My instinct is that there could be 2 approaches.

A. Fireorm can validate recursively B. Writing a custom validator

Yunus1903 commented 2 years ago

Any update on this?