Skip to content

Commit e0919c2

Browse files
authored
feat: Add Entity.expiresAt() - entity TTL configuration (#920)
1 parent a39c966 commit e0919c2

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

packages/experimental/src/entity/Entity.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ First three members: ${JSON.stringify(processedEntity.slice(0, 3), null, 2)}`;
298298
return undefined;
299299
}
300300

301+
static expiresAt(
302+
{ expiresAt }: { expiresAt: number; date: number },
303+
input: any,
304+
): number {
305+
return expiresAt;
306+
}
307+
301308
static denormalize<T extends typeof Entity>(
302309
this: T,
303310
input: Readonly<Partial<AbstractInstanceType<T>>>,

packages/normalizr/src/entities/Entity.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ First three members: ${JSON.stringify(input.slice(0, 3), null, 2)}`;
239239
return id;
240240
}
241241

242+
static expiresAt(
243+
{ expiresAt }: { expiresAt: number; date: number },
244+
input: any,
245+
): number {
246+
return expiresAt;
247+
}
248+
242249
static infer(args, indexes, infer): any {
243250
if (!args[0]) return undefined as any;
244251
const id = this.pk(args[0], undefined, '');

packages/normalizr/src/normalize.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const addEntities =
6161
};
6262
};
6363
},
64-
{ expiresAt, date }: { expiresAt: number; date: number },
64+
meta: { expiresAt: number; date: number },
6565
) =>
6666
(schema: any, processedEntity: any, id: any) => {
6767
const schemaKey = schema.key;
@@ -82,7 +82,7 @@ const addEntities =
8282

8383
// if either of these is undefined, it resolves to 'false' which
8484
// means we fallback to 'newer' (processedEntity) takes priority
85-
const preferExisting = entityMeta[schemaKey][id]?.date > date;
85+
const preferExisting = entityMeta[schemaKey][id]?.date > meta.date;
8686
if (typeof processedEntity !== typeof inStoreEntity) {
8787
entities[schemaKey][id] = preferExisting
8888
? inStoreEntity
@@ -135,10 +135,14 @@ Entity: ${JSON.stringify(entity, undefined, 2)}`);
135135
}
136136
// set this after index updates so we know what indexes to remove from
137137
existingEntities[schemaKey][id] = entities[schemaKey][id];
138+
// TODO: eventually assume this exists and don't check for conditional. probably early 2022
139+
const entityExpiresAt = schema.expiresAt
140+
? schema.expiresAt(meta, processedEntity)
141+
: meta.expiresAt;
138142
entityMeta[schemaKey][id] =
139-
entityMeta[schemaKey][id]?.expiresAt >= expiresAt
143+
entityMeta[schemaKey][id]?.expiresAt >= entityExpiresAt
140144
? entityMeta[schemaKey][id]
141-
: { expiresAt, date };
145+
: { expiresAt: entityExpiresAt, date: meta.date };
142146
};
143147

144148
function expectedSchemaType(schema: Schema) {

0 commit comments

Comments
 (0)