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

Make Repository a generic class / infer Entity from Schema

Open jonahschueller opened this issue 1 year ago • 4 comments

It seems like the current version of redis-om does not have any support for a custom repository entity type. Apparently, the Entity type is hard coded into the Repository class (correct me if I am wrong).

I think this is a good point to introduce some Typescript Generics support.

I am thinking of something like this:

interface Person extends Entity {
   name: string
   age: number
}

const PersonSchema = new Schema(...)

const personRepo = new Repository<Person>(PersonSchema, client)

With this approach, the save, fetch, ... functions of the repository would give proper IntelliSense.

I assume the changes wouldn't be too much. Something like:


class Repository<E extends Entity = Entity> {

   async save(entity: E)

   ...

}

A more advanced approach would be to infer the resulting entity type from the Schema definition. However, this would need a lot of refactoring of the type definitions.

jonahschueller avatar Jan 23 '24 21:01 jonahschueller

I had the same issue.

Eji4h avatar Feb 16 '24 19:02 Eji4h

Seems like a no brainer, I don't know why this wasn't implemented on the first place. +1

AlaaZorkane avatar Feb 29 '24 16:02 AlaaZorkane

It would be very useful. But i feel the improvement may be way more complex than we think. +1

Wato1818 avatar Mar 07 '24 16:03 Wato1818