redis-om-node icon indicating copy to clipboard operation
redis-om-node copied to clipboard

Use reflection to do object mapping for a more declarative API.

Open max-abclabs opened this issue 3 years ago • 4 comments

Currently using the schema in ts looks like this. This feels like 2x the code necessary since you are declaring the schema twice. With reflection the API would be more declarative and make this code much smaller and easier to understand.

Without reflection:

interface Album {
  artist: string;
  title: string;
  year: number;
  genres: string[];
  outOfPublication: boolean;
}

class Album extends Entity {}

let schema = new Schema(Album, {
  artist: { type: 'string' },
  title: { type: 'string' },
  year: { type: 'number' },
  genres: { type: 'array' },
  outOfPublication: { type: 'boolean' }
})

With reflection:

@Entity()
class Album {
  @Property()
  artist: string;
  @Property()
  title: string;
  @Property()
  year: string;
  @Property()
  genres: string[];
  @Property()
  outOfPublication: boolean;
}

let schema = new Schema(Album);

This is just an example but demonstrates how declarative and powerful reflection can make this api.

max-abclabs avatar Jan 12 '22 11:01 max-abclabs

You're right and this bugged me too. I thought about it for a good long while and chose not to use decorators because a) they are still an experimental feature and b) the experience from a JavaScript developer's point of view was not nearly as tidy. Settled on the lowest common denominator.

guyroyse avatar Jan 12 '22 18:01 guyroyse

I understand your reasoning however there are a couple of thing I want to add to the table.

a) Thinking about decorators as an experimental feature should not dismay you from using them. Plenty of production ready frameworks use decorators successfully such as Angular and Nestjs.

b) Furthermore only because you support decorators does not mean that you only support a declarative decorator API. It is possible to support both. What i mean is, this current api should be ablo co-exist with a declarative decoration API without any breaking changes. Which in turn should not make js developers frown away from using it :)

max-abclabs avatar Jan 12 '22 19:01 max-abclabs

I agree with point B. I suspect point A will be resolved by the time I get to adding this feature. ;) I'll add it to the backlog.

guyroyse avatar Jan 18 '22 02:01 guyroyse

Hello I am max-abclabs but on my private github account.

I could look into this when I have some time over and submit a PR if you don't mind?

redsuperbat avatar Jan 27 '22 19:01 redsuperbat

This issue is obviated but Redis OM 0.4 which eliminates the Entity class. Closing.

guyroyse avatar Jul 24 '23 19:07 guyroyse

This issue is obviated but Redis OM 0.4 which eliminates the Entity class. Closing.

Can you elaborate? I'm currently looking for a way to use decorators to declare my entities using this library, is this supported now 2 years later?

tomerh2001 avatar Apr 06 '24 15:04 tomerh2001