Compare commits

..
Author SHA1 Message Date
Will Schurman 02d2f24bb6 v0.25.2 (#169) 2022-03-11 17:09:48 -08:00
Will Schurman e76ca8c2f7 fix: lru-cache yarn workspace version resolution (#168) 2022-03-11 17:07:02 -08:00
Will Schurman 0736e20987 v0.25.1 (#167) 2022-03-10 15:26:07 -08:00
Will Schurman 5c82edcd9f fix: add composed cache to index (#166) 2022-03-10 15:24:09 -08:00
Will Schurman 96e1023d40 v0.25.0 (#165) 2022-03-10 15:05:15 -08:00
Will Schurman 3a81a4c059 feat: privacy policy evaluation context (#162) 2022-03-10 14:44:12 -08:00
Will Schurman 6b6e70e75f feat: composed cache adapter, composed secondary cache, local memory secondary cache (#164) 2022-03-10 14:42:01 -08:00
Quinlan Jung bc378b6c79 chore: better variable and method naming (#160) 2022-02-16 18:37:51 -08:00
Quinlan Jung fefa4100fc v0.24.0 (#159)
* v0.24.0

* changelog
2022-02-16 18:37:39 -08:00
Quinlan Jung 85d35752fa feat: add local memory cache package (#158)
* feat: add local memory cache package

* some pr feedback

* more tests
2022-02-15 18:44:20 -08:00
Quinlan Jung 09d48a617c chore: refactor cachers to use an abstract class and interface (#157)
* chore: refactor cachers to use an abstract class and interface

* fix lint

* changelog
2022-02-14 18:42:27 -08:00
Will Schurman 0adcf7238f [entity] Add docblocks for EntityFields and EntityFieldDefinition (#156) 2022-02-11 14:20:45 -08:00
Quinlan Jung 41bd0722c9 v0.23.0 (#155)
* v0.23.0

* update changelog
2022-02-09 13:59:40 -08:00
Quinlan Jung 03a1fce067 move responsibility of transformer to generic redis cacher (#153)
* will's commit

* integ tests

* fix codecov
2022-02-07 15:04:02 -08:00
84 changed files with 3206 additions and 463 deletions

No files matched your search

+58
View File
@@ -3,6 +3,64 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
### Bug Fixes
* lru-cache yarn workspace version resolution ([#168](https://github.com/expo/entity/issues/168)) ([e76ca8c](https://github.com/expo/entity/commit/e76ca8c2f72da84aa372a64ef1898ade220ed0a8))
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
### Bug Fixes
* add composed cache to index ([#166](https://github.com/expo/entity/issues/166)) ([5c82edc](https://github.com/expo/entity/commit/5c82edcd9f26854878d29950a05852e117bbd723))
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* composed cache adapter, composed secondary cache, local memory secondary cache ([#164](https://github.com/expo/entity/issues/164)) ([6b6e70e](https://github.com/expo/entity/commit/6b6e70e75f7e42a4dce13edeb406a4d88d9264f0))
* privacy policy evaluation context ([#162](https://github.com/expo/entity/issues/162)) ([3a81a4c](https://github.com/expo/entity/commit/3a81a4c059e2e995eb800e35e76b3d432c22b23f))
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
### Features
* add local memory cache package ([#158](https://github.com/expo/entity/issues/158)) ([85d3575](https://github.com/expo/entity/commit/85d35752fa0919a62efb776a762a6829e83ca7af))
### Chores
* Refactor cachers to use an abstract class and interface ([#157](https://github.com/expo/entity/pull/157))
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
### Breaking changes
* Move data transforming responsibilities to the CacheAdapter instead of the ReadThroughEntityCache. Affects any classes extending the EntityCacheAdapter and relying on the transformation behavior of the ReadThroughEntityCache. ([#153](https://github.com/expo/entity/pull/153))
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
+1 -1
View File
@@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.22.0"
"version": "0.25.2"
}
+6 -2
View File
@@ -22,6 +22,7 @@
"@types/ioredis": "^4.26.4",
"@types/jest": "^26.0.10",
"@types/jsbn": "^1.2.29",
"@types/lru-cache": "^7.4.0",
"@types/node": "^14.6.0",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.14.0",
@@ -32,6 +33,7 @@
"eslint-plugin-tsdoc": "^0.2.11",
"jest": "^26.6.3",
"lerna": "^4.0.0",
"lru-cache": "^7.3.1",
"nullthrows": "^1.1.1",
"pg": "^8.6.0",
"prettier": "^2.3.2",
@@ -43,9 +45,11 @@
},
"dependencies": {
"@expo/entity": "file:packages/entity",
"@expo/entity-cache-adapter-local-memory": "file:packages/entity-cache-adapter-local-memory",
"@expo/entity-cache-adapter-redis": "file:packages/entity-cache-adapter-redis",
"@expo/entity-database-adapter-knex": "file:packages/entity-database-adapter-knex",
"@expo/entity-secondary-cache-redis": "file:packages/entity-secondary-cache-redis",
"@expo/entity-ip-address-field": "file:packages/entity-ip-address-field"
"@expo/entity-ip-address-field": "file:packages/entity-ip-address-field",
"@expo/entity-secondary-cache-local-memory": "file:packages/entity-secondary-cache-local-memory",
"@expo/entity-secondary-cache-redis": "file:packages/entity-secondary-cache-redis"
}
}
@@ -0,0 +1,41 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
### Bug Fixes
* lru-cache yarn workspace version resolution ([#168](https://github.com/expo/entity/issues/168)) ([e76ca8c](https://github.com/expo/entity/commit/e76ca8c2f72da84aa372a64ef1898ade220ed0a8))
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-cache-adapter-local-memory
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* composed cache adapter, composed secondary cache, local memory secondary cache ([#164](https://github.com/expo/entity/issues/164)) ([6b6e70e](https://github.com/expo/entity/commit/6b6e70e75f7e42a4dce13edeb406a4d88d9264f0))
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
### Features
* add local memory cache package ([#158](https://github.com/expo/entity/issues/158)) ([85d3575](https://github.com/expo/entity/commit/85d35752fa0919a62efb776a762a6829e83ca7af))
@@ -0,0 +1,46 @@
# `@expo/entity-cache-adapter-local-memory`
Cross-request [LRU](https://github.com/isaacs/node-lru-cache) cache adapter for `@expo/entity`. Use
this cache with caution - it is nonstandard. The cache is shared between requests in the node process.
[Documentation](https://expo.github.io/entity/modules/_expo_cache_adapter_local_memory.html)
## Why NOT use this cache
Because this is an in-memory cache, cross-box invalidation is not possible. Do not use this cache
if you have the following use cases:
- The objects stored have high mutability
- Reading a stale object from the cache is not acceptable in your application
- Cross-box invalidation is not possible
## Typical use cases
If your application sees many requests fetching the same objects, you can save a trip to your cache
cluster and backing datastore by using this in-memory cache. Here are some good use cases:
- The objects stored are mostly immutable
- You have a low TTL setting in your cache
## Usage
During `EntityCompanionProvider` instantiation:
```typescript
export const createDefaultEntityCompanionProvider = (
metricsAdapter: IEntityMetricsAdapter = new NoOpEntityMetricsAdapter()
): EntityCompanionProvider => {
return new EntityCompanionProvider(
metricsAdapter,
{
...
},
{
['local-memory']: {
cacheAdapterProvider: new LocalMemoryCacheAdapterProvider.getProvider(),
},
}
);
};
```
@@ -0,0 +1,38 @@
{
"name": "@expo/entity-cache-adapter-local-memory",
"version": "0.25.2",
"description": "Cross-request local memory cache adapter for @expo/entity",
"files": [
"build",
"src"
],
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src",
"lint-fix": "eslint src --fix",
"test": "jest --rootDir . --config ../../resources/jest.config.js",
"integration": "../../resources/run-with-docker yarn integration-no-setup",
"integration-no-setup": "jest --config ../../resources/jest-integration.config.js --rootDir . --runInBand --passWithNoTests",
"barrelsby": "barrelsby --directory src --location top --exclude tests__ --singleQuotes --exportDefault --delete"
},
"engines": {
"node": ">=12"
},
"keywords": [
"entity"
],
"author": "Expo",
"license": "MIT",
"peerDependencies": {
"@expo/entity": "*"
},
"dependencies": {
"lru-cache": "^7.3.1"
},
"devDependencies": {
"@expo/entity": "^0.25.2"
}
}
@@ -0,0 +1,156 @@
import { CacheLoadResult, CacheStatus, IEntityGenericCacher } from '@expo/entity';
import LRUCache from 'lru-cache';
// Sentinel value we store in local memory to negatively cache a database miss.
// The sentinel value is distinct from any (positively) cached value.
export const DOES_NOT_EXIST_LOCAL_MEMORY_CACHE = Symbol('doesNotExist');
type LocalMemoryCacheValue<TFields> = Readonly<TFields> | typeof DOES_NOT_EXIST_LOCAL_MEMORY_CACHE;
export type LocalMemoryCache<TFields> = LRUCache<string, LocalMemoryCacheValue<TFields>>;
type LRUCacheOptionsV7<K, V> = {
/**
* the number of most recently used items to keep.
* note that we may store fewer items than this if maxSize is hit.
*/
max: number;
/**
* if you wish to track item size, you must provide a maxSize
* note that we still will only keep up to max *actual items*,
* so size tracking may cause fewer than max items to be stored.
* At the extreme, a single item of maxSize size will cause everything
* else in the cache to be dropped when it is added. Use with caution!
* Note also that size tracking can negatively impact performance,
* though for most cases, only minimally.
*/
maxSize?: number;
/**
* function to calculate size of items. useful if storing strings or
* buffers or other items where memory size depends on the object itself.
* also note that oversized items do NOT immediately get dropped from
* the cache, though they will cause faster turnover in the storage.
*/
sizeCalculation?: (value: V, key: K) => number;
/**
* function to call when the item is removed from the cache
* Note that using this can negatively impact performance.
*/
dispose?: (value: V, key: K) => void;
/**
* max time to live for items before they are considered stale
* note that stale items are NOT preemptively removed by default,
* and MAY live in the cache, contributing to its LRU max, long after
* they have expired.
* Also, as this cache is optimized for LRU/MRU operations, some of
* the staleness/TTL checks will reduce performance, as they will incur
* overhead by deleting items.
* Must be a positive integer in ms, defaults to 0, which means "no TTL"
*/
ttl?: number;
/**
* return stale items from cache.get() before disposing of them
* boolean, default false
*/
allowStale?: boolean;
/**
* update the age of items on cache.get(), renewing their TTL
* boolean, default false
*/
updateAgeOnGet?: boolean;
/**
* update the age of items on cache.has(), renewing their TTL
* boolean, default false
*/
updateAgeOnHas?: boolean;
/**
* update the "recently-used"-ness of items on cache.has()
* boolean, default false
*/
updateRecencyOnHas?: boolean;
};
export default class GenericLocalMemoryCacher<TFields> implements IEntityGenericCacher<TFields> {
constructor(private readonly localMemoryCache: LocalMemoryCache<TFields>) {}
static createLRUCache<TFields>(
options: { maxSize?: number; ttlSeconds?: number } = {}
): LocalMemoryCache<TFields> {
const DEFAULT_LRU_CACHE_MAX_AGE_SECONDS = 10;
const DEFAULT_LRU_CACHE_SIZE = 10000;
const maxAgeSeconds = options.ttlSeconds ?? DEFAULT_LRU_CACHE_MAX_AGE_SECONDS;
const lruCacheOptions: LRUCacheOptionsV7<string, TFields> = {
max: options.maxSize ?? DEFAULT_LRU_CACHE_SIZE,
maxSize: options.maxSize ?? DEFAULT_LRU_CACHE_SIZE,
sizeCalculation: (value: LocalMemoryCacheValue<TFields>) =>
value === DOES_NOT_EXIST_LOCAL_MEMORY_CACHE ? 0 : 1,
ttl: maxAgeSeconds * 1000, // convert to ms
};
return new LRUCache<string, LocalMemoryCacheValue<TFields>>(lruCacheOptions as any);
}
static createNoOpCache<TFields>(): LocalMemoryCache<TFields> {
return new LRUCache<string, LocalMemoryCacheValue<TFields>>({
max: 1,
maxSize: 1,
sizeCalculation: () => 10, // make all things larger than max size
});
}
public async loadManyAsync(
keys: readonly string[]
): Promise<ReadonlyMap<string, CacheLoadResult<TFields>>> {
const cacheResults = new Map<string, CacheLoadResult<TFields>>();
for (const key of keys) {
const cacheResult = this.localMemoryCache.get(key);
if (cacheResult === DOES_NOT_EXIST_LOCAL_MEMORY_CACHE) {
cacheResults.set(key, {
status: CacheStatus.NEGATIVE,
});
} else if (cacheResult) {
cacheResults.set(key, {
status: CacheStatus.HIT,
item: cacheResult as unknown as TFields,
});
} else {
cacheResults.set(key, {
status: CacheStatus.MISS,
});
}
}
return cacheResults;
}
public async cacheManyAsync(objectMap: ReadonlyMap<string, Readonly<TFields>>): Promise<void> {
for (const [key, item] of objectMap) {
this.localMemoryCache.set(key, item);
}
}
public async cacheDBMissesAsync(keys: readonly string[]): Promise<void> {
for (const key of keys) {
this.localMemoryCache.set(key, DOES_NOT_EXIST_LOCAL_MEMORY_CACHE);
}
}
public async invalidateManyAsync(keys: readonly string[]): Promise<void> {
for (const key of keys) {
this.localMemoryCache.delete(key);
}
}
public makeCacheKey(parts: string[]): string {
const delimiter = ':';
const escapedParts = parts.map((part) =>
part.replace('\\', '\\\\').replace(delimiter, `\\${delimiter}`)
);
return escapedParts.join(delimiter);
}
}
@@ -0,0 +1,80 @@
import { EntityCacheAdapter, CacheLoadResult, EntityConfiguration, mapKeys } from '@expo/entity';
import invariant from 'invariant';
import GenericLocalMemoryCacher, { LocalMemoryCache } from './GenericLocalMemoryCacher';
export default class LocalMemoryCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
private readonly genericLocalMemoryCacher: GenericLocalMemoryCacher<TFields>;
constructor(
entityConfiguration: EntityConfiguration<TFields>,
localMemoryCache: LocalMemoryCache<TFields>
) {
super(entityConfiguration);
this.genericLocalMemoryCacher = new GenericLocalMemoryCacher(localMemoryCache);
}
public async loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
const localMemoryCacheKeyToFieldValueMapping = new Map(
fieldValues.map((fieldValue) => [this.makeCacheKey(fieldName, fieldValue), fieldValue])
);
const cacheResults = await this.genericLocalMemoryCacher.loadManyAsync(
Array.from(localMemoryCacheKeyToFieldValueMapping.keys())
);
return mapKeys(cacheResults, (cacheKey) => {
const fieldValue = localMemoryCacheKeyToFieldValueMapping.get(cacheKey);
invariant(
fieldValue !== undefined,
'Unspecified cache key %s returned from generic local memory cacher',
cacheKey
);
return fieldValue;
});
}
public async cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {
await this.genericLocalMemoryCacher.cacheManyAsync(
mapKeys(objectMap, (fieldValue) => this.makeCacheKey(fieldName, fieldValue))
);
}
public async cacheDBMissesAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
await this.genericLocalMemoryCacher.cacheDBMissesAsync(
fieldValues.map((fieldValue) => this.makeCacheKey(fieldName, fieldValue))
);
}
public async invalidateManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
await this.genericLocalMemoryCacher.invalidateManyAsync(
fieldValues.map((fieldValue) => this.makeCacheKey(fieldName, fieldValue))
);
}
private makeCacheKey<N extends keyof TFields>(
fieldName: N,
fieldValue: NonNullable<TFields[N]>
): string {
const columnName = this.entityConfiguration.entityToDBFieldsKeyMapping.get(fieldName);
invariant(columnName, `database field mapping missing for ${fieldName}`);
const parts = [
this.entityConfiguration.tableName,
`${this.entityConfiguration.cacheKeyVersion}`,
columnName,
String(fieldValue),
];
return this.genericLocalMemoryCacher.makeCacheKey(parts);
}
}
@@ -0,0 +1,45 @@
import {
computeIfAbsent,
EntityCacheAdapter,
EntityConfiguration,
IEntityCacheAdapterProvider,
} from '@expo/entity';
import GenericLocalMemoryCacher, { LocalMemoryCache } from './GenericLocalMemoryCacher';
import LocalMemoryCacheAdapter from './LocalMemoryCacheAdapter';
export default class LocalMemoryCacheAdapterProvider implements IEntityCacheAdapterProvider {
// local memory cache adapters should be shared/reused across requests
static localMemoryCacheAdapterMap = new Map<string, LocalMemoryCacheAdapter<any>>();
static getNoOpProvider(): IEntityCacheAdapterProvider {
return new LocalMemoryCacheAdapterProvider(<TFields>() =>
GenericLocalMemoryCacher.createNoOpCache<TFields>()
);
}
static getProvider(
options: { maxSize?: number; ttlSeconds?: number } = {}
): IEntityCacheAdapterProvider {
return new LocalMemoryCacheAdapterProvider(<TFields>() =>
GenericLocalMemoryCacher.createLRUCache<TFields>(options)
);
}
private constructor(
private readonly localMemoryCacheCreator: <TFields>() => LocalMemoryCache<TFields>
) {}
public getCacheAdapter<TFields>(
entityConfiguration: EntityConfiguration<TFields>
): EntityCacheAdapter<TFields> {
return computeIfAbsent(
LocalMemoryCacheAdapterProvider.localMemoryCacheAdapterMap,
entityConfiguration.tableName,
() => {
const localMemoryCache = this.localMemoryCacheCreator<TFields>();
return new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
}
);
}
}
@@ -0,0 +1,30 @@
import GenericLocalMemoryCacher from '../GenericLocalMemoryCacher';
describe(GenericLocalMemoryCacher, () => {
describe(GenericLocalMemoryCacher.createLRUCache, () => {
it('creates a cache with default options', () => {
const cache = GenericLocalMemoryCacher.createLRUCache();
expect(cache.max).toBe(10000);
expect(cache.maxSize).toBe(10000);
expect(cache.ttl).toBe(10000);
});
it('respects specified options', () => {
const cache = GenericLocalMemoryCacher.createLRUCache({
ttlSeconds: 3,
maxSize: 10,
});
expect(cache.max).toBe(10);
expect(cache.maxSize).toBe(10);
expect(cache.ttl).toBe(3000);
});
});
describe(GenericLocalMemoryCacher.createNoOpCache, () => {
it('creates a no-op cache', () => {
const cache = GenericLocalMemoryCacher.createNoOpCache<{ hello: 'world' }>();
cache.set('a', { hello: 'world' });
expect(cache.get('a')).toBeUndefined();
});
});
});
@@ -0,0 +1,174 @@
import { CacheStatus, ViewerContext } from '@expo/entity';
import { v4 as uuidv4 } from 'uuid';
import GenericLocalMemoryCacher from '../GenericLocalMemoryCacher';
import LocalMemoryCacheAdapter from '../LocalMemoryCacheAdapter';
import LocalMemoryCacheAdapterProvider from '../LocalMemoryCacheAdapterProvider';
import LocalMemoryTestEntity from '../testfixtures/LocalMemoryTestEntity';
import {
createLocalMemoryTestEntityCompanionProvider,
createNoopLocalMemoryIntegrationTestEntityCompanionProvider,
} from '../testfixtures/createLocalMemoryTestEntityCompanionProvider';
class TestViewerContext extends ViewerContext {}
describe(LocalMemoryCacheAdapter, () => {
beforeEach(async () => {
LocalMemoryCacheAdapterProvider.localMemoryCacheAdapterMap.clear();
});
it('has correct caching behavior', async () => {
const viewerContext = new TestViewerContext(createLocalMemoryTestEntityCompanionProvider());
const cacheAdapter = viewerContext.entityCompanionProvider.getCompanionForEntity(
LocalMemoryTestEntity,
LocalMemoryTestEntity.getCompanionDefinition()
)['tableDataCoordinator']['cacheAdapter'];
const cacheKeyMaker = cacheAdapter['makeCacheKey'].bind(cacheAdapter);
const date = new Date();
const entity1Created = await LocalMemoryTestEntity.creator(viewerContext)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
// loading an entity should put it in cache
const entity1 = await LocalMemoryTestEntity.loader(viewerContext)
.enforcing()
.loadByIDAsync(entity1Created.getID());
const entitySpecificGenericCacher =
LocalMemoryCacheAdapterProvider.localMemoryCacheAdapterMap.get(
LocalMemoryTestEntity.getCompanionDefinition().entityConfiguration.tableName
)!['genericLocalMemoryCacher'];
const cachedResult = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', entity1.getID()),
]);
const cachedValue = cachedResult.get(cacheKeyMaker('id', entity1.getID()))!;
expect(cachedValue).toMatchObject({
status: CacheStatus.HIT,
item: {
id: entity1.getID(),
name: 'blah',
dateField: date,
},
});
// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();
const entityNonExistentResult = await LocalMemoryTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
expect(entityNonExistentResult.ok).toBe(false);
const nonExistentCachedResult = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', nonExistentId),
]);
expect(nonExistentCachedResult.get(cacheKeyMaker('id', nonExistentId))).toMatchObject({
status: CacheStatus.NEGATIVE,
});
// load again through entities framework to ensure it reads negative result
const entityNonExistentResult2 = await LocalMemoryTestEntity.loader(
viewerContext
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult2.ok).toBe(false);
// invalidate from cache to ensure it invalidates correctly
await LocalMemoryTestEntity.loader(viewerContext).invalidateFieldsAsync(entity1.getAllFields());
const cachedResultMiss = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', entity1.getID()),
]);
const cachedValueMiss = cachedResultMiss.get(cacheKeyMaker('id', entity1.getID()));
expect(cachedValueMiss).toMatchObject({ status: CacheStatus.MISS });
});
it('shares the cache between different requests', async () => {
const genericLocalMemoryCacherLoadManySpy = jest.spyOn(
GenericLocalMemoryCacher.prototype as unknown as any,
'loadManyAsync'
);
const viewerContext = new TestViewerContext(createLocalMemoryTestEntityCompanionProvider());
const date = new Date();
const entity1Created = await LocalMemoryTestEntity.creator(viewerContext)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
// loading an entity should put it in cache
await LocalMemoryTestEntity.loader(viewerContext)
.enforcing()
.loadByIDAsync(entity1Created.getID());
// load entity with a different request
const viewerContext2 = new TestViewerContext(createLocalMemoryTestEntityCompanionProvider());
const entity1WithVc2 = await LocalMemoryTestEntity.loader(viewerContext2)
.enforcing()
.loadByIDAsync(entity1Created.getID());
const cacheAdapter = viewerContext.entityCompanionProvider.getCompanionForEntity(
LocalMemoryTestEntity,
LocalMemoryTestEntity.getCompanionDefinition()
)['tableDataCoordinator']['cacheAdapter'];
const cacheKeyMaker = cacheAdapter['makeCacheKey'].bind(cacheAdapter);
expect(entity1WithVc2.getAllFields()).toMatchObject({
id: entity1WithVc2.getID(),
name: 'blah',
dateField: date,
});
expect(genericLocalMemoryCacherLoadManySpy).toBeCalledWith([
cacheKeyMaker('id', entity1WithVc2.getID()),
]);
expect(genericLocalMemoryCacherLoadManySpy).toBeCalledTimes(2);
});
it('respects the parameters of a noop cache', async () => {
const viewerContext = new TestViewerContext(
createNoopLocalMemoryIntegrationTestEntityCompanionProvider()
);
const cacheAdapter = viewerContext.entityCompanionProvider.getCompanionForEntity(
LocalMemoryTestEntity,
LocalMemoryTestEntity.getCompanionDefinition()
)['tableDataCoordinator']['cacheAdapter'];
const cacheKeyMaker = cacheAdapter['makeCacheKey'].bind(cacheAdapter);
const date = new Date();
const entity1Created = await LocalMemoryTestEntity.creator(viewerContext)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
// loading an entity will try to put it in cache but it's a noop cache, so it should be a miss
const entity1 = await LocalMemoryTestEntity.loader(viewerContext)
.enforcing()
.loadByIDAsync(entity1Created.getID());
const entitySpecificGenericCacher =
LocalMemoryCacheAdapterProvider.localMemoryCacheAdapterMap.get(
LocalMemoryTestEntity.getCompanionDefinition().entityConfiguration.tableName
)!['genericLocalMemoryCacher'];
const cachedResult = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', entity1.getID()),
]);
const cachedValue = cachedResult.get(cacheKeyMaker('id', entity1.getID()))!;
expect(cachedValue).toMatchObject({
status: CacheStatus.MISS,
});
// a non existent db fetch should try to write negative result ('') but it's a noop cache, so it should be a miss
const nonExistentId = uuidv4();
const entityNonExistentResult = await LocalMemoryTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
expect(entityNonExistentResult.ok).toBe(false);
const nonExistentCachedResult = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', nonExistentId),
]);
expect(nonExistentCachedResult.get(cacheKeyMaker('id', nonExistentId))).toMatchObject({
status: CacheStatus.MISS,
});
});
});
@@ -0,0 +1,107 @@
import { CacheStatus, UUIDField, EntityConfiguration } from '@expo/entity';
import GenericLocalMemoryCacher, {
DOES_NOT_EXIST_LOCAL_MEMORY_CACHE,
} from '../GenericLocalMemoryCacher';
import LocalMemoryCacheAdapter from '../LocalMemoryCacheAdapter';
type BlahFields = {
id: string;
};
const entityConfiguration = new EntityConfiguration<BlahFields>({
idField: 'id',
tableName: 'blah',
schema: {
id: new UUIDField({ columnName: 'id', cache: true }),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'local-memory',
});
describe(LocalMemoryCacheAdapter, () => {
describe('loadManyAsync', () => {
it('returns appropriate cache results', async () => {
const cacheAdapter = new LocalMemoryCacheAdapter(
entityConfiguration,
GenericLocalMemoryCacher.createLRUCache()
);
const cacheHits = new Map<string, Readonly<BlahFields>>([['test-id-1', { id: 'test-id-1' }]]);
await cacheAdapter.cacheManyAsync('id', cacheHits);
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
const results = await cacheAdapter.loadManyAsync('id', [
'test-id-1',
'test-id-2',
'test-id-3',
]);
expect(results.get('test-id-1')).toMatchObject({
status: CacheStatus.HIT,
item: { id: 'test-id-1' },
});
expect(results.get('test-id-2')).toMatchObject({ status: CacheStatus.NEGATIVE });
expect(results.get('test-id-3')).toMatchObject({ status: CacheStatus.MISS });
expect(results.size).toBe(3);
});
it('returns empty map when passed empty array of fieldValues', async () => {
const cacheAdapter = new LocalMemoryCacheAdapter(
entityConfiguration,
GenericLocalMemoryCacher.createLRUCache()
);
const results = await cacheAdapter.loadManyAsync('id', []);
expect(results).toEqual(new Map());
});
});
describe('cacheManyAsync', () => {
it('correctly caches all objects', async () => {
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
const cacheKey = cacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(localMemoryCache.get(cacheKey)).toMatchObject({
id: 'test-id-1',
});
});
});
describe('cacheDBMissesAsync', () => {
it('correctly caches misses', async () => {
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-1']);
const cacheKey = cacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(localMemoryCache.get(cacheKey)).toEqual(DOES_NOT_EXIST_LOCAL_MEMORY_CACHE);
});
});
describe('invalidateManyAsync', () => {
it('invalidates correctly', async () => {
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
await cacheAdapter.invalidateManyAsync('id', ['test-id-1', 'test-id-2']);
const results = await cacheAdapter.loadManyAsync('id', ['test-id-1', 'test-id-2']);
expect(results.get('test-id-1')).toMatchObject({ status: CacheStatus.MISS });
expect(results.get('test-id-2')).toMatchObject({ status: CacheStatus.MISS });
});
it('returns when passed empty array of fieldValues', async () => {
const cacheAdapter = new LocalMemoryCacheAdapter(
entityConfiguration,
GenericLocalMemoryCacher.createLRUCache<BlahFields>({})
);
await cacheAdapter.invalidateManyAsync('id', []);
});
});
});
@@ -0,0 +1,10 @@
/* eslint-disable tsdoc/syntax */
/**
* @packageDocumentation
* @module @expo/entity-cache-adapter-local-memory
*/
export { default as GenericLocalMemoryCacher } from './GenericLocalMemoryCacher';
export * from './GenericLocalMemoryCacher';
export { default as LocalMemoryCacheAdapter } from './LocalMemoryCacheAdapter';
export { default as LocalMemoryCacheAdapterProvider } from './LocalMemoryCacheAdapterProvider';
@@ -0,0 +1,100 @@
import {
AlwaysAllowPrivacyPolicyRule,
EntityPrivacyPolicy,
ViewerContext,
UUIDField,
DateField,
StringField,
EntityConfiguration,
EntityCompanionDefinition,
Entity,
} from '@expo/entity';
export type LocalMemoryTestEntityFields = {
id: string;
name: string;
dateField: Date | null;
};
export default class LocalMemoryTestEntity extends Entity<
LocalMemoryTestEntityFields,
string,
ViewerContext
> {
static getCompanionDefinition(): EntityCompanionDefinition<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity,
LocalMemoryTestEntityPrivacyPolicy
> {
return localMemoryTestEntityCompanionDefinition;
}
}
export class LocalMemoryTestEntityPrivacyPolicy extends EntityPrivacyPolicy<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
> {
protected override readonly createRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly readRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly updateRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly deleteRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
}
export const localMemoryTestEntityConfiguration =
new EntityConfiguration<LocalMemoryTestEntityFields>({
idField: 'id',
tableName: 'local_memory_test_entities',
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
name: new StringField({
columnName: 'name',
cache: true,
}),
dateField: new DateField({
columnName: 'date_field',
}),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'local-memory',
});
const localMemoryTestEntityCompanionDefinition = new EntityCompanionDefinition({
entityClass: LocalMemoryTestEntity,
entityConfiguration: localMemoryTestEntityConfiguration,
privacyPolicyClass: LocalMemoryTestEntityPrivacyPolicy,
});
@@ -0,0 +1,48 @@
import {
NoOpEntityMetricsAdapter,
IEntityMetricsAdapter,
EntityCompanionProvider,
StubQueryContextProvider,
StubDatabaseAdapterProvider,
} from '@expo/entity';
import LocalMemoryCacheAdapterProvider from '../LocalMemoryCacheAdapterProvider';
export const createLocalMemoryTestEntityCompanionProvider = (
localMemoryOptions: { maxSize?: number; ttlSeconds?: number } = {},
metricsAdapter: IEntityMetricsAdapter = new NoOpEntityMetricsAdapter()
): EntityCompanionProvider => {
const localMemoryCacheAdapterProvider =
localMemoryOptions.maxSize === 0 && localMemoryOptions.ttlSeconds === 0
? LocalMemoryCacheAdapterProvider.getNoOpProvider()
: LocalMemoryCacheAdapterProvider.getProvider(localMemoryOptions);
return new EntityCompanionProvider(
metricsAdapter,
new Map([
[
'postgres',
{
adapterProvider: new StubDatabaseAdapterProvider(),
queryContextProvider: StubQueryContextProvider,
},
],
]),
new Map([
[
'local-memory',
{
cacheAdapterProvider: localMemoryCacheAdapterProvider,
},
],
])
);
};
export const createNoopLocalMemoryIntegrationTestEntityCompanionProvider = (
metricsAdapter: IEntityMetricsAdapter = new NoOpEntityMetricsAdapter()
): EntityCompanionProvider => {
return createLocalMemoryTestEntityCompanionProvider(
{ maxSize: 0, ttlSeconds: 0 },
metricsAdapter
);
};
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build",
},
"include": ["src"]
}
@@ -3,6 +3,49 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
### Chores
* Refactor cachers to use an abstract class and interface ([#157](https://github.com/expo/entity/pull/157))
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
### Chores
* Move data transforming responsibilities to the CacheAdapter instead of the ReadThroughEntityCache. ([#153](https://github.com/expo/entity/pull/153))
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-cache-adapter-redis",
"version": "0.22.0",
"version": "0.25.2",
"description": "Redis cache adapter for @expo/entity",
"files": [
"build",
@@ -33,6 +33,6 @@
"ioredis": "^4.27.3"
},
"devDependencies": {
"@expo/entity": "^0.22.0"
"@expo/entity": "^0.25.2"
}
}
@@ -1,6 +1,14 @@
import { CacheLoadResult, CacheStatus } from '@expo/entity';
import {
CacheLoadResult,
CacheStatus,
EntityConfiguration,
transformCacheObjectToFields,
transformFieldsToCacheObject,
IEntityGenericCacher,
} from '@expo/entity';
import { Redis } from 'ioredis';
import { redisTransformerMap } from './RedisCommon';
import wrapNativeRedisCallAsync from './errors/wrapNativeRedisCallAsync';
// Sentinel value we store in Redis to negatively cache a database miss.
@@ -26,12 +34,15 @@ export interface GenericRedisCacheContext {
ttlSecondsNegative: number;
}
export default class GenericRedisCacher {
constructor(private readonly context: GenericRedisCacheContext) {}
export default class GenericRedisCacher<TFields> implements IEntityGenericCacher<TFields> {
constructor(
private readonly context: GenericRedisCacheContext,
private readonly entityConfiguration: EntityConfiguration<TFields>
) {}
public async loadManyAsync(
keys: readonly string[]
): Promise<ReadonlyMap<string, CacheLoadResult>> {
): Promise<ReadonlyMap<string, CacheLoadResult<TFields>>> {
if (keys.length === 0) {
return new Map();
}
@@ -40,7 +51,7 @@ export default class GenericRedisCacher {
this.context.redisClient.mget(...keys)
);
const results = new Map<string, CacheLoadResult>();
const results = new Map<string, CacheLoadResult<TFields>>();
for (let i = 0; i < keys.length; i++) {
const key = keys[i]!;
const redisResult = redisResults[i];
@@ -52,7 +63,11 @@ export default class GenericRedisCacher {
} else if (redisResult) {
results.set(key, {
status: CacheStatus.HIT,
item: JSON.parse(redisResult),
item: transformCacheObjectToFields(
this.entityConfiguration,
redisTransformerMap,
JSON.parse(redisResult)
),
});
} else {
results.set(key, {
@@ -63,7 +78,7 @@ export default class GenericRedisCacher {
return results;
}
public async cacheManyAsync(objectMap: ReadonlyMap<string, object>): Promise<void> {
public async cacheManyAsync(objectMap: ReadonlyMap<string, Readonly<TFields>>): Promise<void> {
if (objectMap.size === 0) {
return;
}
@@ -72,7 +87,9 @@ export default class GenericRedisCacher {
objectMap.forEach((object, key) => {
redisTransaction = redisTransaction.set(
key,
JSON.stringify(object),
JSON.stringify(
transformFieldsToCacheObject(this.entityConfiguration, redisTransformerMap, object)
),
'EX',
this.context.ttlSecondsPositive
);
@@ -80,7 +97,7 @@ export default class GenericRedisCacher {
await wrapNativeRedisCallAsync(() => redisTransaction.exec());
}
public async cacheDBMissesAsync(keys: string[]): Promise<void> {
public async cacheDBMissesAsync(keys: readonly string[]): Promise<void> {
if (keys.length === 0) {
return;
}
@@ -97,7 +114,7 @@ export default class GenericRedisCacher {
await wrapNativeRedisCallAsync(() => redisTransaction.exec());
}
public async invalidateManyAsync(keys: string[]): Promise<void> {
public async invalidateManyAsync(keys: readonly string[]): Promise<void> {
if (keys.length === 0) {
return;
}
@@ -1,18 +1,8 @@
import {
EntityCacheAdapter,
EntityConfiguration,
CacheLoadResult,
FieldTransformerMap,
mapKeys,
transformCacheObjectToFields,
CacheStatus,
transformFieldsToCacheObject,
} from '@expo/entity';
import { EntityCacheAdapter, EntityConfiguration, CacheLoadResult, mapKeys } from '@expo/entity';
import invariant from 'invariant';
import { Redis } from 'ioredis';
import GenericRedisCacher from './GenericRedisCacher';
import { redisTransformerMap } from './RedisCommon';
export interface RedisCacheAdapterContext {
/**
@@ -52,51 +42,33 @@ export interface RedisCacheAdapterContext {
}
export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
/**
* Transformer definitions for field types. Used to modify values as they are read from or written to
* the cache. Override in concrete subclasses to change transformation behavior.
* If a field type is not present in the map, then fields of that type will not be transformed.
*/
private readonly redisFieldTransformer: FieldTransformerMap = redisTransformerMap;
private readonly genericRedisCacher: GenericRedisCacher;
private readonly genericRedisCacher: GenericRedisCacher<TFields>;
constructor(
entityConfiguration: EntityConfiguration<TFields>,
private readonly context: RedisCacheAdapterContext
) {
super(entityConfiguration);
this.genericRedisCacher = new GenericRedisCacher({
redisClient: context.redisClient,
ttlSecondsNegative: context.ttlSecondsNegative,
ttlSecondsPositive: context.ttlSecondsPositive,
});
this.genericRedisCacher = new GenericRedisCacher(
{
redisClient: context.redisClient,
ttlSecondsNegative: context.ttlSecondsNegative,
ttlSecondsPositive: context.ttlSecondsPositive,
},
entityConfiguration
);
}
public async loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult>> {
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
const redisCacheKeyToFieldValueMapping = new Map(
fieldValues.map((fieldValue) => [this.makeCacheKey(fieldName, fieldValue), fieldValue])
);
const rawCacheResults = await this.genericRedisCacher.loadManyAsync(
const cacheResults = await this.genericRedisCacher.loadManyAsync(
Array.from(redisCacheKeyToFieldValueMapping.keys())
);
const cacheResults = new Map<string, CacheLoadResult>();
for (const [redisCacheKey, rawCacheResult] of rawCacheResults) {
if (rawCacheResult.status === CacheStatus.HIT) {
cacheResults.set(redisCacheKey, {
status: CacheStatus.HIT,
item: transformCacheObjectToFields(
this.entityConfiguration,
this.redisFieldTransformer,
rawCacheResult.item
),
});
} else {
cacheResults.set(redisCacheKey, rawCacheResult);
}
}
return mapKeys(cacheResults, (redisCacheKey) => {
const fieldValue = redisCacheKeyToFieldValueMapping.get(redisCacheKey);
@@ -111,21 +83,10 @@ export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFiel
public async cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, object>
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {
const cacheObjectMap = new Map<NonNullable<TFields[N]>, object>();
for (const [fieldValue, object] of objectMap) {
cacheObjectMap.set(
fieldValue,
transformFieldsToCacheObject(
this.entityConfiguration,
this.redisFieldTransformer,
object as Readonly<TFields>
)
);
}
await this.genericRedisCacher.cacheManyAsync(
mapKeys(cacheObjectMap, (fieldValue) => this.makeCacheKey(fieldName, fieldValue))
mapKeys(objectMap, (fieldValue) => this.makeCacheKey(fieldName, fieldValue))
);
}
@@ -0,0 +1,125 @@
import { CacheStatus, ViewerContext } from '@expo/entity';
import Redis from 'ioredis';
import { URL } from 'url';
import GenericRedisCacher from '../GenericRedisCacher';
import { RedisCacheAdapterContext } from '../RedisCacheAdapter';
import RedisTestEntity, {
redisTestEntityConfiguration,
RedisTestEntityFields,
} from '../testfixtures/RedisTestEntity';
import { createRedisIntegrationTestEntityCompanionProvider } from '../testfixtures/createRedisIntegrationTestEntityCompanionProvider';
class TestViewerContext extends ViewerContext {}
describe(GenericRedisCacher, () => {
let redisCacheAdapterContext: RedisCacheAdapterContext;
beforeAll(() => {
redisCacheAdapterContext = {
redisClient: new Redis(new URL(process.env['REDIS_URL']!).toString()),
makeKeyFn(...parts: string[]): string {
const delimiter = ':';
const escapedParts = parts.map((part) =>
part.replace('\\', '\\\\').replace(delimiter, `\\${delimiter}`)
);
return escapedParts.join(delimiter);
},
cacheKeyPrefix: 'test-',
cacheKeyVersion: 1,
ttlSecondsPositive: 86400, // 1 day
ttlSecondsNegative: 600, // 10 minutes
};
});
beforeEach(async () => {
await redisCacheAdapterContext.redisClient.flushdb();
});
afterAll(async () => {
redisCacheAdapterContext.redisClient.disconnect();
});
it('has correct caching and loading behavior', async () => {
const viewerContext = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(redisCacheAdapterContext)
);
const genericRedisCacher = new GenericRedisCacher(
{
redisClient: redisCacheAdapterContext.redisClient,
ttlSecondsNegative: redisCacheAdapterContext.ttlSecondsNegative,
ttlSecondsPositive: redisCacheAdapterContext.ttlSecondsPositive,
},
redisTestEntityConfiguration
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
const testKey = `test-id-key-${entity1Created.getID()}`;
const objectMap = new Map<string, Readonly<RedisTestEntityFields>>([
[testKey, entity1Created.getAllFields()],
]);
await genericRedisCacher.cacheManyAsync(objectMap);
const cachedJSON = await redisCacheAdapterContext.redisClient.get(testKey);
const cachedValue = JSON.parse(cachedJSON!);
expect(cachedValue).toMatchObject({
id: entity1Created.getID(),
dateField: date.toISOString(),
});
const loadedObjectMap = await genericRedisCacher.loadManyAsync([testKey]);
const cacheLoadResult = loadedObjectMap.get(testKey)!;
expect(cacheLoadResult).toMatchObject({
status: CacheStatus.HIT,
item: entity1Created.getAllFields(),
});
expect(loadedObjectMap.size).toBe(1);
});
it('has correct negative caching behaviour', async () => {
const genericRedisCacher = new GenericRedisCacher(
{
redisClient: redisCacheAdapterContext.redisClient,
ttlSecondsNegative: redisCacheAdapterContext.ttlSecondsNegative,
ttlSecondsPositive: redisCacheAdapterContext.ttlSecondsPositive,
},
redisTestEntityConfiguration
);
const testKey = `test-id-key-non-existent-id`;
await genericRedisCacher.cacheDBMissesAsync([testKey]);
const loadedObjectMap = await genericRedisCacher.loadManyAsync([testKey]);
const cacheLoadResult = loadedObjectMap.get(testKey)!;
expect(cacheLoadResult.status).toBe(CacheStatus.NEGATIVE);
});
it('has correct invalidation behaviour', async () => {
const viewerContext = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(redisCacheAdapterContext)
);
const genericRedisCacher = new GenericRedisCacher(
{
redisClient: redisCacheAdapterContext.redisClient,
ttlSecondsNegative: redisCacheAdapterContext.ttlSecondsNegative,
ttlSecondsPositive: redisCacheAdapterContext.ttlSecondsPositive,
},
redisTestEntityConfiguration
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
const testKey = `test-id-key-${entity1Created.getID()}`;
const objectMap = new Map<string, Readonly<RedisTestEntityFields>>([
[testKey, entity1Created.getAllFields()],
]);
await genericRedisCacher.cacheManyAsync(objectMap);
await genericRedisCacher.invalidateManyAsync([testKey]);
const loadedObjectMap = await genericRedisCacher.loadManyAsync([testKey]);
const cacheLoadResult = loadedObjectMap.get(testKey)!;
expect(cacheLoadResult.status).toBe(CacheStatus.MISS);
});
});
@@ -1,4 +1,4 @@
import { CacheStatus, UUIDField, EntityConfiguration, DateField } from '@expo/entity';
import { CacheStatus, UUIDField, EntityConfiguration } from '@expo/entity';
import { Redis, Pipeline } from 'ioredis';
import { mock, when, instance, anything, verify } from 'ts-mockito';
@@ -6,7 +6,6 @@ import RedisCacheAdapter from '../RedisCacheAdapter';
type BlahFields = {
id: string;
date: Date | null;
};
const entityConfiguration = new EntityConfiguration<BlahFields>({
@@ -14,7 +13,6 @@ const entityConfiguration = new EntityConfiguration<BlahFields>({
tableName: 'blah',
schema: {
id: new UUIDField({ columnName: 'id', cache: true }),
date: new DateField({ columnName: 'date' }),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'redis',
@@ -64,40 +62,6 @@ describe(RedisCacheAdapter, () => {
const results = await cacheAdapter.loadManyAsync('id', []);
expect(results).toEqual(new Map());
});
it('transforms a cache object to an Entity object', async () => {
const redisResults = new Map();
const mockRedisClient = mock<Redis>();
// need to have one anything() for each element of spread, in this case 1
when(mockRedisClient.mget(anything())).thenCall(async (...keys) =>
keys.map((k) => redisResults.get(k) ?? null)
);
const cacheAdapter = new RedisCacheAdapter(entityConfiguration, {
redisClient: instance(mockRedisClient),
makeKeyFn: (...parts) => parts.join(':'),
cacheKeyVersion: 1,
cacheKeyPrefix: 'hello-',
ttlSecondsPositive: 1,
ttlSecondsNegative: 2,
});
const date = new Date();
redisResults.set(
cacheAdapter['makeCacheKey']('id', 'wat'),
JSON.stringify({ id: 'wat', date: date.toISOString() })
);
const results = await cacheAdapter.loadManyAsync('id', ['wat']);
expect(results.get('wat')).toMatchObject({
status: CacheStatus.HIT,
item: { id: 'wat', date },
});
expect(results.size).toBe(1);
});
});
describe('cacheManyAsync', () => {
@@ -134,41 +98,6 @@ describe(RedisCacheAdapter, () => {
ttl: 1,
});
});
it('transforms an Entity object to a cache object', async () => {
const redisResults = new Map();
const mockPipeline = mock<Pipeline>();
when(mockPipeline.set(anything(), anything(), anything(), anything())).thenCall(
(key, value, code, ttl) => {
redisResults.set(key, { value, code, ttl });
return pipeline;
}
);
when(mockPipeline.exec()).thenResolve({} as any);
const pipeline = instance(mockPipeline);
const mockRedisClient = mock<Redis>();
when(mockRedisClient.multi()).thenReturn(pipeline);
const cacheAdapter = new RedisCacheAdapter(entityConfiguration, {
redisClient: instance(mockRedisClient),
makeKeyFn: (...parts) => parts.join(':'),
cacheKeyVersion: 1,
cacheKeyPrefix: 'hello-',
ttlSecondsPositive: 1,
ttlSecondsNegative: 2,
});
const date = new Date();
await cacheAdapter.cacheManyAsync('id', new Map([['wat', { id: 'wat', date }]]));
const cacheKey = cacheAdapter['makeCacheKey']('id', 'wat');
expect(redisResults.get(cacheKey)).toMatchObject({
value: JSON.stringify({ id: 'wat', date: date.toISOString() }),
code: 'EX',
ttl: 1,
});
});
});
describe('cacheDBMissesAsync', () => {
@@ -3,6 +3,46 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-database-adapter-knex",
"version": "0.22.0",
"version": "0.25.2",
"description": "Knex database adapter for @expo/entity",
"files": [
"build",
@@ -33,7 +33,7 @@
"knex": "^1.0.2"
},
"devDependencies": {
"@expo/entity": "^0.22.0",
"@expo/entity": "^0.25.2",
"pg": "^8.7.1"
}
}
+43
View File
@@ -3,6 +3,49 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-example
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-example
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* privacy policy evaluation context ([#162](https://github.com/expo/entity/issues/162)) ([3a81a4c](https://github.com/expo/entity/commit/3a81a4c059e2e995eb800e35e76b3d432c22b23f))
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
**Note:** Version bump only for package @expo/entity-example
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
**Note:** Version bump only for package @expo/entity-example
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity-example
+2 -2
View File
@@ -1,7 +1,7 @@
{
"name": "@expo/entity-example",
"private": true,
"version": "0.22.0",
"version": "0.25.2",
"description": "An example integration of the @expo/entity framework",
"scripts": {
"tsc": "tsc",
@@ -23,7 +23,7 @@
"author": "Expo",
"license": "MIT",
"dependencies": {
"@expo/entity": "^0.22.0",
"@expo/entity": "^0.25.2",
"apollo-server-koa": "^2.16.1",
"graphql": "^15.3.0",
"koa": "^2.13.0",
@@ -3,6 +3,7 @@ import {
ReadonlyEntity,
EntityQueryContext,
RuleEvaluationResult,
EntityPrivacyPolicyEvaluationContext,
} from '@expo/entity';
import { ExampleViewerContext } from '../viewerContexts';
@@ -34,6 +35,7 @@ export default class AllowIfUserOwnerPrivacyRule<
async evaluateAsync(
viewerContext: ExampleViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity
): Promise<RuleEvaluationResult> {
if (viewerContext.isUserViewerContext()) {
@@ -3,6 +3,46 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-full-integration-tests
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity-full-integration-tests
@@ -1,7 +1,7 @@
{
"name": "@expo/entity-full-integration-tests",
"private": true,
"version": "0.22.0",
"version": "0.25.2",
"description": "Full redis and knex integration tests for the entity framework",
"scripts": {
"tsc": "tsc",
@@ -27,8 +27,8 @@
"@expo/entity-database-adapter-knex": "*"
},
"devDependencies": {
"@expo/entity": "^0.22.0",
"@expo/entity-cache-adapter-redis": "^0.22.0",
"@expo/entity-database-adapter-knex": "^0.22.0"
"@expo/entity": "^0.25.2",
"@expo/entity-cache-adapter-redis": "^0.25.2",
"@expo/entity-database-adapter-knex": "^0.25.2"
}
}
@@ -3,6 +3,46 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-ip-address-field
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-ip-address-field
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
**Note:** Version bump only for package @expo/entity-ip-address-field
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
**Note:** Version bump only for package @expo/entity-ip-address-field
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
**Note:** Version bump only for package @expo/entity-ip-address-field
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity-ip-address-field
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-ip-address-field",
"version": "0.22.0",
"version": "0.25.2",
"description": "IP address EntityField definitions for @expo/entity",
"files": [
"build",
@@ -28,7 +28,7 @@
"ip-address": "^8.1.0"
},
"devDependencies": {
"@expo/entity": "^0.22.0",
"@expo/entity": "^0.25.2",
"@types/jsbn": "^1.2.29"
},
"peerDependencies": {
@@ -0,0 +1,27 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-secondary-cache-local-memory
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-secondary-cache-local-memory
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* composed cache adapter, composed secondary cache, local memory secondary cache ([#164](https://github.com/expo/entity/issues/164)) ([6b6e70e](https://github.com/expo/entity/commit/6b6e70e75f7e42a4dce13edeb406a4d88d9264f0))
@@ -0,0 +1,24 @@
# `@expo/entity-secondary-cache-local-memory`
Cross-request [LRU](https://github.com/isaacs/node-lru-cache) secondary cache for `@expo/entity`. Use
this cache with caution - it is nonstandard. The cache is shared between requests in the node process.
[Documentation](https://expo.github.io/entity/modules/_expo_secondary_cache_local_memory.html)
## Usage
1. Create a concrete implementation of abstract class `EntitySecondaryCacheLoader`, in this example `TestEntitySecondaryCacheLoader`. The underlying data can come from anywhere, but an entity is constructed from the data and then authorized for the viewer.
2. Create an instance of your `EntitySecondaryCacheLoader`, passing in a `LocalMemorySecondaryEntityCache`.
```typescript
const secondaryCacheLoader = new TestSecondaryLocalMemoryCacheLoader(
new LocalMemorySecondaryEntityCache(
GenericLocalMemoryCacher.createLRUCache<LocalMemoryTestEntityFields>({})
),
LocalMemoryTestEntity.loader(viewerContext)
);
```
3. Load entities through it:
```typescript
const loadParams = { id: createdEntity.getID() };
const results = await secondaryCacheLoader.loadManyAsync([loadParams]);
```
@@ -0,0 +1,38 @@
{
"name": "@expo/entity-secondary-cache-local-memory",
"version": "0.25.2",
"description": "Local memory secondary cache for @expo/entity",
"files": [
"build",
"src"
],
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src",
"lint-fix": "eslint src --fix",
"test": "jest --rootDir . --config ../../resources/jest.config.js --passWithNoTests",
"integration": "../../resources/run-with-docker yarn integration-no-setup",
"integration-no-setup": "jest --config ../../resources/jest-integration.config.js --rootDir . --runInBand --passWithNoTests",
"barrelsby": "barrelsby --directory src --location top --exclude tests__ --singleQuotes --exportDefault --delete"
},
"engines": {
"node": ">=12"
},
"keywords": [
"entity"
],
"author": "Expo",
"license": "MIT",
"peerDependencies": {
"@expo/entity": "*",
"@expo/entity-cache-adapter-local-memory": "*"
},
"devDependencies": {
"@expo/entity": "^0.25.2",
"@expo/entity-cache-adapter-local-memory": "^0.25.2",
"nullthrows": "^1.1.1"
}
}
@@ -0,0 +1,21 @@
import { GenericSecondaryEntityCache } from '@expo/entity';
import {
GenericLocalMemoryCacher,
LocalMemoryCache,
} from '@expo/entity-cache-adapter-local-memory';
/**
* A local memory {@link GenericSecondaryEntityCache}.
*
* @remarks
*
* TLoadParams must be JSON stringifyable.
*/
export default class LocalMemorySecondaryEntityCache<
TFields,
TLoadParams
> extends GenericSecondaryEntityCache<TFields, TLoadParams> {
constructor(localMemoryCache: LocalMemoryCache<TFields>) {
super(new GenericLocalMemoryCacher(localMemoryCache), (params) => JSON.stringify(params));
}
}
@@ -0,0 +1,228 @@
import {
EntitySecondaryCacheLoader,
mapMapAsync,
ViewerContext,
NoOpEntityMetricsAdapter,
IEntityMetricsAdapter,
EntityCompanionProvider,
StubQueryContextProvider,
StubDatabaseAdapterProvider,
AlwaysAllowPrivacyPolicyRule,
EntityPrivacyPolicy,
UUIDField,
StringField,
EntityConfiguration,
EntityCompanionDefinition,
Entity,
} from '@expo/entity';
import {
GenericLocalMemoryCacher,
LocalMemoryCacheAdapterProvider,
} from '@expo/entity-cache-adapter-local-memory';
import nullthrows from 'nullthrows';
import LocalMemorySecondaryEntityCache from '../LocalMemorySecondaryEntityCache';
export type LocalMemoryTestEntityFields = {
id: string;
name: string;
};
export default class LocalMemoryTestEntity extends Entity<
LocalMemoryTestEntityFields,
string,
ViewerContext
> {
static getCompanionDefinition(): EntityCompanionDefinition<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity,
LocalMemoryTestEntityPrivacyPolicy
> {
return localMemoryTestEntityCompanionDefinition;
}
}
export class LocalMemoryTestEntityPrivacyPolicy extends EntityPrivacyPolicy<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
> {
protected override readonly createRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly readRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly updateRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
protected override readonly deleteRules = [
new AlwaysAllowPrivacyPolicyRule<
LocalMemoryTestEntityFields,
string,
ViewerContext,
LocalMemoryTestEntity
>(),
];
}
export const localMemoryTestEntityConfiguration =
new EntityConfiguration<LocalMemoryTestEntityFields>({
idField: 'id',
tableName: 'local_memory_test_entities',
schema: {
id: new UUIDField({
columnName: 'id',
}),
name: new StringField({
columnName: 'name',
}),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'local-memory',
});
const localMemoryTestEntityCompanionDefinition = new EntityCompanionDefinition({
entityClass: LocalMemoryTestEntity,
entityConfiguration: localMemoryTestEntityConfiguration,
privacyPolicyClass: LocalMemoryTestEntityPrivacyPolicy,
});
export const createTestEntityCompanionProvider = (
metricsAdapter: IEntityMetricsAdapter = new NoOpEntityMetricsAdapter()
): EntityCompanionProvider => {
return new EntityCompanionProvider(
metricsAdapter,
new Map([
[
'postgres',
{
adapterProvider: new StubDatabaseAdapterProvider(),
queryContextProvider: StubQueryContextProvider,
},
],
]),
new Map([
[
'local-memory',
{
cacheAdapterProvider: LocalMemoryCacheAdapterProvider.getProvider(),
},
],
])
);
};
class TestViewerContext extends ViewerContext {}
type TestLoadParams = { id: string };
const FAKE_ID = 'fake';
class TestSecondaryLocalMemoryCacheLoader extends EntitySecondaryCacheLoader<
TestLoadParams,
LocalMemoryTestEntityFields,
string,
TestViewerContext,
LocalMemoryTestEntity,
LocalMemoryTestEntityPrivacyPolicy
> {
public databaseLoadCount = 0;
protected async fetchObjectsFromDatabaseAsync(
loadParamsArray: readonly Readonly<TestLoadParams>[]
): Promise<ReadonlyMap<Readonly<TestLoadParams>, Readonly<LocalMemoryTestEntityFields> | null>> {
this.databaseLoadCount += loadParamsArray.length;
const emptyMap = new Map(loadParamsArray.map((p) => [p, null]));
return await mapMapAsync(emptyMap, async (_value, loadParams) => {
if (loadParams.id === FAKE_ID) {
return null;
}
return nullthrows(
(
await this.entityLoader
.enforcing()
.loadManyByFieldEqualityConjunctionAsync([
{ fieldName: 'id', fieldValue: loadParams.id },
])
)[0]
).getAllFields();
});
}
}
describe(LocalMemorySecondaryEntityCache, () => {
it('Loads through secondary loader, caches, and invalidates', async () => {
const viewerContext = new TestViewerContext(createTestEntityCompanionProvider());
const createdEntity = await LocalMemoryTestEntity.creator(viewerContext)
.setField('name', 'wat')
.enforceCreateAsync();
const secondaryCacheLoader = new TestSecondaryLocalMemoryCacheLoader(
new LocalMemorySecondaryEntityCache(
GenericLocalMemoryCacher.createLRUCache<LocalMemoryTestEntityFields>({})
),
LocalMemoryTestEntity.loader(viewerContext)
);
const loadParams = { id: createdEntity.getID() };
const results = await secondaryCacheLoader.loadManyAsync([loadParams]);
expect(nullthrows(results.get(loadParams)).enforceValue().getID()).toEqual(
createdEntity.getID()
);
expect(secondaryCacheLoader.databaseLoadCount).toEqual(1);
const results2 = await secondaryCacheLoader.loadManyAsync([loadParams]);
expect(nullthrows(results2.get(loadParams)).enforceValue().getID()).toEqual(
createdEntity.getID()
);
expect(secondaryCacheLoader.databaseLoadCount).toEqual(1);
await secondaryCacheLoader.invalidateManyAsync([loadParams]);
const results3 = await secondaryCacheLoader.loadManyAsync([loadParams]);
expect(nullthrows(results3.get(loadParams)).enforceValue().getID()).toEqual(
createdEntity.getID()
);
expect(secondaryCacheLoader.databaseLoadCount).toEqual(2);
});
it('correctly handles uncached and unfetchable load params', async () => {
const viewerContext = new TestViewerContext(createTestEntityCompanionProvider());
const secondaryCacheLoader = new TestSecondaryLocalMemoryCacheLoader(
new LocalMemorySecondaryEntityCache(
GenericLocalMemoryCacher.createLRUCache<LocalMemoryTestEntityFields>({})
),
LocalMemoryTestEntity.loader(viewerContext)
);
const loadParams = { id: FAKE_ID };
const results = await secondaryCacheLoader.loadManyAsync([loadParams]);
expect(results.size).toBe(1);
expect(results.get(loadParams)).toBe(null);
});
});
@@ -0,0 +1,7 @@
/* eslint-disable tsdoc/syntax */
/**
* @packageDocumentation
* @module @expo/entity-secondary-cache-local-memory
*/
export { default as LocalMemorySecondaryEntityCache } from './LocalMemorySecondaryEntityCache';
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "build",
},
"include": ["src"]
}
@@ -3,6 +3,51 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity-secondary-cache-redis
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
**Note:** Version bump only for package @expo/entity-secondary-cache-redis
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* composed cache adapter, composed secondary cache, local memory secondary cache ([#164](https://github.com/expo/entity/issues/164)) ([6b6e70e](https://github.com/expo/entity/commit/6b6e70e75f7e42a4dce13edeb406a4d88d9264f0))
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
### Chores
* Refactor cachers to use an abstract class and interface ([#157](https://github.com/expo/entity/pull/157))
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
### Chores
* Move data transforming responsibilities to the CacheAdapter instead of the RedisSecondaryEntityCache. ([#153](https://github.com/expo/entity/pull/153))
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity-secondary-cache-redis
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-secondary-cache-redis",
"version": "0.22.0",
"version": "0.25.2",
"description": "Redis secondary cache for @expo/entity",
"files": [
"build",
@@ -34,8 +34,8 @@
"ioredis": "^4.17.3"
},
"devDependencies": {
"@expo/entity": "^0.22.0",
"@expo/entity-cache-adapter-redis": "^0.22.0",
"@expo/entity": "^0.25.2",
"@expo/entity-cache-adapter-redis": "^0.25.2",
"nullthrows": "^1.1.1"
}
}
@@ -1,136 +1,18 @@
import {
CacheStatus,
EntityConfiguration,
filterMap,
ISecondaryEntityCache,
transformCacheObjectToFields,
transformFieldsToCacheObject,
zipToMap,
} from '@expo/entity';
import {
GenericRedisCacheContext,
GenericRedisCacher,
redisTransformerMap,
} from '@expo/entity-cache-adapter-redis';
import invariant from 'invariant';
import { EntityConfiguration, GenericSecondaryEntityCache } from '@expo/entity';
import { GenericRedisCacheContext, GenericRedisCacher } from '@expo/entity-cache-adapter-redis';
/**
* A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular
* single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes
* a call to {@link EntityLoader.loadManyByFieldEqualityConjunctionAsync} is guaranteed to return at most one entity.
* A redis {@link GenericSecondaryEntityCache}.
*/
export default class RedisSecondaryEntityCache<TFields, TLoadParams>
implements ISecondaryEntityCache<TFields, TLoadParams>
{
private readonly genericRedisCacher: GenericRedisCacher;
export default class RedisSecondaryEntityCache<
TFields,
TLoadParams
> extends GenericSecondaryEntityCache<TFields, TLoadParams> {
constructor(
private readonly entityConfiguration: EntityConfiguration<TFields>,
entityConfiguration: EntityConfiguration<TFields>,
genericRedisCacheContext: GenericRedisCacheContext,
private readonly constructRedisKey: (params: Readonly<TLoadParams>) => string
constructRedisKey: (params: Readonly<TLoadParams>) => string
) {
this.genericRedisCacher = new GenericRedisCacher(genericRedisCacheContext);
}
/**
* Read-through cache function. Steps:
*
* 1. Check for cached objects
* 2. Query the fetcher for loadParams not in the cache
* 3. Cache the results from the fetcher
* 4. Negatively cache anything missing from the fetcher
* 5. Return the full set of data for the query.
*
* @param loadParamsArray - array of loadParams to load from the cache
* @param fetcher - closure used to provide underlying data source objects for loadParams
* @returns map from loadParams to the entity field object
*/
public async loadManyThroughAsync(
loadParamsArray: readonly Readonly<TLoadParams>[],
fetcher: (
fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]
) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>
): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>> {
const redisKeys = loadParamsArray.map(this.constructRedisKey);
const redisKeyToLoadParamsMap = zipToMap(redisKeys, loadParamsArray);
const cacheLoadResults = await this.genericRedisCacher.loadManyAsync(redisKeys);
invariant(
cacheLoadResults.size === loadParamsArray.length,
`${this.constructor.name} loadMany should return a result for each key`
);
const redisKeysToFetch = Array.from(
filterMap(
cacheLoadResults,
(cacheLoadResult) => cacheLoadResult.status === CacheStatus.MISS
).keys()
);
// put transformed cache hits in result map
const results: Map<Readonly<TLoadParams>, Readonly<TFields> | null> = new Map();
cacheLoadResults.forEach((cacheLoadResult, redisKey) => {
if (cacheLoadResult.status === CacheStatus.HIT) {
const loadParams = redisKeyToLoadParamsMap.get(redisKey);
invariant(loadParams !== undefined, 'load params should be in redis key map');
results.set(
loadParams,
transformCacheObjectToFields(
this.entityConfiguration,
redisTransformerMap,
cacheLoadResult.item
)
);
}
});
// fetch any misses from DB, add DB objects to results, cache DB results, inform cache of any missing DB results
if (redisKeysToFetch.length > 0) {
const loadParamsToFetch = redisKeysToFetch.map((redisKey) => {
const loadParams = redisKeyToLoadParamsMap.get(redisKey);
invariant(loadParams !== undefined, 'load params should be in redis key map');
return loadParams;
});
const fetchResults = await fetcher(loadParamsToFetch);
const fetchMisses = loadParamsToFetch.filter((loadParams) => {
// all values of fetchResults should be field objects or undefined
return !fetchResults.get(loadParams);
});
for (const fetchMiss of fetchMisses) {
results.set(fetchMiss, null);
}
const objectsToCache: Map<string, object> = new Map();
for (const [loadParams, object] of fetchResults.entries()) {
if (object) {
objectsToCache.set(
this.constructRedisKey(loadParams),
transformFieldsToCacheObject(this.entityConfiguration, redisTransformerMap, object)
);
results.set(loadParams, object);
}
}
await Promise.all([
this.genericRedisCacher.cacheManyAsync(objectsToCache),
this.genericRedisCacher.cacheDBMissesAsync(fetchMisses.map(this.constructRedisKey)),
]);
}
return results;
}
/**
* Invalidate the cache for objects cached by constructRedisKey(loadParams).
*
* @param loadParamsArray - load params to invalidate
*/
public async invalidateManyAsync(
loadParamsArray: readonly Readonly<TLoadParams>[]
): Promise<void> {
await this.genericRedisCacher.invalidateManyAsync(loadParamsArray.map(this.constructRedisKey));
super(new GenericRedisCacher(genericRedisCacheContext, entityConfiguration), constructRedisKey);
}
}
+52
View File
@@ -3,6 +3,58 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.25.2](https://github.com/expo/entity/compare/v0.25.1...v0.25.2) (2022-03-12)
**Note:** Version bump only for package @expo/entity
## [0.25.1](https://github.com/expo/entity/compare/v0.25.0...v0.25.1) (2022-03-10)
### Bug Fixes
* add composed cache to index ([#166](https://github.com/expo/entity/issues/166)) ([5c82edc](https://github.com/expo/entity/commit/5c82edcd9f26854878d29950a05852e117bbd723))
# [0.25.0](https://github.com/expo/entity/compare/v0.24.0...v0.25.0) (2022-03-10)
### Features
* composed cache adapter, composed secondary cache, local memory secondary cache ([#164](https://github.com/expo/entity/issues/164)) ([6b6e70e](https://github.com/expo/entity/commit/6b6e70e75f7e42a4dce13edeb406a4d88d9264f0))
* privacy policy evaluation context ([#162](https://github.com/expo/entity/issues/162)) ([3a81a4c](https://github.com/expo/entity/commit/3a81a4c059e2e995eb800e35e76b3d432c22b23f))
# [0.24.0](https://github.com/expo/entity/compare/v0.23.0...v0.24.0) (2022-02-16)
**Note:** Version bump only for package @expo/entity
### Chores
* Refactor cachers to use an abstract class and interface ([#157](https://github.com/expo/entity/pull/157))
# [0.23.0](https://github.com/expo/entity/compare/v0.22.0...v0.23.0) (2022-02-09)
### Breaking changes
* Move data transforming responsibilities to the CacheAdapter instead of the ReadThroughEntityCache. Affects any classes extending the EntityCacheAdapter and relying on the transformation behavior of the ReadThroughEntityCache. ([#153](https://github.com/expo/entity/pull/153))
# [0.22.0](https://github.com/expo/entity/compare/v0.21.0...v0.22.0) (2022-02-04)
**Note:** Version bump only for package @expo/entity
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@expo/entity",
"version": "0.22.0",
"version": "0.25.2",
"description": "A privacy-first data model",
"files": [
"build",
@@ -0,0 +1,86 @@
import nullthrows from 'nullthrows';
import EntityCacheAdapter from './EntityCacheAdapter';
import EntityConfiguration from './EntityConfiguration';
import { CacheStatus, CacheLoadResult } from './internal/ReadThroughEntityCache';
export default class ComposedEntityCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
/**
* A {@link EntityCacheAdapter} that composes other {@link EntityCacheAdapter} instances.
*
* @param entityConfiguration - configuration for entity being loaded
* @param cacheAdapters - list of cache adapters to compose in order of precedence.
* Earlier cache adapters are read from first and written to (including invalidations) last.
* Typically, caches closer to the application should be ordered before caches closer to the database.
*/
constructor(
entityConfiguration: EntityConfiguration<TFields>,
private readonly cacheAdapters: EntityCacheAdapter<TFields>[]
) {
super(entityConfiguration);
}
public async loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
const retMap = new Map<NonNullable<TFields[N]>, CacheLoadResult<TFields>>();
let unfulfilledFieldValues = fieldValues;
for (const cacheAdapter of this.cacheAdapters) {
const cacheResultsFromAdapter = await cacheAdapter.loadManyAsync(
fieldName,
unfulfilledFieldValues
);
const newUnfulfilledFieldValues = [];
for (const [fieldValue, cacheResult] of cacheResultsFromAdapter) {
if (cacheResult.status === CacheStatus.MISS) {
newUnfulfilledFieldValues.push(fieldValue);
} else {
retMap.set(fieldValue, cacheResult);
}
}
unfulfilledFieldValues = newUnfulfilledFieldValues;
}
for (const fieldValue of unfulfilledFieldValues) {
retMap.set(fieldValue, { status: CacheStatus.MISS });
}
return retMap;
}
public async cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {
// write to lower layers first
for (let i = this.cacheAdapters.length - 1; i >= 0; i--) {
const cacheAdapter = nullthrows(this.cacheAdapters[i]);
await cacheAdapter.cacheManyAsync(fieldName, objectMap);
}
}
public async cacheDBMissesAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
// write to lower layers first
for (let i = this.cacheAdapters.length - 1; i >= 0; i--) {
const cacheAdapter = nullthrows(this.cacheAdapters[i]);
await cacheAdapter.cacheDBMissesAsync(fieldName, fieldValues);
}
}
public async invalidateManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
// delete from lower layers first
for (let i = this.cacheAdapters.length - 1; i >= 0; i--) {
const cacheAdapter = nullthrows(this.cacheAdapters[i]);
await cacheAdapter.invalidateManyAsync(fieldName, fieldValues);
}
}
}
@@ -0,0 +1,63 @@
import nullthrows from 'nullthrows';
import { ISecondaryEntityCache } from './EntitySecondaryCacheLoader';
export default class ComposedSecondaryEntityCache<TLoadParams, TFields>
implements ISecondaryEntityCache<TFields, TLoadParams>
{
/**
* A {@link ISecondaryEntityCache} that composes other {@link ISecondaryEntityCache} instances.
*
* @param secondaryEntityCaches - list of caches to compose in order of precedence.
* Earlier caches are read from first and written to (including invalidations) last.
* Typically, caches closer to the application should be ordered before caches closer to the database.
*/
constructor(
private readonly secondaryEntityCaches: ISecondaryEntityCache<TFields, TLoadParams>[]
) {}
async loadManyThroughAsync(
loadParamsArray: readonly Readonly<TLoadParams>[],
fetcher: (
fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]
) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>
): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>> {
return await ComposedSecondaryEntityCache.loadManyThroughRecursivelyAsync(
this.secondaryEntityCaches,
loadParamsArray,
fetcher
);
}
private static async loadManyThroughRecursivelyAsync<TLoadParams, TFields>(
secondaryEntityCaches: ISecondaryEntityCache<TFields, TLoadParams>[],
loadParamsArray: readonly Readonly<TLoadParams>[],
fetcher: (
fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]
) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>
): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>> {
if (secondaryEntityCaches.length === 0) {
return await fetcher(loadParamsArray);
}
const [firstCache, ...restCaches] = secondaryEntityCaches;
return await nullthrows(firstCache).loadManyThroughAsync(
loadParamsArray,
(fetcherLoadParamsArray) =>
ComposedSecondaryEntityCache.loadManyThroughRecursivelyAsync(
restCaches,
fetcherLoadParamsArray,
fetcher
)
);
}
async invalidateManyAsync(loadParamsArray: readonly Readonly<TLoadParams>[]): Promise<void> {
// invalidate lower layers first
for (let i = this.secondaryEntityCaches.length - 1; i >= 0; i--) {
const secondaryEntityCache = nullthrows(this.secondaryEntityCaches[i]);
await secondaryEntityCache.invalidateManyAsync(loadParamsArray);
}
}
}
+6 -4
View File
@@ -69,7 +69,7 @@ export default abstract class Entity<
return viewerContext
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forCreate(queryContext);
.forCreate(queryContext, { cascadingDeleteCause: null });
}
/**
@@ -111,7 +111,7 @@ export default abstract class Entity<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forUpdate(existingEntity, queryContext);
.forUpdate(existingEntity, queryContext, { cascadingDeleteCause: null });
}
/**
@@ -152,7 +152,7 @@ export default abstract class Entity<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forDelete(existingEntity, queryContext)
.forDelete(existingEntity, queryContext, { cascadingDeleteCause: null })
.deleteAsync();
}
@@ -194,7 +194,7 @@ export default abstract class Entity<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
.forDelete(existingEntity, queryContext)
.forDelete(existingEntity, queryContext, { cascadingDeleteCause: null })
.enforceDeleteAsync();
}
@@ -251,6 +251,7 @@ export default abstract class Entity<
privacyPolicy.authorizeUpdateAsync(
existingEntity.getViewerContext(),
queryContext,
{ cascadingDeleteCause: null },
existingEntity,
companion.getMetricsAdapter()
)
@@ -304,6 +305,7 @@ export default abstract class Entity<
privacyPolicy.authorizeDeleteAsync(
existingEntity.getViewerContext(),
queryContext,
{ cascadingDeleteCause: null },
existingEntity,
companion.getMetricsAdapter()
)
@@ -69,7 +69,7 @@ export default class EntityAssociationLoader<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getLoaderFactory()
.forLoad(queryContext);
.forLoad(queryContext, { cascadingDeleteCause: null });
return (await loader.loadByIDAsync(associatedEntityID as unknown as TAssociatedID)) as Result<
null extends TFields[TIdentifyingField] ? TAssociatedEntity | null : TAssociatedEntity
@@ -123,7 +123,7 @@ export default class EntityAssociationLoader<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getLoaderFactory()
.forLoad(queryContext);
.forLoad(queryContext, { cascadingDeleteCause: null });
return await loader.loadManyByFieldEqualingAsync(
associatedEntityFieldContainingThisID,
thisID as any
@@ -180,7 +180,7 @@ export default class EntityAssociationLoader<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getLoaderFactory()
.forLoad(queryContext);
.forLoad(queryContext, { cascadingDeleteCause: null });
return await loader.loadByFieldEqualingAsync(
associatedEntityLookupByField,
associatedFieldValue as any
@@ -238,7 +238,7 @@ export default class EntityAssociationLoader<
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getLoaderFactory()
.forLoad(queryContext);
.forLoad(queryContext, { cascadingDeleteCause: null });
return await loader.loadManyByFieldEqualingAsync(
associatedEntityLookupByField,
associatedFieldValue as any
+2 -2
View File
@@ -17,7 +17,7 @@ export default abstract class EntityCacheAdapter<TFields> {
public abstract loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult>>;
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>>;
/**
* Cache many objects fetched from the DB.
@@ -26,7 +26,7 @@ export default abstract class EntityCacheAdapter<TFields> {
*/
public abstract cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, object>
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void>;
/**
@@ -28,6 +28,9 @@ export enum EntityEdgeDeletionBehavior {
SET_NULL,
}
/**
* Defines an association between entities. An association is primarily used to define cascading deletion behavior.
*/
export interface EntityAssociationDefinition<
TViewerContext extends ViewerContext,
TAssociatedFields,
@@ -83,6 +86,10 @@ export interface EntityAssociationDefinition<
edgeDeletionBehavior?: EntityEdgeDeletionBehavior;
}
/**
* Definition for a field referencing a column in the underlying database. Specifies things like
* cache behavior and associations, and handles input validation.
*/
export abstract class EntityFieldDefinition<T> {
readonly columnName: string;
readonly cache: boolean;
@@ -93,6 +100,7 @@ export abstract class EntityFieldDefinition<T> {
* @param cache - Whether or not to cache loaded instances of the entity by this field. The column name is
* used to derive a cache key for the cache entry. If true, this column must be able uniquely
* identify the entity.
* @param association - Defines the association behavior for an entity that this column references.
*/
constructor({
columnName,
+45
View File
@@ -2,59 +2,104 @@ import { validate as validateUUID } from 'uuid';
import { EntityFieldDefinition } from './EntityFieldDefinition';
/**
* {@link EntityFieldDefinition} for a column with a JS string type.
*/
export class StringField extends EntityFieldDefinition<string> {
protected validateInputValueInternal(value: string): boolean {
return typeof value === 'string';
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS string type.
* Enforces that the string is a valid UUID.
*/
export class UUIDField extends StringField {
protected override validateInputValueInternal(value: string): boolean {
return validateUUID(value);
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS Date type.
*/
export class DateField extends EntityFieldDefinition<Date> {
protected validateInputValueInternal(value: Date): boolean {
return value instanceof Date;
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS boolean type.
*/
export class BooleanField extends EntityFieldDefinition<boolean> {
protected validateInputValueInternal(value: boolean): boolean {
return typeof value === 'boolean';
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS number type.
* Enforces that the number is an integer.
*/
export class IntField extends EntityFieldDefinition<number> {
protected validateInputValueInternal(value: number): boolean {
return typeof value === 'number' && Number.isInteger(value);
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS number type.
* Enforces that the number is a float (which includes integers in JS).
*/
export class FloatField extends EntityFieldDefinition<number> {
protected validateInputValueInternal(value: number): boolean {
return typeof value === 'number';
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS string array type.
* Enforces that every member of the string array is a string.
*/
export class StringArrayField extends EntityFieldDefinition<string[]> {
protected validateInputValueInternal(value: string[]): boolean {
return Array.isArray(value) && value.every((subValue) => typeof subValue === 'string');
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS JSON object type.
*/
export class JSONObjectField extends EntityFieldDefinition<object> {
protected validateInputValueInternal(value: object): boolean {
return typeof value === 'object' && !Array.isArray(value);
}
}
/**
* {@link EntityFieldDefinition} for a enum column with a JS string or number type.
*/
export class EnumField extends EntityFieldDefinition<string | number> {
protected validateInputValueInternal(value: string | number): boolean {
return typeof value === 'number' || typeof value === 'string';
}
}
/**
* {@link EntityFieldDefinition} for a column with a JS JSON array type.
*/
export class JSONArrayField extends EntityFieldDefinition<any[]> {
protected validateInputValueInternal(value: any[]): boolean {
return Array.isArray(value);
}
}
/**
* {@link EntityFieldDefinition} for a column that may be a JS JSON array type.
* Does not do any validation.
*/
export class MaybeJSONArrayField extends EntityFieldDefinition<any | any[]> {
protected validateInputValueInternal(_value: any): boolean {
return true;
+5 -1
View File
@@ -9,7 +9,7 @@ import {
QuerySelectionModifiers,
isSingleValueFieldEqualityCondition,
} from './EntityDatabaseAdapter';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -40,6 +40,7 @@ export default class EntityLoader<
constructor(
private readonly viewerContext: TViewerContext,
private readonly queryContext: EntityQueryContext,
private readonly privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext,
private readonly entityConfiguration: EntityConfiguration<TFields>,
private readonly entityClass: IEntityClass<
TFields,
@@ -218,6 +219,7 @@ export default class EntityLoader<
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
this.privacyPolicyEvaluationContext,
uncheckedEntityResult.value,
this.metricsAdapter
)
@@ -272,6 +274,7 @@ export default class EntityLoader<
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
this.privacyPolicyEvaluationContext,
uncheckedEntityResult.value,
this.metricsAdapter
)
@@ -332,6 +335,7 @@ export default class EntityLoader<
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
this.privacyPolicyEvaluationContext,
uncheckedEntityResult.value,
this.metricsAdapter
)
+4 -2
View File
@@ -1,7 +1,7 @@
import { IEntityClass } from './Entity';
import EntityConfiguration from './EntityConfiguration';
import EntityLoader from './EntityLoader';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -47,11 +47,13 @@ export default class EntityLoaderFactory<
*/
forLoad(
viewerContext: TViewerContext,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): EntityLoader<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
this.entityConfiguration,
this.entityClass,
this.privacyPolicyClass,
+13 -3
View File
@@ -22,9 +22,19 @@ export type EntityValidatorMutationInfo<
previousValue: TEntity;
};
export type EntityMutationTriggerDeleteCascadeInfo<> = {
/**
* Information about a cascading deletion.
*/
export type EntityCascadingDeletionInfo = {
/**
* The entity that is being mutated at this step in the cascaded deletion.
*/
entity: Entity<any, any, any, any>;
cascadingDeleteCause: EntityMutationTriggerDeleteCascadeInfo | null;
/**
* The cascade deletion that caused this mutation.
*/
cascadingDeleteCause: EntityCascadingDeletionInfo | null;
};
export type EntityTriggerMutationInfo<
@@ -43,5 +53,5 @@ export type EntityTriggerMutationInfo<
}
| {
type: EntityMutationType.DELETE;
cascadingDeleteCause: EntityMutationTriggerDeleteCascadeInfo | null;
cascadingDeleteCause: EntityCascadingDeletionInfo | null;
};
+44 -21
View File
@@ -11,14 +11,14 @@ import {
EntityValidatorMutationInfo,
EntityMutationType,
EntityTriggerMutationInfo,
EntityMutationTriggerDeleteCascadeInfo,
EntityCascadingDeletionInfo,
} from './EntityMutationInfo';
import EntityMutationTriggerConfiguration, {
EntityMutationTrigger,
EntityNonTransactionalMutationTrigger,
} from './EntityMutationTriggerConfiguration';
import EntityMutationValidator from './EntityMutationValidator';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext, EntityTransactionalQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -44,6 +44,7 @@ abstract class BaseMutator<
constructor(
protected readonly viewerContext: TViewerContext,
protected readonly queryContext: EntityQueryContext,
protected readonly privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext,
protected readonly entityConfiguration: EntityConfiguration<TFields>,
protected readonly entityClass: IEntityClass<
TFields,
@@ -220,6 +221,7 @@ export class CreateMutator<
this.privacyPolicy.authorizeCreateAsync(
this.viewerContext,
queryContext,
this.privacyPolicyEvaluationContext,
temporaryEntityForPrivacyCheck,
this.metricsAdapter
)
@@ -249,7 +251,11 @@ export class CreateMutator<
const insertResult = await this.databaseAdapter.insertAsync(queryContext, this.fieldsForEntity);
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
const entityLoader = this.entityLoaderFactory.forLoad(
this.viewerContext,
queryContext,
this.privacyPolicyEvaluationContext
);
queryContext.appendPostCommitInvalidationCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, insertResult)
);
@@ -309,6 +315,7 @@ export class UpdateMutator<
constructor(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext,
entityConfiguration: EntityConfiguration<TFields>,
entityClass: IEntityClass<
TFields,
@@ -348,6 +355,7 @@ export class UpdateMutator<
super(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entityConfiguration,
entityClass,
privacyPolicy,
@@ -409,6 +417,7 @@ export class UpdateMutator<
this.privacyPolicy.authorizeUpdateAsync(
this.viewerContext,
queryContext,
this.privacyPolicyEvaluationContext,
entityAboutToBeUpdated,
this.metricsAdapter
)
@@ -443,7 +452,11 @@ export class UpdateMutator<
this.updatedFields
);
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
const entityLoader = this.entityLoaderFactory.forLoad(
this.viewerContext,
queryContext,
this.privacyPolicyEvaluationContext
);
queryContext.appendPostCommitInvalidationCallback(
entityLoader.invalidateFieldsAsync.bind(
@@ -506,6 +519,7 @@ export class DeleteMutator<
constructor(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext,
entityConfiguration: EntityConfiguration<TFields>,
entityClass: IEntityClass<
TFields,
@@ -545,6 +559,7 @@ export class DeleteMutator<
super(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entityConfiguration,
entityClass,
privacyPolicy,
@@ -578,7 +593,7 @@ export class DeleteMutator<
private async deleteInTransactionAsync(
processedEntityIdentifiersFromTransitiveDeletions: Set<string>,
skipDatabaseDeletion: boolean,
cascadingDeleteCause: EntityMutationTriggerDeleteCascadeInfo | null
cascadingDeleteCause: EntityCascadingDeletionInfo | null
): Promise<Result<void>> {
return await this.queryContext.runInTransactionIfNotInTransactionAsync((innerQueryContext) =>
this.deleteInternalAsync(
@@ -594,12 +609,13 @@ export class DeleteMutator<
queryContext: EntityTransactionalQueryContext,
processedEntityIdentifiersFromTransitiveDeletions: Set<string>,
skipDatabaseDeletion: boolean,
cascadingDeleteCause: EntityMutationTriggerDeleteCascadeInfo | null
cascadingDeleteCause: EntityCascadingDeletionInfo | null
): Promise<Result<void>> {
const authorizeDeleteResult = await asyncResult(
this.privacyPolicy.authorizeDeleteAsync(
this.viewerContext,
queryContext,
{ cascadingDeleteCause },
this.entity,
this.metricsAdapter
)
@@ -636,7 +652,9 @@ export class DeleteMutator<
);
}
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext, {
cascadingDeleteCause,
});
queryContext.appendPostCommitInvalidationCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, this.entity.getAllDatabaseFields())
);
@@ -684,7 +702,7 @@ export class DeleteMutator<
entity: TEntity,
queryContext: EntityTransactionalQueryContext,
processedEntityIdentifiers: Set<string>,
cascadingDeleteCause: EntityMutationTriggerDeleteCascadeInfo | null
cascadingDeleteCause: EntityCascadingDeletionInfo | null
): Promise<void> {
// prevent infinite reference cycles by keeping track of entities already processed
if (processedEntityIdentifiers.has(entity.getUniqueIdentifier())) {
@@ -732,10 +750,15 @@ export class DeleteMutator<
.getViewerScopedEntityCompanionForClass(entityClass)
.getMutatorFactory();
const newCascadingDeleteCause = {
entity,
cascadingDeleteCause,
};
let inboundReferenceEntities: readonly ReadonlyEntity<any, any, any, any>[];
if (associatedEntityLookupByField) {
inboundReferenceEntities = await loaderFactory
.forLoad(queryContext)
.forLoad(queryContext, { cascadingDeleteCause: newCascadingDeleteCause })
.enforcing()
.loadManyByFieldEqualingAsync(
fieldName,
@@ -743,7 +766,7 @@ export class DeleteMutator<
);
} else {
inboundReferenceEntities = await loaderFactory
.forLoad(queryContext)
.forLoad(queryContext, { cascadingDeleteCause: newCascadingDeleteCause })
.enforcing()
.loadManyByFieldEqualingAsync(fieldName, entity.getID());
}
@@ -754,14 +777,13 @@ export class DeleteMutator<
await Promise.all(
inboundReferenceEntities.map((inboundReferenceEntity) =>
mutatorFactory
.forDelete(inboundReferenceEntity, queryContext)
.forDelete(inboundReferenceEntity, queryContext, {
cascadingDeleteCause: newCascadingDeleteCause,
})
.deleteInTransactionAsync(
processedEntityIdentifiers,
/* skipDatabaseDeletion */ true, // deletion is handled by DB
{
entity,
cascadingDeleteCause,
}
newCascadingDeleteCause
)
)
);
@@ -771,7 +793,9 @@ export class DeleteMutator<
await Promise.all(
inboundReferenceEntities.map((inboundReferenceEntity) =>
mutatorFactory
.forUpdate(inboundReferenceEntity, queryContext)
.forUpdate(inboundReferenceEntity, queryContext, {
cascadingDeleteCause: newCascadingDeleteCause,
})
.setField(fieldName, null)
.enforceUpdateAsync()
)
@@ -782,14 +806,13 @@ export class DeleteMutator<
await Promise.all(
inboundReferenceEntities.map((inboundReferenceEntity) =>
mutatorFactory
.forDelete(inboundReferenceEntity, queryContext)
.forDelete(inboundReferenceEntity, queryContext, {
cascadingDeleteCause: newCascadingDeleteCause,
})
.deleteInTransactionAsync(
processedEntityIdentifiers,
/* skipDatabaseDeletion */ false,
{
entity,
cascadingDeleteCause,
}
newCascadingDeleteCause
)
)
);
+10 -4
View File
@@ -5,7 +5,7 @@ import EntityLoaderFactory from './EntityLoaderFactory';
import EntityMutationTriggerConfiguration from './EntityMutationTriggerConfiguration';
import EntityMutationValidator from './EntityMutationValidator';
import { CreateMutator, UpdateMutator, DeleteMutator } from './EntityMutator';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import ViewerContext from './ViewerContext';
import IEntityMetricsAdapter from './metrics/IEntityMetricsAdapter';
@@ -72,11 +72,13 @@ export default class EntityMutatorFactory<
*/
forCreate(
viewerContext: TViewerContext,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): CreateMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return new CreateMutator(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
this.entityConfiguration,
this.entityClass,
this.privacyPolicy,
@@ -96,11 +98,13 @@ export default class EntityMutatorFactory<
*/
forUpdate(
existingEntity: TEntity,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): UpdateMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return new UpdateMutator(
existingEntity.getViewerContext(),
queryContext,
privacyPolicyEvaluationContext,
this.entityConfiguration,
this.entityClass,
this.privacyPolicy,
@@ -120,11 +124,13 @@ export default class EntityMutatorFactory<
*/
forDelete(
existingEntity: TEntity,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): DeleteMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return new DeleteMutator(
existingEntity.getViewerContext(),
queryContext,
privacyPolicyEvaluationContext,
this.entityConfiguration,
this.entityClass,
this.privacyPolicy,
+31 -1
View File
@@ -1,3 +1,4 @@
import { EntityCascadingDeletionInfo } from './EntityMutationInfo';
import { EntityQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -7,6 +8,17 @@ import IEntityMetricsAdapter, {
} from './metrics/IEntityMetricsAdapter';
import PrivacyPolicyRule, { RuleEvaluationResult } from './rules/PrivacyPolicyRule';
/**
* Information about the reason this privacy policy is being evaluated.
*/
export type EntityPrivacyPolicyEvaluationContext = {
/**
* When this privacy policy is being evaluated as a result of a cascading deletion, this will be populated
* with information on the cascading delete.
*/
cascadingDeleteCause: EntityCascadingDeletionInfo | null;
};
export enum EntityPrivacyPolicyEvaluationMode {
ENFORCE,
DRY_RUN,
@@ -127,6 +139,7 @@ export default abstract class EntityPrivacyPolicy<
async authorizeCreateAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
metricsAdapter: IEntityMetricsAdapter
): Promise<TEntity> {
@@ -134,6 +147,7 @@ export default abstract class EntityPrivacyPolicy<
this.createRules,
viewerContext,
queryContext,
evaluationContext,
entity,
EntityAuthorizationAction.CREATE,
metricsAdapter
@@ -151,6 +165,7 @@ export default abstract class EntityPrivacyPolicy<
async authorizeReadAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
metricsAdapter: IEntityMetricsAdapter
): Promise<TEntity> {
@@ -158,6 +173,7 @@ export default abstract class EntityPrivacyPolicy<
this.readRules,
viewerContext,
queryContext,
evaluationContext,
entity,
EntityAuthorizationAction.READ,
metricsAdapter
@@ -175,6 +191,7 @@ export default abstract class EntityPrivacyPolicy<
async authorizeUpdateAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
metricsAdapter: IEntityMetricsAdapter
): Promise<TEntity> {
@@ -182,6 +199,7 @@ export default abstract class EntityPrivacyPolicy<
this.updateRules,
viewerContext,
queryContext,
evaluationContext,
entity,
EntityAuthorizationAction.UPDATE,
metricsAdapter
@@ -199,6 +217,7 @@ export default abstract class EntityPrivacyPolicy<
async authorizeDeleteAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
metricsAdapter: IEntityMetricsAdapter
): Promise<TEntity> {
@@ -206,6 +225,7 @@ export default abstract class EntityPrivacyPolicy<
this.deleteRules,
viewerContext,
queryContext,
evaluationContext,
entity,
EntityAuthorizationAction.DELETE,
metricsAdapter
@@ -216,6 +236,7 @@ export default abstract class EntityPrivacyPolicy<
ruleset: readonly PrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>[],
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
action: EntityAuthorizationAction,
metricsAdapter: IEntityMetricsAdapter
@@ -228,6 +249,7 @@ export default abstract class EntityPrivacyPolicy<
ruleset,
viewerContext,
queryContext,
evaluationContext,
entity,
action
);
@@ -256,6 +278,7 @@ export default abstract class EntityPrivacyPolicy<
ruleset,
viewerContext,
queryContext,
evaluationContext,
entity,
action
);
@@ -285,6 +308,7 @@ export default abstract class EntityPrivacyPolicy<
ruleset,
viewerContext,
queryContext,
evaluationContext,
entity,
action
);
@@ -315,12 +339,18 @@ export default abstract class EntityPrivacyPolicy<
ruleset: readonly PrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>[],
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity,
action: EntityAuthorizationAction
): Promise<TEntity> {
for (let i = 0; i < ruleset.length; i++) {
const rule = ruleset[i]!;
const ruleEvaluationResult = await rule.evaluateAsync(viewerContext, queryContext, entity);
const ruleEvaluationResult = await rule.evaluateAsync(
viewerContext,
queryContext,
evaluationContext,
entity
);
switch (ruleEvaluationResult) {
case RuleEvaluationResult.DENY:
throw new EntityNotAuthorizedError<
@@ -0,0 +1,98 @@
import invariant from 'invariant';
import { ISecondaryEntityCache } from './EntitySecondaryCacheLoader';
import IEntityGenericCacher from './IEntityGenericCacher';
import { CacheStatus } from './internal/ReadThroughEntityCache';
import { filterMap, zipToMap } from './utils/collections/maps';
/**
* A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular
* single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes
* a call to {@link EntityLoader.loadManyByFieldEqualityConjunctionAsync} is guaranteed to return at most one entity.
*/
export default abstract class GenericSecondaryEntityCache<TFields, TLoadParams>
implements ISecondaryEntityCache<TFields, TLoadParams>
{
constructor(
protected readonly cacher: IEntityGenericCacher<TFields>,
protected readonly constructCacheKey: (params: Readonly<TLoadParams>) => string
) {}
public async loadManyThroughAsync(
loadParamsArray: readonly Readonly<TLoadParams>[],
fetcher: (
fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]
) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>
): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>> {
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
const cacheKeyToLoadParamsMap = zipToMap(cacheKeys, loadParamsArray);
const cacheLoadResults = await this.cacher.loadManyAsync(cacheKeys);
invariant(
cacheLoadResults.size === loadParamsArray.length,
`${this.constructor.name} loadMany should return a result for each key`
);
const cacheKeysToFetch = Array.from(
filterMap(
cacheLoadResults,
(cacheLoadResult) => cacheLoadResult.status === CacheStatus.MISS
).keys()
);
// put cache hits in result map
const results: Map<Readonly<TLoadParams>, Readonly<TFields> | null> = new Map();
cacheLoadResults.forEach((cacheLoadResult, cacheKey) => {
if (cacheLoadResult.status === CacheStatus.HIT) {
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
invariant(loadParams !== undefined, 'load params should be in cache key map');
results.set(loadParams, cacheLoadResult.item);
}
});
// fetch any misses from DB, add DB objects to results, cache DB results, inform cache of any missing DB results
if (cacheKeysToFetch.length > 0) {
const loadParamsToFetch = cacheKeysToFetch.map((cacheKey) => {
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
invariant(loadParams !== undefined, 'load params should be in cache key map');
return loadParams;
});
const fetchResults = await fetcher(loadParamsToFetch);
const fetchMisses = loadParamsToFetch.filter((loadParams) => {
// all values of fetchResults should be field objects or undefined
return !fetchResults.get(loadParams);
});
for (const fetchMiss of fetchMisses) {
results.set(fetchMiss, null);
}
const objectsToCache: Map<string, Readonly<TFields>> = new Map();
for (const [loadParams, object] of fetchResults.entries()) {
if (object) {
objectsToCache.set(this.constructCacheKey(loadParams), object);
results.set(loadParams, object);
}
}
await Promise.all([
this.cacher.cacheManyAsync(objectsToCache),
this.cacher.cacheDBMissesAsync(fetchMisses.map(this.constructCacheKey)),
]);
}
return results;
}
/**
* Invalidate the cache for objects cached by constructCacheKey(loadParams).
*
* @param loadParamsArray - load params to invalidate
*/
public invalidateManyAsync(loadParamsArray: readonly Readonly<TLoadParams>[]): Promise<void> {
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
return this.cacher.invalidateManyAsync(cacheKeys);
}
}
@@ -0,0 +1,15 @@
import { CacheLoadResult } from './internal/ReadThroughEntityCache';
/**
* A cacher stores and loads key-value pairs. It also supports negative caching - it stores the absence
* of keys that don't exist in the backing datastore.
*/
export default interface IEntityGenericCacher<TFields> {
loadManyAsync(keys: readonly string[]): Promise<ReadonlyMap<string, CacheLoadResult<TFields>>>;
cacheManyAsync(objectMap: ReadonlyMap<string, Readonly<TFields>>): Promise<void>;
cacheDBMissesAsync(keys: readonly string[]): Promise<void>;
invalidateManyAsync(keys: readonly string[]): Promise<void>;
}
+1 -1
View File
@@ -156,6 +156,6 @@ export default abstract class ReadonlyEntity<
return viewerContext
.getViewerScopedEntityCompanionForClass(this)
.getLoaderFactory()
.forLoad(queryContext);
.forLoad(queryContext, { cascadingDeleteCause: null });
}
}
@@ -1,6 +1,6 @@
import EntityLoader from './EntityLoader';
import EntityLoaderFactory from './EntityLoaderFactory';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -35,8 +35,13 @@ export default class ViewerScopedEntityLoaderFactory<
) {}
forLoad(
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): EntityLoader<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
return this.entityLoaderFactory.forLoad(
this.viewerContext,
queryContext,
privacyPolicyEvaluationContext
);
}
}
@@ -1,6 +1,6 @@
import { CreateMutator, UpdateMutator, DeleteMutator } from './EntityMutator';
import EntityMutatorFactory from './EntityMutatorFactory';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
@@ -35,22 +35,37 @@ export default class ViewerScopedEntityMutatorFactory<
) {}
forCreate(
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): CreateMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return this.entityMutatorFactory.forCreate(this.viewerContext, queryContext);
return this.entityMutatorFactory.forCreate(
this.viewerContext,
queryContext,
privacyPolicyEvaluationContext
);
}
forUpdate(
existingEntity: TEntity,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): UpdateMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return this.entityMutatorFactory.forUpdate(existingEntity, queryContext);
return this.entityMutatorFactory.forUpdate(
existingEntity,
queryContext,
privacyPolicyEvaluationContext
);
}
forDelete(
existingEntity: TEntity,
queryContext: EntityQueryContext
queryContext: EntityQueryContext,
privacyPolicyEvaluationContext: EntityPrivacyPolicyEvaluationContext
): DeleteMutator<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
return this.entityMutatorFactory.forDelete(existingEntity, queryContext);
return this.entityMutatorFactory.forDelete(
existingEntity,
queryContext,
privacyPolicyEvaluationContext
);
}
}
@@ -0,0 +1,280 @@
import invariant from 'invariant';
import ComposedEntityCacheAdapter from '../ComposedEntityCacheAdapter';
import EntityCacheAdapter from '../EntityCacheAdapter';
import EntityConfiguration from '../EntityConfiguration';
import { UUIDField } from '../EntityFields';
import { CacheLoadResult, CacheStatus } from '../internal/ReadThroughEntityCache';
type BlahFields = {
id: string;
};
const entityConfiguration = new EntityConfiguration<BlahFields>({
idField: 'id',
tableName: 'blah',
schema: {
id: new UUIDField({ columnName: 'id', cache: true }),
},
databaseAdapterFlavor: 'postgres',
cacheAdapterFlavor: 'local-memory-and-redis',
});
export const DOES_NOT_EXIST_LOCAL_MEMORY_CACHE = Symbol('doesNotExist');
type LocalMemoryCacheValue<TFields> = Readonly<TFields> | typeof DOES_NOT_EXIST_LOCAL_MEMORY_CACHE;
class TestLocalCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
constructor(
entityConfiguration: EntityConfiguration<TFields>,
private readonly cache: Map<string, LocalMemoryCacheValue<TFields>>
) {
super(entityConfiguration);
}
public async loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
const localMemoryCacheKeyToFieldValueMapping = new Map(
fieldValues.map((fieldValue) => [this.makeCacheKey(fieldName, fieldValue), fieldValue])
);
const cacheResults = new Map<NonNullable<TFields[N]>, CacheLoadResult<TFields>>();
for (const [cacheKey, fieldValue] of localMemoryCacheKeyToFieldValueMapping) {
const cacheResult = this.cache.get(cacheKey);
if (cacheResult === DOES_NOT_EXIST_LOCAL_MEMORY_CACHE) {
cacheResults.set(fieldValue, {
status: CacheStatus.NEGATIVE,
});
} else if (cacheResult) {
cacheResults.set(fieldValue, {
status: CacheStatus.HIT,
item: cacheResult,
});
} else {
cacheResults.set(fieldValue, {
status: CacheStatus.MISS,
});
}
}
return cacheResults;
}
public async cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {
for (const [fieldValue, item] of objectMap) {
const cacheKey = this.makeCacheKey(fieldName, fieldValue);
this.cache.set(cacheKey, item);
}
}
public async cacheDBMissesAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
for (const fieldValue of fieldValues) {
const cacheKey = this.makeCacheKey(fieldName, fieldValue);
this.cache.set(cacheKey, DOES_NOT_EXIST_LOCAL_MEMORY_CACHE);
}
}
public async invalidateManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
for (const fieldValue of fieldValues) {
const cacheKey = this.makeCacheKey(fieldName, fieldValue);
this.cache.delete(cacheKey);
}
}
private makeCacheKey<N extends keyof TFields>(
fieldName: N,
fieldValue: NonNullable<TFields[N]>
): string {
const columnName = this.entityConfiguration.entityToDBFieldsKeyMapping.get(fieldName);
invariant(columnName, `database field mapping missing for ${fieldName}`);
const parts = [
this.entityConfiguration.tableName,
`${this.entityConfiguration.cacheKeyVersion}`,
columnName,
String(fieldValue),
];
const delimiter = ':';
const escapedParts = parts.map((part) =>
part.replace('\\', '\\\\').replace(delimiter, `\\${delimiter}`)
);
return escapedParts.join(delimiter);
}
}
function makeTestCacheAdapters(): {
primaryCache: Map<string, LocalMemoryCacheValue<BlahFields>>;
primaryCacheAdapter: TestLocalCacheAdapter<BlahFields>;
fallbackCache: Map<string, LocalMemoryCacheValue<BlahFields>>;
fallbackCacheAdapter: TestLocalCacheAdapter<BlahFields>;
cacheAdapter: ComposedEntityCacheAdapter<BlahFields>;
} {
const primaryCache = new Map();
const primaryCacheAdapter = new TestLocalCacheAdapter(entityConfiguration, primaryCache);
const fallbackCache = new Map();
const fallbackCacheAdapter = new TestLocalCacheAdapter(entityConfiguration, fallbackCache);
const cacheAdapter = new ComposedEntityCacheAdapter(entityConfiguration, [
primaryCacheAdapter,
fallbackCacheAdapter,
]);
return {
primaryCache,
primaryCacheAdapter,
fallbackCache,
fallbackCacheAdapter,
cacheAdapter,
};
}
describe(ComposedEntityCacheAdapter, () => {
describe('loadManyAsync', () => {
it('returns primary results when populated', async () => {
const { primaryCacheAdapter, cacheAdapter } = makeTestCacheAdapters();
const cacheHits = new Map<string, Readonly<BlahFields>>([['test-id-1', { id: 'test-id-1' }]]);
await primaryCacheAdapter.cacheManyAsync('id', cacheHits);
await primaryCacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
const results = await cacheAdapter.loadManyAsync('id', [
'test-id-1',
'test-id-2',
'test-id-3',
]);
expect(results.get('test-id-1')).toMatchObject({
status: CacheStatus.HIT,
item: { id: 'test-id-1' },
});
expect(results.get('test-id-2')).toMatchObject({ status: CacheStatus.NEGATIVE });
expect(results.get('test-id-3')).toMatchObject({ status: CacheStatus.MISS });
expect(results.size).toBe(3);
});
it('returns fallback adapter results primary is empty', async () => {
const { primaryCacheAdapter, cacheAdapter } = makeTestCacheAdapters();
const cacheHits = new Map<string, Readonly<BlahFields>>([['test-id-1', { id: 'test-id-1' }]]);
await primaryCacheAdapter.cacheManyAsync('id', cacheHits);
await primaryCacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
const results = await cacheAdapter.loadManyAsync('id', [
'test-id-1',
'test-id-2',
'test-id-3',
]);
expect(results.get('test-id-1')).toMatchObject({
status: CacheStatus.HIT,
item: { id: 'test-id-1' },
});
expect(results.get('test-id-2')).toMatchObject({ status: CacheStatus.NEGATIVE });
expect(results.get('test-id-3')).toMatchObject({ status: CacheStatus.MISS });
expect(results.size).toBe(3);
});
it('returns empty map when passed empty array of fieldValues', async () => {
const { cacheAdapter } = makeTestCacheAdapters();
const results = await cacheAdapter.loadManyAsync('id', []);
expect(results).toEqual(new Map());
});
it('handles 0 cache adapter compose case', async () => {
const cacheAdapter = new ComposedEntityCacheAdapter(entityConfiguration, []);
const results = await cacheAdapter.loadManyAsync('id', []);
expect(results).toEqual(new Map());
});
});
describe('cacheManyAsync', () => {
it('correctly caches all objects', async () => {
const {
primaryCache,
primaryCacheAdapter,
fallbackCache,
fallbackCacheAdapter,
cacheAdapter,
} = makeTestCacheAdapters();
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
const primaryLocalMemoryCacheKey = primaryCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(primaryCache.get(primaryLocalMemoryCacheKey)).toMatchObject({
id: 'test-id-1',
});
const fallbackLocalMemoryCacheKey = fallbackCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(fallbackCache.get(fallbackLocalMemoryCacheKey)).toMatchObject({
id: 'test-id-1',
});
});
});
describe('cacheDBMissesAsync', () => {
it('correctly caches misses', async () => {
const {
primaryCache,
primaryCacheAdapter,
fallbackCache,
fallbackCacheAdapter,
cacheAdapter,
} = makeTestCacheAdapters();
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-1']);
const primaryLocalMemoryCacheKey = primaryCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(primaryCache.get(primaryLocalMemoryCacheKey)).toBe(DOES_NOT_EXIST_LOCAL_MEMORY_CACHE);
const fallbackLocalMemoryCacheKey = fallbackCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(fallbackCache.get(fallbackLocalMemoryCacheKey)).toBe(
DOES_NOT_EXIST_LOCAL_MEMORY_CACHE
);
});
});
describe('invalidateManyAsync', () => {
it('invalidates correctly', async () => {
const {
primaryCache,
primaryCacheAdapter,
fallbackCache,
fallbackCacheAdapter,
cacheAdapter,
} = makeTestCacheAdapters();
const cacheHits = new Map<string, Readonly<BlahFields>>([['test-id-1', { id: 'test-id-1' }]]);
await primaryCacheAdapter.cacheManyAsync('id', cacheHits);
await primaryCacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
await fallbackCacheAdapter.cacheManyAsync('id', cacheHits);
await fallbackCacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
await cacheAdapter.invalidateManyAsync('id', ['test-id-1', 'test-id-2']);
const primaryLocalMemoryCacheKey1 = primaryCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(primaryCache.get(primaryLocalMemoryCacheKey1)).toBe(undefined);
const primaryLocalMemoryCacheKey2 = primaryCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(primaryCache.get(primaryLocalMemoryCacheKey2)).toBe(undefined);
const fallbackLocalMemoryCacheKey1 = fallbackCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(fallbackCache.get(fallbackLocalMemoryCacheKey1)).toBe(undefined);
const fallbackLocalMemoryCacheKey2 = fallbackCacheAdapter['makeCacheKey']('id', 'test-id-1');
expect(fallbackCache.get(fallbackLocalMemoryCacheKey2)).toBe(undefined);
});
it('returns when passed empty array of fieldValues', async () => {
const { cacheAdapter } = makeTestCacheAdapters();
await cacheAdapter.invalidateManyAsync('id', []);
});
});
});
@@ -0,0 +1,101 @@
import invariant from 'invariant';
import nullthrows from 'nullthrows';
import ComposedSecondaryEntityCache from '../ComposedSecondaryEntityCache';
import { ISecondaryEntityCache } from '../EntitySecondaryCacheLoader';
type TestFields = { id: string };
type TestLoadParams = { lp: string };
class TestEntitySecondaryCache implements ISecondaryEntityCache<TestFields, TestLoadParams> {
constructor(
private readonly prefilledResults: Map<Readonly<TestLoadParams>, Readonly<TestFields>>
) {}
async loadManyThroughAsync(
loadParamsArray: readonly Readonly<TestLoadParams>[],
fetcher: (
fetcherLoadParamsArray: readonly Readonly<TestLoadParams>[]
) => Promise<ReadonlyMap<Readonly<TestLoadParams>, Readonly<TestFields> | null>>
): Promise<ReadonlyMap<Readonly<TestLoadParams>, Readonly<TestFields> | null>> {
// this does an unusual method of calling fetcher, but there's no constraint that says fetcher can only be called once
// so this tests that
const retMap = new Map<Readonly<TestLoadParams>, Readonly<TestFields> | null>();
for (const loadParams of loadParamsArray) {
if (this.prefilledResults.has(loadParams)) {
retMap.set(loadParams, nullthrows(this.prefilledResults.get(loadParams)));
} else {
const fetcherResult = await fetcher([loadParams]);
const toSet = fetcherResult.get(loadParams);
invariant(toSet !== undefined, 'should be set');
retMap.set(loadParams, toSet);
}
}
return retMap;
}
async invalidateManyAsync(loadParamsArray: readonly Readonly<TestLoadParams>[]): Promise<void> {
for (const loadParams of loadParamsArray) {
this.prefilledResults.delete(loadParams);
}
}
}
describe(ComposedSecondaryEntityCache, () => {
it('composes correctly', async () => {
// TODO(wschurman): investigate whether we can use immutable or something to do better object equality for the map keys
const lp1 = { lp: '1' };
const lp2 = { lp: '2' };
const lp3 = { lp: '3' };
const primarySecondaryEntityCache = new TestEntitySecondaryCache(
new Map([[lp1, { id: 'primary-1' }]])
);
const fallbackSecondaryEntityCache = new TestEntitySecondaryCache(
new Map([[lp2, { id: 'fallback-2' }]])
);
const composedSecondaryEntityCache = new ComposedSecondaryEntityCache([
primarySecondaryEntityCache,
fallbackSecondaryEntityCache,
]);
const results = await composedSecondaryEntityCache.loadManyThroughAsync(
[lp1, lp2, lp3],
async (fetcherLoadParamsArray) =>
new Map(fetcherLoadParamsArray.map((flp) => [flp, { id: `db-fetched-${flp.lp}` }]))
);
expect(results.get(lp1)).toEqual({ id: 'primary-1' });
expect(results.get(lp2)).toEqual({ id: 'fallback-2' });
expect(results.get(lp3)).toEqual({ id: 'db-fetched-3' });
await composedSecondaryEntityCache.invalidateManyAsync([lp1, lp2, lp3]);
const resultsAfterInvalidate = await composedSecondaryEntityCache.loadManyThroughAsync(
[lp1, lp2, lp3],
async (fetcherLoadParamsArray) =>
new Map(fetcherLoadParamsArray.map((flp) => [flp, { id: `db-fetched-${flp.lp}` }]))
);
expect(resultsAfterInvalidate.get(lp1)).toEqual({ id: 'db-fetched-1' });
expect(resultsAfterInvalidate.get(lp2)).toEqual({ id: 'db-fetched-2' });
expect(resultsAfterInvalidate.get(lp3)).toEqual({ id: 'db-fetched-3' });
});
it('handles n=0 compose case', async () => {
const lp1 = { lp: '1' };
const composedSecondaryEntityCache = new ComposedSecondaryEntityCache<
TestLoadParams,
TestFields
>([]);
const results = await composedSecondaryEntityCache.loadManyThroughAsync(
[lp1],
async (fetcherLoadParamsArray) =>
new Map(fetcherLoadParamsArray.map((flp) => [flp, { id: `db-fetched-${flp.lp}` }]))
);
expect(results.get(lp1)).toEqual({ id: 'db-fetched-1' });
});
});
@@ -5,7 +5,7 @@ import Entity from '../Entity';
import EntityCompanionProvider, { EntityCompanionDefinition } from '../EntityCompanionProvider';
import EntityConfiguration from '../EntityConfiguration';
import { UUIDField } from '../EntityFields';
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
import { enforceResultsAsync } from '../entityUtils';
@@ -51,6 +51,7 @@ class DenyIfNotOwnerPrivacyPolicyRule extends PrivacyPolicyRule<
async evaluateAsync(
viewerContext: TestUserViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: BlahEntity
): Promise<RuleEvaluationResult> {
if (viewerContext.getUserID() === entity.getField('ownerID')) {
+286 -45
View File
@@ -5,10 +5,13 @@ import { EntityEdgeDeletionBehavior } from '../EntityFieldDefinition';
import { UUIDField } from '../EntityFields';
import { EntityTriggerMutationInfo, EntityMutationType } from '../EntityMutationInfo';
import { EntityMutationTrigger } from '../EntityMutationTriggerConfiguration';
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
import { EntityTransactionalQueryContext } from '../EntityQueryContext';
import EntityPrivacyPolicy, {
EntityPrivacyPolicyEvaluationContext,
EntityAuthorizationAction,
} from '../EntityPrivacyPolicy';
import { EntityTransactionalQueryContext, EntityQueryContext } from '../EntityQueryContext';
import { CacheStatus } from '../internal/ReadThroughEntityCache';
import AlwaysAllowPrivacyPolicyRule from '../rules/AlwaysAllowPrivacyPolicyRule';
import PrivacyPolicyRule, { RuleEvaluationResult } from '../rules/PrivacyPolicyRule';
import TestViewerContext from '../testfixtures/TestViewerContext';
import { InMemoryFullCacheStubCacheAdapter } from '../utils/testing/StubCacheAdapter';
import { createUnitTestEntityCompanionProvider } from '../utils/testing/createUnitTestEntityCompanionProvider';
@@ -27,35 +30,83 @@ interface GrandChildFields {
parent_id: string;
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<
any,
string,
TestViewerContext,
any,
any
> {
protected override readonly readRules = [
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
];
protected override readonly createRules = [
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
];
protected override readonly updateRules = [
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
];
protected override readonly deleteRules = [
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
];
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) => {
const triggerExecutionCounts = {
parent: 0,
child: 0,
grandchild: 0,
ParentEntity: 0,
ChildEntity: 0,
GrandChildEntity: 0,
};
const privacyPolicyEvaluationRecords = {
shouldRecord: false,
ParentEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
[EntityAuthorizationAction.DELETE]: [],
},
ChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
[EntityAuthorizationAction.DELETE]: [],
},
GrandChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
[EntityAuthorizationAction.DELETE]: [],
},
};
class AlwaysAllowPrivacyPolicyRuleThatRecords extends PrivacyPolicyRule<
any,
string,
TestViewerContext,
any,
any
> {
constructor(private readonly action: EntityAuthorizationAction) {
super();
}
async evaluateAsync(
_viewerContext: TestViewerContext,
_queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: any
): Promise<RuleEvaluationResult> {
if (privacyPolicyEvaluationRecords.shouldRecord) {
(privacyPolicyEvaluationRecords as any)[entity.constructor.name][this.action].push(
evaluationContext
);
}
return RuleEvaluationResult.ALLOW;
}
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<
any,
string,
TestViewerContext,
any,
any
> {
protected override readonly readRules = [
new AlwaysAllowPrivacyPolicyRuleThatRecords(EntityAuthorizationAction.READ),
];
protected override readonly createRules = [
new AlwaysAllowPrivacyPolicyRuleThatRecords(EntityAuthorizationAction.CREATE),
];
protected override readonly updateRules = [
new AlwaysAllowPrivacyPolicyRuleThatRecords(EntityAuthorizationAction.UPDATE),
];
protected override readonly deleteRules = [
new AlwaysAllowPrivacyPolicyRuleThatRecords(EntityAuthorizationAction.DELETE),
];
}
class ParentCheckInfoTrigger extends EntityMutationTrigger<
ParentFields,
string,
@@ -76,7 +127,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
throw new Error('Parent entity should not have casade delete cause');
}
triggerExecutionCounts.parent++;
triggerExecutionCounts.ParentEntity++;
}
}
@@ -111,7 +162,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
throw new Error('Child entity should not have two-level casade delete cause');
}
triggerExecutionCounts.child++;
triggerExecutionCounts.ChildEntity++;
}
}
@@ -165,7 +216,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
throw new Error('GrandChild entity should not have three-level casade delete cause');
}
triggerExecutionCounts.grandchild++;
triggerExecutionCounts.GrandChildEntity++;
}
}
@@ -297,14 +348,20 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
ChildEntity,
GrandChildEntity,
triggerExecutionCounts,
privacyPolicyEvaluationRecords,
};
};
describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
describe('EntityEdgeDeletionBehavior.CASCADE_DELETE', () => {
it('deletes', async () => {
const { ParentEntity, ChildEntity, GrandChildEntity, triggerExecutionCounts } =
makeEntityClasses(EntityEdgeDeletionBehavior.CASCADE_DELETE);
const {
ParentEntity,
ChildEntity,
GrandChildEntity,
triggerExecutionCounts,
privacyPolicyEvaluationRecords,
} = makeEntityClasses(EntityEdgeDeletionBehavior.CASCADE_DELETE);
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
@@ -326,7 +383,9 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
GrandChildEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(grandchild.getID())
).resolves.not.toBeNull();
privacyPolicyEvaluationRecords.shouldRecord = true;
await ParentEntity.enforceDeleteAsync(parent);
privacyPolicyEvaluationRecords.shouldRecord = false;
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
@@ -340,17 +399,82 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
// two calls for each trigger, one beforeDelete, one afterDelete
expect(triggerExecutionCounts).toMatchObject({
parent: 2,
child: 2,
grandchild: 2,
ParentEntity: 2,
ChildEntity: 2,
GrandChildEntity: 2,
});
expect(privacyPolicyEvaluationRecords).toMatchObject({
ParentEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for parent (since it's being deleted)
[EntityAuthorizationAction.DELETE]: [{ cascadingDeleteCause: null }],
},
ChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
// one READ auth action for child in order to delete via cascade
[EntityAuthorizationAction.READ]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for child (since it's being deleted via cascade)
[EntityAuthorizationAction.DELETE]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
},
GrandChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
// one READ auth action for grandchild in order to delete via cascade
[EntityAuthorizationAction.READ]: [
{
cascadingDeleteCause: {
entity: expect.any(ChildEntity),
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
},
],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for grandchild (since it's being deleted via cascade)
[EntityAuthorizationAction.DELETE]: [
{
cascadingDeleteCause: {
entity: expect.any(ChildEntity),
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
},
],
},
});
});
});
describe('EntityEdgeDeletionBehavior.SET_NULL', () => {
it('sets null', async () => {
const { ParentEntity, ChildEntity, GrandChildEntity, triggerExecutionCounts } =
makeEntityClasses(EntityEdgeDeletionBehavior.SET_NULL);
const {
ParentEntity,
ChildEntity,
GrandChildEntity,
triggerExecutionCounts,
privacyPolicyEvaluationRecords,
} = makeEntityClasses(EntityEdgeDeletionBehavior.SET_NULL);
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
@@ -373,7 +497,9 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
GrandChildEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(grandchild.getID())
).resolves.not.toBeNull();
privacyPolicyEvaluationRecords.shouldRecord = true;
await ParentEntity.enforceDeleteAsync(parent);
privacyPolicyEvaluationRecords.shouldRecord = false;
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
@@ -392,17 +518,70 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
// two calls for only parent trigger, one beforeDelete, one afterDelete
// when using set null the descendants aren't deleted
expect(triggerExecutionCounts).toMatchObject({
parent: 2,
child: 0,
grandchild: 0,
ParentEntity: 2,
ChildEntity: 0,
GrandChildEntity: 0,
});
expect(privacyPolicyEvaluationRecords).toMatchObject({
ParentEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for parent (since it's being deleted)
[EntityAuthorizationAction.DELETE]: [{ cascadingDeleteCause: null }],
},
ChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
// two READs auth action for Child during parent deletion:
// 1. Read to initiate the SET_NULL (to update the entity)
// 1. Read automatically post-update
// no other entities are read since it is not cascaded past first entity
[EntityAuthorizationAction.READ]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
// one UPDATE to set null
[EntityAuthorizationAction.UPDATE]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
[EntityAuthorizationAction.DELETE]: [],
},
GrandChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
[EntityAuthorizationAction.DELETE]: [],
},
});
});
});
describe('EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE', () => {
it('invalidates the cache', async () => {
const { ParentEntity, ChildEntity, GrandChildEntity, triggerExecutionCounts } =
makeEntityClasses(EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE);
const {
ParentEntity,
ChildEntity,
GrandChildEntity,
triggerExecutionCounts,
privacyPolicyEvaluationRecords,
} = makeEntityClasses(EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE);
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
@@ -447,7 +626,9 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
]);
expect(grandChildCachedBefore.get(child.getID())?.status).toEqual(CacheStatus.HIT);
privacyPolicyEvaluationRecords.shouldRecord = true;
await ParentEntity.enforceDeleteAsync(parent);
privacyPolicyEvaluationRecords.shouldRecord = false;
const childCachedAfter = await childCacheAdapter.loadManyAsync('parent_id', [parent.getID()]);
expect(childCachedAfter.get(parent.getID())?.status).toEqual(CacheStatus.MISS);
@@ -469,9 +650,69 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
// two calls for each trigger, one beforeDelete, one afterDelete
expect(triggerExecutionCounts).toMatchObject({
parent: 2,
child: 2,
grandchild: 2,
ParentEntity: 2,
ChildEntity: 2,
GrandChildEntity: 2,
});
expect(privacyPolicyEvaluationRecords).toMatchObject({
ParentEntity: {
[EntityAuthorizationAction.CREATE]: [],
[EntityAuthorizationAction.READ]: [],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for parent (since it's being deleted)
[EntityAuthorizationAction.DELETE]: [{ cascadingDeleteCause: null }],
},
ChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
// one READ auth action for child in order to delete via cascade
[EntityAuthorizationAction.READ]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for child (since it's being deleted via cascade)
[EntityAuthorizationAction.DELETE]: [
{
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
],
},
GrandChildEntity: {
[EntityAuthorizationAction.CREATE]: [],
// one READ auth action for grandchild in order to delete via cascade
[EntityAuthorizationAction.READ]: [
{
cascadingDeleteCause: {
entity: expect.any(ChildEntity),
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
},
],
[EntityAuthorizationAction.UPDATE]: [],
// one DELETE auth action for grandchild (since it's being deleted via cascade)
[EntityAuthorizationAction.DELETE]: [
{
cascadingDeleteCause: {
entity: expect.any(ChildEntity),
cascadingDeleteCause: {
entity: expect.any(ParentEntity),
cascadingDeleteCause: null,
},
},
},
],
},
});
});
});
@@ -5,7 +5,7 @@ import { EntityCompanionDefinition } from '../EntityCompanionProvider';
import EntityConfiguration from '../EntityConfiguration';
import { StringField } from '../EntityFields';
import EntityLoader from '../EntityLoader';
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
import EntityPrivacyPolicy, { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import ViewerContext from '../ViewerContext';
import EntityDataManager from '../internal/EntityDataManager';
import ReadThroughEntityCache from '../internal/ReadThroughEntityCache';
@@ -118,6 +118,7 @@ export const testEntityCompanion = new EntityCompanionDefinition({
describe(EntityLoader, () => {
it('handles thrown errors and literals from constructor', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -154,6 +155,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -3,6 +3,7 @@ import { mock, instance, verify, spy, deepEqual, anyOfClass, anything, when } fr
import { v4 as uuidv4 } from 'uuid';
import EntityLoader from '../EntityLoader';
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import ViewerContext from '../ViewerContext';
import { enforceResultsAsync } from '../entityUtils';
import EntityDataManager from '../internal/EntityDataManager';
@@ -21,6 +22,7 @@ describe(EntityLoader, () => {
it('loads entities', async () => {
const dateToInsert = new Date();
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -69,6 +71,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -113,6 +116,7 @@ describe(EntityLoader, () => {
const privacyPolicy = new TestEntityPrivacyPolicy();
const spiedPrivacyPolicy = spy(privacyPolicy);
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -169,6 +173,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -192,6 +197,7 @@ describe(EntityLoader, () => {
spiedPrivacyPolicy.authorizeReadAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
anyOfClass(TestEntity),
anything()
)
@@ -209,6 +215,7 @@ describe(EntityLoader, () => {
const spiedPrivacyPolicy = spy(privacyPolicy);
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -247,6 +254,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -255,12 +263,19 @@ describe(EntityLoader, () => {
);
const entity = await enforceAsyncResult(entityLoader.loadByIDAsync(id1));
verify(
spiedPrivacyPolicy.authorizeReadAsync(viewerContext, queryContext, entity, anything())
spiedPrivacyPolicy.authorizeReadAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
anything()
)
).once();
});
it('invalidates upon invalidate one', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(TestEntityPrivacyPolicy));
@@ -271,6 +286,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -286,6 +302,7 @@ describe(EntityLoader, () => {
it('invalidates upon invalidate by field', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(TestEntityPrivacyPolicy));
@@ -296,6 +313,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -310,6 +328,7 @@ describe(EntityLoader, () => {
it('invalidates upon invalidate by entity', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(TestEntityPrivacyPolicy));
@@ -324,6 +343,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -338,6 +358,7 @@ describe(EntityLoader, () => {
it('returns error result when not allowed', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicyMock = mock(TestEntityPrivacyPolicy);
@@ -354,6 +375,7 @@ describe(EntityLoader, () => {
privacyPolicyMock.authorizeReadAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
anyOfClass(TestEntity),
anything()
)
@@ -365,6 +387,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -380,6 +403,7 @@ describe(EntityLoader, () => {
it('throws upon database adapter error', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(TestEntityPrivacyPolicy));
@@ -396,6 +420,7 @@ describe(EntityLoader, () => {
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
privacyPolicy,
@@ -25,6 +25,7 @@ import EntityMutationTriggerConfiguration, {
} from '../EntityMutationTriggerConfiguration';
import EntityMutationValidator from '../EntityMutationValidator';
import EntityMutatorFactory from '../EntityMutatorFactory';
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityTransactionalQueryContext, EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
import { enforceResultsAsync } from '../entityUtils';
@@ -340,6 +341,7 @@ describe(EntityMutatorFactory, () => {
describe('forCreate', () => {
it('creates entities', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -363,7 +365,7 @@ describe(EntityMutatorFactory, () => {
},
]);
const newEntity = await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.enforceCreateAsync();
expect(newEntity).toBeTruthy();
@@ -371,6 +373,7 @@ describe(EntityMutatorFactory, () => {
it('checks privacy', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -397,7 +400,7 @@ describe(EntityMutatorFactory, () => {
const spiedPrivacyPolicy = spy(privacyPolicy);
await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.enforceCreateAsync();
@@ -405,6 +408,7 @@ describe(EntityMutatorFactory, () => {
spiedPrivacyPolicy.authorizeCreateAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
privacyPolicyEvaluationContext,
anyOfClass(TestEntity),
anything()
)
@@ -413,6 +417,7 @@ describe(EntityMutatorFactory, () => {
it('executes triggers', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -439,7 +444,7 @@ describe(EntityMutatorFactory, () => {
const triggerSpies = setUpMutationTriggerSpies(mutationTriggers);
await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.enforceCreateAsync();
@@ -460,6 +465,7 @@ describe(EntityMutatorFactory, () => {
it('executes validators', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -486,7 +492,7 @@ describe(EntityMutatorFactory, () => {
const validatorSpies = setUpMutationValidatorSpies(mutationValidators);
await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.enforceCreateAsync();
@@ -497,6 +503,7 @@ describe(EntityMutatorFactory, () => {
describe('forUpdate', () => {
it('updates entities', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -521,11 +528,13 @@ describe(EntityMutatorFactory, () => {
]);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id2)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id2)
);
const updatedEntity = await entityMutatorFactory
.forUpdate(existingEntity, queryContext)
.forUpdate(existingEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh2')
.enforceUpdateAsync();
@@ -534,13 +543,16 @@ describe(EntityMutatorFactory, () => {
expect(updatedEntity.getField('stringField')).toEqual('huh2');
const reloadedEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id2)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id2)
);
expect(reloadedEntity.getAllFields()).toMatchObject(updatedEntity.getAllFields());
});
it('checks privacy', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -568,11 +580,13 @@ describe(EntityMutatorFactory, () => {
const spiedPrivacyPolicy = spy(privacyPolicy);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id2)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id2)
);
await entityMutatorFactory
.forUpdate(existingEntity, queryContext)
.forUpdate(existingEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh2')
.enforceUpdateAsync();
@@ -580,6 +594,7 @@ describe(EntityMutatorFactory, () => {
spiedPrivacyPolicy.authorizeUpdateAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
privacyPolicyEvaluationContext,
anyOfClass(TestEntity),
anything()
)
@@ -588,6 +603,7 @@ describe(EntityMutatorFactory, () => {
it('executes triggers', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -615,11 +631,13 @@ describe(EntityMutatorFactory, () => {
const triggerSpies = setUpMutationTriggerSpies(mutationTriggers);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id2)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id2)
);
await entityMutatorFactory
.forUpdate(existingEntity, queryContext)
.forUpdate(existingEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh2')
.enforceUpdateAsync();
@@ -639,6 +657,7 @@ describe(EntityMutatorFactory, () => {
});
it('executes validators', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -666,11 +685,13 @@ describe(EntityMutatorFactory, () => {
const validatorSpies = setUpMutationValidatorSpies(mutationValidators);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id2)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id2)
);
await entityMutatorFactory
.forUpdate(existingEntity, queryContext)
.forUpdate(existingEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh2')
.enforceUpdateAsync();
@@ -684,6 +705,7 @@ describe(EntityMutatorFactory, () => {
describe('forDelete', () => {
it('deletes entities', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -699,21 +721,28 @@ describe(EntityMutatorFactory, () => {
]);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id1)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id1)
);
expect(existingEntity).toBeTruthy();
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory
.forDelete(existingEntity, queryContext, privacyPolicyEvaluationContext)
.enforceDeleteAsync();
await expect(
enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id1)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id1)
)
).rejects.toBeInstanceOf(Error);
});
it('checks privacy', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -732,15 +761,20 @@ describe(EntityMutatorFactory, () => {
const spiedPrivacyPolicy = spy(privacyPolicy);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id1)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id1)
);
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory
.forDelete(existingEntity, queryContext, privacyPolicyEvaluationContext)
.enforceDeleteAsync();
verify(
spiedPrivacyPolicy.authorizeDeleteAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
anything(),
anyOfClass(TestEntity),
anything()
)
@@ -749,6 +783,7 @@ describe(EntityMutatorFactory, () => {
it('executes triggers', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -767,10 +802,14 @@ describe(EntityMutatorFactory, () => {
const triggerSpies = setUpMutationTriggerSpies(mutationTriggers);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id1)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id1)
);
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory
.forDelete(existingEntity, queryContext, privacyPolicyEvaluationContext)
.enforceDeleteAsync();
verifyTriggerCounts(
viewerContext,
@@ -789,6 +828,7 @@ describe(EntityMutatorFactory, () => {
it('does not execute validators', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -807,10 +847,14 @@ describe(EntityMutatorFactory, () => {
const validatorSpies = setUpMutationValidatorSpies(mutationValidators);
const existingEntity = await enforceAsyncResult(
entityLoaderFactory.forLoad(viewerContext, queryContext).loadByIDAsync(id1)
entityLoaderFactory
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadByIDAsync(id1)
);
await entityMutatorFactory.forDelete(existingEntity, queryContext).enforceDeleteAsync();
await entityMutatorFactory
.forDelete(existingEntity, queryContext, privacyPolicyEvaluationContext)
.enforceDeleteAsync();
verifyValidatorCounts(viewerContext, validatorSpies, 0, {
type: EntityMutationType.DELETE as any,
@@ -820,6 +864,7 @@ describe(EntityMutatorFactory, () => {
it('invalidates cache for fields upon create', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
@@ -836,21 +881,21 @@ describe(EntityMutatorFactory, () => {
const entites1 = await enforceResultsAsync(
entityLoaderFactory
.forLoad(viewerContext, queryContext)
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadManyByFieldEqualingAsync('stringField', 'huh')
);
expect(entites1).toHaveLength(1);
await enforceAsyncResult(
entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.createAsync()
);
const entities2 = await enforceResultsAsync(
entityLoaderFactory
.forLoad(viewerContext, queryContext)
.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)
.loadManyByFieldEqualingAsync('stringField', 'huh')
);
expect(entities2).toHaveLength(2);
@@ -858,6 +903,7 @@ describe(EntityMutatorFactory, () => {
it('throws error when field not valid', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const id1 = uuidv4();
const { entityMutatorFactory } = createEntityMutatorFactory([
@@ -873,19 +919,19 @@ describe(EntityMutatorFactory, () => {
await expect(
entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 10 as any)
.createAsync()
).rejects.toThrowError('Entity field not valid: TestEntity (stringField = 10)');
const createdEntity = await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'hello')
.enforceCreateAsync();
await expect(
entityMutatorFactory
.forUpdate(createdEntity, queryContext)
.forUpdate(createdEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 10 as any)
.updateAsync()
).rejects.toThrowError('Entity field not valid: TestEntity (stringField = 10)');
@@ -893,6 +939,7 @@ describe(EntityMutatorFactory, () => {
it('returns error result when not authorized to create', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicyMock = mock(SimpleTestEntityPrivacyPolicy);
const databaseAdapter = instance(mock<EntityDatabaseAdapter<SimpleTestFields>>());
@@ -916,6 +963,7 @@ describe(EntityMutatorFactory, () => {
privacyPolicyMock.authorizeCreateAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
anything(),
anyOfClass(SimpleTestEntity),
anything()
)
@@ -924,6 +972,7 @@ describe(EntityMutatorFactory, () => {
privacyPolicyMock.authorizeUpdateAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
anything(),
anyOfClass(SimpleTestEntity),
anything()
)
@@ -932,6 +981,7 @@ describe(EntityMutatorFactory, () => {
privacyPolicyMock.authorizeDeleteAsync(
viewerContext,
anyOfClass(EntityTransactionalQueryContext),
anything(),
anyOfClass(SimpleTestEntity),
anything()
)
@@ -949,7 +999,7 @@ describe(EntityMutatorFactory, () => {
);
const entityCreateResult = await entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.createAsync();
expect(entityCreateResult.ok).toBe(false);
expect(entityCreateResult.reason).toEqual(rejectionError);
@@ -961,14 +1011,14 @@ describe(EntityMutatorFactory, () => {
});
const entityUpdateResult = await entityMutatorFactory
.forUpdate(fakeEntity, queryContext)
.forUpdate(fakeEntity, queryContext, privacyPolicyEvaluationContext)
.updateAsync();
expect(entityUpdateResult.ok).toBe(false);
expect(entityUpdateResult.reason).toEqual(rejectionError);
expect(entityUpdateResult.value).toBe(undefined);
const entityDeleteResult = await entityMutatorFactory
.forDelete(fakeEntity, queryContext)
.forDelete(fakeEntity, queryContext, privacyPolicyEvaluationContext)
.deleteAsync();
expect(entityDeleteResult.ok).toBe(false);
expect(entityDeleteResult.reason).toEqual(rejectionError);
@@ -977,6 +1027,7 @@ describe(EntityMutatorFactory, () => {
it('throws error when db adapter throws', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(SimpleTestEntityPrivacyPolicy));
const databaseAdapterMock = mock<EntityDatabaseAdapter<SimpleTestFields>>();
@@ -1032,37 +1083,48 @@ describe(EntityMutatorFactory, () => {
});
await expect(
entityMutatorFactory.forCreate(viewerContext, queryContext).createAsync()
entityMutatorFactory
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.createAsync()
).rejects.toEqual(rejectionError);
await expect(
entityMutatorFactory.forUpdate(fakeEntity, queryContext).updateAsync()
entityMutatorFactory
.forUpdate(fakeEntity, queryContext, privacyPolicyEvaluationContext)
.updateAsync()
).rejects.toEqual(rejectionError);
await expect(
entityMutatorFactory.forDelete(fakeEntity, queryContext).deleteAsync()
entityMutatorFactory
.forDelete(fakeEntity, queryContext, privacyPolicyEvaluationContext)
.deleteAsync()
).rejects.toEqual(rejectionError);
});
it('records metrics appropriately', async () => {
const viewerContext = mock<ViewerContext>();
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = StubQueryContextProvider.getQueryContext();
const { entityMutatorFactory, metricsAdapter } = createEntityMutatorFactory([]);
const spiedMetricsAdapter = spy(metricsAdapter);
const newEntity = await enforceAsyncResult(
entityMutatorFactory
.forCreate(viewerContext, queryContext)
.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'huh')
.createAsync()
);
await enforceAsyncResult(
entityMutatorFactory
.forUpdate(newEntity, queryContext)
.forUpdate(newEntity, queryContext, privacyPolicyEvaluationContext)
.setField('stringField', 'wat')
.updateAsync()
);
await enforceAsyncResult(entityMutatorFactory.forDelete(newEntity, queryContext).deleteAsync());
await enforceAsyncResult(
entityMutatorFactory
.forDelete(newEntity, queryContext, privacyPolicyEvaluationContext)
.deleteAsync()
);
verify(
spiedMetricsAdapter.logMutatorMutationEvent(
@@ -8,6 +8,7 @@ import EntityPrivacyPolicy, {
EntityPrivacyPolicyEvaluator,
EntityAuthorizationAction,
EntityPrivacyPolicyEvaluationMode,
EntityPrivacyPolicyEvaluationContext,
} from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
@@ -166,6 +167,7 @@ class AlwaysThrowPrivacyPolicyRule extends PrivacyPolicyRule<
evaluateAsync(
_viewerContext: ViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
_entity: BlahEntity
): Promise<RuleEvaluationResult> {
throw new Error('WooHoo!');
@@ -245,12 +247,19 @@ describe(EntityPrivacyPolicy, () => {
it('throws EntityNotAuthorizedError when deny', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
const policy = new AlwaysDenyPolicy();
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toBeInstanceOf(EntityNotAuthorizedError);
verify(
metricsAdapterMock.logAuthorizationEvent(
@@ -267,6 +276,7 @@ describe(EntityPrivacyPolicy, () => {
it('returns entity when allowed', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -274,6 +284,7 @@ describe(EntityPrivacyPolicy, () => {
const approvedEntity = await policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
);
@@ -293,12 +304,19 @@ describe(EntityPrivacyPolicy, () => {
it('throws EntityNotAuthorizedError when all skipped', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
const policy = new SkipAllPolicy();
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toBeInstanceOf(EntityNotAuthorizedError);
verify(
metricsAdapterMock.logAuthorizationEvent(
@@ -315,12 +333,19 @@ describe(EntityPrivacyPolicy, () => {
it('throws EntityNotAuthorizedError when empty policy', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
const policy = new EmptyPolicy();
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toBeInstanceOf(EntityNotAuthorizedError);
verify(
metricsAdapterMock.logAuthorizationEvent(
@@ -337,12 +362,19 @@ describe(EntityPrivacyPolicy, () => {
it('throws when rule throws', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
const policy = new ThrowAllPolicy();
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toThrowError('WooHoo!');
verify(metricsAdapterMock.logAuthorizationEvent(anything())).never();
});
@@ -352,6 +384,7 @@ describe(EntityPrivacyPolicy, () => {
it('returns entity when denied but calls denialHandler', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -362,6 +395,7 @@ describe(EntityPrivacyPolicy, () => {
const approvedEntity = await policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
);
@@ -384,6 +418,7 @@ describe(EntityPrivacyPolicy, () => {
it('does not log when not denied', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -394,6 +429,7 @@ describe(EntityPrivacyPolicy, () => {
const approvedEntity = await policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
);
@@ -416,6 +452,7 @@ describe(EntityPrivacyPolicy, () => {
it('passes through other errors', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -424,7 +461,13 @@ describe(EntityPrivacyPolicy, () => {
const policySpy = spy(policy);
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toThrowError('WooHoo!');
verify(policySpy.denyHandler(anyOfClass(EntityNotAuthorizedError))).never();
@@ -437,6 +480,7 @@ describe(EntityPrivacyPolicy, () => {
it('denies when denied but calls denialHandler', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -445,7 +489,13 @@ describe(EntityPrivacyPolicy, () => {
const policySpy = spy(policy);
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toBeInstanceOf(EntityNotAuthorizedError);
verify(policySpy.denyHandler(anyOfClass(EntityNotAuthorizedError))).once();
@@ -465,6 +515,7 @@ describe(EntityPrivacyPolicy, () => {
it('does not log when not denied', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -475,6 +526,7 @@ describe(EntityPrivacyPolicy, () => {
const approvedEntity = await policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
);
@@ -497,6 +549,7 @@ describe(EntityPrivacyPolicy, () => {
it('passes through other errors', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = instance(mock(EntityQueryContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapterMock = mock<IEntityMetricsAdapter>();
const metricsAdapter = instance(metricsAdapterMock);
const entity = new BlahEntity(viewerContext, { id: '1' });
@@ -505,7 +558,13 @@ describe(EntityPrivacyPolicy, () => {
const policySpy = spy(policy);
await expect(
policy.authorizeCreateAsync(viewerContext, queryContext, entity, metricsAdapter)
policy.authorizeCreateAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
entity,
metricsAdapter
)
).rejects.toThrowError('WooHoo!');
verify(policySpy.denyHandler(anyOfClass(EntityNotAuthorizedError))).never();
@@ -79,6 +79,7 @@ describe(EntitySecondaryCacheLoader, () => {
vc1,
anyOfClass(EntityNonTransactionalQueryContext),
anything(),
anything(),
anything()
)
).once();
@@ -1,6 +1,7 @@
import { mock, verify, instance } from 'ts-mockito';
import EntityLoaderFactory from '../EntityLoaderFactory';
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
import ViewerScopedEntityLoaderFactory from '../ViewerScopedEntityLoaderFactory';
@@ -8,6 +9,7 @@ import ViewerScopedEntityLoaderFactory from '../ViewerScopedEntityLoaderFactory'
describe(ViewerScopedEntityLoaderFactory, () => {
it('correctly scopes viewer to entity loads', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = instance(mock(EntityQueryContext));
const baseLoader = mock<EntityLoaderFactory<any, any, any, any, any, any>>(EntityLoaderFactory);
const baseLoaderInstance = instance(baseLoader);
@@ -21,8 +23,8 @@ describe(ViewerScopedEntityLoaderFactory, () => {
any
>(baseLoaderInstance, viewerContext);
viewerScopedEntityLoader.forLoad(queryContext);
viewerScopedEntityLoader.forLoad(queryContext, privacyPolicyEvaluationContext);
verify(baseLoader.forLoad(viewerContext, queryContext)).once();
verify(baseLoader.forLoad(viewerContext, queryContext, privacyPolicyEvaluationContext)).once();
});
});
@@ -1,6 +1,7 @@
import { mock, instance, verify } from 'ts-mockito';
import EntityMutatorFactory from '../EntityMutatorFactory';
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
import ViewerScopedEntityMutatorFactory from '../ViewerScopedEntityMutatorFactory';
@@ -9,6 +10,7 @@ import TestEntity, { TestFields, TestEntityPrivacyPolicy } from '../testfixtures
describe(ViewerScopedEntityMutatorFactory, () => {
it('correctly scopes viewer to entity mutations', async () => {
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const queryContext = instance(mock(EntityQueryContext));
const baseMutatorFactory =
mock<
@@ -25,8 +27,10 @@ describe(ViewerScopedEntityMutatorFactory, () => {
keyof TestFields
>(baseMutatorFactoryInstance, viewerContext);
viewerScopedEntityLoader.forCreate(queryContext);
viewerScopedEntityLoader.forCreate(queryContext, privacyPolicyEvaluationContext);
verify(baseMutatorFactory.forCreate(viewerContext, queryContext)).once();
verify(
baseMutatorFactory.forCreate(viewerContext, queryContext, privacyPolicyEvaluationContext)
).once();
});
});
+4
View File
@@ -4,6 +4,9 @@
* @module @expo/entity
*/
export { default as ComposedEntityCacheAdapter } from './ComposedEntityCacheAdapter';
export { default as ComposedSecondaryEntityCache } from './ComposedSecondaryEntityCache';
export { default as GenericSecondaryEntityCache } from './GenericSecondaryEntityCache';
export { default as EnforcingEntityLoader } from './EnforcingEntityLoader';
export { default as Entity } from './Entity';
export * from './Entity';
@@ -43,6 +46,7 @@ export * from './EntityQueryContext';
export { default as IEntityCacheAdapterProvider } from './IEntityCacheAdapterProvider';
export { default as IEntityDatabaseAdapterProvider } from './IEntityDatabaseAdapterProvider';
export { default as EntityQueryContextProvider } from './EntityQueryContextProvider';
export { default as IEntityGenericCacher } from './IEntityGenericCacher';
export { default as ReadonlyEntity } from './ReadonlyEntity';
export { default as ViewerContext } from './ViewerContext';
export { default as ViewerScopedEntityCompanion } from './ViewerScopedEntityCompanion';
@@ -10,10 +10,10 @@ export enum CacheStatus {
NEGATIVE,
}
export type CacheLoadResult =
export type CacheLoadResult<TFields> =
| {
status: CacheStatus.HIT;
item: Readonly<object>;
item: Readonly<TFields>;
}
| {
status: CacheStatus.MISS;
@@ -82,7 +82,7 @@ export default class ReadThroughEntityCache<TFields> {
const results: Map<NonNullable<TFields[N]>, readonly Readonly<TFields>[]> = new Map();
cacheLoadResults.forEach((cacheLoadResult, fieldValue) => {
if (cacheLoadResult.status === CacheStatus.HIT) {
results.set(fieldValue, [cacheLoadResult.item as Readonly<TFields>]);
results.set(fieldValue, [cacheLoadResult.item]);
}
});
@@ -95,7 +95,7 @@ export default class ReadThroughEntityCache<TFields> {
return !objectsFromFulfillerForFv || objectsFromFulfillerForFv.length === 0;
});
const objectsToCache: Map<NonNullable<TFields[N]>, object> = new Map();
const objectsToCache: Map<NonNullable<TFields[N]>, Readonly<TFields>> = new Map();
for (const [fieldValue, objects] of dbFetchResults.entries()) {
if (objects.length > 1) {
// multiple objects received for what was supposed to be a unique query, don't add to return map nor cache
@@ -1,3 +1,4 @@
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ReadonlyEntity from '../ReadonlyEntity';
import ViewerContext from '../ViewerContext';
@@ -16,6 +17,7 @@ export default class AlwaysAllowPrivacyPolicyRule<
async evaluateAsync(
_viewerContext: TViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
_entity: TEntity
): Promise<RuleEvaluationResult> {
return RuleEvaluationResult.ALLOW;
@@ -1,3 +1,4 @@
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ReadonlyEntity from '../ReadonlyEntity';
import ViewerContext from '../ViewerContext';
@@ -16,6 +17,7 @@ export default class AlwaysDenyPrivacyPolicyRule<
async evaluateAsync(
_viewerContext: TViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
_entity: TEntity
): Promise<RuleEvaluationResult> {
return RuleEvaluationResult.DENY;
@@ -1,3 +1,4 @@
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ReadonlyEntity from '../ReadonlyEntity';
import ViewerContext from '../ViewerContext';
@@ -16,6 +17,7 @@ export default class AlwaysSkipPrivacyPolicyRule<
async evaluateAsync(
_viewerContext: TViewerContext,
_queryContext: EntityQueryContext,
_evaluationContext: EntityPrivacyPolicyEvaluationContext,
_entity: TEntity
): Promise<RuleEvaluationResult> {
return RuleEvaluationResult.SKIP;
@@ -1,3 +1,4 @@
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ReadonlyEntity from '../ReadonlyEntity';
import ViewerContext from '../ViewerContext';
@@ -45,6 +46,7 @@ export default abstract class PrivacyPolicyRule<
abstract evaluateAsync(
viewerContext: TViewerContext,
queryContext: EntityQueryContext,
evaluationContext: EntityPrivacyPolicyEvaluationContext,
entity: TEntity
): Promise<RuleEvaluationResult>;
}
@@ -1,5 +1,6 @@
import { mock, instance, anything } from 'ts-mockito';
import { EntityPrivacyPolicyEvaluationContext } from '../../EntityPrivacyPolicy';
import { EntityQueryContext } from '../../EntityQueryContext';
import ViewerContext from '../../ViewerContext';
import { describePrivacyPolicyRule } from '../../utils/testing/PrivacyPolicyRuleTestUtils';
@@ -10,6 +11,7 @@ describePrivacyPolicyRule(new AlwaysAllowPrivacyPolicyRule(), {
{
viewerContext: instance(mock(ViewerContext)),
queryContext: instance(mock(EntityQueryContext)),
evaluationContext: instance(mock<EntityPrivacyPolicyEvaluationContext>()),
entity: anything(),
},
],
@@ -1,5 +1,6 @@
import { mock, instance, anything } from 'ts-mockito';
import { EntityPrivacyPolicyEvaluationContext } from '../../EntityPrivacyPolicy';
import { EntityQueryContext } from '../../EntityQueryContext';
import ViewerContext from '../../ViewerContext';
import { describePrivacyPolicyRule } from '../../utils/testing/PrivacyPolicyRuleTestUtils';
@@ -10,6 +11,7 @@ describePrivacyPolicyRule(new AlwaysDenyPrivacyPolicyRule(), {
{
viewerContext: instance(mock(ViewerContext)),
queryContext: instance(mock(EntityQueryContext)),
evaluationContext: instance(mock<EntityPrivacyPolicyEvaluationContext>()),
entity: anything(),
},
],
@@ -1,5 +1,6 @@
import { mock, instance, anything } from 'ts-mockito';
import { EntityPrivacyPolicyEvaluationContext } from '../../EntityPrivacyPolicy';
import { EntityQueryContext } from '../../EntityQueryContext';
import ViewerContext from '../../ViewerContext';
import { describePrivacyPolicyRule } from '../../utils/testing/PrivacyPolicyRuleTestUtils';
@@ -10,6 +11,7 @@ describePrivacyPolicyRule(new AlwaysSkipPrivacyPolicyRule(), {
{
viewerContext: instance(mock(ViewerContext)),
queryContext: instance(mock(EntityQueryContext)),
evaluationContext: instance(mock<EntityPrivacyPolicyEvaluationContext>()),
entity: anything(),
},
],
@@ -1,3 +1,4 @@
import { EntityPrivacyPolicyEvaluationContext } from '../../EntityPrivacyPolicy';
import { EntityQueryContext } from '../../EntityQueryContext';
import ReadonlyEntity from '../../ReadonlyEntity';
import ViewerContext from '../../ViewerContext';
@@ -12,6 +13,7 @@ export interface Case<
> {
viewerContext: TViewerContext;
queryContext: EntityQueryContext;
evaluationContext: EntityPrivacyPolicyEvaluationContext;
entity: TEntity;
}
@@ -48,9 +50,11 @@ export const describePrivacyPolicyRuleWithAsyncTestCase = <
if (allowCases && allowCases.size > 0) {
describe('allow cases', () => {
test.each(Array.from(allowCases.keys()))('%p', async (caseKey) => {
const { viewerContext, queryContext, entity } = await allowCases.get(caseKey)!();
const { viewerContext, queryContext, evaluationContext, entity } = await allowCases.get(
caseKey
)!();
await expect(
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, entity)
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, evaluationContext, entity)
).resolves.toEqual(RuleEvaluationResult.ALLOW);
});
});
@@ -59,9 +63,11 @@ export const describePrivacyPolicyRuleWithAsyncTestCase = <
if (skipCases && skipCases.size > 0) {
describe('skip cases', () => {
test.each(Array.from(skipCases.keys()))('%p', async (caseKey) => {
const { viewerContext, queryContext, entity } = await skipCases.get(caseKey)!();
const { viewerContext, queryContext, evaluationContext, entity } = await skipCases.get(
caseKey
)!();
await expect(
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, entity)
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, evaluationContext, entity)
).resolves.toEqual(RuleEvaluationResult.SKIP);
});
});
@@ -70,9 +76,11 @@ export const describePrivacyPolicyRuleWithAsyncTestCase = <
if (denyCases && denyCases.size > 0) {
describe('deny cases', () => {
test.each(Array.from(denyCases.keys()))('%p', async (caseKey) => {
const { viewerContext, queryContext, entity } = await denyCases.get(caseKey)!();
const { viewerContext, queryContext, evaluationContext, entity } = await denyCases.get(
caseKey
)!();
await expect(
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, entity)
privacyPolicyRule.evaluateAsync(viewerContext, queryContext, evaluationContext, entity)
).resolves.toEqual(RuleEvaluationResult.DENY);
});
});
@@ -3,7 +3,6 @@ import invariant from 'invariant';
import EntityCacheAdapter from '../../EntityCacheAdapter';
import EntityConfiguration from '../../EntityConfiguration';
import IEntityCacheAdapterProvider from '../../IEntityCacheAdapterProvider';
import { FieldTransformerMap } from '../../internal/EntityFieldTransformationUtils';
import { CacheStatus, CacheLoadResult } from '../../internal/ReadThroughEntityCache';
export class NoCacheStubCacheAdapterProvider implements IEntityCacheAdapterProvider {
@@ -15,15 +14,11 @@ export class NoCacheStubCacheAdapterProvider implements IEntityCacheAdapterProvi
}
export class NoCacheStubCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
public getFieldTransformerMap(): FieldTransformerMap {
return new Map();
}
public async loadManyAsync<N extends keyof TFields>(
_fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult>> {
return fieldValues.reduce((acc: Map<NonNullable<TFields[N]>, CacheLoadResult>, v) => {
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
return fieldValues.reduce((acc: Map<NonNullable<TFields[N]>, CacheLoadResult<TFields>>, v) => {
acc.set(v, {
status: CacheStatus.MISS,
});
@@ -33,7 +28,7 @@ export class NoCacheStubCacheAdapter<TFields> extends EntityCacheAdapter<TFields
public async cacheManyAsync<N extends keyof TFields>(
_fieldName: N,
_objectMap: ReadonlyMap<NonNullable<TFields[N]>, object>
_objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {}
public async cacheDBMissesAsync<N extends keyof TFields>(
@@ -53,27 +48,26 @@ export class InMemoryFullCacheStubCacheAdapterProvider implements IEntityCacheAd
getCacheAdapter<TFields>(
entityConfiguration: EntityConfiguration<TFields>
): EntityCacheAdapter<TFields> {
return new InMemoryFullCacheStubCacheAdapter(entityConfiguration, this.cache);
return new InMemoryFullCacheStubCacheAdapter(
entityConfiguration,
this.cache as Map<string, Readonly<TFields>>
);
}
}
export class InMemoryFullCacheStubCacheAdapter<TFields> extends EntityCacheAdapter<TFields> {
constructor(
entityConfiguration: EntityConfiguration<TFields>,
readonly cache: Map<string, Readonly<object>>
readonly cache: Map<string, Readonly<TFields>>
) {
super(entityConfiguration);
}
public getFieldTransformerMap(): FieldTransformerMap {
return new Map();
}
public async loadManyAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult>> {
const results = new Map<NonNullable<TFields[N]>, CacheLoadResult>();
): Promise<ReadonlyMap<NonNullable<TFields[N]>, CacheLoadResult<TFields>>> {
const results = new Map<NonNullable<TFields[N]>, CacheLoadResult<TFields>>();
fieldValues.forEach((fieldValue) => {
const cacheKey = this.createCacheKey(fieldName, fieldValue);
if (!this.cache.has(cacheKey)) {
@@ -94,7 +88,7 @@ export class InMemoryFullCacheStubCacheAdapter<TFields> extends EntityCacheAdapt
public async cacheManyAsync<N extends keyof TFields>(
fieldName: N,
objectMap: ReadonlyMap<NonNullable<TFields[N]>, object>
objectMap: ReadonlyMap<NonNullable<TFields[N]>, Readonly<TFields>>
): Promise<void> {
objectMap.forEach((obj, fieldValue) => {
const cacheKey = this.createCacheKey(fieldName, fieldValue);
+23 -5
View File
@@ -507,28 +507,36 @@
exec-sh "^0.3.2"
minimist "^1.2.0"
"@expo/entity-cache-adapter-local-memory@file:packages/entity-cache-adapter-local-memory":
version "0.25.1"
dependencies:
lru-cache "^7.3.1"
"@expo/entity-cache-adapter-redis@file:packages/entity-cache-adapter-redis":
version "0.21.0"
version "0.25.1"
dependencies:
ioredis "^4.27.3"
"@expo/entity-database-adapter-knex@file:packages/entity-database-adapter-knex":
version "0.21.0"
version "0.25.1"
dependencies:
knex "^1.0.2"
"@expo/entity-ip-address-field@file:packages/entity-ip-address-field":
version "0.21.0"
version "0.25.1"
dependencies:
ip-address "^8.1.0"
"@expo/entity-secondary-cache-local-memory@file:packages/entity-secondary-cache-local-memory":
version "0.25.1"
"@expo/entity-secondary-cache-redis@file:packages/entity-secondary-cache-redis":
version "0.21.0"
version "0.25.1"
dependencies:
ioredis "^4.17.3"
"@expo/entity@file:packages/entity":
version "0.21.0"
version "0.25.1"
dependencies:
"@expo/results" "^1.0.0"
dataloader "^2.0.0"
@@ -1995,6 +2003,11 @@
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9"
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==
"@types/lru-cache@^7.4.0":
version "7.4.0"
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-7.4.0.tgz#74e50606962cb90448d928d6f0c7c872592fc84d"
integrity sha512-jZ/Tb2/3vXw4VYd9AImFC/n6XT3WywZNAxwY8Ox9eM87M9ta9G7KzCx4aKo2Zllvr02k40eY328cjOO3WuK5Kw==
"@types/mime@*":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
@@ -6565,6 +6578,11 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"
lru-cache@^7.3.1:
version "7.4.4"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.4.4.tgz#a3dabc394ec07e2285af52fd24d0d74b3ac71c29"
integrity sha512-2XbUJmlpIbmc9JvNNmtLzHlF31srxoDxuiQiwBHic7RZyHyltbTdzoO6maRqpdEhOOG5GD80EXvzAU0wR15ccg==
lunr@^2.3.9:
version "2.3.9"
resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1"