Compare commits

..
14 Commits
53 changed files with 3264 additions and 1737 deletions

No files matched your search

+36
View File
@@ -3,6 +3,42 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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`
## 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.8.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,33 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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.8.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.8.0"
}
}
@@ -3,6 +3,35 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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.8.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.8.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);
}
}
@@ -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,
+28
View File
@@ -3,6 +3,34 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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.8.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.8.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,28 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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.8.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.8.0",
"@expo/entity-cache-adapter-redis": "^0.8.0",
"@expo/entity-database-adapter-knex": "^0.8.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');
});
});
@@ -10,9 +10,12 @@ 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 { createKnexIntegrationTestEntityCompanionProvider } from '../testfixtures/createKnexIntegrationTestEntityCompanionProvider';
import { createFullIntegrationTestEntityCompanionProvider } from '../testfixtures/createFullIntegrationTestEntityCompanionProvider';
interface ParentFields {
id: string;
@@ -127,6 +130,7 @@ async function dropPostgresTable(knex: Knex): Promise<void> {
describe('EntityMutator.processEntityDeletionForInboundEdgesAsync', () => {
let knexInstance: Knex;
let redisCacheAdapterContext: RedisCacheAdapterContext;
beforeAll(() => {
knexInstance = Knex({
@@ -139,21 +143,37 @@ 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
};
});
beforeEach(async () => {
await createOrTruncatePostgresTables(knexInstance);
await redisCacheAdapterContext.redisClient.flushdb();
});
afterAll(async () => {
await dropPostgresTable(knexInstance);
knexInstance.destroy();
await knexInstance.destroy();
redisCacheAdapterContext.redisClient.disconnect();
});
describe('EntityEdgeDeletionBehavior.INVALIDATE_CACHE', () => {
it('invalidates the cache', async () => {
const viewerContext = new ViewerContext(
createKnexIntegrationTestEntityCompanionProvider(knexInstance)
createFullIntegrationTestEntityCompanionProvider(knexInstance, redisCacheAdapterContext)
);
const parent = await ParentEntity.creator(viewerContext).enforceCreateAsync();
@@ -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;
@@ -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,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"]
}
+36
View File
@@ -3,6 +3,42 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [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.8.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[]> {
@@ -99,7 +99,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityFieldContainingThisID: keyof TAssociatedFields,
associatedEntityFieldContainingThisID: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -153,7 +153,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityLookupByField: keyof TAssociatedFields,
associatedEntityLookupByField: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -207,7 +207,7 @@ export default class EntityAssociationLoader<
TAssociatedPrivacyPolicy,
TAssociatedSelectedFields
>,
associatedEntityLookupByField: keyof TAssociatedFields,
associatedEntityLookupByField: keyof Pick<TAssociatedFields, TAssociatedSelectedFields>,
queryContext: EntityQueryContext = this.entity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(associatedEntityClass)
@@ -491,5 +491,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
);
});
}
+71 -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,19 @@ export default class EntityLoader<
async invalidateFieldsAsync(objectFields: Readonly<TFields>): Promise<void> {
await this.dataManager.invalidateObjectFieldsAsync(objectFields);
}
/**
* 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 fieldsObject - 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>;
}
+73 -44
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,9 @@ 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)
);
const unauthorizedEntityAfterInsert = new this.entityClass(this.viewerContext, insertResult);
const newEntity = await entityLoader
@@ -208,6 +222,14 @@ export class CreateMutator<
newEntity
);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
newEntity
)
);
return result(newEntity);
}
}
@@ -308,7 +330,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 +343,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 +388,12 @@ 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)
);
const unauthorizedEntityAfterUpdate = new this.entityClass(this.viewerContext, updateResult);
const updatedEntity = await entityLoader
@@ -392,6 +411,14 @@ export class UpdateMutator<
updatedEntity
);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
updatedEntity
)
);
return result(updatedEntity);
}
}
@@ -473,7 +500,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 +516,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 +563,9 @@ 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 this.executeMutationTriggersOrValidatorsAsync(
this.mutationTriggers.afterDelete,
@@ -557,7 +578,15 @@ export class DeleteMutator<
this.entity
);
return result(this.entity);
queryContext.appendPostCommitCallback(
this.executeNonTransactionalMutationTriggersOrValidatorsAsync.bind(
this,
this.mutationTriggers.afterCommit,
this.entity
)
);
return result();
}
/**
+19 -9
View File
@@ -1,4 +1,6 @@
import IEntityQueryContextProvider from './IEntityQueryContextProvider';
import EntityQueryContextProvider from './EntityQueryContextProvider';
export type PostCommitCallback = (...args: any) => Promise<any>;
/**
* Entity framework representation of transactional and non-transactional database
@@ -24,7 +26,7 @@ export abstract class EntityQueryContext {
export class EntityNonTransactionalQueryContext extends EntityQueryContext {
constructor(
queryInterface: any,
private readonly entityQueryContextProvider: IEntityQueryContextProvider
private readonly entityQueryContextProvider: EntityQueryContextProvider
) {
super(queryInterface);
}
@@ -34,23 +36,31 @@ export class EntityNonTransactionalQueryContext extends EntityQueryContext {
}
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 {
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(): boolean {
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,11 @@ describe(EnforcingEntityLoader, () => {
const loaderProperties = Object.getOwnPropertyNames(EntityLoader.prototype);
// ensure known differences still exist for sanity check
const knownLoaderOnlyDifferences = ['enforcing', 'invalidateFieldsAsync'];
const knownLoaderOnlyDifferences = [
'enforcing',
'invalidateFieldsAsync',
'tryConstructEntities',
];
expect(loaderProperties).toEqual(expect.arrayContaining(knownLoaderOnlyDifferences));
const loaderPropertiesWithoutKnownDifferences = loaderProperties.filter(
@@ -57,7 +57,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -138,7 +139,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -200,7 +202,8 @@ describe(EntityLoader, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
instance(mock<IEntityMetricsAdapter>())
instance(mock<IEntityMetricsAdapter>()),
TestEntity.name
);
const entityLoader = new EntityLoader(
viewerContext,
@@ -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,
@@ -927,6 +931,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.CREATE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -934,6 +939,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.UPDATE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -941,6 +947,7 @@ describe(EntityMutatorFactory, () => {
spiedMetricsAdapter.logMutatorMutationEvent(
objectContaining({
type: EntityMetricsMutationType.DELETE,
entityClassName: TestEntity.name,
})
)
).once();
@@ -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';
@@ -5,7 +5,7 @@ import EntityDatabaseAdapter, {
QuerySelectionModifiers,
} from '../EntityDatabaseAdapter';
import { EntityQueryContext } from '../EntityQueryContext';
import IEntityQueryContextProvider from '../IEntityQueryContextProvider';
import EntityQueryContextProvider from '../EntityQueryContextProvider';
import { partitionErrors } from '../entityUtils';
import {
timeAndLogLoadEventAsync,
@@ -30,8 +30,9 @@ export default class EntityDataManager<TFields> {
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>(
@@ -86,7 +87,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));
}
@@ -127,7 +129,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 +157,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,
@@ -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;
}
}
@@ -14,7 +14,7 @@ import {
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,
@@ -69,7 +69,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -116,7 +117,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -163,7 +165,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 +174,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
@@ -205,7 +209,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -240,7 +245,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -283,7 +289,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -321,7 +328,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -359,7 +367,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const dbSpy = jest.spyOn(databaseAdapter, 'fetchManyWhereAsync');
@@ -396,7 +405,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -441,7 +451,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
new NoOpEntityMetricsAdapter()
new NoOpEntityMetricsAdapter(),
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -467,7 +478,8 @@ describe(EntityDataManager, () => {
databaseAdapter,
entityCache,
StubQueryContextProvider,
metricsAdapter
metricsAdapter,
TestEntity.name
);
const queryContext = StubQueryContextProvider.getQueryContext();
@@ -476,6 +488,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY,
entityClassName: TestEntity.name,
count: 1,
})
)
@@ -495,6 +508,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_EQUALITY_CONJUNCTION,
entityClassName: TestEntity.name,
count: 1,
})
)
@@ -520,6 +534,7 @@ describe(EntityDataManager, () => {
metricsAdapterMock.logDataManagerLoadEvent(
objectContaining({
type: EntityMetricsLoadType.LOAD_MANY_RAW,
entityClassName: TestEntity.name,
count: 0,
})
)
@@ -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,6 +19,7 @@ export enum EntityMetricsMutationType {
export interface EntityMetricsMutationEvent {
type: EntityMetricsMutationType;
entityClassName: string;
duration: number;
}
@@ -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({}));
}
}
+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