-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathrepository.ts
372 lines (328 loc) · 12.4 KB
/
repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import {Client, CreateOptions, RedisConnection, RedisHashData, RedisJsonData} from '../client'
import {Entity, EntityId, EntityKeyName} from '../entity'
import {buildRediSearchSchema} from '../indexer'
import {Schema} from '../schema'
import {RawSearch, Search} from '../search'
import {fromRedisHash, fromRedisJson, toRedisHash, toRedisJson} from '../transformer'
/**
* A repository is the main interaction point for reading, writing, and
* removing {@link Entity | Entities} from Redis. Create one by calling
* {@link Client.fetchRepository} and passing in a {@link Schema}. Then
* use the {@link Repository#fetch}, {@link Repository#save}, and
* {@link Repository#remove} methods to manage your data:
*
* ```typescript
* 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 {@link Entity}
* before you save it:
*
* ```typescript
* const foo = await repository.createEntity()
* foo.aString = 'bar'
* foo.aBoolean = false
* await repository.save(foo)
* ```
*
* If you want to use the {@link Repository#search} method, you need to create an index
* first, and you need RediSearch or RedisJSON installed on your instance of Redis:
*
* ```typescript
* await repository.createIndex()
* const entities = await repository.search()
* .where('aString').eq('bar')
* .and('aBoolean').is.false().returnAll()
* ```
*/
export class Repository<T extends Entity = Record<string, any>> {
// NOTE: Not using "#" private as the spec needs to check calls on this class. Will be resolved when Client class is removed.
private readonly client: Client
readonly #schema: Schema<T>
/**
* Creates a new {@link Repository}.
*
* @param schema The schema defining that data in the repository.
* @param clientOrConnection A client to talk to Redis.
*/
constructor(schema: Schema<T>, clientOrConnection: Client | RedisConnection) {
this.#schema = schema
if (clientOrConnection instanceof Client) {
this.client = clientOrConnection
} else {
this.client = new Client()
this.client.useNoClose(clientOrConnection)
}
}
/**
* Creates an index in Redis for use by the {@link Repository#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.
*/
async createIndex() {
const currentIndexHash = await this.client.get(this.#schema.indexHashName)
const incomingIndexHash = this.#schema.indexHash
if (currentIndexHash !== incomingIndexHash) {
await this.dropIndex()
const {
indexName, indexHashName, dataStructure,
schemaName: prefix, useStopWords, stopWords
} = this.#schema
const schema = buildRediSearchSchema(this.#schema)
const options: CreateOptions = {
ON: dataStructure,
PREFIX: `${prefix}:`
}
if (useStopWords === 'OFF') {
options.STOPWORDS = []
} else if (useStopWords === 'CUSTOM') {
options.STOPWORDS = stopWords
}
await Promise.all([
this.client.createIndex(indexName, schema, options),
this.client.set(indexHashName, incomingIndexHash)
])
}
}
/**
* Removes an existing index from Redis. Use this method if you want to swap out your index
* because your {@link Entity} has changed. Requires that RediSearch and RedisJSON are installed
* on your instance of Redis.
*/
async dropIndex() {
try {
await Promise.all([
this.client.unlink(this.#schema.indexHashName),
this.client.dropIndex(this.#schema.indexName)
])
} catch (e) {
/* NOTE: It would be better if this error handler was only around the call
to `.dropIndex`. Might muss up the code a bit though. Life is full of
tough choices. */
if (e instanceof Error && (e.message === "Unknown Index name" || e.message === "Unknown index name")) {
// no-op: the thing we are dropping doesn't exist
} else {
throw e
}
}
}
/**
* Insert or update an {@link Entity} to Redis using its entityId property
* if present. If it's not, one is generated.
*
* @param entity The Entity to save.
* @returns A copy of the provided Entity with EntityId and EntityKeyName properties added.
*/
async save(entity: T): Promise<T>
/**
* Insert or update the {@link Entity} to Redis using the provided entityId.
*
* @param id The id to save the Entity under.
* @param entity The Entity to save.
* @returns A copy of the provided Entity with EntityId and EntityKeyName properties added.
*/
async save(id: string, entity: T): Promise<T>
async save(entityOrId: T | string, maybeEntity?: T): Promise<T> {
let entity: T | undefined
let entityId: string | undefined
if (typeof entityOrId !== 'string') {
entity = entityOrId
entityId = entity[EntityId] ?? await this.#schema.generateId()
} else {
entity = maybeEntity
entityId = entityOrId
}
const keyName = `${this.#schema.schemaName}:${entityId}`
const clonedEntity = { ...entity, [EntityId]: entityId, [EntityKeyName]: keyName } as T
await this.writeEntity(clonedEntity)
return clonedEntity
}
/**
* Read and return an {@link Entity} from Redis for the given id. If
* the {@link Entity} is not found, returns an empty {@link Entity}.
*
* @param id The ID of the {@link Entity} you seek.
* @returns The matching Entity.
*/
async fetch(id: string): Promise<T>
/**
* Read and return the {@link Entity | Entities} from Redis with the given IDs. If
* a particular {@link Entity} is not found, returns that {@link Entity} as empty.
*
* @param ids The IDs of the {@link Entity | Entities} you seek.
* @returns The matching Entities.
*/
async fetch(...ids: string[]): Promise<T[]>
/**
* Read and return the {@link Entity | Entities} from Redis with the given IDs. If
* a particular {@link Entity} is not found, returns that {@link Entity} as empty.
*
* @param ids The IDs of the {@link Entity | Entities} you seek.
* @returns The matching Entities.
*/
async fetch(ids: string[]): Promise<T[]>
async fetch(ids: string | string[]): Promise<T | T[]> {
if (arguments.length > 1) return this.readEntities([...arguments])
if (Array.isArray(ids)) return this.readEntities(ids)
const [entity] = await this.readEntities([ids])
return entity!
}
/**
* Remove an {@link Entity} from Redis for the given id. If the {@link Entity} is
* not found, does nothing.
*
* @param id The ID of the {@link Entity} you wish to delete.
*/
async remove(id: string): Promise<void>
/**
* Remove the {@link Entity | Entities} from Redis for the given ids. If a
* particular {@link Entity} is not found, does nothing.
*
* @param ids The IDs of the {@link Entity | Entities} you wish to delete.
*/
async remove(...ids: string[]): Promise<void>
/**
* Remove the {@link Entity | Entities} from Redis for the given ids. If a
* particular {@link Entity} is not found, does nothing.
*
* @param ids The IDs of the {@link Entity | Entities} you wish to delete.
*/
async remove(ids: string[]): Promise<void>
async remove(ids: string | string[]): Promise<void> {
// TODO: clean code
const keys = arguments.length > 1
? this.makeKeys([...arguments])
: Array.isArray(ids)
? this.makeKeys(ids)
: ids ? this.makeKeys([ids]) : []
if (keys.length === 0) return
await this.client.unlink(...keys)
}
/**
* Set the time to live of the {@link Entity}. If the {@link Entity} is not
* found, does nothing.
*
* @param id The ID of the {@link Entity} to set and expiration for.
* @param ttlInSeconds The time to live in seconds.
*/
async expire(id: string, ttlInSeconds: number): Promise<void>
/**
* Set the time to live of the {@link Entity | Entities} in Redis with the given
* ids. If a particular {@link Entity} is not found, does nothing.
*
* @param ids The IDs of the {@link Entity | Entities} you wish to delete.
* @param ttlInSeconds The time to live in seconds.
*/
async expire(ids: string[], ttlInSeconds: number): Promise<void>
async expire(idOrIds: string | string[], ttlInSeconds: number): Promise<void> {
const ids = typeof(idOrIds) === 'string' ? [ idOrIds ] : idOrIds
await Promise.all(
ids.map(id => {
const key = this.makeKey(id)
return this.client.expire(key, ttlInSeconds)
})
)
}
/**
* Use Date object to set the {@link Entity}'s time to live. If the {@link Entity}
* is not found, does nothing.
*
* @param id The ID of the {@link Entity} to set an expiration date for.
* @param expirationDate The time the data should expire.
*/
async expireAt(id: string, expirationDate: Date): Promise<void>;
/**
* Use Date object to set the {@link Entity | Entities} in Redis with the given
* ids. If a particular {@link Entity} is not found, does nothing.
*
* @param ids The IDs of the {@link Entity | Entities} to set an expiration date for.
* @param expirationDate The time the data should expire.
*/
async expireAt(ids: string[], expirationDate: Date): Promise<void>;
async expireAt(idOrIds: string | string[], expirationDate: Date) {
const ids = typeof idOrIds === 'string' ? [idOrIds] : idOrIds;
if (Date.now() >= expirationDate.getTime()) {
throw new Error(
`${expirationDate.toString()} is invalid. Expiration date must be in the future.`
);
}
await Promise.all(
ids.map((id) => {
const key = this.makeKey(id);
return this.client.expireAt(key, expirationDate);
})
);
}
/**
* Kicks off the process of building a query. Requires that RediSearch (and optionally
* RedisJSON) be installed on your instance of Redis.
*
* @returns A {@link Search} object.
*/
search(): Search<T> {
return new Search(this.#schema, this.client)
}
/**
* 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.
* @returns A {@link RawSearch} object.
*/
searchRaw(query: string): RawSearch<T> {
return new RawSearch(this.#schema, this.client, query)
}
private async writeEntity(entity: T): Promise<void> {
return this.#schema.dataStructure === 'HASH' ? this.#writeEntityToHash(entity) : this.writeEntityToJson(entity)
}
private async readEntities(ids: string[]): Promise<T[]> {
return this.#schema.dataStructure === 'HASH' ? this.readEntitiesFromHash(ids) : this.readEntitiesFromJson(ids)
}
async #writeEntityToHash(entity: Entity): Promise<void> {
const keyName = entity[EntityKeyName]!
const hashData: RedisHashData = toRedisHash(this.#schema, entity)
if (Object.keys(hashData).length === 0) {
await this.client.unlink(keyName)
} else {
await this.client.hsetall(keyName, hashData)
}
}
private async readEntitiesFromHash(ids: string[]): Promise<T[]> {
return Promise.all(
ids.map(async (entityId): Promise<T> => {
const keyName = this.makeKey(entityId)
const hashData = await this.client.hgetall(keyName)
const entityData = fromRedisHash(this.#schema, hashData)
return {...entityData, [EntityId]: entityId, [EntityKeyName]: keyName} as T
}))
}
private async writeEntityToJson(entity: Entity): Promise<void> {
const keyName = entity[EntityKeyName]!
const jsonData: RedisJsonData = toRedisJson(this.#schema, entity)
await this.client.jsonset(keyName, jsonData)
}
private async readEntitiesFromJson(ids: string[]): Promise<T[]> {
return Promise.all(
ids.map(async (entityId): Promise<T> => {
const keyName = this.makeKey(entityId)
const jsonData = await this.client.jsonget(keyName) ?? {}
const entityData = fromRedisJson(this.#schema, jsonData)
return {...entityData, [EntityId]: entityId, [EntityKeyName]: keyName} as T
}))
}
private makeKeys(ids: string[]): string[] {
return ids.map(id => this.makeKey(id))
}
private makeKey(id: string): string {
return `${this.#schema.schemaName}:${id}`
}
}