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()
• new Repository(schema
, clientOrConnection
)
Creates a new Repository. Equivalent to calling fetchRepository.
Name | Type | Description |
---|---|---|
schema |
Schema |
The schema defining that data in the repository. |
clientOrConnection |
Client | RedisConnection |
- |
▸ createAndSave(entityData
): Promise
<Entity
>
Creates and saves an Entity. Equivalent of calling createEntity followed by save.
Name | Type | Description |
---|---|---|
entityData |
EntityData |
The data to be saved. |
Promise
<Entity
>
The newly created and saved Entity.
▸ createAndSave(id
, entityData
): Promise
<Entity
>
Creates and saves an Entity to using the provided entityId. Equivalent of calling createEntity followed by save.
Name | Type | Description |
---|---|---|
id |
string |
The entityId to save to. |
entityData |
EntityData |
The data to be saved. |
Promise
<Entity
>
The newly created and saved Entity.
▸ createEntity(): Entity
Creates an empty Entity with a generated entityId property.
A newly created Entity.
▸ createEntity(id
): Entity
Creates an empty Entity with a provided entityId.
Name | Type | Description |
---|---|---|
id |
string |
The provided entityId. |
A newly created Entity.
▸ createEntity(entityData
): Entity
Creates an Entity populated with provided data and a generated entityId property.
Name | Type | Description |
---|---|---|
entityData |
EntityData |
The provided entity data. |
A newly created Entity.
▸ createEntity(id
, entityData
): Entity
Creates an Entity populated with provided data and a provided entityId.
Name | Type | Description |
---|---|---|
id |
string |
The provided entityId. |
entityData |
EntityData |
The provided entity data. |
A newly created Entity.
▸ 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
>
▸ 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
>
▸ 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
>
▸ 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 |
- |
Promise
<void
>
▸ fetch(id
): Promise
<Entity
>
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
<Entity
>
The matching Entity.
▸ fetch(...ids
): Promise
<Entity
[]>
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
<Entity
[]>
The matching Entities.
▸ fetch(ids
): Promise
<Entity
[]>
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
<Entity
[]>
The matching Entities.
▸ 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
>
▸ 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
>
▸ 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
>
▸ save(entity
): Promise
<string
>
Insert or update an Entity to Redis using its entityId property if present. If it's not, one is generated.
Name | Type | Description |
---|---|---|
entity |
Entity |
The Entity to save. |
Promise
<string
>
The provided or generated entityId.
▸ save(id
, entity
): Promise
<string
>
Insert or update the Entity to Redis using the provided entityId.
Name | Type | Description |
---|---|---|
id |
string |
The Entity to save. |
entity |
Entity |
The Entity to save. |
Promise
<string
>
The provided or generated entityId.
▸ search(): Search
Kicks off the process of building a query. Requires that RediSearch (and optionally RedisJSON) be installed on your instance of Redis.
A Search object.
▸ searchRaw(query
): RawSearch
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 |
A RawSearch object.