redis-om / Repository
A repository is the main interaction point for reading, writing, and removing Entities from Redis. Create one by calling fetchRepository and passing in a Schema. Then use the fetch, save, and remove methods to manage your data:
const repository = client.fetchRepository(schema)
const foo = await repository.fetch('01FK6TCJBDK41RJ766A4SBWDJ9')
foo.aString = 'bar'
foo.aBoolean = false
await repository.save(foo)
Use the repository to create a new instance of an Entity before you save it:
const foo = await repository.createEntity()
foo.aString = 'bar'
foo.aBoolean = false
await repository.save(foo)
If you want to use the 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()
Name | Type |
---|---|
T |
extends Entity = Record <string , any > |
• new Repository<T
>(schema
, clientOrConnection
)
Creates a new Repository.
Name | Type |
---|---|
T |
extends Entity = Record <string , any > |
Name | Type | Description |
---|---|---|
schema |
Schema <T > |
The schema defining that data in the repository. |
clientOrConnection |
Client | RedisConnection |
A client to talk to Redis. |
lib/repository/repository.ts:56
▸ createIndex(): Promise
<void
>
Creates an index in Redis for use by the search method. Does not create a new index if the index hasn't changed. Requires that RediSearch and RedisJSON are 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 your index because your Entity has changed. Requires that RediSearch and RedisJSON are installed on your instance of Redis.
Promise
<void
>
lib/repository/repository.ts:109
▸ 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:242
▸ expire(ids
, ttlInSeconds
): Promise
<void
>
Set the time to live of the Entities in Redis with the given ids. If a particular Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
ids |
string [] |
The IDs of the Entities you wish to delete. |
ttlInSeconds |
number |
The time to live in seconds. |
Promise
<void
>
lib/repository/repository.ts:251
▸ expireAt(id
, expirationDate
): Promise
<void
>
Use Date object to set the Entity's time to live. If the Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity to set an expiration date for. |
expirationDate |
Date |
The time the data should expire. |
Promise
<void
>
lib/repository/repository.ts:270
▸ expireAt(ids
, expirationDate
): Promise
<void
>
Use Date object to set the Entities in Redis with the given ids. If a particular Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
ids |
string [] |
The IDs of the Entities to set an expiration date for. |
expirationDate |
Date |
The time the data should expire. |
Promise
<void
>
lib/repository/repository.ts:279
▸ fetch(id
): Promise
<T
>
Read and return an Entity from Redis for the given id. If the Entity is not found, returns an empty Entity.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity you seek. |
Promise
<T
>
The matching Entity.
lib/repository/repository.ts:171
▸ fetch(...ids
): Promise
<T
[]>
Read and return the Entities from Redis with the given IDs. If a particular Entity is not found, returns that Entity as empty.
Name | Type | Description |
---|---|---|
...ids |
string [] |
The IDs of the Entities you seek. |
Promise
<T
[]>
The matching Entities.
lib/repository/repository.ts:180
▸ fetch(ids
): Promise
<T
[]>
Read and return the Entities from Redis with the given IDs. If a particular Entity is not found, returns that Entity as empty.
Name | Type | Description |
---|---|---|
ids |
string [] |
The IDs of the Entities you seek. |
Promise
<T
[]>
The matching Entities.
lib/repository/repository.ts:189
▸ remove(id
): Promise
<void
>
Remove an Entity from Redis for the given id. If the Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
id |
string |
The ID of the Entity you wish to delete. |
Promise
<void
>
lib/repository/repository.ts:205
▸ remove(...ids
): Promise
<void
>
Remove the Entities from Redis for the given ids. If a particular Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
...ids |
string [] |
The IDs of the Entities you wish to delete. |
Promise
<void
>
lib/repository/repository.ts:213
▸ remove(ids
): Promise
<void
>
Remove the Entities from Redis for the given ids. If a particular Entity is not found, does nothing.
Name | Type | Description |
---|---|---|
ids |
string [] |
The IDs of the Entities you wish to delete. |
Promise
<void
>
lib/repository/repository.ts:221
▸ save(entity
): Promise
<T
>
Insert or update an Entity to Redis using its entityId property if present. If it's not, one is generated.
Name | Type | Description |
---|---|---|
entity |
T |
The Entity to save. |
Promise
<T
>
A copy of the provided Entity with EntityId and EntityKeyName properties added.
lib/repository/repository.ts:134
▸ save(id
, entity
): Promise
<T
>
Insert or update the Entity to Redis using the provided entityId.
Name | Type | Description |
---|---|---|
id |
string |
The id to save the Entity under. |
entity |
T |
The Entity to save. |
Promise
<T
>
A copy of the provided Entity with EntityId and EntityKeyName properties added.
lib/repository/repository.ts:143
▸ search(): Search
<T
>
Kicks off the process of building a query. Requires that RediSearch (and optionally RedisJSON) be installed on your instance of Redis.
Search
<T
>
A Search object.
lib/repository/repository.ts:302
▸ searchRaw(query
): RawSearch
<T
>
Creates a search that bypasses 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.
Refer to https://redis.io/docs/stack/search/reference/query_syntax/ for details on RediSearch query syntax.
Query
The raw RediSearch query you want to rune.
Name | Type |
---|---|
query |
string |
RawSearch
<T
>
A RawSearch object.