Skip to content

Latest commit

 

History

History
340 lines (203 loc) · 8.11 KB

Repository.md

File metadata and controls

340 lines (203 loc) · 8.11 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 calling Client.fetchRepository and passing in a Schema. Then use the Repository.fetch, Repository.save, and Repository.remove methods to manage your data:

const repository = client.fetchRepository<Foo>(schema);

const 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 an Entity you want to create before you save it:

const 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();
const 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

Properties

Methods

Properties

client

Protected client: Client

Defined in

lib/repository/repository.ts:49


schema

Protected schema: Schema<TEntity>

Defined in

lib/repository/repository.ts:50

Methods

createAndSave

createAndSave(data?): Promise<TEntity>

Creates and saves an Entity. Equivalent of calling Repository.createEntity followed by Repository.save.

Parameters

Name Type Description
data EntityData Optional values with which to initialize the entity.

Returns

Promise<TEntity>

The newly created and saved Entity.

Defined in

lib/repository/repository.ts:130


createEntity

createEntity(data?): TEntity

Creates an Entity with a populated Entity.entityId property.

Parameters

Name Type Description
data EntityData Optional values with which to initialize the entity.

Returns

TEntity

A newly created Entity.

Defined in

lib/repository/repository.ts:108


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:62


dropIndex

dropIndex(): Promise<void>

Removes an existing index from Redis. Use this method if you want to swap out your 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:90


expire

expire(id, ttlInSeconds): Promise<void>

Set the time to live of the Entity. If the Entity is not found, does nothing.

Parameters

Name Type Description
id string The ID of the Entity to set and expiration for.
ttlInSeconds number THe time to live in seconds.

Returns

Promise<void>

Defined in

lib/repository/repository.ts:176


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:143


fetchMany

fetchMany(...ids): Promise<TEntity[]>

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

Parameters

Name Type Description
...ids string[] The IDs of the Entities you seek.

Returns

Promise<TEntity[]>

The matching Entities.

Defined in

lib/repository/repository.ts:155


remove

remove(...ids): Promise<void>

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

Parameters

Name Type
...ids string[]

Returns

Promise<void>

Defined in

lib/repository/repository.ts:164


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:119


search

search(): Search<TEntity>

Kicks off the process of building a query. Requires that RediSearch (and optionally RedisJSON) be is installed on your instance of Redis.

Returns

Search<TEntity>

A Search object.

Defined in

lib/repository/repository.ts:187


searchRaw

searchRaw(query): RawSearch<TEntity>

Creates a search that bypassed Redis OM and instead allows you to execute a raw RediSearch query. Requires that RediSearch (and optionally RedisJSON) be installed on your instance of Redis.

query The raw RediSearch query you want to rune.

Parameters

Name Type
query string

Returns

RawSearch<TEntity>

A RawSearch object.

Defined in

lib/repository/repository.ts:199