redis-om / Repository
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();
Name | Type | Description |
---|---|---|
TEntity |
extends Entity |
The type of Entity that this repository manages. |
• new Repository<TEntity
>(schema
, client
)
Constructs a new Repository.
Name | Type | Description |
---|---|---|
TEntity |
extends Entity <TEntity > |
The type of Entity that this repository manages. |
Name | Type | Description |
---|---|---|
schema |
Schema <TEntity > |
The Schema for this Repository. |
client |
Client |
An open Client. |
lib/repository/repository.ts:60
▸ createAndSave(data?
): Promise
<TEntity
>
Creates and saves an Entity. Equivalent of calling {@link Entity.createEntity} followed by {@link Entity.save}.
Name | Type | Description |
---|---|---|
data |
Record <string , any > |
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 |
Record <string , any > |
Optional values with which to initialize the entity. |
TEntity
A newly created Entity.
lib/repository/repository.ts:107
▸ 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:71
▸ 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.
Promise
<void
>
lib/repository/repository.ts:90
▸ 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:184
▸ 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:124
▸ search(): Search
<TEntity
>
Kicks off the processes of building a query. Requires that RediSearch or RedisJSON is installed on your instance of Redis.
Search
<TEntity
>
A Search object.