Skip to content

Commit

Permalink
chore: Store created and updated datetimes on buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
smessie committed Oct 16, 2024
1 parent 4e61491 commit 995dffb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/repositories/MongoDBRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ export class MongoDBRepository implements Repository {
},
update: {
$addToSet: { members: record.payload },
$set: {
updated: Date.now(),
},
$setOnInsert: {
created: Date.now(),
},
},
upsert: true,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -190,6 +199,12 @@ export class MongoDBRepository implements Repository {
value: value,
},
},
$set: {
updated: Date.now(),
},
$setOnInsert: {
created: Date.now(),
},
},
upsert: true,
},
Expand Down
29 changes: 27 additions & 2 deletions src/repositories/RedisRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export class RedisRepository implements Repository {
bucket: string,
bulk: Promise<string | number | null>[],
): Promise<void> {
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`,
Expand All @@ -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 },
),
);
}
Expand All @@ -148,6 +165,14 @@ export class RedisRepository implements Repository {
value: string | undefined,
bulk: Promise<string | number | null>[],
): Promise<void> {
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`,
Expand Down

0 comments on commit 995dffb

Please sign in to comment.