Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/entity/src/EntityConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export class CompositeFieldInfo<
keyDefinition.compositeField.length === new Set(keyDefinition.compositeField).size,
'Composite field must have unique sub-fields',
);
const compositeFieldHolder = new CompositeFieldHolder(keyDefinition.compositeField);
const compositeFieldHolder = new CompositeFieldHolder<TFields, TIDField>(
keyDefinition.compositeField,
);
return [
compositeFieldHolder.serialize(),
{ compositeFieldHolder, cache: keyDefinition.cache ?? false },
Expand All @@ -79,8 +81,9 @@ export class CompositeFieldInfo<
public getCompositeFieldHolderForCompositeField(
compositeField: EntityCompositeField<TFields>,
): CompositeFieldHolder<TFields, TIDField> | undefined {
return this.compositeFieldInfoMap.get(new CompositeFieldHolder(compositeField).serialize())
?.compositeFieldHolder;
return this.compositeFieldInfoMap.get(
new CompositeFieldHolder<TFields, TIDField>(compositeField).serialize(),
)?.compositeFieldHolder;
}

public getAllCompositeFieldHolders(): readonly CompositeFieldHolder<TFields, TIDField>[] {
Expand All @@ -89,7 +92,7 @@ export class CompositeFieldInfo<

public canCacheCompositeField(compositeField: EntityCompositeField<TFields>): boolean {
const compositeFieldInfo = this.compositeFieldInfoMap.get(
new CompositeFieldHolder(compositeField).serialize(),
new CompositeFieldHolder<TFields, TIDField>(compositeField).serialize(),
);
invariant(
compositeFieldInfo,
Expand All @@ -107,7 +110,7 @@ export class EntityConfiguration<
TFields extends Record<string, any>,
TIDField extends keyof TFields,
> {
readonly idField: keyof TFields;
readonly idField: TIDField;
readonly tableName: string;
readonly cacheableKeys: ReadonlySet<keyof TFields>;
readonly compositeFieldInfo: CompositeFieldInfo<TFields, TIDField>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,14 @@ describe(AuthorizationResultBasedEntityLoader, () => {
new SingleFieldValueHolder<TestFields, 'dateField'>(date),
],
[
new CompositeFieldHolder(['stringField', 'intField']),
new CompositeFieldHolder<TestFields, 'customIdField'>(['stringField', 'intField']),
new CompositeFieldValueHolder({ stringField: 'huh', intField: 5 }),
],
[
new CompositeFieldHolder(['stringField', 'testIndexedField']),
new CompositeFieldHolder<TestFields, 'customIdField'>([
'stringField',
'testIndexedField',
]),
new CompositeFieldValueHolder({ stringField: 'huh', testIndexedField: 'h1' }),
],
]),
Expand Down Expand Up @@ -605,11 +608,14 @@ describe(AuthorizationResultBasedEntityLoader, () => {
new SingleFieldValueHolder<TestFields, 'dateField'>(date),
],
[
new CompositeFieldHolder(['stringField', 'intField']),
new CompositeFieldHolder<TestFields, 'customIdField'>(['stringField', 'intField']),
new CompositeFieldValueHolder({ stringField: 'huh', intField: 5 }),
],
[
new CompositeFieldHolder(['stringField', 'testIndexedField']),
new CompositeFieldHolder<TestFields, 'customIdField'>([
'stringField',
'testIndexedField',
]),
new CompositeFieldValueHolder({ stringField: 'huh', testIndexedField: 'h1' }),
],
]),
Expand Down Expand Up @@ -708,11 +714,14 @@ describe(AuthorizationResultBasedEntityLoader, () => {
new SingleFieldValueHolder<TestFields, 'dateField'>(date),
],
[
new CompositeFieldHolder(['stringField', 'intField']),
new CompositeFieldHolder<TestFields, 'customIdField'>(['stringField', 'intField']),
new CompositeFieldValueHolder({ stringField: 'huh', intField: 5 }),
],
[
new CompositeFieldHolder(['stringField', 'testIndexedField']),
new CompositeFieldHolder<TestFields, 'customIdField'>([
'stringField',
'testIndexedField',
]),
new CompositeFieldValueHolder({ stringField: 'huh', testIndexedField: 'h1' }),
],
]),
Expand Down
4 changes: 2 additions & 2 deletions packages/entity/src/__tests__/EntityConfiguration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ describe(EntityConfiguration, () => {
expect(blahEntityConfiguration.databaseAdapterFlavor).toEqual('postgres');
expect(blahEntityConfiguration.cacheAdapterFlavor).toEqual('redis');
expect(blahEntityConfiguration.compositeFieldInfo.getAllCompositeFieldHolders()).toEqual([
new CompositeFieldHolder(['id', 'cacheable']),
new CompositeFieldHolder(['id', 'uniqueButNotCacheable']),
new CompositeFieldHolder<BlahT, 'id'>(['id', 'cacheable']),
new CompositeFieldHolder<BlahT, 'id'>(['id', 'uniqueButNotCacheable']),
]);
});

Expand Down
22 changes: 13 additions & 9 deletions packages/entity/src/__tests__/EntityDatabaseAdapter-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe(EntityDatabaseAdapter, () => {
const adapter = new TestEntityDatabaseAdapter({ fetchResults: [{ string_field: 'hello' }] });
const result = await adapter.fetchManyWhereAsync(
queryContext,
new SingleFieldHolder('stringField'),
new SingleFieldHolder<TestFields, 'customIdField', 'stringField'>('stringField'),
[new SingleFieldValueHolder('hello')],
);
expect(result.get(new SingleFieldValueHolder('hello'))).toEqual([{ stringField: 'hello' }]);
Expand All @@ -100,7 +100,7 @@ describe(EntityDatabaseAdapter, () => {
});
const result = await adapter.fetchManyWhereAsync(
queryContext,
new SingleFieldHolder('stringField'),
new SingleFieldHolder<TestFields, 'customIdField', 'stringField'>('stringField'),
[new SingleFieldValueHolder('hello'), new SingleFieldValueHolder('wat')],
);
expect(result.get(new SingleFieldValueHolder('hello'))).toEqual([{ stringField: 'hello' }]);
Expand Down Expand Up @@ -140,7 +140,7 @@ describe(EntityDatabaseAdapter, () => {
const adapter = new TestEntityDatabaseAdapter({});
const result = await adapter.fetchManyWhereAsync(
queryContext,
new SingleFieldHolder('stringField'),
new SingleFieldHolder<TestFields, 'customIdField', 'stringField'>('stringField'),
[new SingleFieldValueHolder('what'), new SingleFieldValueHolder('who')],
);
expect(Array.from(result.keys()).map((v) => v.fieldValue)).toEqual(['what', 'who']);
Expand All @@ -152,9 +152,11 @@ describe(EntityDatabaseAdapter, () => {
fetchResults: [{ string_field: null }],
});
await expect(
adapter.fetchManyWhereAsync(queryContext, new SingleFieldHolder('stringField'), [
new SingleFieldValueHolder('hello'),
]),
adapter.fetchManyWhereAsync(
queryContext,
new SingleFieldHolder<TestFields, 'customIdField', 'stringField'>('stringField'),
[new SingleFieldValueHolder('hello')],
),
).rejects.toThrow(
'One or more fields from the object is invalid for key SingleField(stringField); {"stringField":null}. This may indicate a faulty database adapter implementation.',
);
Expand All @@ -166,9 +168,11 @@ describe(EntityDatabaseAdapter, () => {
fetchResults: [{ string_field: undefined }],
});
await expect(
adapter.fetchManyWhereAsync(queryContext, new SingleFieldHolder('stringField'), [
new SingleFieldValueHolder('hello'),
]),
adapter.fetchManyWhereAsync(
queryContext,
new SingleFieldHolder<TestFields, 'customIdField', 'stringField'>('stringField'),
[new SingleFieldValueHolder('hello')],
),
).rejects.toThrow(
'One or more fields from the object is invalid for key SingleField(stringField); {}. This may indicate a faulty database adapter implementation.',
);
Expand Down
20 changes: 10 additions & 10 deletions packages/entity/src/__tests__/EntityEdges-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
'id'
>;
const childCachedBefore = await childCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<ChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(parent.getID())],
);
expect(childCachedBefore.get(new SingleFieldValueHolder(parent.getID()))?.status).toEqual(
Expand All @@ -783,9 +783,9 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
GrandChildEntity,
)['entityCompanion']['tableDataCoordinator'][
'cacheAdapter'
] as InMemoryFullCacheStubCacheAdapter<ChildFields, 'id'>;
] as InMemoryFullCacheStubCacheAdapter<GrandChildFields, 'id'>;
const grandChildCachedBefore = await grandChildCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<GrandChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(child.getID())],
);
expect(grandChildCachedBefore.get(new SingleFieldValueHolder(child.getID()))?.status).toEqual(
Expand All @@ -797,15 +797,15 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
privacyPolicyEvaluationRecords.shouldRecord = false;

const childCachedAfter = await childCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<ChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(parent.getID())],
);
expect(childCachedAfter.get(new SingleFieldValueHolder(parent.getID()))?.status).toEqual(
CacheStatus.MISS,
);

const grandChildCachedAfter = await grandChildCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<GrandChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(child.getID())],
);
expect(grandChildCachedAfter.get(new SingleFieldValueHolder(child.getID()))?.status).toEqual(
Expand Down Expand Up @@ -922,7 +922,7 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
'id'
>;
const childCachedBefore = await childCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<ChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(parent.getID())],
);
expect(childCachedBefore.get(new SingleFieldValueHolder(parent.getID()))?.status).toEqual(
Expand All @@ -933,9 +933,9 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
GrandChildEntity,
)['entityCompanion']['tableDataCoordinator'][
'cacheAdapter'
] as InMemoryFullCacheStubCacheAdapter<ChildFields, 'id'>;
] as InMemoryFullCacheStubCacheAdapter<GrandChildFields, 'id'>;
const grandChildCachedBefore = await grandChildCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<GrandChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(child.getID())],
);
expect(grandChildCachedBefore.get(new SingleFieldValueHolder(child.getID()))?.status).toEqual(
Expand All @@ -947,15 +947,15 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
privacyPolicyEvaluationRecords.shouldRecord = false;

const childCachedAfter = await childCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<ChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(parent.getID())],
);
expect(childCachedAfter.get(new SingleFieldValueHolder(parent.getID()))?.status).toEqual(
CacheStatus.MISS,
);

const grandChildCachedAfter = await grandChildCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_id'),
new SingleFieldHolder<GrandChildFields, 'id', 'parent_id'>('parent_id'),
[new SingleFieldValueHolder(child.getID())],
);
expect(grandChildCachedAfter.get(new SingleFieldValueHolder(child.getID()))?.status).toEqual(
Expand Down
8 changes: 4 additions & 4 deletions packages/entity/src/__tests__/EntityMutator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ const createEntityMutatorFactory = (

installExtensions: () => {},

getDatabaseAdapter<TFields extends Record<'customIdField', any>>(
_entityConfiguration: EntityConfiguration<TFields, 'customIdField'>,
): EntityDatabaseAdapter<TFields, 'customIdField'> {
return databaseAdapter as any as EntityDatabaseAdapter<TFields, 'customIdField'>;
getDatabaseAdapter<TFields extends Record<string, any>, TIDField extends keyof TFields>(
_entityConfiguration: EntityConfiguration<TFields, TIDField>,
): EntityDatabaseAdapter<TFields, TIDField> {
return databaseAdapter as any as EntityDatabaseAdapter<TFields, TIDField>;
},
};
const metricsAdapter = new NoOpEntityMetricsAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ describe('EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE', () => {
'cacheAdapter'
] as InMemoryFullCacheStubCacheAdapter<CategoryFields, 'id'>;
const subCategoryCachedBefore = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[new SingleFieldValueHolder(parentCategory.getID())],
);
expect(
subCategoryCachedBefore.get(new SingleFieldValueHolder(parentCategory.getID()))?.status,
).toEqual(CacheStatus.HIT);

const subSubCategoryCachedBefore = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[new SingleFieldValueHolder(subCategory.getID())],
);
expect(
Expand All @@ -268,15 +268,15 @@ describe('EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE', () => {
await CategoryEntity.deleter(parentCategory).deleteAsync();

const subCategoryCachedAfter = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[new SingleFieldValueHolder(parentCategory.getID())],
);
expect(
subCategoryCachedAfter.get(new SingleFieldValueHolder(parentCategory.getID()))?.status,
).toEqual(CacheStatus.MISS);

const subSubCategoryCachedAfter = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[new SingleFieldValueHolder(subCategory.getID())],
);
expect(
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE', () => {
'cacheAdapter'
] as InMemoryFullCacheStubCacheAdapter<CategoryFields, 'id'>;
const categoriesCachedBefore = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[
new SingleFieldValueHolder(categoryA.getID()),
new SingleFieldValueHolder(categoryB.getID()),
Expand All @@ -345,7 +345,7 @@ describe('EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE', () => {
await CategoryEntity.deleter(categoryA).deleteAsync();

const categoriesCachedAfter = await categoryCacheAdapter.loadManyAsync(
new SingleFieldHolder('parent_category_id'),
new SingleFieldHolder<CategoryFields, 'id', 'parent_category_id'>('parent_category_id'),
[
new SingleFieldValueHolder(categoryA.getID()),
new SingleFieldValueHolder(categoryB.getID()),
Expand Down
Loading