redis / redis-om-node

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

How To Set a Primare Key Field in Schema #183

Closed thisiseddy-ab closed 1 year ago

thisiseddy-ab commented 1 year ago

I'm Trying to use Redis OM For Node and Python,the problem the JSON data that python generates has a field for pk(Primary Key,Entity Id ) but node JSON does not.

I can Set e specific Primary Key in Python like this

def Base(model_prefix):
    class BaseModel():
        class Meta:
            global_key_prefix = RPU_REDIS_DATA_PREFIX
            model_key_prefix = model_prefix
            database =  get_redis_connection(host=RPU_REDIS_DATA_HOST,port=RPU_REDIS_DATA_PORT,db=RPU_REDIS_DATA_DB,decode_responses=True)
    return BaseModel

class User(Base("User"),JsonModel):
    My_Id : str = Field(primary_key=True,index=True,default=str(ULID()))
    username: str = Field(index=True)
    name: str
    lastname: str
class User extends Entity{
}

const User_Schema = new Schema(User,{
    My_Id: { type: 'string', indexed: true },
    username : { type: 'string', indexed: true },
    name: { type: 'string', indexed: false },
    lastname: { type: 'string', indexed: false },
},{
    dataStructure: 'JSON',
    prefix: `${RPU_REDIS_DATA_PREFIX}:User`,
})

is there any solution how to set a primary key in node or just copy Entity Id as a Field Schema

guyroyse commented 1 year ago

Redis OM for node doesn't store the entity ID in the JSON document. You can easily add it as a field if you like. Do note that redis-om@beta, if that's the version you are using, will allow you to specify the entity ID when you save an entity. You'll still need to make it a field and populate it.

thisiseddy-ab commented 1 year ago

Redis OM for node doesn't store the entity ID in the JSON document. You can easily add it as a field if you like. Do note that redis-om@beta, if that's the version you are using, will allow you to specify the entity ID when you save an entity. You'll still need to make it a field and populate it.

For Now I Solved Like This

Pupptter_Service - Is Extended Entity Class

        const My_Id = ulid()
        const Service = new Pupptter_Service(Pupptter_Service_Schema, My_Id,
            {
                My_Id: My_Id,
                Scheme: "http",
                Host : "loclahost",
                Port : PORT,
                Status : "UP",
                created_at : moment.utc().toISOString({keepOffset:true}),
                unix_created_at : moment.utc().unix(),
            })
        const Service_Id = await Pupptter_Service_Repository.save(Service)

Im going to use redis-om@beta when it becomes Standart because i cant find the documentation for it