You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

ts-redis-entity

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-redis-entity

A package that helps you to save a simple entity structure to redis. With various features like expire, update with conditions.

0.0.8
latest
Source
npmnpm
Version published
Weekly downloads
3
-25%
Maintainers
1
Weekly downloads
 
Created
Source

ts-redis-entity

NPM version Test Test coverage David deps

A package that helps you to save a simple entity structure to redis. With various features like expire, update with conditions.

The entity structure is saved using hmset.

Quick Start

import {BaseEntity, Column, Entity, tsRedisEntity} from "ts-redis-entity";

@Entity({namespace: "QuickStart", connection: "default"})
class QuickStart extends BaseEntity {
    @Column()
    public id: string = "";

    @Column()
    public number: number = 10;
}

async function quickStartExamples() {
    await tsRedisEntity.addConnection("default", { port: 6380, host: "127.0.0.1" });
    const entity = QuickStart.new({id: "1", number: 10});
    await entity.create({expire: 60}); // expire in 60 seconds
    const foundEntity = await QuickStart.find("1");
    if (foundEntity) {
        const getValues = foundEntity.getValues();
        foundEntity.setValues({number: 20});
        await foundEntity.update({expire: new Date(new Date().getTime() + 60 * 1000)});
    }
}

More Examples


import {BaseEntity, Column, Entity, errorCodes, OperationError, tsRedisEntity} from "ts-redis-entity";

@Entity({namespace: "TestingEntity", connection: "default"})
class TestingEntity extends BaseEntity {
    @Column()
    public id: string = "";

    @Column()
    public value: string = "";

    @Column()
    public number?: number = 0;

    @Column()
    public date?: Date = new Date();

    @Column({parseDate: true})
    public object?: any = {};

    @Column()
    public array?: any[] = [];

    @Column()
    public boolean?: boolean = false;

    @Column()
    public nullableString: string | null = null;
}

async function saveIfExamples() {
    const entity = await TestingEntity.new({id: "prefix", number: 10}).create();
    const foundEntity = await TestingEntity.find("prefix");
    
    entity.number = 11;
    await entity.update();
    
    if (foundEntity) {
        try {
            foundEntity.number = 12;
            await foundEntity.updateIf({number: 11});
        } catch (err) {
            if (err instanceof OperationError) {
                console.assert(err.errorCode === errorCodes.conditionNotMatch);
            }
        }
    }
}

async function finaAllExamples() {
    const entity1 = await TestingEntity.new({id: "prefix1"}).create();
    const entity2 = await TestingEntity.new({id: "prefix2"}).create();
    const keys = await TestingEntity.findAllIds("prefix");
    // keys = ["prefix1", "prefix2"]
    
    const stream = TestingEntity.scanAllIds("prefix1");
    stream.on("data", ids => console.log(ids));
    stream.on("end", () => console.log("end"));
}

Keywords

redis

FAQs

Package last updated on 03 Oct 2022

Did you know?

Socket

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