简体   繁体   中英

Redis-om redis node js createEntity

It is possible to use the libary redis-om create own keyname with node js? I try to save slug as keyname but i cant do that.

My Schema:

 import { Entity, Schema } from "redis-om"; import client from "../redis.js"; class Person extends Entity {} export const personSchema = new Schema( Person, { firstName: { type: "string" }, lastName: { type: "string" }, age: { type: "number" }, verified: { type: "boolean" }, }, { dataStructure: "JSON", } ); export const personRepository = client.fetchRepository(personSchema);

Try this in controller and it didn;t work. CreateIndex() also didnt work:-(

 import expressAsyncHandler from "express-async-handler"; import { personRepository } from "../models/personModel.js"; export const creatingPerson = expressAsyncHandler(async (req, res) => { const person = personRepository.createEntity(req.params.id); const newPerson = await person.createAndSave(req.body); res.status(200).json({ newPerson }); });

So it is possible to use slug as keyname with redis-om? for example Person:001

Base on document about schemaoptions you can customise key name like you want

  1. prefix

The string that comes before the ID when creating Redis keys for Entities. Defaults to the class name of the Entity. Combined with the results of idStrategy to generate a key. If prefix is Foo and idStrategy returns 12345 then the generated key would be Foo:12345

  1. idStrategy

A function that generates a random Entity ID. Defaults to a function that generates ULIDs. Combined with prefix to generate a Redis key. If prefix is Foo and idStratgey returns 12345 then the generated key would be Foo:12345.

const myIDGeneratorFunc = (): string => "123";

class Person extends Entity { }
export const personSchema = new Schema(
    Person,
    {
        firstName: { type: "string" },
        lastName: { type: "string" },
        age: { type: "number" },
        verified: { type: "boolean" },
    },
    {
        dataStructure: "JSON",
        prefix: "Person",
        idStrategy: myIDGeneratorFunc,
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM