redis / redis-om-node

Object mapping, and more, for Redis and Node.js. Written in TypeScript.
MIT License
1.16k stars 78 forks source link

Choosing an alternate/custom `EntityId` field (or at the very least, exposing it by default). #194

Open humblecoder opened 1 year ago

humblecoder commented 1 year ago

So, I've seen the following two posts concerning EntityId customization:

https://github.com/redis/redis-om-node/issues/186 https://github.com/redis/redis-om-node/issues/40

However, I'm actually interested in selecting the field-name to which the value gets assigned, and revealing it (via toJSON(), or whatever else) for use with CRUD/REST functionality . As it stands, I currently destructure ({...myObj, id: myObj[EntityId]}) or use some version of Reflect.ownKeys(myObj) every time I need it. Would be nice to just have it "built-in" and available when the object is CRUDed.

I'm almost certain I'm missing something obvious in the documentation. 🤔

guyroyse commented 1 year ago

I've been noodling on this a bit as the restructuring is a bit of a hassle. For now, that's the best way.

When I was putting this together, I first thought I would just store the entityId as a field named entityId on the returned object. But then I was concerened what would happen if that field was already there. So, I used a Symbol to avoid any conflicts.

I still want to stick with the Symbols as I think that utility is there. But I'm also thinking that maybe I could add a configuration option to the Schema to map the Entity symbol to a named property of your choice. And if that option isn't specified, then just default to current behavior.

I'm gonna mark this as a new feature and see about adding it in the near future.

isque03 commented 11 months ago

This would be helpful: "But I'm also thinking that maybe I could add a configuration option to the Schema to map the Entity symbol to a named property of your choice."

Thanks very much for your work on the project @guyroyse

venticello commented 11 months ago

I'm missing the ability to specify ID as a field. As is done in Redis om python.

class Thing(HashModel):
    thing_id: int = Field(index=True, primary_key=True, )
    class Meta:
        global_key_prefix = "MyProject"
        model_key_prefix = "thing"

This way I can easily use the keys that I already have in my database. For example, my keys MyProject:thing:12345

TomerAvni commented 9 months ago

Hi there, Thanks for this, very helpful! Is there any progress here, or a way to expose it by default (similar to the previous version)? Also, can someone here explain the logic behind the need to explicitly call a function whenever you need to use the id. It is sooooooo error prone, it means you need to remember this every time you create a connection between entities, return forward this entity as a POJO, etc. I am also not familiar with any other framework that does it (sql/nosql). I honestly don't see the point, I'd love to understand it better... But more important to get a solution, because for now, it makes this version not upgradable.

Thanks!