redis-om / Repository
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:
let repository = client.fetchRepository<Foo>(schema);
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 an 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();
Name | Type | Description |
---|---|---|
TEntity |
extends Entity |
The type of Entity that this repository manages. |
• Protected
client: Client
lib/repository/repository.ts:56
▸ createAndSave(data?
): Promise
<TEntity
>
Creates and saves an Entity. Equivalent of calling Repository.createEntity followed by Repository.save.
Name | Type | Description |
---|---|---|
data |
EntityCreationData |
Optional values with which to initialize the entity. |
Promise
<TEntity
>
The newly created and saved Entity.
lib/repository/repository.ts:150
▸ createEntity(data?
): TEntity
Creates an Entity with a populated Entity.entityId property.
Name | Type | Description |
---|---|---|
data |
EntityCreationData |
Optional values with which to initialize the entity. |
TEntity
A newly created Entity.
lib/repository/repository.ts:115
▸ 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.
Promise
<void
>
lib/repository/repository.ts:69
▸ 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.
Promise
<void
>
lib/repository/repository.ts:97
▸ expire(id
, ttlInSeconds
): Promise
<void
>
Set the time to live of the Entity. If the Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity to set and expiration for. |
ttlInSeconds |
number |
THe time to live in seconds. |
Promise
<void
>
lib/repository/repository.ts:185
▸ 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
.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity you seek. |
Promise
<TEntity
>
The matching Entity.
lib/repository/repository.ts:163
▸ remove(id
): Promise
<void
>
Remove an Entity from Redis with the given id. If the Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity you with to delete. |
Promise
<void
>
lib/repository/repository.ts:174
▸ 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.
Name | Type | Description |
---|---|---|
entity |
TEntity |
The Entity to save. |
Promise
<string
>
The ID of the Entity just saved.
lib/repository/repository.ts:132
▸ 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.
Search
<TEntity
>
A Search object.
lib/repository/repository.ts:196
▸ 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.
Name | Type |
---|---|
query |
string |
RawSearch
<TEntity
>
A RawSearch object.