I have some strange behaviour happening with my Redis-OM application, I understand that this is still a very BETA version of the software but I just wanted to make sure I wasn't doing something dumb (which I might be)
So I am setting up an application to keep a playlist of video ID's within a Room that I store temporarily in a Redis Cloud Database.
I have a function that creates a Room, one that fetches the Room details (everything currently in the room) and one that adds a new video to the Playlist within that room. (see below) - NOTE: the data variable within createRoom(data) is just a string of the Room ID
class Room extends Entity {}
let schema = new Schema(
Room,
{
code: { type: 'string' },
playlist: {
type: 'array',
videos: {
type: 'object',
},
},
},
{
dataStructure: 'JSON',
}
);
export async function createRoom(data) {
await connect();
const repository = new Repository(schema, client);
const room = repository.createEntity(data);
const id = await repository.save(room);
await client.execute(['EXPIRE', `Room:${id}`, 43200]);
return id;
}
export async function getRoom(code) {
await connect();
const repository = new Repository(schema, client);
const room = await repository
.search()
.where('code')
.equals(code)
.returnFirst();
return room;
}
export async function addVideoToRoom(code, videoDetails) {
const room = await getRoom(code);
await room.playlist.push(videoDetails);
await connect();
const repository = new Repository(schema, client);
const id = await repository.save(room);
return id;
}
The primary issue that I'm having is adding a second video to the playlist. What happens is
- Create Room - adds a new room to the DB
- Search for video
- Click to add video to DB (video is successfully added)
- Click to add second video to DB (fails because
getRoom(code)fails - returns null)
This was working yesterday, however I'm not sure why it no longer works.
If anyone has any idea's why that might be please let me know, I have a feeling it may be how I'm handling clients or indexes with Redis so I have popped my functions for those below too.
const client = new Client();
async function connect() {
if (!client.isOpen()) {
await client.open(process.env.REDIS_URL);
}
}
export async function createIndex() {
await connect();
const repository = new Repository(schema, client);
await repository.dropIndex();
await repository.createIndex();
}
Thanks very much programmers of Stack - If I'm being super dumb I do apologise.
CodePudding user response:
Redis OM for Node.js supports neither nested objects nor a type of 'object' within the Schema. Valid types are 'string', 'number', 'boolean', and 'array'. Arrays are only arrays of strings. The rest are self-explanatory.
If you want to have a Room that has multiple Videos, you need to define a Room entity, perhaps with a playlist that is defined as an array not of objects, but of Video ids.
Details on this can be found in the README.
