Socket
Socket
Sign inDemoInstall

redisk

Package Overview
Dependencies
6
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redisk

TypeScript ORM for Redis.


Version published
Maintainers
1
Created

Readme

Source

Redisk

npm version

Redisk is a TypeScript ORM library for Redis.

Features:

  • Store entities.
  • Single relation support.
  • Unique keys support.
  • Retrieve entities by his primary keys or his unique keys.
  • Indexes support.
  • List entities with common filters, like limit, count and sort by.
  • Find entities with multiple conditions.
  • Search (Similar to LIKE in SQL)
  • And much more.

Quick overview

const redisk = Redisk.init({url: 'redis://127.0.0.1:6379/0'});

@Entity('user')
export class User {

  @Primary()
  @Property()
  public readonly id: string;

  @Property()
  public readonly name: string;

  constructor(
      id: string,
      name: string,
    ) {
      this.id = id;
      this.name = name;
  }
}

await redisk.save(new User('::id::', 'Foo'));

console.log(await redisk.getOne(User, '::id::'));

Installation

npm install redisk --save

Contents

Connection

const redisk = Redisk.init(options);

options

PropertyDescription
urlURL of the Redis server. Format [redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
hostHost of the Redis server
portPort of the Redis server
dbNumber of the db (Default: 0)
passwordPassword of the Redis server

Closing connection to Redis:

await redisk.close();

Models

Model definition

@Entity('user', { canBeListed: true })
export class User {

  @Primary()
  @Property()
  public readonly id: string;

  @Property({sortable: false, searchable: true})
  public readonly name: string;

  @Unique()
  @Property()
  public readonly email: string;

  @Index()
  @Property()
  public readonly color: string;

  @HasOne(Group, {cascadeInsert: true, cascadeUpdate: true})
  @Property()
  public readonly group: Group;

  @Property({sortable: true, searchable: false})
  public readonly created: Date;

  constructor(
      id: string,
      name: string,
      email: string,
      color: string,
      group: Group,
      created: Date,
    ) {
      this.id = id;
      this.name = name;
      this.email = email;
      this.color = color;
      this.group = group;
      this.created = created;
  }
}

Entity

Use the decorator Entity to convert your class into a Redisk entity.

You can pass the option canBeListed to 'false' (Default is true) to save some space.

@Entity('user', { canBeListed: true })
export class User {
}

Property

The decorator Property is used to save the fields into redis. Optionally, you can pass the options sortable if you want to use the field to sort in the 'list' method or searchable if you want to use pattern matching in this field.

Both options are false by default.

@Entity('user')
export class User {

    @Property({sortable: true, searchable: false})
    public readonly created: Date;

}
Supported types

Redisk support multiple types to store and query.

  • String
  • Date (Will be saved as a timestamp)
  • Boolean
  • Number

All other types will be converted to a string.

Primary

Primary decorator is used to define the primary key of the entity. It can only be one primary key and his value must be unique for all the same entities.

@Entity('user')
export class User {

  @Primary()
  @Property()
  public readonly id: string;
}

Unique

This decorator is used to make the value of this field unique for all the same entities. Then you can use it to query the entity.

@Entity('user')
export class User {

  @Unique()
  @Property()
  public readonly email: string;
}

Index

Use the decorator Index on the fields that you want to query later with the find() method.

@Entity('user')
export class User {

  @Index()
  @Property()
  public readonly color: string;
}

Embedding other entities

You can make one to one relations with the HasOne decorator.

Cascade inserts and updates are supported.

@Entity('user')
export class User {

  @HasOne(Group, {cascadeInsert: true, cascadeUpdate: true})
  @Property()
  public readonly group: Group;
}

Queries

Save and update

await redisk.save(new User(id, name));

Get by primary key

await redisk.getOne(User, id);

Get by unique key

const value = 'john@doe.com';
const uniqueKeyName = 'email';
await redisk.getOne(User, value, uniqueKeyName);

Count

await redisk.count(User);

List all

await redisk.list(User); // Returns an array of entities

const limit = 10;
const offset = 0;
await redis.list(User, limit, offset); // Returns 10 user entities

await redisk.list(User, undefined, undefined, {
    field: 'created',
    strategy: 'DESC',
}); // Returns an array of entities sorted by his creation date in descending order

Find all by index

Simple
const conditions = [
    {
        key: 'color',
        value: 'red',
    },
];
await redisk.find(User, conditions, limit, offset); // Returns an array of entities that match the conditions
Multiple conditions
const conditions = [
    {
        key: 'color',
        value: 'red',
    },
    {
        key: 'color',
        value: 'blue',
    },
];
await redisk.find(User, conditions, limit, offset, 'OR'); // Returns an array of entities that his color field is 'red' or 'blue'
const conditions = [
    {
        key: 'color',
        value: 'red',
    },
    {
        key: 'food',
        value: 'avocado',
    },
];
await redisk.find(User, conditions, limit, offset, 'AND'); // Returns an array of entities that his color field is 'red' and his food field is 'avocado'

Pattern matching

You can search entities by properties marked as searchables.

const condition = {
    key: 'name',
    value: 'John',
};
const maxNumberOfResults = 10;
await redisk.search(User, condition, maxNumberOfResults);

Delete

await redisk.delete(User, id);

Keywords

FAQs

Last updated on 08 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc