Class: Repository
July 15, 2024 · View on GitHub
redis-om / Repository
Class: Repository<T>
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()
Type parameters
| Name | Type |
|---|---|
T | extends Entity = Record<string, any> |
Table of contents
Constructors
Methods
Constructors
constructor
• new Repository<T>(schema, clientOrConnection)
Creates a new Repository.
Type parameters
| Name | Type |
|---|---|
T | extends Entity = Record<string, any> |
Parameters
| Name | Type | Description |
|---|---|---|
schema | Schema<T> | The schema defining that data in the repository. |
clientOrConnection | Client | RedisConnection | A client to talk to Redis. |
Defined in
lib/repository/repository.ts:56
Methods
createIndex
▸ 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.
Returns
Promise<void>
Defined in
lib/repository/repository.ts:71
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 and RedisJSON are installed on your instance of Redis.
Returns
Promise<void>
Defined in
lib/repository/repository.ts:109
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: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.
Parameters
| Name | Type | Description |
|---|---|---|
ids | string[] | The IDs of the Entities you wish to delete. |
ttlInSeconds | number | The time to live in seconds. |
Returns
Promise<void>
Defined in
lib/repository/repository.ts:251
expireAt
▸ expireAt(id, expirationDate): Promise<void>
Use Date object to set the Entity's time to live. If the Entity is not found, does nothing.
Parameters
| Name | Type | Description |
|---|---|---|
id | string | The ID of the Entity to set an expiration date for. |
expirationDate | Date | The time the data should expire. |
Returns
Promise<void>
Defined in
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.
Parameters
| Name | Type | Description |
|---|---|---|
ids | string[] | The IDs of the Entities to set an expiration date for. |
expirationDate | Date | The time the data should expire. |
Returns
Promise<void>
Defined in
lib/repository/repository.ts:279
fetch
▸ 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.
Parameters
| Name | Type | Description |
|---|---|---|
id | string | The ID of the Entity you seek. |
Returns
Promise<T>
The matching Entity.
Defined in
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.
Parameters
| Name | Type | Description |
|---|---|---|
...ids | string[] | The IDs of the Entities you seek. |
Returns
Promise<T[]>
The matching Entities.
Defined in
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.
Parameters
| Name | Type | Description |
|---|---|---|
ids | string[] | The IDs of the Entities you seek. |
Returns
Promise<T[]>
The matching Entities.
Defined in
lib/repository/repository.ts:189
remove
▸ remove(id): Promise<void>
Remove an Entity from Redis for the given id. If the Entity is not found, does nothing.
Parameters
| Name | Type | Description |
|---|---|---|
id | string | The ID of the Entity you wish to delete. |
Returns
Promise<void>
Defined in
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.
Parameters
| Name | Type | Description |
|---|---|---|
...ids | string[] | The IDs of the Entities you wish to delete. |
Returns
Promise<void>
Defined in
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.
Parameters
| Name | Type | Description |
|---|---|---|
ids | string[] | The IDs of the Entities you wish to delete. |
Returns
Promise<void>
Defined in
lib/repository/repository.ts:221
save
▸ save(entity): Promise<T>
Insert or update an Entity to Redis using its entityId property if present. If it's not, one is generated.
Parameters
| Name | Type | Description |
|---|---|---|
entity | T | The Entity to save. |
Returns
Promise<T>
A copy of the provided Entity with EntityId and EntityKeyName properties added.
Defined in
lib/repository/repository.ts:134
▸ save(id, entity): Promise<T>
Insert or update the Entity to Redis using the provided entityId.
Parameters
| Name | Type | Description |
|---|---|---|
id | string | The id to save the Entity under. |
entity | T | The Entity to save. |
Returns
Promise<T>
A copy of the provided Entity with EntityId and EntityKeyName properties added.
Defined in
lib/repository/repository.ts:143
search
▸ search(): Search<T>
Kicks off the process of building a query. Requires that RediSearch (and optionally RedisJSON) be installed on your instance of Redis.
Returns
Search<T>
A Search object.
Defined in
lib/repository/repository.ts:302
searchRaw
▸ 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.
Parameters
| Name | Type |
|---|---|
query | string |
Returns
RawSearch<T>
A RawSearch object.