Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Repository a generic class / infer Entity from Schema #227

Open
jonahschueller opened this issue Jan 23, 2024 · 4 comments
Open

Make Repository a generic class / infer Entity from Schema #227

jonahschueller opened this issue Jan 23, 2024 · 4 comments

Comments

@jonahschueller
Copy link

jonahschueller commented Jan 23, 2024

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.

@Eji4h
Copy link

Eji4h commented Feb 16, 2024

I had the same issue.

@AlaaZorkane
Copy link

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

@Wato1818
Copy link

Wato1818 commented Mar 7, 2024

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

@dennisofficial
Copy link
Contributor

I feel like this should be the norm. TypeScript is for strict typing, I can't type an Entity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants