Skip to content

Latest commit

 

History

History
231 lines (140 loc) · 5.66 KB

Repository.md

File metadata and controls

231 lines (140 loc) · 5.66 KB

redis-om / Repository

Class: Repository<TEntity>

A repository is the main interaction point for reading, writing, and removing Entities from Redis. Create one by passing in a Schema and a Client. Then use the Repository.fetch, Repository.save, and Repository.remove methods to manage your data:

let repository = new Repository<Foo>(schema, client);

let foo = await repository.fetch('01FK6TCJBDK41RJ766A4SBWDJ9');
foo.aString = 'bar';
foo.aBoolean = false;
await repository.save(foo);

Be sure to use the repository to create a new instance of Entity you want to create before you save it:

let foo = await repository.createEntity();
foo.aString = 'bar';
foo.aBoolean = false;
await repository.save(foo);

If you want to the Repository.search method, you need to create an index first, and you need RediSearch or RedisJSON installed on your instance of Redis:

await repository.createIndex();
let entities = await repository.search()
  .where('aString').eq('bar')
  .and('aBoolean').is.false().returnAll();

Type parameters

Name Type Description
TEntity extends Entity The type of Entity that this repository manages.

Table of contents

Constructors

Methods

Constructors

constructor

new Repository<TEntity>(schema, client)

Constructs a new Repository.

Type parameters

Name Type Description
TEntity extends Entity<TEntity> The type of Entity that this repository manages.

Parameters

Name Type Description
schema Schema<TEntity> The Schema for this Repository.
client Client An open Client.

Defined in

lib/repository/repository.ts:60

Methods

createEntity

createEntity(): TEntity

Creates an empty Entity with a populated Entity.entityId property.

Returns

TEntity

A newly created Entity.

Defined in

lib/repository/repository.ts:92


createIndex

createIndex(): Promise<void>

Creates an index in Redis for use by the Repository.search method. Requires that RediSearch or RedisJSON is installed on your instance of Redis.

Returns

Promise<void>

Defined in

lib/repository/repository.ts:71


dropIndex

dropIndex(): Promise<void>

Removes an existing index from Redis. Use this method if you want to swap out you index because your Entity has changed. Requires that RediSearch or RedisJSON is installed on your instance of Redis.

Returns

Promise<void>

Defined in

lib/repository/repository.ts:84


fetch

fetch(id): Promise<TEntity>

Read and return an Entity from Redis with the given id. If the Entity is not found, returns an Entity with all properties set to null.

Parameters

Name Type Description
id string The ID of the Entity you seek.

Returns

Promise<TEntity>

The matching Entity.

Defined in

lib/repository/repository.ts:130


remove

remove(id): Promise<void>

Remove an Entity from Redis with the given id. If the Entity is not found, does nothing.

Parameters

Name Type Description
id string The ID of the Entity you with to delete.

Returns

Promise<void>

Defined in

lib/repository/repository.ts:151


save

save(entity): Promise<string>

Save the Entity to Redis. If it already exists, it will be updated. If it doesn't exist, it will be created.

Parameters

Name Type Description
entity TEntity The Entity to save.

Returns

Promise<string>

The ID of the Entity just saved.

Defined in

lib/repository/repository.ts:103


search

search(): Search<TEntity>

Kicks off the processes of building a query. Requires that RediSearch or RedisJSON is installed on your instance of Redis.

Returns

Search<TEntity>

A Search object.

Defined in

lib/repository/repository.ts:162