From d527e00f5937d57baaa42dd59d80439b0d6110e3 Mon Sep 17 00:00:00 2001 From: smessie Date: Tue, 15 Oct 2024 15:09:18 +0200 Subject: [PATCH] chore: Store created and updated datetimes on buckets --- src/repositories/MongoDBRepository.ts | 17 +++++++++++++++- src/repositories/RedisRepository.ts | 29 +++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/repositories/MongoDBRepository.ts b/src/repositories/MongoDBRepository.ts index 9a0e83f..436b6d5 100644 --- a/src/repositories/MongoDBRepository.ts +++ b/src/repositories/MongoDBRepository.ts @@ -140,6 +140,12 @@ export class MongoDBRepository implements Repository { }, update: { $addToSet: { members: record.payload }, + $set: { + updated: Date.now(), + }, + $setOnInsert: { + created: Date.now(), + }, }, upsert: true, }, @@ -162,7 +168,10 @@ export class MongoDBRepository implements Repository { id: bucket.id, }, update: { - $set: bucket, + $set: { ...bucket, updated: Date.now() }, + $setOnInsert: { + created: Date.now(), + }, }, upsert: true, }, @@ -190,6 +199,12 @@ export class MongoDBRepository implements Repository { value: value, }, }, + $set: { + updated: Date.now(), + }, + $setOnInsert: { + created: Date.now(), + }, }, upsert: true, }, diff --git a/src/repositories/RedisRepository.ts b/src/repositories/RedisRepository.ts index 87e2ef0..18dd6d4 100644 --- a/src/repositories/RedisRepository.ts +++ b/src/repositories/RedisRepository.ts @@ -110,6 +110,14 @@ export class RedisRepository implements Repository { bucket: string, bulk: Promise[], ): Promise { + bulk.push( + this.client.json.set( + `${this.index}:${encodeURIComponent(record.stream)}:${encodeURIComponent(bucket)}`, + "$.updated", + Date.now(), + { XX: true }, + ), + ); bulk.push( this.client.sAdd( `${this.index}:${encodeURIComponent(record.stream)}:${encodeURIComponent(bucket)}:members`, @@ -133,11 +141,20 @@ export class RedisRepository implements Repository { delete bucket.empty; + // Make sure the key exists to then use JSON.MERGE, and initialize the key with the created timestamp. bulk.push( this.client.json.set( `${this.index}:${encodeURIComponent(bucket.streamId)}:${encodeURIComponent(bucket.id)}`, - ".", - bucket, + "$", + { created: Date.now() }, + { NX: true }, + ), + ); + bulk.push( + this.client.json.merge( + `${this.index}:${encodeURIComponent(bucket.streamId)}:${encodeURIComponent(bucket.id)}`, + "$", + { updated: Date.now(), ...bucket }, ), ); } @@ -148,6 +165,14 @@ export class RedisRepository implements Repository { value: string | undefined, bulk: Promise[], ): Promise { + bulk.push( + this.client.json.set( + `${this.index}:${encodeURIComponent(relation.stream)}:${encodeURIComponent(relation.origin)}`, + "$.updated", + Date.now(), + { XX: true }, + ), + ); bulk.push( this.client.sAdd( `${this.index}:${encodeURIComponent(relation.stream)}:${encodeURIComponent(relation.origin)}:relations`,