DEV Community

Michael Otu
Michael Otu

Posted on

Now we are talking redis

I have had my mishap. I turned right and left, not knowing what is wrong. Typescript saved the day and now I have to learn some specifics of Typescript.

I started with redis-om version 0.2.0. Then upgraded to version 0.3.6 which is the current version.

Create a connection

// client.ts
import { Client } from "redis-om";

const REDIS_URI = process.env.REDIS_URI;

const client: Client = new Client();

const connectRedis = async () => {
  if (!client.isOpen()) {
    await client.open(REDIS_URI);
  }

  const command = ["PING", "Redis server running"];
  const log = await client.execute(command)
  console.log(log);
};

connectRedis();

export default client;
Enter fullscreen mode Exit fullscreen mode

Create Schema

The only thing here that is different from the others so far is that this is ts and according to the docs, we have to create an interface with the same name as the entity.

// schema.ts
import { Entity, Schema, SchemaDefinition } from "redis-om";

// This is necessary for ts
interface UserEntity {
  username: string;
  password: string;
  email: string;
}

class UserEntity extends Entity {}

const UserSchemaStructure: SchemaDefinition = {
  username: {
    type: "string"
  },
  password: {
    type: "string"
  },
  email: {
    type: "string"
  }
};

export default new Schema(UserEntity, UserSchemaStructure, {
  dataStructure: "JSON"
});
Enter fullscreen mode Exit fullscreen mode

Create a repository

From what I have done so far, we can create a repository using new Repository(schema, client) or client.fetchRepository(schema). The latter worked. The form gave an error that Repository is an abstract class. So we have to extend it and implement its abstract methods, writeEntity and readEntity. I went with the former since it makes my work faster.

// repository.ts
import { Entity, Repository } from "redis-om";
import client from "./client";
import schema from "./schema";

const repository: Repository<Entity> = client.fetchRepository(schema);

export default repository;
Enter fullscreen mode Exit fullscreen mode

I look like a ts noob.

Create a row

We will use the repository to create a new user. From what I have done so far, we can do:

// index.ts
import repository from "./repository";

const user = await repository.createAndSave({
  username: "johndoe",
  email: "johndoe@gmail.com",
  password: "PASSjohndoe"
});

console.log(user);

// Output from the console log
/* 
{
  entityId: "01GB1W8GFDDX6FQN9H7F4T1808",
  username: "johndoe",
  password: "PASSjohndoe"
  email: "johndoe@gmail.com"
}
*/
Enter fullscreen mode Exit fullscreen mode

or

// index.ts
import repository from "./repository";

const user = repository.createEntity({
  username: "johndoe",
  email: "johndoe@gmail.com",
  password: "PASSjohndoe"
});

const id = await repository.save(user);

// Output from the console log
// 01GB1W8GFDDX6FQN9H7F4T1808 // ID of the row created
Enter fullscreen mode Exit fullscreen mode

Conclusion

There is not much to say here than, to keep trying and sleep when you have to. Even though I was doing the right thing all the time and not getting the output I am expecting, I kept looking for others ways and posting the issue I was facing on other platforms, hoping another person had faced the same issue. Typescript worked for me even though I never thought of using Typescript in the first place. Now another path to learning has opened.

Latest comments (1)

Collapse
 
seek4samurai profile image
Gourav Singh Rawat • Edited

I understand sometimes it gets really frustrating when nothing works as plan. But it's about your plans that drive you.
Here are some tips that I follow when nothing works and all I see are errors:- (Spoiler: Last three are most important)

1. Read the problem...
Wherever the error is, try to read it and understand it and guess on how you can possibly fix it.

2. Try Googling the Error...
Don't search the whole error log, try looking for a general error statements or error codes. Search them on Stackoverflow or Github though they will show up on google as well.

3. Try to look for keywords...
I've figured out a trick like if you can't actually find any solution on the Google try looking for keywords in the error logs. Recently I was installing an Application that was not exactly stating the error in details I tried looking for it but could not find anything. Later I decided to just go through the whole error logs manually (though I could barely understand a word), but still I read it. I saw somethings related to missing some modules like mitmproxy and it's versions I tried googling it and it's versions, and finally got an answer in Github issues section.

4. Call for help...
Try to join forums, discord servers, communities and Stackoverflow or any other Stack exchange website that could help you. This is the 4th point because before asking try searching by yourself as much as you can. Over there give a nicely formatted question and provide all things they can possibly use to help you. Show them what errors you're getting, what were you trying to do and where it's not working as expected.

5. It's OK to lose sometimes...
One day I worked whole night on a problem, trying to solve and at morning 4 am, I fixed it. But today I actually laugh at myself about that day like was that worth it?
Try to convince yourself and take rest. Mostly when we are tired we miss most basic things, getting rest is equally important.

6. Try not to panic on your keyboard or other things...
Frustrating on things, breaking them makes me get into more anxiety actually, I've felt that, I decided to quit games that make me rage sometimes and stopped watching things that makes me even a little angry. Controlling your anger is a Godly strength in itself :)

7. Trying to solve an Error and failing doesn't count as Time wasted...(I believe)
Yes :) you don't waste your time in solving errors, you actually confront things that actually challenge developers. Most people who fail to become developers quit because of things that tried fighting and solving, no matter if you failed or succeeded, your day is not counted as day wasted, you learned something out of it!

Good Luck!!!