Compare commits

..
Author SHA1 Message Date
William Schurman 6222f0f464 feat: dataloaders within transactions 2020-12-10 15:34:56 -08:00
Will Schurman e3b2b235f8 v0.10.0 (#97) 2020-11-16 20:27:33 -08:00
Will Schurman d9603298e0 fix: fix null handling in load paths (try 2) (#96) 2020-11-16 20:03:32 -08:00
Will Schurman b22c734275 v0.9.1 (#94) 2020-11-06 09:11:55 -08:00
Will Schurman 012c05d62d fix: edge deletion behavior cyclic import structure again (#93) 2020-11-06 08:52:37 -08:00
Will Schurman ead9561cc7 v0.9.0 (#92) 2020-11-05 14:40:17 -08:00
Will Schurman d1a6f13396 feat: add ability to invalidate an entity manually (#91) 2020-11-05 12:38:14 -08:00
Will Schurman cca8fc1987 fix: edge deletion behavior cyclic import structure (#89) 2020-11-05 10:08:50 -08:00
Will Schurman 86aa5b291d v0.8.1 (#87) 2020-10-09 12:07:19 -07:00
Will Schurman 4026fc1a61 docs: update to add 2FA token note to release process 2020-10-09 10:59:58 -07:00
Will Schurman a263e74b8a fix!: add entity name to loader counter metric events (#86) 2020-10-09 10:48:00 -07:00
Will Schurman f737fa5b28 v0.8.0 (#85) 2020-10-09 10:11:22 -07:00
Will Schurman 884320ac53 docs: update readme one last time for release process 2020-10-09 10:06:34 -07:00
Will Schurman 40906202cf docs: update readme again for release process 2020-10-09 10:04:30 -07:00
Will Schurman 6cdd720c98 docs: update readme for release process (#84) 2020-10-09 09:58:16 -07:00
Will Schurman f4cdc01c6c feat: add entityClassName to metrics adapter events (#83) 2020-10-08 14:47:20 -07:00
Will Schurman d0cd8d88f2 v0.7.1 (#82) 2020-09-30 20:31:20 -07:00
Will Schurman f98f7d14b6 fix: correct type of runInTransactionAsync (#81) 2020-09-30 20:19:40 -07:00
Will Schurman f4b30cb704 v0.7.0 (#80) 2020-09-30 19:55:22 -07:00
Will Schurman 22aa907b11 docs: fix typedoc (#79) 2020-08-25 14:05:28 -07:00
Will Schurman 9891e74694 fix: upgrade packages (#78) 2020-08-25 13:38:00 -07:00
Will Schurman dbc3c81b35 fix: move cache invalidation to after transaction commit (#77) 2020-08-25 11:16:16 -07:00
Will Schurman 62bfd6c703 fix!: constuctor thrown exceptions for loadMany (#74) 2020-07-29 10:35:28 -07:00
Will Schurman 7e829f30fd fix: fix index export for EntityMutationTrigger and EntityMutationValidator (#73) 2020-07-28 15:09:45 -07:00
Will Schurman cd2df8da4b fix: fix incorrect loader types for field selecitons (#72) 2020-07-28 15:01:33 -07:00
65 changed files with 4075 additions and 1969 deletions

No files matched your search

+82
View File
@@ -3,6 +3,88 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
### Bug Fixes
* fix null handling in load paths (try 2) ([#96](https://github.com/expo/entity/issues/96)) ([d960329](https://github.com/expo/entity/commit/d9603298e0cc5c3c2b0cca959876e9195555fb57))
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
### Bug Fixes
* edge deletion behavior cyclic import structure again ([#93](https://github.com/expo/entity/issues/93)) ([012c05d](https://github.com/expo/entity/commit/012c05d62d2ecd559eb331c6ca358052bfe66dc8))
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
### Bug Fixes
* edge deletion behavior cyclic import structure ([#89](https://github.com/expo/entity/issues/89)) ([cca8fc1](https://github.com/expo/entity/commit/cca8fc19870057ebc7033081b59fab4a142fbf1a))
### Features
* add ability to invalidate an entity manually ([#91](https://github.com/expo/entity/issues/91)) ([d1a6f13](https://github.com/expo/entity/commit/d1a6f1339648942f12b3fb605eb329aefe5cdd63))
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package root
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
### Features
* add entityClassName to metrics adapter events ([#83](https://github.com/expo/entity/issues/83)) ([f4cdc01](https://github.com/expo/entity/commit/f4cdc01c6cb8868c24ce3da2274f0901687e05bd))
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
### Bug Fixes
* correct type of runInTransactionAsync ([#81](https://github.com/expo/entity/issues/81)) ([f98f7d1](https://github.com/expo/entity/commit/f98f7d14b6149a496a7a3e97dd63d89b2842cc26))
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* fix incorrect loader types for field selecitons ([#72](https://github.com/expo/entity/issues/72)) ([cd2df8d](https://github.com/expo/entity/commit/cd2df8da4b81bcd4769e950d3124c18886df2816))
* fix index export for EntityMutationTrigger and EntityMutationValidator ([#73](https://github.com/expo/entity/issues/73)) ([7e829f3](https://github.com/expo/entity/commit/7e829f30fde0bd343cb8a584ccda5530cf61ec33))
* move cache invalidation to after transaction commit ([#77](https://github.com/expo/entity/issues/77)) ([dbc3c81](https://github.com/expo/entity/commit/dbc3c81b359a668b5134189ce6de49b5893bb5e7))
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
# [0.6.0](https://github.com/expo/entity/compare/v0.5.2...v0.6.0) (2020-07-23)
+15
View File
@@ -78,6 +78,21 @@ Entity is not limited in where it can or should be used, but was designed for us
Note: The entity framework instance should not be shared across multiple requests since it contains a unique memoized [Dataloader](https://github.com/graphql/dataloader#class-dataloader). A long-lived instance is prone to data synchronization issues, especially when the application is scaled horizontally and multiple shared caches would exist for the same data.
## Releasing
To release a new version, let's say `v1.0.0` for example:
1. `git checkout -b bump-v1.0.0`
1. `yarn lerna version minor -- --no-push --conventional-commits`
1. `git push -u origin bump-v1.0.0`
1. Create a PR from the `bump-v1.0.0` branch, wait for tests, and commit the PR using GitHub interface.
1. `git checkout master && git pull`
1. `git tag -d v1.0.0`
1. `git tag v1.0.0`
1. `git push origin v1.0.0`
1. In GitHub release interface, create a new release from the tag, copy changelog changes to release description.
1. `yarn clean && yarn tsc`
1. In each public subpackage, run `npm publish` and enter 2FA token.
## License
The Entity source code is made available under the [MIT license](LICENSE).
+1 -1
View File
@@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.6.0"
"version": "0.10.0"
}
+19 -19
View File
@@ -14,28 +14,28 @@
},
"devDependencies": {
"@expo/entity": "file:packages/entity",
"@expo/results": "^0.3.0",
"@types/invariant": "^2.2.32",
"@types/ioredis": "^4.14.9",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/uuid": "^7.0.3",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"@expo/results": "^1.0.0",
"@types/invariant": "^2.2.33",
"@types/ioredis": "^4.17.3",
"@types/jest": "^26.0.10",
"@types/node": "^14.6.0",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"barrelsby": "^2.2.0",
"eslint": "^6.8.0",
"eslint-config-universe": "^3.0.0",
"eslint-plugin-tsdoc": "^0.2.4",
"jest": "^24.9.0",
"lerna": "^3.20.2",
"pg": "^8.0.3",
"prettier": "^2.0.5",
"ts-jest": "~24.2.0",
"ts-mockito": "^2.5.0",
"eslint": "^7.7.0",
"eslint-config-universe": "^4.0.0",
"eslint-plugin-tsdoc": "^0.2.6",
"jest": "^26.4.2",
"lerna": "^3.22.1",
"pg": "^8.3.2",
"prettier": "^2.1.0",
"ts-jest": "~26.2.0",
"ts-mockito": "^2.6.1",
"typedoc": "^0.17.6",
"typedoc-plugin-lerna-packages": "^0.3.0",
"typescript": "^3.9.2",
"uuid": "^7.0.3"
"typescript": "^4.0.2",
"uuid": "^8.3.0"
},
"dependencies": {
"@expo/entity": "file:packages/entity",
@@ -3,6 +3,65 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
# [0.6.0](https://github.com/expo/entity/compare/v0.5.2...v0.6.0) (2020-07-23)
**Note:** Version bump only for package @expo/entity-cache-adapter-redis
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-cache-adapter-redis",
"version": "0.6.0",
"version": "0.10.0",
"description": "Redis cache adapter for @expo/entity",
"files": [
"build",
@@ -12,9 +12,9 @@
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src --ext '.ts'",
"test": "jest --rootDir --config ../../resources/jest.config.js",
"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",
"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": {
@@ -29,9 +29,9 @@
"@expo/entity": "*"
},
"dependencies": {
"ioredis": "^4.16.1"
"ioredis": "^4.17.3"
},
"devDependencies": {
"@expo/entity": "^0.6.0"
"@expo/entity": "^0.10.0"
}
}
@@ -3,6 +3,67 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
**Note:** Version bump only for package @expo/entity-database-adapter-knex
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* fix index export for EntityMutationTrigger and EntityMutationValidator ([#73](https://github.com/expo/entity/issues/73)) ([7e829f3](https://github.com/expo/entity/commit/7e829f30fde0bd343cb8a584ccda5530cf61ec33))
* move cache invalidation to after transaction commit ([#77](https://github.com/expo/entity/issues/77)) ([dbc3c81](https://github.com/expo/entity/commit/dbc3c81b359a668b5134189ce6de49b5893bb5e7))
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
# [0.6.0](https://github.com/expo/entity/compare/v0.5.2...v0.6.0) (2020-07-23)
@@ -1,6 +1,6 @@
{
"name": "@expo/entity-database-adapter-knex",
"version": "0.6.0",
"version": "0.10.0",
"description": "Knex database adapter for @expo/entity",
"files": [
"build",
@@ -12,9 +12,9 @@
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src --ext '.ts'",
"test": "jest --rootDir --config ../../resources/jest.config.js --passWithNoTests",
"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",
"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": {
@@ -29,9 +29,9 @@
"@expo/entity": "*"
},
"dependencies": {
"knex": "^0.20.11"
"knex": "^0.21.5"
},
"devDependencies": {
"@expo/entity": "^0.6.0"
"@expo/entity": "^0.10.0"
}
}
@@ -1,25 +1,21 @@
import {
IEntityQueryContextProvider,
EntityNonTransactionalQueryContext,
EntityTransactionalQueryContext,
} from '@expo/entity';
import { EntityQueryContextProvider } from '@expo/entity';
import Knex from 'knex';
/**
* Query context provider for knex (postgres).
*/
export default class PostgresEntityQueryContextProvider implements IEntityQueryContextProvider {
constructor(private readonly knexInstance: Knex) {}
getQueryContext(): EntityNonTransactionalQueryContext {
return new EntityNonTransactionalQueryContext(this.knexInstance, this);
export default class PostgresEntityQueryContextProvider extends EntityQueryContextProvider {
constructor(private readonly knexInstance: Knex) {
super();
}
async runInTransactionAsync<T>(
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
return await this.knexInstance.transaction(async (trx) => {
return await transactionScope(new EntityTransactionalQueryContext(trx));
});
protected getQueryInterface(): any {
return this.knexInstance;
}
protected createTransactionRunner<T>(): (
transactionScope: (trx: any) => Promise<T>
) => Promise<T> {
return (transactionScope) => this.knexInstance.transaction(transactionScope);
}
}
@@ -1,184 +0,0 @@
import {
EntityPrivacyPolicy,
ViewerContext,
AlwaysAllowPrivacyPolicyRule,
Entity,
EntityCompanionDefinition,
EntityConfiguration,
DatabaseAdapterFlavor,
CacheAdapterFlavor,
UUIDField,
EntityEdgeDeletionBehavior,
} from '@expo/entity';
import Knex from 'knex';
import { createKnexIntegrationTestEntityCompanionProvider } from '../testfixtures/createKnexIntegrationTestEntityCompanionProvider';
interface ParentFields {
id: string;
}
interface ChildFields {
id: string;
parent_id: string;
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<any, string, ViewerContext, any, any> {
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
}
class ParentEntity extends Entity<ParentFields, string, ViewerContext> {
static getCompanionDefinition(): EntityCompanionDefinition<
ParentFields,
string,
ViewerContext,
ParentEntity,
TestEntityPrivacyPolicy
> {
return parentEntityCompanion;
}
}
class ChildEntity extends Entity<ChildFields, string, ViewerContext> {
static getCompanionDefinition(): EntityCompanionDefinition<
ChildFields,
string,
ViewerContext,
ChildEntity,
TestEntityPrivacyPolicy
> {
return childEntityCompanion;
}
}
const parentEntityConfiguration = new EntityConfiguration<ParentFields>({
idField: 'id',
tableName: 'parents',
inboundEdges: [ChildEntity],
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});
const childEntityConfiguration = new EntityConfiguration<ChildFields>({
idField: 'id',
tableName: 'children',
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
parent_id: new UUIDField({
columnName: 'parent_id',
cache: true,
association: {
associatedEntityClass: ParentEntity,
edgeDeletionBehavior: EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE,
},
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});
const parentEntityCompanion = new EntityCompanionDefinition({
entityClass: ParentEntity,
entityConfiguration: parentEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
});
const childEntityCompanion = new EntityCompanionDefinition({
entityClass: ChildEntity,
entityConfiguration: childEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
});
async function createOrTruncatePostgresTables(knex: Knex): Promise<void> {
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'); // for uuid_generate_v4()
await knex.schema.createTable('parents', (table) => {
table.uuid('id').defaultTo(knex.raw('uuid_generate_v4()')).primary();
});
await knex.into('parents').truncate();
await knex.schema.createTable('children', (table) => {
table.uuid('id').defaultTo(knex.raw('uuid_generate_v4()')).primary();
table.uuid('parent_id').references('id').inTable('parents').onDelete('cascade').unique();
});
await knex.into('children').truncate();
}
async function dropPostgresTable(knex: Knex): Promise<void> {
if (await knex.schema.hasTable('children')) {
await knex.schema.dropTable('children');
}
if (await knex.schema.hasTable('parents')) {
await knex.schema.dropTable('parents');
}
}
describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
let knexInstance: Knex;
beforeAll(() => {
knexInstance = Knex({
client: 'pg',
connection: {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: 'localhost',
port: parseInt(process.env.PGPORT!, 10),
database: process.env.PGDATABASE,
},
});
});
beforeEach(async () => {
await createOrTruncatePostgresTables(knexInstance);
});
afterAll(async () => {
await dropPostgresTable(knexInstance);
knexInstance.destroy();
});
describe('EntityEdgeDeletionBehavior.INVALIDATE_CACHE', () => {
it('invalidates the cache', async () => {
const viewerContext = new ViewerContext(
createKnexIntegrationTestEntityCompanionProvider(knexInstance)
);
const parent = await ParentEntity.creator(viewerContext).enforceCreateAsync();
const child = await ChildEntity.creator(viewerContext)
.setField('parent_id', parent.getID())
.enforceCreateAsync();
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
).resolves.not.toBeNull();
await expect(
ChildEntity.loader(viewerContext)
.enforcing()
.loadByFieldEqualingAsync('parent_id', parent.getID())
).resolves.not.toBeNull();
await ParentEntity.enforceDeleteAsync(parent);
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
).resolves.toBeNull();
await expect(
ChildEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(child.getID())
).resolves.toBeNull();
});
});
});
@@ -34,7 +34,7 @@ describe('postgres entity integration', () => {
afterAll(async () => {
await PostgresTestEntity.dropPostgresTable(knexInstance);
knexInstance.destroy();
await knexInstance.destroy();
});
it('supports parallel partial updates', async () => {
@@ -27,7 +27,7 @@ describe('postgres entity integration', () => {
afterAll(async () => {
await InvalidTestEntity.dropPostgresTable(knexInstance);
knexInstance.destroy();
await knexInstance.destroy();
});
describe('invalid operation result counts', () => {
@@ -9,8 +9,9 @@ import {
CacheAdapterFlavor,
EntityCompanionDefinition,
Entity,
EntityMutationExecutable,
EntityMutationTrigger,
EntityQueryContext,
EntityNonTransactionalMutationTrigger,
} from '@expo/entity';
import Knex from 'knex';
@@ -97,7 +98,7 @@ class PostgresTriggerTestEntityPrivacyPolicy extends EntityPrivacyPolicy<
];
}
class ThrowConditionallyTrigger extends EntityMutationExecutable<
class ThrowConditionallyTrigger extends EntityMutationTrigger<
PostgresTriggerTestEntityFields,
string,
ViewerContext,
@@ -118,6 +119,26 @@ class ThrowConditionallyTrigger extends EntityMutationExecutable<
}
}
class ThrowConditionallyNonTransactionalTrigger extends EntityNonTransactionalMutationTrigger<
PostgresTriggerTestEntityFields,
string,
ViewerContext,
PostgresTriggerTestEntity
> {
constructor(private fieldName: keyof PostgresTriggerTestEntityFields, private badValue: string) {
super();
}
async executeAsync(
_viewerContext: ViewerContext,
entity: PostgresTriggerTestEntity
): Promise<void> {
if (entity.getField(this.fieldName) === this.badValue) {
throw new Error(`${this.fieldName} cannot have value ${this.badValue}`);
}
}
}
export const postgresTestEntityConfiguration = new EntityConfiguration<
PostgresTriggerTestEntityFields
>({
@@ -149,6 +170,6 @@ const postgresTestEntityCompanionDefinition = new EntityCompanionDefinition({
afterDelete: [new ThrowConditionallyTrigger('name', 'afterDelete')],
beforeAll: [new ThrowConditionallyTrigger('name', 'beforeAll')],
afterAll: [new ThrowConditionallyTrigger('name', 'afterAll')],
afterCommit: [new ThrowConditionallyTrigger('name', 'afterCommit')],
afterCommit: [new ThrowConditionallyNonTransactionalTrigger('name', 'afterCommit')],
},
});
@@ -9,7 +9,7 @@ import {
CacheAdapterFlavor,
EntityCompanionDefinition,
Entity,
EntityMutationExecutable,
EntityMutationTrigger,
EntityQueryContext,
} from '@expo/entity';
import Knex from 'knex';
@@ -97,7 +97,7 @@ class PostgresValidatorTestEntityPrivacyPolicy extends EntityPrivacyPolicy<
];
}
class ThrowConditionallyTrigger extends EntityMutationExecutable<
class ThrowConditionallyTrigger extends EntityMutationTrigger<
PostgresValidatorTestEntityFields,
string,
ViewerContext,
+60
View File
@@ -3,6 +3,66 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
**Note:** Version bump only for package @expo/entity-example
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
**Note:** Version bump only for package @expo/entity-example
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
**Note:** Version bump only for package @expo/entity-example
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package @expo/entity-example
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
**Note:** Version bump only for package @expo/entity-example
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
**Note:** Version bump only for package @expo/entity-example
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* move cache invalidation to after transaction commit ([#77](https://github.com/expo/entity/issues/77)) ([dbc3c81](https://github.com/expo/entity/commit/dbc3c81b359a668b5134189ce6de49b5893bb5e7))
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
# [0.6.0](https://github.com/expo/entity/compare/v0.5.2...v0.6.0) (2020-07-23)
**Note:** Version bump only for package @expo/entity-example
+13 -13
View File
@@ -1,15 +1,15 @@
{
"name": "@expo/entity-example",
"private": true,
"version": "0.6.0",
"version": "0.10.0",
"description": "An example integration of the @expo/entity framework",
"scripts": {
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src --ext '.ts'",
"test": "jest --rootDir --config ../../resources/jest.config.js",
"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",
"integration-no-setup": "jest --config ../../resources/jest-integration.config.js --rootDir . --runInBand --passWithNoTests",
"barrelsby": "barrelsby --directory src --location top --exclude tests__ --singleQuotes --exportDefault --delete",
"start": "tsnd --transpile-only --inspect --respawn --no-notify --async-stack-traces --throw-deprecation -- src/index.ts"
},
@@ -22,18 +22,18 @@
"author": "Expo",
"license": "MIT",
"dependencies": {
"@expo/entity": "^0.6.0",
"apollo-server-koa": "^2.14.2",
"graphql": "^15.0.0",
"koa": "^2.11.0",
"koa-body": "^4.1.1",
"koa-router": "^8.0.8"
"@expo/entity": "^0.10.0",
"apollo-server-koa": "^2.16.1",
"graphql": "^15.3.0",
"koa": "^2.13.0",
"koa-body": "^4.2.0",
"koa-router": "^9.4.0"
},
"devDependencies": {
"@types/koa": "^2.11.3",
"@types/koa-router": "^7.4.0",
"@types/supertest": "^2.0.9",
"@types/koa": "^2.11.4",
"@types/koa-router": "^7.4.1",
"@types/supertest": "^2.0.10",
"supertest": "^4.0.2",
"ts-node-dev": "^1.0.0-pre.44"
"ts-node-dev": "^1.0.0-pre.60"
}
}
@@ -1,17 +1,13 @@
import {
IEntityQueryContextProvider,
EntityNonTransactionalQueryContext,
EntityTransactionalQueryContext,
} from '@expo/entity';
import { EntityQueryContextProvider } from '@expo/entity';
export default class InMemoryQueryContextProvider implements IEntityQueryContextProvider {
getQueryContext(): EntityNonTransactionalQueryContext {
return new EntityNonTransactionalQueryContext({}, this);
export default class InMemoryQueryContextProvider extends EntityQueryContextProvider {
protected getQueryInterface(): any {
return {};
}
async runInTransactionAsync<T>(
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
return await transactionScope(new EntityTransactionalQueryContext({}));
protected createTransactionRunner<T>(): (
transactionScope: (queryInterface: any) => Promise<T>
) => Promise<T> {
return (transactionScope) => Promise.resolve(transactionScope({}));
}
}
+2 -2
View File
@@ -2,14 +2,14 @@ import { EntityCompanionProvider } from '@expo/entity';
import { ApolloServer } from 'apollo-server-koa';
import Koa from 'koa';
import koaBody from 'koa-body';
import KoaRouter from 'koa-router';
import KoaRouter, { RouterContext } from 'koa-router';
import { entityCompanionMiddleware, viewerContextMiddleware } from './middleware';
import notesRouter from './routers/notesRouter';
import { typeDefs, resolvers } from './schema';
import { ExampleViewerContext } from './viewerContexts';
export type ExampleContext = Koa.ParameterizedContext<ExampleState>;
export type ExampleContext = RouterContext<ExampleState>;
/**
* Koa provides a per-request state container to place custom properties.
@@ -0,0 +1,66 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
**Note:** Version bump only for package @expo/entity-full-integration-tests
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
### Bug Fixes
* edge deletion behavior cyclic import structure again ([#93](https://github.com/expo/entity/issues/93)) ([012c05d](https://github.com/expo/entity/commit/012c05d62d2ecd559eb331c6ca358052bfe66dc8))
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
### Bug Fixes
* edge deletion behavior cyclic import structure ([#89](https://github.com/expo/entity/issues/89)) ([cca8fc1](https://github.com/expo/entity/commit/cca8fc19870057ebc7033081b59fab4a142fbf1a))
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
**Note:** Version bump only for package @expo/entity-full-integration-tests
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
**Note:** Version bump only for package @expo/entity-full-integration-tests
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* move cache invalidation to after transaction commit ([#77](https://github.com/expo/entity/issues/77)) ([dbc3c81](https://github.com/expo/entity/commit/dbc3c81b359a668b5134189ce6de49b5893bb5e7))
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
@@ -0,0 +1,5 @@
# `@expo/entity-full-integration-tests`
Full integration tests for testing combinations of database and cache adapters.
[Documentation](https://expo.github.io/entity/modules/_expo_entity_full_integration_tests.html)
@@ -0,0 +1,33 @@
{
"name": "@expo/entity-full-integration-tests",
"private": true,
"version": "0.10.0",
"description": "Full redis and knex integration tests for the entity framework",
"scripts": {
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src --ext '.ts'",
"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-redis": "*",
"@expo/entity-database-adapter-knex": "*"
},
"devDependencies": {
"@expo/entity": "^0.10.0",
"@expo/entity-cache-adapter-redis": "^0.10.0",
"@expo/entity-database-adapter-knex": "^0.10.0"
}
}
@@ -0,0 +1,205 @@
import {
EntityPrivacyPolicy,
ViewerContext,
AlwaysAllowPrivacyPolicyRule,
Entity,
EntityCompanionDefinition,
EntityConfiguration,
DatabaseAdapterFlavor,
CacheAdapterFlavor,
UUIDField,
} from '@expo/entity';
import { RedisCacheAdapterContext } from '@expo/entity-cache-adapter-redis';
import Redis from 'ioredis';
import Knex from 'knex';
import { URL } from 'url';
import { createFullIntegrationTestEntityCompanionProvider } from '../testfixtures/createFullIntegrationTestEntityCompanionProvider';
interface TestFields {
id: string;
other_string: string;
third_string: string;
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<
TestFields,
string,
ViewerContext,
TestEntity
> {
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
}
class TestEntity extends Entity<TestFields, string, ViewerContext> {
static getCompanionDefinition(): EntityCompanionDefinition<
TestFields,
string,
ViewerContext,
TestEntity,
TestEntityPrivacyPolicy
> {
return testEntityCompanion;
}
}
const testEntityConfiguration = new EntityConfiguration<TestFields>({
idField: 'id',
tableName: 'testentities',
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
other_string: new UUIDField({
columnName: 'other_string',
cache: true,
}),
third_string: new UUIDField({
columnName: 'third_string',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});
const testEntityCompanion = new EntityCompanionDefinition({
entityClass: TestEntity,
entityConfiguration: testEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
});
async function createOrTruncatePostgresTables(knex: Knex): Promise<void> {
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'); // for uuid_generate_v4()
await knex.schema.createTable('testentities', (table) => {
table.uuid('id').defaultTo(knex.raw('uuid_generate_v4()')).primary();
table.string('other_string').notNullable();
table.string('third_string').notNullable();
});
await knex.into('testentities').truncate();
}
async function dropPostgresTable(knex: Knex): Promise<void> {
if (await knex.schema.hasTable('children')) {
await knex.schema.dropTable('children');
}
if (await knex.schema.hasTable('parents')) {
await knex.schema.dropTable('parents');
}
}
describe('Entity cache inconsistency', () => {
let knexInstance: Knex;
let redisCacheAdapterContext: RedisCacheAdapterContext;
beforeAll(() => {
knexInstance = Knex({
client: 'pg',
connection: {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: 'localhost',
port: parseInt(process.env.PGPORT!, 10),
database: process.env.PGDATABASE,
},
});
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 createOrTruncatePostgresTables(knexInstance);
await redisCacheAdapterContext.redisClient.flushdb();
});
afterAll(async () => {
await dropPostgresTable(knexInstance);
await knexInstance.destroy();
redisCacheAdapterContext.redisClient.disconnect();
});
test('lots of updates in long-ish running transactions', async () => {
const viewerContext = new ViewerContext(
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
const entity1 = await TestEntity.creator(viewerContext)
.setField('other_string', 'hello')
.setField('third_string', 'initial')
.enforceCreateAsync();
// put entities in cache and dataloader
await TestEntity.loader(viewerContext).enforcing().loadByIDAsync(entity1.getID());
await TestEntity.loader(viewerContext)
.enforcing()
.loadByFieldEqualingAsync('other_string', 'hello');
let openBarrier1: () => void;
const barrier1 = new Promise((resolve) => {
openBarrier1 = resolve;
});
let openBarrier2: () => void;
const barrier2 = new Promise((resolve) => {
openBarrier2 = resolve;
});
await Promise.all([
// do a load after the transaction below updates the entity but before transaction commits to ensure
// that the cache is cleared after the transaction commits rather than in the middle where the changes
// may not be visible by other requests (ViewerContexts) which would cache the incorrect
// value during the read-through cache.
(async () => {
await barrier1;
const viewerContextInternal = new ViewerContext(
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
await TestEntity.loader(viewerContextInternal).enforcing().loadByIDAsync(entity1.getID());
openBarrier2!();
})(),
TestEntity.runInTransactionAsync(viewerContext, async (queryContext) => {
await TestEntity.updater(entity1, queryContext)
.setField('third_string', 'updated')
.enforceUpdateAsync();
openBarrier1();
// wait for to ensure the transaction isn't committed until after load above occurs
await barrier2;
}),
]);
// ensure cache consistency
const viewerContextLast = new ViewerContext(
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
const loadedById = await TestEntity.loader(viewerContextLast)
.enforcing()
.loadByIDAsync(entity1.getID());
const loadedByField = await TestEntity.loader(viewerContextLast)
.enforcing()
.loadByFieldEqualingAsync('other_string', 'hello');
expect(loadedById.getField('third_string')).toEqual('updated');
expect(loadedByField!.getField('third_string')).toEqual('updated');
});
});
@@ -0,0 +1,108 @@
import { ViewerContext } from '@expo/entity';
import { RedisCacheAdapterContext } from '@expo/entity-cache-adapter-redis';
import Redis from 'ioredis';
import Knex from 'knex';
import { URL } from 'url';
import { createFullIntegrationTestEntityCompanionProvider } from '../testfixtures/createFullIntegrationTestEntityCompanionProvider';
import ChildEntity from './entities/ChildEntity';
import ParentEntity from './entities/ParentEntity';
async function createOrTruncatePostgresTables(knex: Knex): Promise<void> {
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'); // for uuid_generate_v4()
await knex.schema.createTable('parents', (table) => {
table.uuid('id').defaultTo(knex.raw('uuid_generate_v4()')).primary();
});
await knex.into('parents').truncate();
await knex.schema.createTable('children', (table) => {
table.uuid('id').defaultTo(knex.raw('uuid_generate_v4()')).primary();
table.uuid('parent_id').references('id').inTable('parents').onDelete('cascade').unique();
});
await knex.into('children').truncate();
}
async function dropPostgresTable(knex: Knex): Promise<void> {
if (await knex.schema.hasTable('children')) {
await knex.schema.dropTable('children');
}
if (await knex.schema.hasTable('parents')) {
await knex.schema.dropTable('parents');
}
}
describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
let knexInstance: Knex;
let redisCacheAdapterContext: RedisCacheAdapterContext;
beforeAll(() => {
knexInstance = Knex({
client: 'pg',
connection: {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: 'localhost',
port: parseInt(process.env.PGPORT!, 10),
database: process.env.PGDATABASE,
},
});
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 createOrTruncatePostgresTables(knexInstance);
await redisCacheAdapterContext.redisClient.flushdb();
});
afterAll(async () => {
await dropPostgresTable(knexInstance);
await knexInstance.destroy();
redisCacheAdapterContext.redisClient.disconnect();
});
describe('EntityEdgeDeletionBehavior.INVALIDATE_CACHE', () => {
it('invalidates the cache', async () => {
const viewerContext = new ViewerContext(
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
const parent = await ParentEntity.creator(viewerContext).enforceCreateAsync();
const child = await ChildEntity.creator(viewerContext)
.setField('parent_id', parent.getID())
.enforceCreateAsync();
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
).resolves.not.toBeNull();
await expect(
ChildEntity.loader(viewerContext)
.enforcing()
.loadByFieldEqualingAsync('parent_id', parent.getID())
).resolves.not.toBeNull();
await ParentEntity.enforceDeleteAsync(parent);
await expect(
ParentEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(parent.getID())
).resolves.toBeNull();
await expect(
ChildEntity.loader(viewerContext).enforcing().loadByIDNullableAsync(child.getID())
).resolves.toBeNull();
});
});
});
@@ -10,10 +10,13 @@ import {
UUIDField,
EntityEdgeDeletionBehavior,
} from '@expo/entity';
import { RedisCacheAdapterContext } from '@expo/entity-cache-adapter-redis';
import Redis from 'ioredis';
import Knex from 'knex';
import { URL } from 'url';
import { v4 as uuidv4 } from 'uuid';
import { createKnexIntegrationTestEntityCompanionProvider } from '../testfixtures/createKnexIntegrationTestEntityCompanionProvider';
import { createFullIntegrationTestEntityCompanionProvider } from '../testfixtures/createFullIntegrationTestEntityCompanionProvider';
interface CategoryFields {
id: string;
@@ -64,7 +67,7 @@ const makeEntityClasses = async (knex: Knex, edgeDeletionBehavior: EntityEdgeDel
const categoryEntityConfiguration = new EntityConfiguration<CategoryFields>({
idField: 'id',
tableName: categoriesTableName,
inboundEdges: [OtherEntity],
getInboundEdges: () => [OtherEntity],
schema: {
id: new UUIDField({
columnName: 'id',
@@ -74,7 +77,7 @@ const makeEntityClasses = async (knex: Knex, edgeDeletionBehavior: EntityEdgeDel
columnName: 'parent_other_id',
cache: true,
association: {
associatedEntityClass: OtherEntity,
getAssociatedEntityClass: () => OtherEntity,
edgeDeletionBehavior,
},
}),
@@ -92,7 +95,7 @@ const makeEntityClasses = async (knex: Knex, edgeDeletionBehavior: EntityEdgeDel
const otherEntityConfiguration = new EntityConfiguration<OtherFields>({
idField: 'id',
tableName: othersTableName,
inboundEdges: [CategoryEntity],
getInboundEdges: () => [CategoryEntity],
schema: {
id: new UUIDField({
columnName: 'id',
@@ -102,7 +105,7 @@ const makeEntityClasses = async (knex: Knex, edgeDeletionBehavior: EntityEdgeDel
columnName: 'parent_category_id',
cache: true,
association: {
associatedEntityClass: CategoryEntity,
getAssociatedEntityClass: () => CategoryEntity,
edgeDeletionBehavior,
},
}),
@@ -154,6 +157,7 @@ const makeEntityClasses = async (knex: Knex, edgeDeletionBehavior: EntityEdgeDel
};
describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
let knexInstance: Knex;
let redisCacheAdapterContext: RedisCacheAdapterContext;
beforeAll(() => {
knexInstance = Knex({
@@ -166,10 +170,25 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
database: process.env.PGDATABASE,
},
});
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
};
});
afterAll(async () => {
knexInstance.destroy();
await knexInstance.destroy();
redisCacheAdapterContext.redisClient.disconnect();
});
it.each([
@@ -183,7 +202,7 @@ describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
);
const viewerContext = new ViewerContext(
createKnexIntegrationTestEntityCompanionProvider(knexInstance)
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
const category1 = await CategoryEntity.creator(viewerContext).enforceCreateAsync();
@@ -0,0 +1,65 @@
import {
AlwaysAllowPrivacyPolicyRule,
CacheAdapterFlavor,
DatabaseAdapterFlavor,
Entity,
EntityCompanionDefinition,
EntityConfiguration,
EntityEdgeDeletionBehavior,
EntityPrivacyPolicy,
UUIDField,
ViewerContext,
} from '@expo/entity';
import ParentEntity from './ParentEntity';
interface ChildFields {
id: string;
parent_id: string;
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<any, string, ViewerContext, any, any> {
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
}
export default class ChildEntity extends Entity<ChildFields, string, ViewerContext> {
static getCompanionDefinition(): EntityCompanionDefinition<
ChildFields,
string,
ViewerContext,
ChildEntity,
TestEntityPrivacyPolicy
> {
return childEntityCompanion;
}
}
const childEntityConfiguration = new EntityConfiguration<ChildFields>({
idField: 'id',
tableName: 'children',
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
parent_id: new UUIDField({
columnName: 'parent_id',
cache: true,
association: {
getAssociatedEntityClass: () => ParentEntity,
edgeDeletionBehavior: EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE,
},
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});
const childEntityCompanion = new EntityCompanionDefinition({
entityClass: ChildEntity,
entityConfiguration: childEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
});
@@ -0,0 +1,56 @@
import {
AlwaysAllowPrivacyPolicyRule,
CacheAdapterFlavor,
DatabaseAdapterFlavor,
Entity,
EntityCompanionDefinition,
EntityConfiguration,
EntityPrivacyPolicy,
UUIDField,
ViewerContext,
} from '@expo/entity';
import ChildEntity from './ChildEntity';
interface ParentFields {
id: string;
}
class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<any, string, ViewerContext, any, any> {
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
}
export default class ParentEntity extends Entity<ParentFields, string, ViewerContext> {
static getCompanionDefinition(): EntityCompanionDefinition<
ParentFields,
string,
ViewerContext,
ParentEntity,
TestEntityPrivacyPolicy
> {
return parentEntityCompanion;
}
}
const parentEntityConfiguration = new EntityConfiguration<ParentFields>({
idField: 'id',
tableName: 'parents',
getInboundEdges: () => [ChildEntity],
schema: {
id: new UUIDField({
columnName: 'id',
cache: true,
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});
const parentEntityCompanion = new EntityCompanionDefinition({
entityClass: ParentEntity,
entityConfiguration: parentEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
});
@@ -0,0 +1,37 @@
import {
NoOpEntityMetricsAdapter,
IEntityMetricsAdapter,
EntityCompanionProvider,
CacheAdapterFlavor,
DatabaseAdapterFlavor,
} from '@expo/entity';
import {
RedisCacheAdapterContext,
RedisCacheAdapterProvider,
} from '@expo/entity-cache-adapter-redis';
import {
PostgresEntityDatabaseAdapterProvider,
PostgresEntityQueryContextProvider,
} from '@expo/entity-database-adapter-knex';
import Knex from 'knex';
export const createFullIntegrationTestEntityCompanionProvider = (
knex: Knex,
redisCacheAdapterContext: RedisCacheAdapterContext,
metricsAdapter: IEntityMetricsAdapter = new NoOpEntityMetricsAdapter()
): EntityCompanionProvider => {
return new EntityCompanionProvider(
metricsAdapter,
{
[DatabaseAdapterFlavor.POSTGRES]: {
adapterProvider: new PostgresEntityDatabaseAdapterProvider(),
queryContextProvider: new PostgresEntityQueryContextProvider(knex),
},
},
{
[CacheAdapterFlavor.REDIS]: {
cacheAdapterProvider: new RedisCacheAdapterProvider(redisCacheAdapterContext),
},
}
);
};
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
},
"include": ["src"]
}
+82
View File
@@ -3,6 +3,88 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
### Bug Fixes
* fix null handling in load paths (try 2) ([#96](https://github.com/expo/entity/issues/96)) ([d960329](https://github.com/expo/entity/commit/d9603298e0cc5c3c2b0cca959876e9195555fb57))
## [0.9.1](https://github.com/expo/entity/compare/v0.9.0...v0.9.1) (2020-11-06)
### Bug Fixes
* edge deletion behavior cyclic import structure again ([#93](https://github.com/expo/entity/issues/93)) ([012c05d](https://github.com/expo/entity/commit/012c05d62d2ecd559eb331c6ca358052bfe66dc8))
# [0.9.0](https://github.com/expo/entity/compare/v0.8.1...v0.9.0) (2020-11-05)
### Bug Fixes
* edge deletion behavior cyclic import structure ([#89](https://github.com/expo/entity/issues/89)) ([cca8fc1](https://github.com/expo/entity/commit/cca8fc19870057ebc7033081b59fab4a142fbf1a))
### Features
* add ability to invalidate an entity manually ([#91](https://github.com/expo/entity/issues/91)) ([d1a6f13](https://github.com/expo/entity/commit/d1a6f1339648942f12b3fb605eb329aefe5cdd63))
## [0.8.1](https://github.com/expo/entity/compare/v0.8.0...v0.8.1) (2020-10-09)
**Note:** Version bump only for package @expo/entity
# [0.8.0](https://github.com/expo/entity/compare/v0.7.1...v0.8.0) (2020-10-09)
### Features
* add entityClassName to metrics adapter events ([#83](https://github.com/expo/entity/issues/83)) ([f4cdc01](https://github.com/expo/entity/commit/f4cdc01c6cb8868c24ce3da2274f0901687e05bd))
## [0.7.1](https://github.com/expo/entity/compare/v0.7.0...v0.7.1) (2020-10-01)
### Bug Fixes
* correct type of runInTransactionAsync ([#81](https://github.com/expo/entity/issues/81)) ([f98f7d1](https://github.com/expo/entity/commit/f98f7d14b6149a496a7a3e97dd63d89b2842cc26))
# [0.7.0](https://github.com/expo/entity/compare/v0.6.0...v0.7.0) (2020-10-01)
### Bug Fixes
* fix incorrect loader types for field selecitons ([#72](https://github.com/expo/entity/issues/72)) ([cd2df8d](https://github.com/expo/entity/commit/cd2df8da4b81bcd4769e950d3124c18886df2816))
* fix index export for EntityMutationTrigger and EntityMutationValidator ([#73](https://github.com/expo/entity/issues/73)) ([7e829f3](https://github.com/expo/entity/commit/7e829f30fde0bd343cb8a584ccda5530cf61ec33))
* move cache invalidation to after transaction commit ([#77](https://github.com/expo/entity/issues/77)) ([dbc3c81](https://github.com/expo/entity/commit/dbc3c81b359a668b5134189ce6de49b5893bb5e7))
* upgrade packages ([#78](https://github.com/expo/entity/issues/78)) ([9891e74](https://github.com/expo/entity/commit/9891e7469467a28589f529c8d87b10fc2232d3ff))
# [0.6.0](https://github.com/expo/entity/compare/v0.5.2...v0.6.0) (2020-07-23)
+5 -5
View File
@@ -1,6 +1,6 @@
{
"name": "@expo/entity",
"version": "0.6.0",
"version": "0.10.0",
"description": "A privacy-first data model",
"files": [
"build",
@@ -12,9 +12,9 @@
"tsc": "tsc",
"clean": "rm -rf build coverage coverage-integration",
"lint": "eslint src --ext '.ts'",
"test": "jest --rootDir --config ../../resources/jest.config.js",
"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",
"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": {
@@ -26,10 +26,10 @@
"author": "Expo",
"license": "MIT",
"dependencies": {
"@expo/results": "^0.3.0",
"@expo/results": "^1.0.0",
"dataloader": "^2.0.0",
"es6-error": "^4.1.1",
"invariant": "^2.2.4",
"uuid": "^7.0.3"
"uuid": "^8.3.0"
}
}
+4 -4
View File
@@ -38,7 +38,7 @@ export default class EnforcingEntityLoader<
* Enforcing version of entity loader method by the same name.
* @throws {@link EntityNotAuthorizedError} when viewer is not authorized to view one or more of the returned entities
*/
async loadManyByFieldEqualingManyAsync<N extends keyof TFields>(
async loadManyByFieldEqualingManyAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly TEntity[]>> {
@@ -55,7 +55,7 @@ export default class EnforcingEntityLoader<
* Enforcing version of entity loader method by the same name.
* @throws {@link EntityNotAuthorizedError} when viewer is not authorized to view one or more of the returned entities
*/
async loadManyByFieldEqualingAsync<N extends keyof TFields>(
async loadManyByFieldEqualingAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldName: N,
fieldValue: NonNullable<TFields[N]>
): Promise<readonly TEntity[]> {
@@ -71,7 +71,7 @@ export default class EnforcingEntityLoader<
* @throws {@link EntityNotAuthorizedError} when viewer is not authorized to view the returned entity
* @throws when multiple entities are found matching the condition
*/
async loadByFieldEqualingAsync<N extends keyof TFields>(
async loadByFieldEqualingAsync<N extends keyof Pick<TFields, TSelectedFields>>(
uniqueFieldName: N,
fieldValue: NonNullable<TFields[N]>
): Promise<TEntity | null> {
@@ -114,7 +114,7 @@ export default class EnforcingEntityLoader<
* Enforcing version of entity loader method by the same name.
* @throws {@link EntityNotAuthorizedError} when viewer is not authorized to view one or more of the returned entities
*/
async loadManyByFieldEqualityConjunctionAsync<N extends keyof TFields>(
async loadManyByFieldEqualityConjunctionAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldEqualityOperands: FieldEqualityCondition<TFields, N>[],
querySelectionModifiers: QuerySelectionModifiers<TFields> = {}
): Promise<readonly TEntity[]> {
+33 -8
View File
@@ -23,6 +23,7 @@ export default class EntityAssociationLoader<
* @param queryContext - query context in which to perform the load
*/
async loadAssociatedEntityAsync<
TIdentifyingField extends keyof Pick<TFields, TSelectedFields>,
TAssociatedFields,
TAssociatedID,
TAssociatedEntity extends ReadonlyEntity<
@@ -40,7 +41,7 @@ export default class EntityAssociationLoader<
>,
TAssociatedSelectedFields extends keyof TAssociatedFields = keyof TAssociatedFields
>(
fieldIdentifyingAssociatedEntity: keyof Pick<TFields, TSelectedFields>,
fieldIdentifyingAssociatedEntity: TIdentifyingField,
associatedEntityClass: IEntityClass<
TAssociatedFields,
TAssociatedID,
@@ -54,14 +55,25 @@ export default class EntityAssociationLoader<
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getQueryContextProvider()
.getQueryContext()
): Promise<Result<TAssociatedEntity>> {
): Promise<
Result<null extends TFields[TIdentifyingField] ? TAssociatedEntity | null : TAssociatedEntity>
> {
const associatedEntityID = this.entity.getField(fieldIdentifyingAssociatedEntity);
if (!associatedEntityID) {
return result(null) as Result<
null extends TFields[TIdentifyingField] ? TAssociatedEntity | null : TAssociatedEntity
>;
}
const loader = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
.getLoaderFactory()
.forLoad(queryContext);
return await loader.loadByIDAsync((associatedEntityID as unknown) as TAssociatedID);
return (await loader.loadByIDAsync((associatedEntityID as unknown) as TAssociatedID)) as Result<
null extends TFields[TIdentifyingField] ? TAssociatedEntity | null : TAssociatedEntity
>;
}
/**
@@ -99,7 +111,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityFieldContainingThisID: keyof TAssociatedFields,
associatedEntityFieldContainingThisID: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -153,7 +165,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityLookupByField: keyof TAssociatedFields,
associatedEntityLookupByField: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -161,6 +173,9 @@ export default class EntityAssociationLoader<
.getQueryContext()
): Promise<Result<TAssociatedEntity> | null> {
const associatedFieldValue = this.entity.getField(fieldIdentifyingAssociatedEntity);
if (!associatedFieldValue) {
return null;
}
const loader = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -207,7 +222,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityLookupByField: keyof TAssociatedFields,
associatedEntityLookupByField: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -215,6 +230,10 @@ export default class EntityAssociationLoader<
.getQueryContext()
): Promise<readonly Result<TAssociatedEntity>[]> {
const associatedFieldValue = this.entity.getField(fieldIdentifyingAssociatedEntity);
if (!associatedFieldValue) {
return [];
}
const loader = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -424,13 +443,19 @@ export default class EntityAssociationLoader<
queryContext
);
} else {
associatedEntityResult = await currentEntity
const associatedEntityResultLocal = await currentEntity
.associationLoader()
.loadAssociatedEntityAsync(
fieldIdentifyingAssociatedEntity,
associatedEntityClass,
queryContext
);
if (associatedEntityResultLocal.ok && associatedEntityResultLocal.value === null) {
associatedEntityResult = null;
} else {
associatedEntityResult = associatedEntityResultLocal;
}
}
if (!associatedEntityResult) {
@@ -491,5 +516,5 @@ export interface EntityLoadThroughDirective<
* Field by which to load the instance of associatedEntityClass. If not provided, the
* associatedEntityClass instance is fetched by its ID.
*/
associatedEntityLookupByField?: keyof TAssociatedFields;
associatedEntityLookupByField?: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>;
}
+11 -4
View File
@@ -4,7 +4,7 @@ import EntityMutationTriggerConfiguration from './EntityMutationTriggerConfigura
import EntityMutationValidator from './EntityMutationValidator';
import EntityMutatorFactory from './EntityMutatorFactory';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import IEntityQueryContextProvider from './IEntityQueryContextProvider';
import EntityQueryContextProvider from './EntityQueryContextProvider';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
import EntityTableDataCoordinator from './internal/EntityTableDataCoordinator';
@@ -76,8 +76,15 @@ export default class EntityCompanion<
metricsAdapter: IEntityMetricsAdapter
) {
const privacyPolicy = new PrivacyPolicyClass();
this.entityLoaderFactory = new EntityLoaderFactory(
tableDataCoordinator.entityConfiguration.idField,
this.entityLoaderFactory = new EntityLoaderFactory<
TFields,
TID,
TViewerContext,
TEntity,
TPrivacyPolicy,
TSelectedFields
>(
tableDataCoordinator.entityConfiguration.idField as keyof Pick<TFields, TSelectedFields>,
entityClass,
privacyPolicy,
tableDataCoordinator.dataManager
@@ -119,7 +126,7 @@ export default class EntityCompanion<
/**
* Get the query context provider for this entity.
*/
getQueryContextProvider(): IEntityQueryContextProvider {
getQueryContextProvider(): EntityQueryContextProvider {
return this.tableDataCoordinator.getQueryContextProvider();
}
}
@@ -4,9 +4,9 @@ import EntityConfiguration from './EntityConfiguration';
import EntityMutationTriggerConfiguration from './EntityMutationTriggerConfiguration';
import EntityMutationValidator from './EntityMutationValidator';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import EntityQueryContextProvider from './EntityQueryContextProvider';
import IEntityCacheAdapterProvider from './IEntityCacheAdapterProvider';
import IEntityDatabaseAdapterProvider from './IEntityDatabaseAdapterProvider';
import IEntityQueryContextProvider from './IEntityQueryContextProvider';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
import EntityTableDataCoordinator from './internal/EntityTableDataCoordinator';
@@ -43,7 +43,7 @@ export enum CacheAdapterFlavor {
*/
export interface DatabaseAdapterFlavorDefinition {
adapterProvider: IEntityDatabaseAdapterProvider;
queryContextProvider: IEntityQueryContextProvider;
queryContextProvider: EntityQueryContextProvider;
}
/**
@@ -211,7 +211,8 @@ export default class EntityCompanionProvider {
>
): EntityCompanion<TFields, TID, TViewerContext, TEntity, TPrivacyPolicy, TSelectedFields> {
const tableDataCoordinator = this.getTableDataCoordinatorForEntity(
entityCompanionDefinition.entityConfiguration
entityCompanionDefinition.entityConfiguration,
entityClass.name
);
return computeIfAbsent(this.companionMap, entityClass.name, () => {
return new EntityCompanion(
@@ -226,7 +227,8 @@ export default class EntityCompanionProvider {
}
private getTableDataCoordinatorForEntity<TFields>(
entityConfiguration: EntityConfiguration<TFields>
entityConfiguration: EntityConfiguration<TFields>,
entityClassName: string
): EntityTableDataCoordinator<TFields> {
return computeIfAbsent(this.tableDataCoordinatorMap, entityConfiguration.tableName, () => {
const entityDatabaseAdapterFlavor = this.databaseAdapterFlavors[
@@ -240,7 +242,8 @@ export default class EntityCompanionProvider {
entityDatabaseAdapterFlavor.adapterProvider,
entityCacheAdapterFlavor.cacheAdapterProvider,
entityDatabaseAdapterFlavor.queryContextProvider,
this.metricsAdapter
this.metricsAdapter,
entityClassName
);
});
}
+4 -4
View File
@@ -13,7 +13,7 @@ export default class EntityConfiguration<TFields> {
readonly cacheableKeys: ReadonlySet<keyof TFields>;
readonly cacheKeyVersion: number;
readonly inboundEdges: IEntityClass<any, any, any, any, any, any>[];
readonly getInboundEdges: () => IEntityClass<any, any, any, any, any, any>[];
readonly schema: ReadonlyMap<keyof TFields, EntityFieldDefinition>;
readonly entityToDBFieldsKeyMapping: ReadonlyMap<keyof TFields, string>;
readonly dbToEntityFieldsKeyMapping: ReadonlyMap<string, keyof TFields>;
@@ -25,7 +25,7 @@ export default class EntityConfiguration<TFields> {
idField,
tableName,
schema,
inboundEdges = [],
getInboundEdges = () => [],
cacheKeyVersion = 0,
databaseAdapterFlavor,
cacheAdapterFlavor,
@@ -33,7 +33,7 @@ export default class EntityConfiguration<TFields> {
idField: keyof TFields;
tableName: string;
schema: Record<keyof TFields, EntityFieldDefinition>;
inboundEdges?: IEntityClass<any, any, any, any, any, any>[];
getInboundEdges?: () => IEntityClass<any, any, any, any, any, any>[];
cacheKeyVersion?: number;
databaseAdapterFlavor: DatabaseAdapterFlavor;
cacheAdapterFlavor: CacheAdapterFlavor;
@@ -44,7 +44,7 @@ export default class EntityConfiguration<TFields> {
this.databaseAdapterFlavor = databaseAdapterFlavor;
this.cacheAdapterFlavor = cacheAdapterFlavor;
this.inboundEdges = inboundEdges;
this.getInboundEdges = getInboundEdges;
// external schema is a Record to typecheck that all fields have FieldDefinitions,
// but internally the most useful representation is a map for lookups
+1 -1
View File
@@ -50,7 +50,7 @@ export interface EntityAssociationDefinition<
/**
* Class of entity on the other end of this edge.
*/
associatedEntityClass: IEntityClass<
getAssociatedEntityClass: () => IEntityClass<
TAssociatedFields,
TAssociatedID,
TViewerContext,
+87 -40
View File
@@ -33,7 +33,7 @@ export default class EntityLoader<
constructor(
private readonly viewerContext: TViewerContext,
private readonly queryContext: EntityQueryContext,
private readonly idField: keyof TFields,
private readonly idField: keyof Pick<TFields, TSelectedFields>,
private readonly entityClass: IEntityClass<
TFields,
TID,
@@ -69,30 +69,38 @@ export default class EntityLoader<
* @returns map from fieldValue to entity results that match the query for that fieldValue,
* where result errors can be UnauthorizedError
*/
async loadManyByFieldEqualingManyAsync<N extends keyof TFields>(
async loadManyByFieldEqualingManyAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Result<TEntity>[]>> {
const fieldValuesToObjects = await this.dataManager.loadManyByFieldEqualingAsync(
const fieldValuesToFieldObjects = await this.dataManager.loadManyByFieldEqualingAsync(
this.queryContext,
fieldName,
fieldValues
);
const fieldValuesToUncheckedEntities = mapMap(fieldValuesToObjects, (v) => {
return v.map((obj) => {
return new this.entityClass(this.viewerContext, obj);
});
});
const fieldValuesToUncheckedEntityResults = mapMap(fieldValuesToFieldObjects, (fieldObjects) =>
this.tryConstructEntities(fieldObjects)
);
return await mapMapAsync(fieldValuesToUncheckedEntities, async (v) => {
return await Promise.all(
v.map((entity) =>
asyncResult(
this.privacyPolicy.authorizeReadAsync(this.viewerContext, this.queryContext, entity)
)
)
);
});
return await mapMapAsync(
fieldValuesToUncheckedEntityResults,
async (uncheckedEntityResults) => {
return await Promise.all(
uncheckedEntityResults.map(async (uncheckedEntityResult) => {
if (!uncheckedEntityResult.ok) {
return uncheckedEntityResult;
}
return await asyncResult(
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
uncheckedEntityResult.value
)
);
})
);
}
);
}
/**
@@ -101,7 +109,7 @@ export default class EntityLoader<
* @param fieldValue - fieldName field value being queried
* @returns array of entity results that match the query for fieldValue, where result error can be UnauthorizedError
*/
async loadManyByFieldEqualingAsync<N extends keyof TFields>(
async loadManyByFieldEqualingAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldName: N,
fieldValue: NonNullable<TFields[N]>
): Promise<readonly Result<TEntity>[]> {
@@ -121,7 +129,7 @@ export default class EntityLoader<
* @returns entity result where uniqueFieldName equals fieldValue, or null if no entity matches the condition.
* @throws when multiple entities match the condition
*/
async loadByFieldEqualingAsync<N extends keyof TFields>(
async loadByFieldEqualingAsync<N extends keyof Pick<TFields, TSelectedFields>>(
uniqueFieldName: N,
fieldValue: NonNullable<TFields[N]>
): Promise<Result<TEntity> | null> {
@@ -189,25 +197,29 @@ export default class EntityLoader<
* @param querySelectionModifiers - limit, offset, and orderBy for the query
* @returns array of entity results that match the query, where result error can be UnauthorizedError
*/
async loadManyByFieldEqualityConjunctionAsync<N extends keyof TFields>(
async loadManyByFieldEqualityConjunctionAsync<N extends keyof Pick<TFields, TSelectedFields>>(
fieldEqualityOperands: FieldEqualityCondition<TFields, N>[],
querySelectionModifiers: QuerySelectionModifiers<TFields> = {}
): Promise<readonly Result<TEntity>[]> {
const fieldValuesArray = await this.dataManager.loadManyByFieldEqualityConjunctionAsync(
const fieldObjects = await this.dataManager.loadManyByFieldEqualityConjunctionAsync(
this.queryContext,
fieldEqualityOperands,
querySelectionModifiers
);
const uncheckedEntities = fieldValuesArray.map((obj) => {
return new this.entityClass(this.viewerContext, obj);
});
const uncheckedEntityResults = this.tryConstructEntities(fieldObjects);
return await Promise.all(
uncheckedEntities.map(async (entity) =>
asyncResult(
this.privacyPolicy.authorizeReadAsync(this.viewerContext, this.queryContext, entity)
)
)
uncheckedEntityResults.map(async (uncheckedEntityResult) => {
if (!uncheckedEntityResult.ok) {
return uncheckedEntityResult;
}
return await asyncResult(
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
uncheckedEntityResult.value
)
);
})
);
}
@@ -241,22 +253,26 @@ export default class EntityLoader<
bindings: any[] | object,
querySelectionModifiers: QuerySelectionModifiers<TFields> = {}
): Promise<readonly Result<TEntity>[]> {
const fieldValuesArray = await this.dataManager.loadManyByRawWhereClauseAsync(
const fieldObjects = await this.dataManager.loadManyByRawWhereClauseAsync(
this.queryContext,
rawWhereClause,
bindings,
querySelectionModifiers
);
const uncheckedEntities = fieldValuesArray.map((obj) => {
return new this.entityClass(this.viewerContext, obj);
});
const uncheckedEntityResults = this.tryConstructEntities(fieldObjects);
return await Promise.all(
uncheckedEntities.map(async (entity) =>
asyncResult(
this.privacyPolicy.authorizeReadAsync(this.viewerContext, this.queryContext, entity)
)
)
uncheckedEntityResults.map(async (uncheckedEntityResult) => {
if (!uncheckedEntityResult.ok) {
return uncheckedEntityResult;
}
return await asyncResult(
this.privacyPolicy.authorizeReadAsync(
this.viewerContext,
this.queryContext,
uncheckedEntityResult.value
)
);
})
);
}
@@ -267,4 +283,35 @@ export default class EntityLoader<
async invalidateFieldsAsync(objectFields: Readonly<TFields>): Promise<void> {
await this.dataManager.invalidateObjectFieldsAsync(objectFields);
}
async invalidateFieldsTransactionalAsync(objectFields: Readonly<TFields>): Promise<void> {
await this.dataManager.invalidateObjectFieldsTransactionalAsync(
this.queryContext,
objectFields
);
}
/**
* Invalidate all caches for an entity. One potential use case would be to keep the entity
* framework in sync with changes made to data outside of the framework.
* @param entity - entity to be invalidated
*/
async invalidateEntityAsync(entity: TEntity): Promise<void> {
await this.invalidateFieldsAsync(entity.getAllDatabaseFields());
}
/**
* Wrap entity construction in try/catch and return Results. Used to prevent constructor errors
* thrown for one entity instantiation from interfering with other entity instantiatons of the same query.
* @param fieldsObjects - array of entity data objects to construct entities
*/
private tryConstructEntities(fieldsObjects: readonly TFields[]): readonly Result<TEntity>[] {
return fieldsObjects.map((fieldsObject) => {
try {
return result(new this.entityClass(this.viewerContext, fieldsObject));
} catch (e) {
return result(e);
}
});
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ export default class EntityLoaderFactory<
TSelectedFields extends keyof TFields
> {
constructor(
private readonly idField: keyof TFields,
private readonly idField: keyof Pick<TFields, TSelectedFields>,
private readonly entityClass: IEntityClass<
TFields,
TID,
@@ -54,7 +54,13 @@ export default interface EntityMutationTriggerConfiguration<
* after any mutation (create, update, delete). If the call to the mutation is wrapped in a transaction,
* this too will be within the transaction.
*/
afterCommit?: EntityMutationTrigger<TFields, TID, TViewerContext, TEntity, TSelectedFields>[];
afterCommit?: EntityNonTransactionalMutationTrigger<
TFields,
TID,
TViewerContext,
TEntity,
TSelectedFields
>[];
}
/**
@@ -75,3 +81,17 @@ export abstract class EntityMutationTrigger<
entity: TEntity
): Promise<void>;
}
/**
* A non-transactional trigger is like a {@link EntityMutationTrigger} but used for afterCommit triggers
* since they explicitly occur outside of the transaction.
*/
export abstract class EntityNonTransactionalMutationTrigger<
TFields,
TID,
TViewerContext extends ViewerContext,
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
TSelectedFields extends keyof TFields = keyof TFields
> {
abstract async executeAsync(viewerContext: TViewerContext, entity: TEntity): Promise<void>;
}
+84 -47
View File
@@ -8,6 +8,7 @@ import { EntityEdgeDeletionBehavior } from './EntityFields';
import EntityLoaderFactory from './EntityLoaderFactory';
import EntityMutationTriggerConfiguration, {
EntityMutationTrigger,
EntityNonTransactionalMutationTrigger,
} from './EntityMutationTriggerConfiguration';
import EntityMutationValidator from './EntityMutationValidator';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
@@ -88,6 +89,24 @@ abstract class BaseMutator<
)
);
}
protected async executeNonTransactionalMutationTriggersOrValidatorsAsync(
triggers:
| EntityNonTransactionalMutationTrigger<
TFields,
TID,
TViewerContext,
TEntity,
TSelectedFields
>[]
| undefined,
entity: TEntity
): Promise<void> {
if (!triggers) {
return;
}
await Promise.all(triggers.map((trigger) => trigger.executeAsync(this.viewerContext, entity)));
}
}
/**
@@ -127,7 +146,8 @@ export class CreateMutator<
async createAsync(): Promise<Result<TEntity>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.CREATE
EntityMetricsMutationType.CREATE,
this.entityClass.name
)(this.createInTransactionAsync());
}
@@ -139,17 +159,9 @@ export class CreateMutator<
}
private async createInTransactionAsync(): Promise<Result<TEntity>> {
const internalResult = await this.queryContext.runInTransactionIfNotInTransactionAsync(
(innerQueryContext) => this.createInternalAsync(innerQueryContext)
return await this.queryContext.runInTransactionIfNotInTransactionAsync((innerQueryContext) =>
this.createInternalAsync(innerQueryContext)
);
if (internalResult.ok) {
await this.executeMutationTriggersOrValidatorsAsync(
this.mutationTriggers.afterCommit,
this.queryContext,
internalResult.value
);
}
return internalResult;
}
private async createInternalAsync(
@@ -190,7 +202,10 @@ export class CreateMutator<
const insertResult = await this.databaseAdapter.insertAsync(queryContext, this.fieldsForEntity);
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
await entityLoader.invalidateFieldsAsync(insertResult);
queryContext.appendPostCommitCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, insertResult)
);
await entityLoader.invalidateFieldsTransactionalAsync(insertResult);
const unauthorizedEntityAfterInsert = new this.entityClass(this.viewerContext, insertResult);
const newEntity = await entityLoader
@@ -208,6 +223,14 @@ export class CreateMutator<
newEntity
);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
newEntity
)
);
return result(newEntity);
}
}
@@ -308,7 +331,8 @@ export class UpdateMutator<
async updateAsync(): Promise<Result<TEntity>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.UPDATE
EntityMetricsMutationType.UPDATE,
this.entityClass.name
)(this.updateInTransactionAsync());
}
@@ -320,17 +344,9 @@ export class UpdateMutator<
}
private async updateInTransactionAsync(): Promise<Result<TEntity>> {
const internalResult = await this.queryContext.runInTransactionIfNotInTransactionAsync(
(innerQueryContext) => this.updateInternalAsync(innerQueryContext)
return await this.queryContext.runInTransactionIfNotInTransactionAsync((innerQueryContext) =>
this.updateInternalAsync(innerQueryContext)
);
if (internalResult.ok) {
await this.executeMutationTriggersOrValidatorsAsync(
this.mutationTriggers.afterCommit,
this.queryContext,
internalResult.value
);
}
return internalResult;
}
private async updateInternalAsync(
@@ -373,8 +389,17 @@ export class UpdateMutator<
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
await entityLoader.invalidateFieldsAsync(this.originalFieldsForEntity);
await entityLoader.invalidateFieldsAsync(this.fieldsForEntity);
queryContext.appendPostCommitCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, this.originalFieldsForEntity)
);
queryContext.appendPostCommitCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, this.fieldsForEntity)
);
await Promise.all([
entityLoader.invalidateFieldsTransactionalAsync(this.originalFieldsForEntity),
entityLoader.invalidateFieldsTransactionalAsync(this.fieldsForEntity),
]);
const unauthorizedEntityAfterUpdate = new this.entityClass(this.viewerContext, updateResult);
const updatedEntity = await entityLoader
@@ -392,6 +417,14 @@ export class UpdateMutator<
updatedEntity
);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
updatedEntity
)
);
return result(updatedEntity);
}
}
@@ -473,7 +506,8 @@ export class DeleteMutator<
async deleteAsync(): Promise<Result<void>> {
return await timeAndLogMutationEventAsync(
this.metricsAdapter,
EntityMetricsMutationType.DELETE
EntityMetricsMutationType.DELETE,
this.entityClass.name
)(this.deleteInTransactionAsync());
}
@@ -488,29 +522,20 @@ export class DeleteMutator<
processedEntityIdentifiersFromTransitiveDeletions: Set<string> = new Set(),
skipDatabaseDeletion: boolean = false
): Promise<Result<void>> {
const internalResult = await this.queryContext.runInTransactionIfNotInTransactionAsync(
(innerQueryContext) =>
this.deleteInternalAsync(
innerQueryContext,
processedEntityIdentifiersFromTransitiveDeletions,
skipDatabaseDeletion
)
return await this.queryContext.runInTransactionIfNotInTransactionAsync((innerQueryContext) =>
this.deleteInternalAsync(
innerQueryContext,
processedEntityIdentifiersFromTransitiveDeletions,
skipDatabaseDeletion
)
);
if (internalResult.ok) {
await this.executeMutationTriggersOrValidatorsAsync(
this.mutationTriggers.afterCommit,
this.queryContext,
internalResult.value
);
}
return internalResult.ok ? result() : result(internalResult.reason);
}
private async deleteInternalAsync(
queryContext: EntityTransactionalQueryContext,
processedEntityIdentifiersFromTransitiveDeletions: Set<string>,
skipDatabaseDeletion: boolean
): Promise<Result<TEntity>> {
): Promise<Result<void>> {
const authorizeDeleteResult = await asyncResult(
this.privacyPolicy.authorizeDeleteAsync(this.viewerContext, queryContext, this.entity)
);
@@ -544,7 +569,10 @@ export class DeleteMutator<
}
const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
await entityLoader.invalidateFieldsAsync(this.entity.getAllDatabaseFields());
queryContext.appendPostCommitCallback(
entityLoader.invalidateFieldsAsync.bind(entityLoader, this.entity.getAllDatabaseFields())
);
await entityLoader.invalidateFieldsTransactionalAsync(this.entity.getAllDatabaseFields());
await this.executeMutationTriggersOrValidatorsAsync(
this.mutationTriggers.afterDelete,
@@ -557,7 +585,15 @@ export class DeleteMutator<
this.entity
);
return result(this.entity);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
this.entity
)
);
return result();
}
/**
@@ -600,7 +636,7 @@ export class DeleteMutator<
TMSelectedFields
>;
const entityConfiguration = companionDefinition.entityConfiguration;
const inboundEdges = entityConfiguration.inboundEdges;
const inboundEdges = entityConfiguration.getInboundEdges();
await Promise.all(
inboundEdges.map(async (entityClass) => {
return await mapMapAsync(
@@ -611,8 +647,9 @@ export class DeleteMutator<
return;
}
const associatedConfiguration = association.associatedEntityClass.getCompanionDefinition()
.entityConfiguration;
const associatedConfiguration = association
.getAssociatedEntityClass()
.getCompanionDefinition().entityConfiguration;
if (associatedConfiguration !== entityConfiguration) {
return;
}
+26 -12
View File
@@ -1,4 +1,8 @@
import IEntityQueryContextProvider from './IEntityQueryContextProvider';
import { v4 as uuidv4 } from 'uuid';
import EntityQueryContextProvider from './EntityQueryContextProvider';
export type PostCommitCallback = (...args: any) => Promise<any>;
/**
* Entity framework representation of transactional and non-transactional database
@@ -10,7 +14,7 @@ import IEntityQueryContextProvider from './IEntityQueryContextProvider';
export abstract class EntityQueryContext {
constructor(private readonly queryInterface: any) {}
abstract isInTransaction(): boolean;
abstract isInTransaction(): this is EntityTransactionalQueryContext;
getQueryInterface(): any {
return this.queryInterface;
@@ -24,33 +28,43 @@ export abstract class EntityQueryContext {
export class EntityNonTransactionalQueryContext extends EntityQueryContext {
constructor(
queryInterface: any,
private readonly entityQueryContextProvider: IEntityQueryContextProvider
private readonly entityQueryContextProvider: EntityQueryContextProvider
) {
super(queryInterface);
}
isInTransaction(): boolean {
isInTransaction(): this is EntityTransactionalQueryContext {
return false;
}
async runInTransactionIfNotInTransactionAsync<T>(
transactionScope: (queryContext: EntityQueryContext) => Promise<T>
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
if (this.isInTransaction()) {
return await transactionScope(this);
} else {
return await this.entityQueryContextProvider.runInTransactionAsync(transactionScope);
}
return await this.entityQueryContextProvider.runInTransactionAsync(transactionScope);
}
}
export class EntityTransactionalQueryContext extends EntityQueryContext {
isInTransaction(): boolean {
public readonly transactionID = uuidv4();
private readonly postCommitCallbacks: PostCommitCallback[] = [];
public appendPostCommitCallback(callback: PostCommitCallback): void {
this.postCommitCallbacks.push(callback);
}
public async runPostCommitCallbacksAsync(): Promise<void> {
const callbacks = [...this.postCommitCallbacks];
this.postCommitCallbacks.length = 0;
await Promise.all(callbacks.map((callback) => callback()));
}
isInTransaction(): this is EntityTransactionalQueryContext {
return true;
}
async runInTransactionIfNotInTransactionAsync<T>(
transactionScope: (queryContext: EntityQueryContext) => Promise<T>
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
return await transactionScope(this);
}
@@ -0,0 +1,46 @@
import {
EntityTransactionalQueryContext,
EntityNonTransactionalQueryContext,
} from './EntityQueryContext';
/**
* A query context provider vends transactional and non-transactional query contexts.
*/
export default abstract class EntityQueryContextProvider {
/**
* Vend a regular (non-transactional) entity query context.
*/
public getQueryContext(): EntityNonTransactionalQueryContext {
return new EntityNonTransactionalQueryContext(this.getQueryInterface(), this);
}
/**
* Get the query interface for constructing a query context.
*/
protected abstract getQueryInterface(): any;
/**
* Vend a transaction runner for use in {@link runInTransactionAsync}.
*/
protected abstract createTransactionRunner<T>(): (
transactionScope: (queryInterface: any) => Promise<T>
) => Promise<T>;
/**
* Start a transaction and execute the provided transaction-scoped closure within the transaction.
* @param transactionScope - async callback to execute within the transaction
*/
async runInTransactionAsync<T>(
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
const [returnedValue, queryContext] = await this.createTransactionRunner<
[T, EntityTransactionalQueryContext]
>()(async (queryInterface) => {
const queryContext = new EntityTransactionalQueryContext(queryInterface);
const result = await transactionScope(queryContext);
return [result, queryContext];
});
await queryContext.runPostCommitCallbacksAsync();
return returnedValue;
}
}
@@ -1,22 +0,0 @@
import {
EntityTransactionalQueryContext,
EntityNonTransactionalQueryContext,
} from './EntityQueryContext';
/**
* A query context provider vends transactional and non-transactional query contexts.
*/
export default interface IEntityQueryContextProvider {
/**
* Vend an entity query context.
*/
getQueryContext(): EntityNonTransactionalQueryContext;
/**
* Start a transaction and execute the provided transaction-scoped closure within the transaction.
* @param transactionScope - async callback to execute within the transaction
*/
runInTransactionAsync<T>(
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T>;
}
+2 -2
View File
@@ -5,7 +5,7 @@ import EntityAssociationLoader from './EntityAssociationLoader';
import { EntityCompanionDefinition } from './EntityCompanionProvider';
import EntityLoader from './EntityLoader';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import { EntityQueryContext } from './EntityQueryContext';
import { EntityQueryContext, EntityTransactionalQueryContext } from './EntityQueryContext';
import ViewerContext from './ViewerContext';
import { pick } from './entityUtils';
@@ -183,7 +183,7 @@ export default abstract class ReadonlyEntity<
TMSelectedFields
>,
viewerContext: TMViewerContext2,
transactionScope: (queryContext: EntityQueryContext) => Promise<TResult>
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<TResult>
): Promise<TResult> {
return await viewerContext
.getViewerScopedEntityCompanionForClass(this)
@@ -1,6 +1,6 @@
import EntityCompanion from './EntityCompanion';
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
import IEntityQueryContextProvider from './IEntityQueryContextProvider';
import EntityQueryContextProvider from './EntityQueryContextProvider';
import ReadonlyEntity from './ReadonlyEntity';
import ViewerContext from './ViewerContext';
import ViewerScopedEntityLoaderFactory from './ViewerScopedEntityLoaderFactory';
@@ -73,7 +73,7 @@ export default class ViewerScopedEntityCompanion<
/**
* Get the query context provider for this entity.
*/
getQueryContextProvider(): IEntityQueryContextProvider {
getQueryContextProvider(): EntityQueryContextProvider {
return this.entityCompanion.getQueryContextProvider();
}
}
@@ -283,7 +283,13 @@ describe(EnforcingEntityLoader, () => {
const loaderProperties = Object.getOwnPropertyNames(EntityLoader.prototype);
// ensure known differences still exist for sanity check
const knownLoaderOnlyDifferences = ['enforcing', 'invalidateFieldsAsync'];
const knownLoaderOnlyDifferences = [
'enforcing',
'invalidateFieldsAsync',
'invalidateEntityAsync',
'invalidateFieldsTransactionalAsync',
'tryConstructEntities',
];
expect(loaderProperties).toEqual(expect.arrayContaining(knownLoaderOnlyDifferences));
const loaderPropertiesWithoutKnownDifferences = loaderProperties.filter(
@@ -9,7 +9,7 @@ import { createUnitTestEntityCompanionProvider } from '../utils/testing/createUn
describe(EntityAssociationLoader, () => {
describe('loadAssociatedEntityAsync', () => {
it('loads associated entities by ID', async () => {
it('loads associated entities by ID and correctly handles a null value', async () => {
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
const testOtherEntity = await enforceAsyncResult(
@@ -24,6 +24,11 @@ describe(EntityAssociationLoader, () => {
testEntity.associationLoader().loadAssociatedEntityAsync('stringField', TestEntity)
);
expect(loadedOther.getID()).toEqual(testOtherEntity.getID());
const loadedOther2 = await enforceAsyncResult(
testEntity.associationLoader().loadAssociatedEntityAsync('nullableField', TestEntity)
);
expect(loadedOther2).toBeNull();
});
});
@@ -76,6 +81,18 @@ describe(EntityAssociationLoader, () => {
.loadAssociatedEntityByFieldEqualingAsync('stringField', TestEntity, 'customIdField');
expect(loadedOtherResult).toBeNull();
});
it('returns null when load-by field is null', async () => {
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
const testEntity = await enforceAsyncResult(
TestEntity.creator(viewerContext).setField('stringField', 'blah').createAsync()
);
const loadedOtherResult = await testEntity
.associationLoader()
.loadAssociatedEntityByFieldEqualingAsync('nullableField', TestEntity, 'customIdField');
expect(loadedOtherResult).toBeNull();
});
});
describe('loadManyAssociatedEntitiesByFieldEqualingAsync', () => {
@@ -102,6 +119,22 @@ describe(EntityAssociationLoader, () => {
expect(loaded.find((e) => e.getID() === testOtherEntity1.getID())).not.toBeUndefined();
expect(loaded.find((e) => e.getID() === testOtherEntity2.getID())).not.toBeUndefined();
});
it('returns empty results when field being queried by is null', async () => {
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
const testEntity = await enforceAsyncResult(TestEntity.creator(viewerContext).createAsync());
const loaded = await enforceResultsAsync(
testEntity
.associationLoader()
.loadManyAssociatedEntitiesByFieldEqualingAsync(
'nullableField',
TestEntity,
'stringField'
)
);
expect(loaded).toHaveLength(0);
});
});
describe('loadAssociatedEntityThroughAsync', () => {
@@ -213,5 +246,22 @@ describe(EntityAssociationLoader, () => {
]);
expect(loaded2Result).toBeNull();
});
it('returns null when chain loading by field is null', async () => {
const companionProvider = createUnitTestEntityCompanionProvider();
const viewerContext = new TestViewerContext(companionProvider);
const testEntity = await enforceAsyncResult(
TestEntity.creator(viewerContext).setField('nullableField', null).createAsync()
);
const loadedResult = await testEntity.associationLoader().loadAssociatedEntityThroughAsync([
{
associatedEntityClass: TestEntity,
fieldIdentifyingAssociatedEntity: 'nullableField',
},
]);
expect(loadedResult).toBeNull();
});
});
});
@@ -81,7 +81,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
const parentEntityConfiguration = new EntityConfiguration<ParentFields>({
idField: 'id',
tableName: 'parents',
inboundEdges: [ChildEntity],
getInboundEdges: () => [ChildEntity],
schema: {
id: new UUIDField({
columnName: 'id',
@@ -95,7 +95,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
const childEntityConfiguration = new EntityConfiguration<ChildFields>({
idField: 'id',
tableName: 'children',
inboundEdges: [GrandChildEntity],
getInboundEdges: () => [GrandChildEntity],
schema: {
id: new UUIDField({
columnName: 'id',
@@ -105,7 +105,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
columnName: 'parent_id',
cache: true,
association: {
associatedEntityClass: ParentEntity,
getAssociatedEntityClass: () => ParentEntity,
edgeDeletionBehavior,
},
}),
@@ -126,7 +126,7 @@ const makeEntityClasses = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) =>
columnName: 'parent_id',
cache: true,
association: {
associatedEntityClass: ChildEntity,
getAssociatedEntityClass: () => ChildEntity,
edgeDeletionBehavior,
},
}),
@@ -36,6 +36,7 @@ describe(EntityLoader, () => {
numberField: 5,
stringField: 'huh',
dateField: dateToInsert,
nullableField: null,
},
{
customIdField: 'world',
@@ -43,6 +44,7 @@ describe(EntityLoader, () => {
numberField: 3,
stringField: 'huh',
dateField: dateToInsert,
nullableField: null,
},
],
],
@@ -57,7 +59,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -111,6 +114,7 @@ describe(EntityLoader, () => {
numberField: 4,
testIndexedField: '4',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -118,6 +122,7 @@ describe(EntityLoader, () => {
numberField: 4,
testIndexedField: '5',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'blah',
@@ -125,6 +130,7 @@ describe(EntityLoader, () => {
numberField: 4,
testIndexedField: '6',
dateField: new Date(),
nullableField: null,
},
],
],
@@ -138,7 +144,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -187,6 +194,7 @@ describe(EntityLoader, () => {
testIndexedField: '1',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
],
],
@@ -200,7 +208,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -257,6 +266,31 @@ describe(EntityLoader, () => {
).once();
});
it('invalidates upon invalidate by entity', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = StubQueryContextProvider.getQueryContext();
const privacyPolicy = instance(mock(TestEntityPrivacyPolicy));
const dataManagerMock = mock(EntityDataManager);
const dataManagerInstance = instance(dataManagerMock);
const entityMock = mock(TestEntity);
when(entityMock.getAllDatabaseFields()).thenReturn({ customIdField: 'hello' } as any);
const entityInstance = instance(entityMock);
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
testEntityConfiguration.idField,
TestEntity,
privacyPolicy,
dataManagerInstance
);
await entityLoader.invalidateEntityAsync(entityInstance);
verify(
dataManagerMock.invalidateObjectFieldsAsync(deepEqual({ customIdField: 'hello' }))
).once();
});
it('returns error result when not allowed', async () => {
const viewerContext = instance(mock(ViewerContext));
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -14,14 +14,11 @@ import EntityDatabaseAdapter from '../EntityDatabaseAdapter';
import EntityLoaderFactory from '../EntityLoaderFactory';
import EntityMutationTriggerConfiguration, {
EntityMutationTrigger,
EntityNonTransactionalMutationTrigger,
} from '../EntityMutationTriggerConfiguration';
import EntityMutationValidator from '../EntityMutationValidator';
import EntityMutatorFactory from '../EntityMutatorFactory';
import {
EntityTransactionalQueryContext,
EntityQueryContext,
EntityNonTransactionalQueryContext,
} from '../EntityQueryContext';
import { EntityTransactionalQueryContext, EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
import { enforceResultsAsync } from '../entityUtils';
import EntityDataManager from '../internal/EntityDataManager';
@@ -56,6 +53,16 @@ class TestMutationTrigger extends EntityMutationTrigger<
): Promise<void> {}
}
class TestNonTransactionalMutationTrigger extends EntityNonTransactionalMutationTrigger<
TestFields,
string,
ViewerContext,
TestEntity,
keyof TestFields
> {
async executeAsync(_viewerContext: ViewerContext, _entity: TestEntity): Promise<void> {}
}
const setUpMutationValidatorSpies = (
mutationValidators: EntityMutationValidator<
TestFields,
@@ -183,11 +190,7 @@ const verifyTriggerCounts = (
).once();
verify(
mutationTriggerSpies.afterCommit![0].executeAsync(
viewerContext,
anyOfClass(EntityNonTransactionalQueryContext),
anyOfClass(TestEntity)
)
mutationTriggerSpies.afterCommit![0].executeAsync(viewerContext, anyOfClass(TestEntity))
).once();
};
@@ -248,7 +251,7 @@ const createEntityMutatorFactory = (
afterDelete: [new TestMutationTrigger()],
beforeAll: [new TestMutationTrigger()],
afterAll: [new TestMutationTrigger()],
afterCommit: [new TestMutationTrigger()],
afterCommit: [new TestNonTransactionalMutationTrigger()],
};
const privacyPolicy = new TestEntityPrivacyPolicy();
const databaseAdapter = new StubDatabaseAdapter<TestFields>(
@@ -266,7 +269,8 @@ const createEntityMutatorFactory = (
databaseAdapter,
entityCache,
StubQueryContextProvider,
metricsAdapter
metricsAdapter,
TestEntity.name
);
const entityLoaderFactory = new EntityLoaderFactory(
testEntityConfiguration.idField,
@@ -306,6 +310,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -313,6 +318,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '5',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
]);
const newEntity = await entityMutatorFactory
@@ -332,6 +338,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -339,6 +346,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '5',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
]);
@@ -368,6 +376,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -375,6 +384,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '5',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
]);
@@ -405,6 +415,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -412,6 +423,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '5',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
]);
@@ -437,6 +449,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -444,6 +457,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -480,6 +494,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -487,6 +502,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -524,6 +540,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -531,6 +548,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -569,6 +587,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -576,6 +595,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '4',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -605,6 +625,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -636,6 +657,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -670,6 +692,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -705,6 +728,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -730,6 +754,7 @@ describe(EntityMutatorFactory, () => {
testIndexedField: '3',
numberField: 3,
dateField: new Date(),
nullableField: null,
},
]);
@@ -927,6 +952,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.CREATE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -934,6 +960,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.UPDATE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -941,6 +968,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.DELETE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -48,7 +48,7 @@ const makeEntityClass = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) => {
const categoryEntityConfiguration = new EntityConfiguration<CategoryFields>({
idField: 'id',
tableName: 'categories',
inboundEdges: [CategoryEntity],
getInboundEdges: () => [CategoryEntity],
schema: {
id: new UUIDField({
columnName: 'id',
@@ -58,7 +58,7 @@ const makeEntityClass = (edgeDeletionBehavior: EntityEdgeDeletionBehavior) => {
columnName: 'parent_category_id',
cache: true,
association: {
associatedEntityClass: CategoryEntity,
getAssociatedEntityClass: () => CategoryEntity,
edgeDeletionBehavior,
},
}),
@@ -63,6 +63,7 @@ describe(ReadonlyEntity, () => {
stringField: '3',
numberField: 4,
dateField: new Date(),
nullableField: null,
});
expect(testEntity.getUniqueIdentifier()).not.toEqual(testEntity2.getUniqueIdentifier());
});
@@ -8,6 +8,7 @@ import EntityConfiguration from '../../EntityConfiguration';
import { UUIDField, EnumField, StringField } from '../../EntityFields';
import EntityPrivacyPolicy from '../../EntityPrivacyPolicy';
import ViewerContext from '../../ViewerContext';
import { successfulResults, failedResults } from '../../entityUtils';
import AlwaysAllowPrivacyPolicyRule from '../../rules/AlwaysAllowPrivacyPolicyRule';
import { createUnitTestEntityCompanionProvider } from '../../utils/testing/createUnitTestEntityCompanionProvider';
@@ -18,11 +19,13 @@ describe('Two entities backed by the same table', () => {
const one = await OneTestEntity.creator(viewerContext)
.setField('entity_type', EntityType.ONE)
.setField('common_other_field', 'wat')
.enforceCreateAsync();
const two = await TwoTestEntity.creator(viewerContext)
.setField('entity_type', EntityType.TWO)
.setField('other_field', 'blah')
.setField('common_other_field', 'wat')
.enforceCreateAsync();
expect(one).toBeInstanceOf(OneTestEntity);
@@ -31,9 +34,42 @@ describe('Two entities backed by the same table', () => {
await expect(
TwoTestEntity.loader(viewerContext).enforcing().loadByIDAsync(one.getID())
).rejects.toThrowError('TwoTestEntity must be instantiated with two data');
await expect(
OneTestEntity.loader(viewerContext).enforcing().loadByIDAsync(two.getID())
).rejects.toThrowError('OneTestEntity must be instantiated with one data');
const manyResults = await OneTestEntity.loader(viewerContext).loadManyByFieldEqualingAsync(
'common_other_field',
'wat'
);
const successfulManyResults = successfulResults(manyResults);
const failedManyResults = failedResults(manyResults);
expect(successfulManyResults).toHaveLength(1);
expect(failedManyResults).toHaveLength(1);
expect(successfulManyResults[0].enforceValue().getID()).toEqual(one.getID());
expect(failedManyResults[0].enforceError().message).toEqual(
'OneTestEntity must be instantiated with one data'
);
const fieldEqualityConjunctionResults = await OneTestEntity.loader(
viewerContext
).loadManyByFieldEqualityConjunctionAsync([
{
fieldName: 'common_other_field',
fieldValue: 'wat',
},
]);
const successfulfieldEqualityConjunctionResultsResults = successfulResults(
fieldEqualityConjunctionResults
);
const failedfieldEqualityConjunctionResultsResults = failedResults(
fieldEqualityConjunctionResults
);
expect(successfulfieldEqualityConjunctionResultsResults).toHaveLength(1);
expect(failedfieldEqualityConjunctionResultsResults).toHaveLength(1);
});
});
@@ -45,11 +81,12 @@ enum EntityType {
interface TestFields {
id: string;
other_field: string;
common_other_field: string;
entity_type: EntityType;
}
type OneTestFields = 'id' | 'entity_type';
type TwoTestFields = 'id' | 'other_field' | 'entity_type';
type OneTestFields = 'id' | 'entity_type' | 'common_other_field';
type TwoTestFields = 'id' | 'other_field' | 'entity_type' | 'common_other_field';
const testEntityConfiguration = new EntityConfiguration<TestFields>({
idField: 'id',
@@ -62,6 +99,9 @@ const testEntityConfiguration = new EntityConfiguration<TestFields>({
other_field: new StringField({
columnName: 'other_field',
}),
common_other_field: new StringField({
columnName: 'common_other_field',
}),
entity_type: new EnumField({
columnName: 'entity_type',
}),
@@ -121,12 +161,12 @@ const oneTestEntityCompanion = new EntityCompanionDefinition({
entityClass: OneTestEntity,
entityConfiguration: testEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
entitySelectedFields: ['id', 'entity_type'],
entitySelectedFields: ['id', 'entity_type', 'common_other_field'],
});
const twoTestEntityCompanion = new EntityCompanionDefinition({
entityClass: TwoTestEntity,
entityConfiguration: testEntityConfiguration,
privacyPolicyClass: TestEntityPrivacyPolicy,
entitySelectedFields: ['id', 'other_field', 'entity_type'],
entitySelectedFields: ['id', 'other_field', 'common_other_field', 'entity_type'],
});
+3 -2
View File
@@ -16,7 +16,8 @@ export * from './EntityFields';
export { default as EntityLoader } from './EntityLoader';
export { default as EntityLoaderFactory } from './EntityLoaderFactory';
export * from './EntityMutator';
export { default as EntityMutationExecutable } from './EntityMutationValidator';
export { default as EntityMutationValidator } from './EntityMutationValidator';
export * from './EntityMutationTriggerConfiguration';
export { default as EntityMutationTriggerConfiguration } from './EntityMutationTriggerConfiguration';
export { default as EntityMutatorFactory } from './EntityMutatorFactory';
export { default as EntityPrivacyPolicy } from './EntityPrivacyPolicy';
@@ -24,7 +25,7 @@ export * from './EntityPrivacyPolicy';
export * from './EntityQueryContext';
export { default as IEntityCacheAdapterProvider } from './IEntityCacheAdapterProvider';
export { default as IEntityDatabaseAdapterProvider } from './IEntityDatabaseAdapterProvider';
export { default as IEntityQueryContextProvider } from './IEntityQueryContextProvider';
export { default as EntityQueryContextProvider } from './EntityQueryContextProvider';
export { default as ReadonlyEntity } from './ReadonlyEntity';
export { default as ViewerContext } from './ViewerContext';
export { default as ViewerScopedEntityCompanion } from './ViewerScopedEntityCompanion';
+118 -22
View File
@@ -4,8 +4,8 @@ import EntityDatabaseAdapter, {
FieldEqualityCondition,
QuerySelectionModifiers,
} from '../EntityDatabaseAdapter';
import { EntityQueryContext } from '../EntityQueryContext';
import IEntityQueryContextProvider from '../IEntityQueryContextProvider';
import { EntityQueryContext, EntityTransactionalQueryContext } from '../EntityQueryContext';
import EntityQueryContextProvider from '../EntityQueryContextProvider';
import { partitionErrors } from '../entityUtils';
import {
timeAndLogLoadEventAsync,
@@ -15,6 +15,13 @@ import IEntityMetricsAdapter, { EntityMetricsLoadType } from '../metrics/IEntity
import { computeIfAbsent, zipToMap } from '../utils/collections/maps';
import ReadThroughEntityCache from './ReadThroughEntityCache';
type FieldDataLoader<TFields> = DataLoader<
NonNullable<TFields[keyof TFields]>,
readonly Readonly<TFields>[]
>;
type FieldDataLoadersMap<TFields> = Map<keyof TFields, FieldDataLoader<TFields>>;
/**
* A data manager is responsible for orchestrating multiple sources of entity
* data including local caches, {@link EntityCacheAdapter}, and {@link EntityDatabaseAdapter}.
@@ -22,22 +29,25 @@ import ReadThroughEntityCache from './ReadThroughEntityCache';
* It is also responsible for invalidating all sources of data when mutated using {@link EntityMutator}.
*/
export default class EntityDataManager<TFields> {
private readonly fieldDataLoaders: Map<
keyof TFields,
DataLoader<NonNullable<TFields[keyof TFields]>, readonly Readonly<TFields>[]>
private readonly regularFieldDataLoaders: FieldDataLoadersMap<TFields> = new Map();
private readonly transactionalFieldDataLoaders: Map<
string,
FieldDataLoadersMap<TFields>
> = new Map();
constructor(
private readonly databaseAdapter: EntityDatabaseAdapter<TFields>,
private readonly entityCache: ReadThroughEntityCache<TFields>,
private readonly queryContextProvider: IEntityQueryContextProvider,
private readonly metricsAdapter: IEntityMetricsAdapter
private readonly queryContextProvider: EntityQueryContextProvider,
private readonly metricsAdapter: IEntityMetricsAdapter,
private readonly entityClassName: string
) {}
private getFieldDataLoaderForFieldName<N extends keyof TFields>(
private getRegularFieldDataLoderForFieldName<N extends keyof TFields>(
fieldName: N
): DataLoader<NonNullable<TFields[N]>, readonly Readonly<TFields>[]> {
return computeIfAbsent(this.fieldDataLoaders, fieldName, () => {
): FieldDataLoader<TFields> {
return computeIfAbsent(this.regularFieldDataLoaders, fieldName, () => {
return new DataLoader(
async (
fieldValues: readonly NonNullable<TFields[N]>[]
@@ -52,16 +62,60 @@ export default class EntityDataManager<TFields> {
});
}
private getTransactionalFieldDataLoaderForFieldName<N extends keyof TFields>(
queryContext: EntityTransactionalQueryContext,
fieldName: N
): FieldDataLoader<TFields> {
const fieldDataLoaderMap = computeIfAbsent(
this.transactionalFieldDataLoaders,
queryContext.transactionID,
() => new Map<keyof TFields, FieldDataLoader<TFields>>()
);
return computeIfAbsent(fieldDataLoaderMap, fieldName, () => {
return new DataLoader(
async (
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<readonly (readonly TFields[])[]> => {
const objectMap = await this.loadManyForTransactionalDataLoaderByFieldEqualingAsync(
fieldName,
fieldValues,
queryContext
);
return fieldValues.map((fv) => objectMap.get(fv) ?? []);
}
);
});
}
private async loadManyForTransactionalDataLoaderByFieldEqualingAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[],
queryContext: EntityTransactionalQueryContext
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>> {
this.metricsAdapter.incrementDataManagerDatabaseLoadCount({
fieldValueCount: fieldValues.length,
entityClassName: this.entityClassName,
});
return await this.databaseAdapter.fetchManyWhereAsync(queryContext, fieldName, fieldValues);
}
private async loadManyForDataLoaderByFieldEqualingAsync<N extends keyof TFields>(
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>> {
this.metricsAdapter.incrementDataManagerCacheLoadCount(fieldValues.length);
this.metricsAdapter.incrementDataManagerCacheLoadCount({
fieldValueCount: fieldValues.length,
entityClassName: this.entityClassName,
});
return await this.entityCache.readManyThroughAsync(
fieldName,
fieldValues,
async (fetcherValues) => {
this.metricsAdapter.incrementDataManagerDatabaseLoadCount(fieldValues.length);
async (fetcherValues: readonly NonNullable<TFields[N]>[]) => {
this.metricsAdapter.incrementDataManagerDatabaseLoadCount({
fieldValueCount: fieldValues.length,
entityClassName: this.entityClassName,
});
return await this.databaseAdapter.fetchManyWhereAsync(
this.queryContextProvider.getQueryContext(),
fieldName,
@@ -86,7 +140,8 @@ export default class EntityDataManager<TFields> {
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>> {
return await timeAndLogLoadMapEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY
EntityMetricsLoadType.LOAD_MANY,
this.entityClassName
)(this.loadManyByFieldEqualingInternalAsync(queryContext, fieldName, fieldValues));
}
@@ -95,13 +150,22 @@ export default class EntityDataManager<TFields> {
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>> {
// don't cache when in transaction, as rollbacks complicate things significantly
if (queryContext.isInTransaction()) {
return await this.databaseAdapter.fetchManyWhereAsync(queryContext, fieldName, fieldValues);
const nullOrUndefinedValueIndex = fieldValues.findIndex(
(value) => value === null || value === undefined
);
if (nullOrUndefinedValueIndex >= 0) {
throw new Error(
`Invalid load: ${this.entityClassName} (${fieldName} = ${fieldValues[nullOrUndefinedValueIndex]})`
);
}
this.metricsAdapter.incrementDataManagerDataloaderLoadCount(fieldValues.length);
const dataLoader = this.getFieldDataLoaderForFieldName(fieldName);
this.metricsAdapter.incrementDataManagerDataloaderLoadCount({
fieldValueCount: fieldValues.length,
entityClassName: this.entityClassName,
});
const dataLoader = queryContext.isInTransaction()
? this.getTransactionalFieldDataLoaderForFieldName(queryContext, fieldName)
: this.getRegularFieldDataLoderForFieldName(fieldName);
const results = await dataLoader.loadMany(fieldValues);
const [values, errors] = partitionErrors(results);
if (errors.length > 0) {
@@ -127,7 +191,8 @@ export default class EntityDataManager<TFields> {
): Promise<readonly Readonly<TFields>[]> {
return await timeAndLogLoadEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION
EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION,
this.entityClassName
)(
this.databaseAdapter.fetchManyByFieldEqualityConjunctionAsync(
queryContext,
@@ -154,7 +219,8 @@ export default class EntityDataManager<TFields> {
): Promise<readonly Readonly<TFields>[]> {
return await timeAndLogLoadEventAsync(
this.metricsAdapter,
EntityMetricsLoadType.LOAD_MANY_RAW
EntityMetricsLoadType.LOAD_MANY_RAW,
this.entityClassName
)(
this.databaseAdapter.fetchManyByRawWhereClauseAsync(
queryContext,
@@ -170,7 +236,20 @@ export default class EntityDataManager<TFields> {
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
await this.entityCache.invalidateManyAsync(fieldName, fieldValues);
const dataLoader = this.getFieldDataLoaderForFieldName(fieldName);
const dataLoader = this.getRegularFieldDataLoderForFieldName(fieldName);
fieldValues.forEach((fieldValue) => dataLoader.clear(fieldValue));
}
private async invalidateManyByFieldEqualingTransactionalAsync<N extends keyof TFields>(
queryContext: EntityQueryContext,
fieldName: N,
fieldValues: readonly NonNullable<TFields[N]>[]
): Promise<void> {
if (!queryContext.isInTransaction()) {
return;
}
const dataLoader = this.getTransactionalFieldDataLoaderForFieldName(queryContext, fieldName);
fieldValues.forEach((fieldValue) => dataLoader.clear(fieldValue));
}
@@ -193,4 +272,21 @@ export default class EntityDataManager<TFields> {
})
);
}
async invalidateObjectFieldsTransactionalAsync(
queryContext: EntityQueryContext,
objectFields: Readonly<TFields>
): Promise<void> {
const keys = Object.keys(objectFields) as (keyof TFields)[];
await Promise.all(
keys.map(async (fieldName: keyof TFields) => {
const value = objectFields[fieldName];
if (value !== undefined) {
await this.invalidateManyByFieldEqualingTransactionalAsync(queryContext, fieldName, [
value as NonNullable<TFields[keyof TFields]>,
]);
}
})
);
}
}
@@ -1,9 +1,9 @@
import EntityCacheAdapter from '../EntityCacheAdapter';
import EntityConfiguration from '../EntityConfiguration';
import EntityDatabaseAdapter from '../EntityDatabaseAdapter';
import EntityQueryContextProvider from '../EntityQueryContextProvider';
import IEntityCacheAdapterProvider from '../IEntityCacheAdapterProvider';
import IEntityDatabaseAdapterProvider from '../IEntityDatabaseAdapterProvider';
import IEntityQueryContextProvider from '../IEntityQueryContextProvider';
import IEntityMetricsAdapter from '../metrics/IEntityMetricsAdapter';
import EntityDataManager from './EntityDataManager';
import ReadThroughEntityCache from './ReadThroughEntityCache';
@@ -22,8 +22,9 @@ export default class EntityTableDataCoordinator<TFields> {
readonly entityConfiguration: EntityConfiguration<TFields>,
databaseAdapterProvider: IEntityDatabaseAdapterProvider,
cacheAdapterProvider: IEntityCacheAdapterProvider,
private readonly queryContextProvider: IEntityQueryContextProvider,
metricsAdapter: IEntityMetricsAdapter
private readonly queryContextProvider: EntityQueryContextProvider,
metricsAdapter: IEntityMetricsAdapter,
entityClassName: string
) {
this.databaseAdapter = databaseAdapterProvider.getDatabaseAdapter(entityConfiguration);
this.cacheAdapter = cacheAdapterProvider.getCacheAdapter(entityConfiguration);
@@ -31,11 +32,12 @@ export default class EntityTableDataCoordinator<TFields> {
this.databaseAdapter,
new ReadThroughEntityCache(entityConfiguration, this.cacheAdapter),
queryContextProvider,
metricsAdapter
metricsAdapter,
entityClassName
);
}
getQueryContextProvider(): IEntityQueryContextProvider {
getQueryContextProvider(): EntityQueryContextProvider {
return this.queryContextProvider;
}
}
@@ -9,12 +9,13 @@ import {
spy,
anyString,
resetCalls,
deepEqual,
} from 'ts-mockito';
import EntityDatabaseAdapter from '../../EntityDatabaseAdapter';
import IEntityMetricsAdapter, { EntityMetricsLoadType } from '../../metrics/IEntityMetricsAdapter';
import NoOpEntityMetricsAdapter from '../../metrics/NoOpEntityMetricsAdapter';
import { testEntityConfiguration, TestFields } from '../../testfixtures/TestEntity';
import TestEntity, { testEntityConfiguration, TestFields } from '../../testfixtures/TestEntity';
import {
NoCacheStubCacheAdapterProvider,
InMemoryFullCacheStubCacheAdapterProvider,
@@ -35,6 +36,7 @@ const getObjects = (): Map<string, TestFields[]> =>
stringField: 'hello',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: '2',
@@ -42,6 +44,7 @@ const getObjects = (): Map<string, TestFields[]> =>
stringField: 'hello',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
{
customIdField: '3',
@@ -49,6 +52,7 @@ const getObjects = (): Map<string, TestFields[]> =>
stringField: 'world',
numberField: 1,
dateField: new Date(),
nullableField: null,
},
],
],
@@ -69,7 +73,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -116,7 +121,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -163,7 +169,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
// use second data manager to ensure that cache is hit instead of data loader
@@ -171,7 +178,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
@@ -205,7 +213,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -240,7 +249,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -283,7 +293,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -321,7 +332,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -359,7 +371,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
@@ -396,7 +409,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -441,7 +455,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -467,7 +482,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
metricsAdapter
metricsAdapter,
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -476,6 +492,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY,
entityClassName: TestEntity.name,
count: 1,
})
)
@@ -495,14 +512,36 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION,
entityClassName: TestEntity.name,
count: 1,
})
)
).once();
verify(metricsAdapterMock.incrementDataManagerDataloaderLoadCount(1)).once();
verify(metricsAdapterMock.incrementDataManagerCacheLoadCount(1)).once();
verify(metricsAdapterMock.incrementDataManagerDatabaseLoadCount(1)).once();
verify(
metricsAdapterMock.incrementDataManagerDataloaderLoadCount(
deepEqual({
fieldValueCount: 1,
entityClassName: TestEntity.name,
})
)
).once();
verify(
metricsAdapterMock.incrementDataManagerCacheLoadCount(
deepEqual({
fieldValueCount: 1,
entityClassName: TestEntity.name,
})
)
).once();
verify(
metricsAdapterMock.incrementDataManagerDatabaseLoadCount(
deepEqual({
fieldValueCount: 1,
entityClassName: TestEntity.name,
})
)
).once();
resetCalls(metricsAdapterMock);
@@ -520,6 +559,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_RAW,
entityClassName: TestEntity.name,
count: 0,
})
)
@@ -529,4 +569,34 @@ describe(EntityDataManager, () => {
verify(metricsAdapterMock.incrementDataManagerCacheLoadCount(anyNumber())).never();
verify(metricsAdapterMock.incrementDataManagerDatabaseLoadCount(anyNumber())).never();
});
it('throws when a load-by value is null or undefined', async () => {
const objects = getObjects();
const dataStore = StubDatabaseAdapter.convertFieldObjectsToDataStore(
testEntityConfiguration,
objects
);
const databaseAdapter = new StubDatabaseAdapter<TestFields>(testEntityConfiguration, dataStore);
const cacheAdapterProvider = new NoCacheStubCacheAdapterProvider();
const cacheAdapter = cacheAdapterProvider.getCacheAdapter(testEntityConfiguration);
const entityCache = new ReadThroughEntityCache(testEntityConfiguration, cacheAdapter);
const entityDataManager = new EntityDataManager(
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
await expect(
entityDataManager.loadManyByFieldEqualingAsync(queryContext, 'nullableField', [null as any])
).rejects.toThrowError('Invalid load: TestEntity (nullableField = null)');
await expect(
entityDataManager.loadManyByFieldEqualingAsync(queryContext, 'nullableField', [
undefined as any,
])
).rejects.toThrowError('Invalid load: TestEntity (nullableField = undefined)');
});
});
@@ -6,7 +6,8 @@ import IEntityMetricsAdapter, {
export const timeAndLogLoadEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
loadType: EntityMetricsLoadType
loadType: EntityMetricsLoadType,
entityClassName: string
) => async <TFields>(promise: Promise<readonly Readonly<TFields>[]>) => {
const startTime = Date.now();
const result = await promise;
@@ -14,6 +15,7 @@ export const timeAndLogLoadEventAsync = (
metricsAdapter.logDataManagerLoadEvent({
type: loadType,
entityClassName,
duration: endTime - startTime,
count: result.length,
});
@@ -23,7 +25,8 @@ export const timeAndLogLoadEventAsync = (
export const timeAndLogLoadMapEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
loadType: EntityMetricsLoadType
loadType: EntityMetricsLoadType,
entityClassName: string
) => async <TFields, N extends keyof TFields>(
promise: Promise<ReadonlyMap<NonNullable<TFields[N]>, readonly Readonly<TFields>[]>>
) => {
@@ -35,6 +38,7 @@ export const timeAndLogLoadMapEventAsync = (
metricsAdapter.logDataManagerLoadEvent({
type: loadType,
entityClassName,
duration: endTime - startTime,
count,
});
@@ -44,7 +48,8 @@ export const timeAndLogLoadMapEventAsync = (
export const timeAndLogMutationEventAsync = (
metricsAdapter: IEntityMetricsAdapter,
mutationType: EntityMetricsMutationType
mutationType: EntityMetricsMutationType,
entityClassName: string
) => async <T>(promise: Promise<T>) => {
const startTime = Date.now();
const result = await promise;
@@ -52,6 +57,7 @@ export const timeAndLogMutationEventAsync = (
metricsAdapter.logMutatorMutationEvent({
type: mutationType,
entityClassName,
duration: endTime - startTime,
});
@@ -6,6 +6,7 @@ export enum EntityMetricsLoadType {
export interface EntityMetricsLoadEvent {
type: EntityMetricsLoadType;
entityClassName: string;
duration: number;
count: number;
}
@@ -18,9 +19,15 @@ export enum EntityMetricsMutationType {
export interface EntityMetricsMutationEvent {
type: EntityMetricsMutationType;
entityClassName: string;
duration: number;
}
export interface IncrementLoadCountEvent {
fieldValueCount: number;
entityClassName: string;
}
/**
* An interface for gathering metrics about the Entity framework. Information about
* entity load and mutation operations is piped to an instance of this adapter.
@@ -43,7 +50,7 @@ export default interface IEntityMetricsAdapter {
* load methods (not equality conjunction or raw).
* @param fieldValueCount - count of field values being loaded for a field
*/
incrementDataManagerDataloaderLoadCount(fieldValueCount: number): void;
incrementDataManagerDataloaderLoadCount(incrementLoadCountEvent: IncrementLoadCountEvent): void;
/**
* Called when a cache load is initiated via the standard
@@ -51,7 +58,7 @@ export default interface IEntityMetricsAdapter {
* miss.
* @param fieldValueCount - count of field values being loaded for a field
*/
incrementDataManagerCacheLoadCount(fieldValueCount: number): void;
incrementDataManagerCacheLoadCount(incrementLoadCountEvent: IncrementLoadCountEvent): void;
/**
* Called when a database load is initiated via the standard
@@ -59,5 +66,5 @@ export default interface IEntityMetricsAdapter {
* miss or when fetching an uncacheable field.
* @param fieldValueCount - count of field values being loaded for a field
*/
incrementDataManagerDatabaseLoadCount(fieldValueCount: number): void;
incrementDataManagerDatabaseLoadCount(incrementLoadCountEvent: IncrementLoadCountEvent): void;
}
@@ -1,12 +1,15 @@
import IEntityMetricsAdapter, {
EntityMetricsLoadEvent,
EntityMetricsMutationEvent,
IncrementLoadCountEvent,
} from './IEntityMetricsAdapter';
export default class NoOpEntityMetricsAdapter implements IEntityMetricsAdapter {
logDataManagerLoadEvent(_loadEvent: EntityMetricsLoadEvent): void {}
logMutatorMutationEvent(_mutationEvent: EntityMetricsMutationEvent): void {}
incrementDataManagerDataloaderLoadCount(_fieldValueCount: number): void {}
incrementDataManagerCacheLoadCount(_fieldValueCount: number): void {}
incrementDataManagerDatabaseLoadCount(_fieldValueCount: number): void {}
incrementDataManagerDataloaderLoadCount(
_incrementLoadCountEvent: IncrementLoadCountEvent
): void {}
incrementDataManagerCacheLoadCount(_incrementLoadCountEvent: IncrementLoadCountEvent): void {}
incrementDataManagerDatabaseLoadCount(_incrementLoadCountEvent: IncrementLoadCountEvent): void {}
}
@@ -18,6 +18,7 @@ export type TestFields = {
stringField: string;
numberField: number;
dateField: Date;
nullableField: string | null;
};
export const testEntityConfiguration = new EntityConfiguration<TestFields>({
@@ -40,6 +41,9 @@ export const testEntityConfiguration = new EntityConfiguration<TestFields>({
dateField: new DateField({
columnName: 'date_field',
}),
nullableField: new StringField({
columnName: 'nullable_field',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
@@ -88,6 +92,7 @@ export default class TestEntity extends Entity<TestFields, string, ViewerContext
stringField: 'hello',
numberField: 1,
dateField: new Date(),
nullableField: null,
})
);
}
@@ -1,18 +1,14 @@
import {
EntityNonTransactionalQueryContext,
EntityTransactionalQueryContext,
} from '../../EntityQueryContext';
import IEntityQueryContextProvider from '../../IEntityQueryContextProvider';
import EntityQueryContextProvider from '../../EntityQueryContextProvider';
class StubQueryContextProvider implements IEntityQueryContextProvider {
getQueryContext(): EntityNonTransactionalQueryContext {
return new EntityNonTransactionalQueryContext({}, this);
class StubQueryContextProvider extends EntityQueryContextProvider {
protected getQueryInterface(): any {
return {};
}
async runInTransactionAsync<T>(
transactionScope: (queryContext: EntityTransactionalQueryContext) => Promise<T>
): Promise<T> {
return await transactionScope(new EntityTransactionalQueryContext({}));
protected createTransactionRunner<T>(): (
transactionScope: (queryInterface: any) => Promise<T>
) => Promise<T> {
return (transactionScope) => Promise.resolve(transactionScope({}));
}
}
@@ -35,6 +35,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 5,
stringField: 'huh',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -42,6 +43,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'wat',
dateField: new Date(),
nullableField: null,
},
],
],
@@ -73,6 +75,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'a',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -80,6 +83,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'b',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -87,6 +91,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'c',
dateField: new Date(),
nullableField: null,
},
],
],
@@ -138,6 +143,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'a',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -145,6 +151,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'b',
dateField: new Date(),
nullableField: null,
},
{
customIdField: 'world',
@@ -152,6 +159,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'c',
dateField: new Date(),
nullableField: null,
},
],
],
@@ -236,6 +244,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'a',
dateField: new Date(),
nullableField: null,
},
],
],
@@ -269,6 +278,7 @@ describe(StubDatabaseAdapter, () => {
numberField: 3,
stringField: 'a',
dateField: new Date(),
nullableField: null,
},
],
],
+1 -1
View File
@@ -8,7 +8,7 @@
"**/__integration-tests__/**/*.ts",
"**/testfixtures/**/*.ts"
],
"lernaExclude": ["@expo/entity-example"],
"lernaExclude": ["@expo/entity-example", "@expo/entity-full-integration-tests"],
"name": "Entity",
"readme": "README.md",
"excludeNotExported": true
+2231 -1446
View File
File diff suppressed because it is too large. Load diff