I am doing redis or what?

Michael Otu - Aug 21 '22 - - Dev Community

First of all, let's start with the errors.

node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

[ErrorReply: ERR unknown command 'JSON.SET', with args beginning with: 'Album:01GAZ32CZWSPB78HE8M75VH1GR' '.' '{"artist":"Mushroomhead","title":"The Righteous & The Butterfly","year":2014,"genres":["m' ]
Enter fullscreen mode Exit fullscreen mode

I have my redis-server running on the background and to see how things work with redis a primary database, I went to redis-om-node. I put some snippets together trying to come to an understanding of the whole flow.

import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(Album, {
  artist: { type: "string" },
  title: { type: "text" },
  year: { type: "number" },
  genres: { type: "string[]" },
  outOfPublication: { type: "boolean" }
});

const albumRepository = client.fetchRepository(albumSchema);

const album = albumRepository.createEntity();
album.artist = "Mushroomhead";
album.title = "The Righteous & The Butterfly";
album.year = 2014;
album.genres = ["metal"];
album.outOfPublication = true;

const id = await albumRepository.save(album); // '01FJYWEYRHYFT8YTEGQBABJ43J'

console.log(id);
Enter fullscreen mode Exit fullscreen mode

Anyone one who has skimmed through the redis-om-node or redis-om will recognize this code.

In package.json file, I have installed these packages since I am changing the primary database from MongoDB to Redis.

...
  "dependencies": {
    "bcrypt": "^5.0.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "joi": "^17.6.0",
    "joi-password": "^3.0.1",
    "jsonwebtoken": "^8.5.1",
    "morgan": "^1.10.0",
    "morgan-json": "^1.1.0",
    "redis": "^4.2.0",
    "redis-om": "^0.3.6",
    "winston": "^3.7.2"
  },
...
Enter fullscreen mode Exit fullscreen mode

Getting an error and not knowing where it came from or what, is something I have dealt with for quite sometime now. I know I am not the oly person. So keep pushing.

I was thinking it was not me but the code or something so I started digging Google and Stackoverflow and dev.to. I found out that I could create a new repository instead of fetching it as I did or it has been done over here, const albumRepository = client.fetchRepository(albumSchema); I updated my code.

import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(
  Album,
  {
    artist: { type: "string" },
    title: { type: "text" },
    year: { type: "number" },
    genres: { type: "string[]" },
    outOfPublication: { type: "boolean" }
  },
  { dataStructure: "JSON" }
);

// const albumRepository = client.fetchRepository(albumSchema);
const repository = new Repository(client, albumSchema);

// const album = albumRepository.createEntity();
const album = repository.createEntity();
album.artist = "Mushroomhead";
album.title = "The Righteous & The Butterfly";
album.year = 2014;
album.genres = ["metal"];
album.outOfPublication = true;

// const id = await albumRepository.save(album);
const id = await repository.save(album);

console.log(id);
Enter fullscreen mode Exit fullscreen mode

Guess what? There was an error which was different from the previous.

/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907
    const id = this.schema.generateId();
                           ^

TypeError: this.schema.generateId is not a function
    at Repository.createEntity (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907:28)
    at file:///home/user/Projects/web/ARMS-redis/src/index.js:24:26
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Enter fullscreen mode Exit fullscreen mode

I went to /home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907

  createEntity(data = {}) {
    const id = this.schema.generateId();
    return new this.schema.entityCtor(this.schema, id, data);
  }
Enter fullscreen mode Exit fullscreen mode

This is the createEntity that has or points to the generateId function that does not exist. There was a class, var Schema = class { ... } inside the same file and it has the generateId function.

I went back to digging and I found out that there is another method on the repository, createAndSave({}). express-redis-om-workshop.

I made modifications:

import { Client, Entity, Schema, Repository } from "redis-om";

const client = new Client();
await client.open("redis://localhost:6379");

class Album extends Entity {}

const albumSchema = new Schema(
  Album,
  {
    artist: { type: "string" },
    title: { type: "text" },
    year: { type: "number" },
    genres: { type: "string[]" },
    outOfPublication: { type: "boolean" }
  },
  { dataStructure: "JSON" }
);

// const albumRepository = client.fetchRepository(albumSchema);
const repository = new Repository(client, albumSchema);

// const album = albumRepository.createEntity();
// const album = repository.createEntity();
// album.artist = "Mushroomhead";
// album.title = "The Righteous & The Butterfly";
// album.year = 2014;
// album.genres = ["metal"];
// album.outOfPublication = true;

// const id = await albumRepository.save(album);
// const id = await repository.save(album);

const id = await repository.createAndSave({
  artist: "Mushroomhead",
  title: "The Righteous & The Butterfly",
  title: "The Righteous & The Butterfly",
  year: 2014,
  genres: ["metal"],
  outOfPublication: true
});

console.log(id);
Enter fullscreen mode Exit fullscreen mode

and still,

/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907
    const id = this.schema.generateId();
                           ^

TypeError: this.schema.generateId is not a function
    at Repository.createEntity (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:907:28)
    at Repository.createAndSave (/home/user/Projects/web/ARMS-redis/node_modules/redis-om/dist/index.js:915:25)
    at file:///home/user/Projects/web/ARMS-redis/src/index.js:34:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Enter fullscreen mode Exit fullscreen mode

At this moment, I can feel the stress and tiredness on my skull (Ghost rider). I watched some anime and walk about a bit. First things first, I went back to the main article about the hackathon and I started glancing through bit by bit, looking for clues. redis.io. Is there any different between the two, redis.com? I don't know so I couldn't tell. redis.io, I read, The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. and I remembered I have read it before or something like that. I click on DOCS as any developer would and before I knew I was at, redis-stack. I realized, redis-stack is not just the same as redis but Extends Redis with modern data models and processing engines. Includes documentation for the bundled Redis modules and RedisInsight.

To conclude, I wasted the whole day looking in the wrong direction, doing the right thing. I installed redis-stack and used this solution to fix another, Could not create server TCP listening socket *:6379.

I am going to sleep as a result. I think I will do well tomorrow.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .