Skip to content

Latest commit

 

History

History
423 lines (257 loc) · 10.4 KB

Repository.md

File metadata and controls

423 lines (257 loc) · 10.4 KB

redis-om / Repository

Class: 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()

Table of contents

Constructors

Methods

Constructors

constructor

new Repository(schema, clientOrConnection)

Creates a new Repository. Equivalent to calling fetchRepository.

Parameters

Name Type Description
schema Schema The schema defining that data in the repository.
clientOrConnection Client | RedisConnection -

Methods

createAndSave

createAndSave(entityData): Promise<Entity>

Creates and saves an Entity. Equivalent of calling createEntity followed by save.

Parameters

Name Type Description
entityData EntityData The data to be saved.

Returns

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.

Parameters

Name Type Description
id string The entityId to save to.
entityData EntityData The data to be saved.

Returns

Promise<Entity>

The newly created and saved Entity.


createEntity

createEntity(): Entity

Creates an empty Entity with a generated entityId property.

Returns

Entity

A newly created Entity.

createEntity(id): Entity

Creates an empty Entity with a provided entityId.

Parameters

Name Type Description
id string The provided entityId.

Returns

Entity

A newly created Entity.

createEntity(entityData): Entity

Creates an Entity populated with provided data and a generated entityId property.

Parameters

Name Type Description
entityData EntityData The provided entity data.

Returns

Entity

A newly created Entity.

createEntity(id, entityData): Entity

Creates an Entity populated with provided data and a provided entityId.

Parameters

Name Type Description
id string The provided entityId.
entityData EntityData The provided entity data.

Returns

Entity

A newly created Entity.


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>


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>


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>

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 -

Returns

Promise<void>


fetch

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.

Parameters

Name Type Description
id string The ID of the Entity you seek.

Returns

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.

Parameters

Name Type Description
...ids string[] The IDs of the Entities you seek.

Returns

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.

Parameters

Name Type Description
ids string[] The IDs of the Entities you seek.

Returns

Promise<Entity[]>

The matching Entities.


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>

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>

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>


save

save(entity): Promise<string>

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 Entity The Entity to save.

Returns

Promise<string>

The provided or generated entityId.

save(id, entity): Promise<string>

Insert or update the Entity to Redis using the provided entityId.

Parameters

Name Type Description
id string The Entity to save.
entity Entity The Entity to save.

Returns

Promise<string>

The provided or generated entityId.


search

search(): Search

Kicks off the process of building a query. Requires that RediSearch (and optionally RedisJSON) be installed on your instance of Redis.

Returns

Search

A Search object.


searchRaw

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.

Parameters

Name Type
query string

Returns

RawSearch

A RawSearch object.