1- import { Client , CreateOptions , RedisConnection , RedisHashData , RedisJsonData } from '../client'
2- import { Entity , EntityId , EntityKeyName } from '../entity'
3- import { buildRediSearchSchema } from '../indexer'
4- import { Schema } from '../schema'
5- import { Search , RawSearch } from '../search'
6- import { fromRedisHash , fromRedisJson , toRedisHash , toRedisJson } from '../transformer'
1+ import { Client , CreateOptions , RedisConnection , RedisHashData , RedisJsonData } from '../client'
2+ import { Entity , EntityId , EntityKeyName } from '../entity'
3+ import { buildRediSearchSchema } from '../indexer'
4+ import { Schema } from '../schema'
5+ import { RawSearch , Search } from '../search'
6+ import { fromRedisHash , fromRedisJson , toRedisHash , toRedisJson } from '../transformer'
77
88/**
99 * A repository is the main interaction point for reading, writing, and
@@ -41,19 +41,19 @@ import { fromRedisHash, fromRedisJson, toRedisHash, toRedisJson } from '../trans
4141 * .and('aBoolean').is.false().returnAll()
4242 * ```
4343 */
44- export class Repository {
44+ export class Repository < T extends Entity = Record < string , any > > {
4545
4646 // NOTE: Not using "#" private as the spec needs to check calls on this class. Will be resolved when Client class is removed.
47- private client : Client
48- #schema: Schema
47+ private readonly client : Client
48+ readonly #schema: Schema < T >
4949
5050 /**
5151 * Creates a new {@link Repository}.
5252 *
5353 * @param schema The schema defining that data in the repository.
54- * @param client A client to talk to Redis.
54+ * @param clientOrConnection A client to talk to Redis.
5555 */
56- constructor ( schema : Schema , clientOrConnection : Client | RedisConnection ) {
56+ constructor ( schema : Schema < T > , clientOrConnection : Client | RedisConnection ) {
5757 this . #schema = schema
5858 if ( clientOrConnection instanceof Client ) {
5959 this . client = clientOrConnection
@@ -131,7 +131,7 @@ export class Repository {
131131 * @param entity The Entity to save.
132132 * @returns A copy of the provided Entity with EntityId and EntityKeyName properties added.
133133 */
134- async save ( entity : Entity ) : Promise < Entity >
134+ async save ( entity : T ) : Promise < T >
135135
136136 /**
137137 * Insert or update the {@link Entity} to Redis using the provided entityId.
@@ -140,10 +140,10 @@ export class Repository {
140140 * @param entity The Entity to save.
141141 * @returns A copy of the provided Entity with EntityId and EntityKeyName properties added.
142142 */
143- async save ( id : string , entity : Entity ) : Promise < Entity >
143+ async save ( id : string , entity : T ) : Promise < T >
144144
145- async save ( entityOrId : Entity | string , maybeEntity ?: Entity ) : Promise < Entity > {
146- let entity : Entity | undefined
145+ async save ( entityOrId : T | string , maybeEntity ?: T ) : Promise < T > {
146+ let entity : T | undefined
147147 let entityId : string | undefined
148148
149149 if ( typeof entityOrId !== 'string' ) {
@@ -155,7 +155,7 @@ export class Repository {
155155 }
156156
157157 const keyName = `${ this . #schema. schemaName } :${ entityId } `
158- const clonedEntity = { ...entity , [ EntityId ] : entityId , [ EntityKeyName ] : keyName }
158+ const clonedEntity = { ...entity , [ EntityId ] : entityId , [ EntityKeyName ] : keyName } as T
159159 await this . writeEntity ( clonedEntity )
160160
161161 return clonedEntity
@@ -168,7 +168,7 @@ export class Repository {
168168 * @param id The ID of the {@link Entity} you seek.
169169 * @returns The matching Entity.
170170 */
171- async fetch ( id : string ) : Promise < Entity >
171+ async fetch ( id : string ) : Promise < T >
172172
173173 /**
174174 * Read and return the {@link Entity | Entities} from Redis with the given IDs. If
@@ -177,7 +177,7 @@ export class Repository {
177177 * @param ids The IDs of the {@link Entity | Entities} you seek.
178178 * @returns The matching Entities.
179179 */
180- async fetch ( ...ids : string [ ] ) : Promise < Entity [ ] >
180+ async fetch ( ...ids : string [ ] ) : Promise < T [ ] >
181181
182182 /**
183183 * Read and return the {@link Entity | Entities} from Redis with the given IDs. If
@@ -186,9 +186,9 @@ export class Repository {
186186 * @param ids The IDs of the {@link Entity | Entities} you seek.
187187 * @returns The matching Entities.
188188 */
189- async fetch ( ids : string [ ] ) : Promise < Entity [ ] >
189+ async fetch ( ids : string [ ] ) : Promise < T [ ] >
190190
191- async fetch ( ids : string | string [ ] ) : Promise < Entity | Entity [ ] > {
191+ async fetch ( ids : string | string [ ] ) : Promise < T | T [ ] > {
192192 if ( arguments . length > 1 ) return this . readEntities ( [ ...arguments ] )
193193 if ( Array . isArray ( ids ) ) return this . readEntities ( ids )
194194
@@ -246,6 +246,7 @@ export class Repository {
246246 * ids. If a particular {@link Entity} is not found, does nothing.
247247 *
248248 * @param ids The IDs of the {@link Entity | Entities} you wish to delete.
249+ * @param ttlInSeconds The time to live in seconds.
249250 */
250251 async expire ( ids : string [ ] , ttlInSeconds : number ) : Promise < void >
251252
@@ -298,7 +299,7 @@ export class Repository {
298299 *
299300 * @returns A {@link Search} object.
300301 */
301- search ( ) : Search {
302+ search ( ) : Search < T > {
302303 return new Search ( this . #schema, this . client )
303304 }
304305
@@ -313,20 +314,19 @@ export class Repository {
313314 * @query The raw RediSearch query you want to rune.
314315 * @returns A {@link RawSearch} object.
315316 */
316- searchRaw ( query : string ) : RawSearch {
317+ searchRaw ( query : string ) : RawSearch < T > {
317318 return new RawSearch ( this . #schema, this . client , query )
318319 }
319320
320- private async writeEntity ( entity : Entity ) : Promise < void > {
321- return this . #schema. dataStructure === 'HASH' ? this . writeEntityToHash ( entity ) : this . writeEntityToJson ( entity )
321+ private async writeEntity ( entity : T ) : Promise < void > {
322+ return this . #schema. dataStructure === 'HASH' ? this . # writeEntityToHash( entity ) : this . writeEntityToJson ( entity )
322323 }
323324
324- private async readEntities ( ids : string [ ] ) : Promise < Entity [ ] > {
325+ private async readEntities ( ids : string [ ] ) : Promise < T [ ] > {
325326 return this . #schema. dataStructure === 'HASH' ? this . readEntitiesFromHash ( ids ) : this . readEntitiesFromJson ( ids )
326327 }
327328
328- // TODO: make this actually private... like with #
329- private async writeEntityToHash ( entity : Entity ) : Promise < void > {
329+ async #writeEntityToHash( entity : Entity ) : Promise < void > {
330330 const keyName = entity [ EntityKeyName ] !
331331 const hashData : RedisHashData = toRedisHash ( this . #schema, entity )
332332 if ( Object . keys ( hashData ) . length === 0 ) {
@@ -336,14 +336,13 @@ export class Repository {
336336 }
337337 }
338338
339- private async readEntitiesFromHash ( ids : string [ ] ) : Promise < Entity [ ] > {
339+ private async readEntitiesFromHash ( ids : string [ ] ) : Promise < T [ ] > {
340340 return Promise . all (
341- ids . map ( async ( entityId ) => {
341+ ids . map ( async ( entityId ) : Promise < T > => {
342342 const keyName = this . makeKey ( entityId )
343343 const hashData = await this . client . hgetall ( keyName )
344344 const entityData = fromRedisHash ( this . #schema, hashData )
345- const entity = { ...entityData , [ EntityId ] : entityId , [ EntityKeyName ] : keyName }
346- return entity
345+ return { ...entityData , [ EntityId ] : entityId , [ EntityKeyName ] : keyName } as T
347346 } ) )
348347 }
349348
@@ -353,14 +352,13 @@ export class Repository {
353352 await this . client . jsonset ( keyName , jsonData )
354353 }
355354
356- private async readEntitiesFromJson ( ids : string [ ] ) : Promise < Entity [ ] > {
355+ private async readEntitiesFromJson ( ids : string [ ] ) : Promise < T [ ] > {
357356 return Promise . all (
358- ids . map ( async ( entityId ) => {
357+ ids . map ( async ( entityId ) : Promise < T > => {
359358 const keyName = this . makeKey ( entityId )
360359 const jsonData = await this . client . jsonget ( keyName ) ?? { }
361360 const entityData = fromRedisJson ( this . #schema, jsonData )
362- const entity = { ...entityData , [ EntityId ] : entityId , [ EntityKeyName ] : keyName }
363- return entity
361+ return { ...entityData , [ EntityId ] : entityId , [ EntityKeyName ] : keyName } as T
364362 } ) )
365363 }
366364
0 commit comments