mirror of
https://github.com/expo/entity.git
synced 2026-11-13 20:28:06 -10:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83547412a2 | ||
|
|
fe2917e6b5 | ||
|
|
29c1a434ab | ||
|
|
15546aad97 | ||
|
|
5208aeedb0 | ||
|
|
b8e07f9ddc |
No files matched your search
@@ -10,7 +10,8 @@
|
||||
"rules": {
|
||||
"no-restricted-imports": ["error",{ "paths": [".", "..", "../.."] }],
|
||||
"tsdoc/syntax": "warn",
|
||||
"no-console": "warn"
|
||||
"no-console": "warn",
|
||||
"handle-callback-err": "off"
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
|
||||
@@ -3,6 +3,25 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add type generic default in test utils ([#104](https://github.com/expo/entity/issues/104)) ([fe2917e](https://github.com/expo/entity/commit/fe2917e6b5af808f166aa354d476e168c41e6d21))
|
||||
* maps utils invariants for collections containing undefined values ([#103](https://github.com/expo/entity/issues/103)) ([29c1a43](https://github.com/expo/entity/commit/29c1a434ab6cd8d30e9f0e7c742065c8d46d8d8a))
|
||||
* remove any cast from EntityLoader ([#100](https://github.com/expo/entity/issues/100)) ([b8e07f9](https://github.com/expo/entity/commit/b8e07f9ddc4077768980c000d61f5ddcc824e2e3))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* better cache adapter error handling ([#102](https://github.com/expo/entity/issues/102)) ([15546aa](https://github.com/expo/entity/commit/15546aad97734ab9ac67f1c012485f096a0e94f1))
|
||||
* better database adapter error handling ([#101](https://github.com/expo/entity/issues/101)) ([5208aee](https://github.com/expo/entity/commit/5208aeedb0192f0460ba3d575770a04175f5c3aa))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -4,5 +4,5 @@
|
||||
],
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"version": "0.10.0"
|
||||
"version": "0.11.0"
|
||||
}
|
||||
@@ -3,6 +3,17 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* better cache adapter error handling ([#102](https://github.com/expo/entity/issues/102)) ([15546aa](https://github.com/expo/entity/commit/15546aad97734ab9ac67f1c012485f096a0e94f1))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/entity-cache-adapter-redis",
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.0",
|
||||
"description": "Redis cache adapter for @expo/entity",
|
||||
"files": [
|
||||
"build",
|
||||
@@ -32,6 +32,6 @@
|
||||
"ioredis": "^4.17.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@expo/entity": "^0.10.0"
|
||||
"@expo/entity": "^0.11.0"
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import { Redis } from 'ioredis';
|
||||
|
||||
import { redisTransformerMap } from './RedisCommon';
|
||||
import wrapNativeRedisCall from './errors/wrapNativeRedisCall';
|
||||
|
||||
// Sentinel value we store in Redis to negatively cache a database miss.
|
||||
// The sentinel value is distinct from any (positively) cached value.
|
||||
@@ -71,7 +72,9 @@ export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFiel
|
||||
}
|
||||
|
||||
const redisKeys = fieldValues.map((fieldValue) => this.makeCacheKey(fieldName, fieldValue));
|
||||
const redisResults = await this.context.redisClient.mget(...redisKeys);
|
||||
const redisResults = await wrapNativeRedisCall(() =>
|
||||
this.context.redisClient.mget(...redisKeys)
|
||||
);
|
||||
|
||||
const results = new Map<NonNullable<TFields[N]>, CacheLoadResult>();
|
||||
for (let i = 0; i < fieldValues.length; i++) {
|
||||
@@ -114,7 +117,7 @@ export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFiel
|
||||
this.context.ttlSecondsPositive
|
||||
);
|
||||
});
|
||||
await redisTransaction.exec();
|
||||
await wrapNativeRedisCall(() => redisTransaction.exec());
|
||||
}
|
||||
|
||||
public async cacheDBMissesAsync<N extends keyof TFields>(
|
||||
@@ -135,7 +138,7 @@ export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFiel
|
||||
this.context.ttlSecondsNegative
|
||||
);
|
||||
});
|
||||
await redisTransaction.exec();
|
||||
await wrapNativeRedisCall(() => redisTransaction.exec());
|
||||
}
|
||||
|
||||
public async invalidateManyAsync<N extends keyof TFields>(
|
||||
@@ -147,7 +150,7 @@ export default class RedisCacheAdapter<TFields> extends EntityCacheAdapter<TFiel
|
||||
}
|
||||
|
||||
const redisKeys = fieldValues.map((fieldValue) => this.makeCacheKey(fieldName, fieldValue));
|
||||
await this.context.redisClient.del(...redisKeys);
|
||||
await wrapNativeRedisCall(() => this.context.redisClient.del(...redisKeys));
|
||||
}
|
||||
|
||||
private makeCacheKey<N extends keyof TFields>(
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { EntityCacheAdapterTransientError, ViewerContext } from '@expo/entity';
|
||||
import Redis from 'ioredis';
|
||||
import { URL } from 'url';
|
||||
|
||||
import RedisCacheAdapter, { RedisCacheAdapterContext } from '../RedisCacheAdapter';
|
||||
import RedisTestEntity from '../testfixtures/RedisTestEntity';
|
||||
import { createRedisIntegrationTestEntityCompanionProvider } from '../testfixtures/createRedisIntegrationTestEntityCompanionProvider';
|
||||
|
||||
class TestViewerContext extends ViewerContext {}
|
||||
|
||||
describe(RedisCacheAdapter, () => {
|
||||
let redisCacheAdapterContext: RedisCacheAdapterContext;
|
||||
|
||||
beforeAll(() => {
|
||||
redisCacheAdapterContext = {
|
||||
redisClient: new Redis(new URL(process.env.REDIS_URL!).toString()),
|
||||
makeKeyFn(...parts: string[]): string {
|
||||
const delimiter = ':';
|
||||
const escapedParts = parts.map((part) =>
|
||||
part.replace('\\', '\\\\').replace(delimiter, `\\${delimiter}`)
|
||||
);
|
||||
return escapedParts.join(delimiter);
|
||||
},
|
||||
cacheKeyPrefix: 'test-',
|
||||
cacheKeyVersion: 1,
|
||||
ttlSecondsPositive: 86400, // 1 day
|
||||
ttlSecondsNegative: 600, // 10 minutes
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await redisCacheAdapterContext.redisClient.flushdb();
|
||||
});
|
||||
|
||||
it('throws when redis is disconnected', async () => {
|
||||
redisCacheAdapterContext.redisClient.disconnect();
|
||||
|
||||
const vc1 = new TestViewerContext(
|
||||
createRedisIntegrationTestEntityCompanionProvider(redisCacheAdapterContext)
|
||||
);
|
||||
|
||||
await expect(
|
||||
RedisTestEntity.creator(vc1).setField('name', 'blah').enforceCreateAsync()
|
||||
).rejects.toThrow(EntityCacheAdapterTransientError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { EntityCacheAdapterTransientError } from '@expo/entity';
|
||||
|
||||
export default async function wrapNativeRedisCall<T>(fn: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (e) {
|
||||
const error = new EntityCacheAdapterTransientError(e.message, e);
|
||||
error.stack = e.stack;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,18 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* better cache adapter error handling ([#102](https://github.com/expo/entity/issues/102)) ([15546aa](https://github.com/expo/entity/commit/15546aad97734ab9ac67f1c012485f096a0e94f1))
|
||||
* better database adapter error handling ([#101](https://github.com/expo/entity/issues/101)) ([5208aee](https://github.com/expo/entity/commit/5208aeedb0192f0460ba3d575770a04175f5c3aa))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/entity-database-adapter-knex",
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.0",
|
||||
"description": "Knex database adapter for @expo/entity",
|
||||
"files": [
|
||||
"build",
|
||||
@@ -32,6 +32,6 @@
|
||||
"knex": "^0.21.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@expo/entity": "^0.10.0"
|
||||
"@expo/entity": "^0.11.0"
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
} from '@expo/entity';
|
||||
import Knex from 'knex';
|
||||
|
||||
import wrapNativePostgresCall from './errors/wrapNativePostgresCall';
|
||||
|
||||
export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDatabaseAdapter<TFields> {
|
||||
protected getFieldTransformerMap(): FieldTransformerMap {
|
||||
return new Map<string, FieldTransformer<any>>([
|
||||
@@ -43,10 +45,12 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
tableField: string,
|
||||
tableValues: readonly any[]
|
||||
): Promise<object[]> {
|
||||
return await queryInterface
|
||||
.select()
|
||||
.from(tableName)
|
||||
.whereRaw('?? = ANY(?)', [tableField, tableValues as any[]]);
|
||||
return await wrapNativePostgresCall(() =>
|
||||
queryInterface
|
||||
.select()
|
||||
.from(tableName)
|
||||
.whereRaw('?? = ANY(?)', [tableField, tableValues as any[]])
|
||||
);
|
||||
}
|
||||
|
||||
private applyQueryModifiersToQuery(
|
||||
@@ -98,7 +102,7 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
}
|
||||
|
||||
query = this.applyQueryModifiersToQuery(query, querySelectionModifiers);
|
||||
return await query;
|
||||
return await wrapNativePostgresCall(() => query);
|
||||
}
|
||||
|
||||
protected async fetchManyByRawWhereClauseInternalAsync(
|
||||
@@ -113,7 +117,7 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
.from(tableName)
|
||||
.whereRaw(rawWhereClause, bindings as any);
|
||||
query = this.applyQueryModifiersToQuery(query, querySelectionModifiers);
|
||||
return await query;
|
||||
return await wrapNativePostgresCall(() => query);
|
||||
}
|
||||
|
||||
protected async insertInternalAsync(
|
||||
@@ -121,7 +125,9 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
tableName: string,
|
||||
object: object
|
||||
): Promise<object[]> {
|
||||
return await queryInterface.insert(object).into(tableName).returning('*');
|
||||
return await wrapNativePostgresCall(() =>
|
||||
queryInterface.insert(object).into(tableName).returning('*')
|
||||
);
|
||||
}
|
||||
|
||||
protected async updateInternalAsync(
|
||||
@@ -131,11 +137,9 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
id: any,
|
||||
object: object
|
||||
): Promise<object[]> {
|
||||
return await queryInterface
|
||||
.update(object)
|
||||
.into(tableName)
|
||||
.where(tableIdField, id)
|
||||
.returning('*');
|
||||
return await wrapNativePostgresCall(() =>
|
||||
queryInterface.update(object).into(tableName).where(tableIdField, id).returning('*')
|
||||
);
|
||||
}
|
||||
|
||||
protected async deleteInternalAsync(
|
||||
@@ -144,6 +148,8 @@ export default class PostgresEntityDatabaseAdapter<TFields> extends EntityDataba
|
||||
tableIdField: string,
|
||||
id: any
|
||||
): Promise<number> {
|
||||
return await queryInterface.into(tableName).where(tableIdField, id).del();
|
||||
return await wrapNativePostgresCall(() =>
|
||||
queryInterface.into(tableName).where(tableIdField, id).del()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import { ViewerContext } from '@expo/entity';
|
||||
import {
|
||||
EntityDatabaseAdapterCheckConstraintError,
|
||||
EntityDatabaseAdapterExclusionConstraintError,
|
||||
EntityDatabaseAdapterForeignKeyConstraintError,
|
||||
EntityDatabaseAdapterNotNullConstraintError,
|
||||
EntityDatabaseAdapterTransientError,
|
||||
EntityDatabaseAdapterUniqueConstraintError,
|
||||
EntityDatabaseAdapterUnknownError,
|
||||
} from '@expo/entity/build/errors/EntityDatabaseAdapterError';
|
||||
import Knex from 'knex';
|
||||
|
||||
import ErrorsTestEntity from '../testfixtures/ErrorsTestEntity';
|
||||
import { createKnexIntegrationTestEntityCompanionProvider } from '../testfixtures/createKnexIntegrationTestEntityCompanionProvider';
|
||||
|
||||
describe('postgres errors', () => {
|
||||
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 ErrorsTestEntity.createOrTruncatePostgresTable(knexInstance);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await ErrorsTestEntity.dropPostgresTable(knexInstance);
|
||||
await knexInstance.destroy();
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterTransientError on Knex timeout', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.enforceCreateAsync();
|
||||
|
||||
const shortTimeoutKnexInstance = Knex({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
user: process.env.PGUSER,
|
||||
password: process.env.PGPASSWORD,
|
||||
host: 'localhost',
|
||||
port: parseInt(process.env.PGPORT!, 10),
|
||||
database: process.env.PGDATABASE,
|
||||
},
|
||||
acquireConnectionTimeout: 1,
|
||||
});
|
||||
const vc2 = new ViewerContext(
|
||||
createKnexIntegrationTestEntityCompanionProvider(shortTimeoutKnexInstance)
|
||||
);
|
||||
await expect(ErrorsTestEntity.loader(vc2).enforcing().loadByIDAsync(1)).rejects.toThrow(
|
||||
EntityDatabaseAdapterTransientError
|
||||
);
|
||||
await shortTimeoutKnexInstance.destroy();
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterNotNullConstraintError when not null is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', null as any)
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterNotNullConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterForeignKeyConstraintError when foreign key is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('fieldForeignKey', 2)
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterForeignKeyConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterUniqueConstraintError when primary key unique constraint is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
|
||||
await ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.enforceCreateAsync();
|
||||
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterUniqueConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterUniqueConstraintError when unique constraint is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 2)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('fieldUnique', 'hello')
|
||||
.enforceCreateAsync();
|
||||
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('fieldUnique', 'hello')
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterUniqueConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterCheckConstraintError when check constraint is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('checkLessThan5', 2)
|
||||
.enforceCreateAsync()
|
||||
).resolves.toBeTruthy();
|
||||
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 2)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('checkLessThan5', 10)
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterCheckConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterExclusionConstraintError when exclusion constraint is violated', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('fieldExclusion', 'what')
|
||||
.enforceCreateAsync()
|
||||
).resolves.toBeTruthy();
|
||||
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 2)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('fieldExclusion', 'what')
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterExclusionConstraintError);
|
||||
});
|
||||
|
||||
it('throws EntityDatabaseAdapterUnknownError otherwise', async () => {
|
||||
const vc = new ViewerContext(createKnexIntegrationTestEntityCompanionProvider(knexInstance));
|
||||
await expect(
|
||||
ErrorsTestEntity.creator(vc)
|
||||
.setField('id', 1)
|
||||
.setField('fieldNonNull', 'hello')
|
||||
.setField('nonExistentColumn', 'what')
|
||||
.enforceCreateAsync()
|
||||
).rejects.toThrow(EntityDatabaseAdapterUnknownError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
EntityDatabaseAdapterError,
|
||||
EntityDatabaseAdapterCheckConstraintError,
|
||||
EntityDatabaseAdapterExclusionConstraintError,
|
||||
EntityDatabaseAdapterForeignKeyConstraintError,
|
||||
EntityDatabaseAdapterNotNullConstraintError,
|
||||
EntityDatabaseAdapterTransientError,
|
||||
EntityDatabaseAdapterUniqueConstraintError,
|
||||
EntityDatabaseAdapterUnknownError,
|
||||
} from '@expo/entity';
|
||||
import { KnexTimeoutError } from 'knex';
|
||||
|
||||
function wrapNativePostgresError(
|
||||
error: Error & { code: string | undefined }
|
||||
): EntityDatabaseAdapterError & Error {
|
||||
const ret = translatePostgresError(error);
|
||||
ret.stack = error.stack;
|
||||
return ret;
|
||||
}
|
||||
|
||||
function translatePostgresError(
|
||||
error: Error & { code: string | undefined }
|
||||
): EntityDatabaseAdapterError & Error {
|
||||
if (error instanceof KnexTimeoutError) {
|
||||
return new EntityDatabaseAdapterTransientError(error.message, error);
|
||||
}
|
||||
|
||||
switch (error.code) {
|
||||
case '23502':
|
||||
return new EntityDatabaseAdapterNotNullConstraintError(error.message, error);
|
||||
case '23503':
|
||||
return new EntityDatabaseAdapterForeignKeyConstraintError(error.message, error);
|
||||
case '23505':
|
||||
return new EntityDatabaseAdapterUniqueConstraintError(error.message, error);
|
||||
case '23514':
|
||||
return new EntityDatabaseAdapterCheckConstraintError(error.message, error);
|
||||
case '23P01':
|
||||
return new EntityDatabaseAdapterExclusionConstraintError(error.message, error);
|
||||
default:
|
||||
return new EntityDatabaseAdapterUnknownError(error.message, error);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function wrapNativePostgresCall<T>(fn: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (e) {
|
||||
throw wrapNativePostgresError(e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import {
|
||||
AlwaysAllowPrivacyPolicyRule,
|
||||
EntityPrivacyPolicy,
|
||||
ViewerContext,
|
||||
StringField,
|
||||
EntityConfiguration,
|
||||
DatabaseAdapterFlavor,
|
||||
CacheAdapterFlavor,
|
||||
EntityCompanionDefinition,
|
||||
Entity,
|
||||
NumberField,
|
||||
} from '@expo/entity';
|
||||
import Knex from 'knex';
|
||||
|
||||
type ErrorsTestEntityFields = {
|
||||
id: number;
|
||||
fieldNonNull: string;
|
||||
fieldForeignKey: number | null;
|
||||
fieldUnique: string | null;
|
||||
checkLessThan5: number | null;
|
||||
fieldExclusion: string | null;
|
||||
nonExistentColumn: string | null;
|
||||
};
|
||||
|
||||
const foreignTableName = 'foreign_table';
|
||||
|
||||
export default class ErrorsTestEntity extends Entity<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext
|
||||
> {
|
||||
static getCompanionDefinition(): EntityCompanionDefinition<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity,
|
||||
ErrorsTestEntityPrivacyPolicy
|
||||
> {
|
||||
return errorsTestEntityCompanionDefinition;
|
||||
}
|
||||
|
||||
public static async createOrTruncatePostgresTable(knex: Knex): Promise<void> {
|
||||
await knex.raw('CREATE EXTENSION IF NOT EXISTS "btree_gist"'); // for gist exclusion on varchar
|
||||
|
||||
const tableName = this.getCompanionDefinition().entityConfiguration.tableName;
|
||||
|
||||
const hasForeignTable = await knex.schema.hasTable(foreignTableName);
|
||||
if (!hasForeignTable) {
|
||||
await knex.schema.createTable(foreignTableName, (table) => {
|
||||
table.integer('id').primary();
|
||||
});
|
||||
}
|
||||
|
||||
const hasTable = await knex.schema.hasTable(tableName);
|
||||
if (!hasTable) {
|
||||
await knex.schema.createTable(tableName, (table) => {
|
||||
table.integer('id').primary();
|
||||
table.string('field_non_null').notNullable();
|
||||
table.integer('field_foreign_key').references(`${foreignTableName}.id`);
|
||||
table.string('field_unique').unique();
|
||||
table.integer('check_less_than_5');
|
||||
table.string('field_exclusion');
|
||||
});
|
||||
|
||||
await knex.raw(
|
||||
`
|
||||
ALTER TABLE :tableName:
|
||||
ADD CONSTRAINT check_less_than_5_check_constraint
|
||||
CHECK (check_less_than_5 IS NULL OR check_less_than_5 < 5);`,
|
||||
{
|
||||
tableName,
|
||||
}
|
||||
);
|
||||
|
||||
await knex.raw(
|
||||
`
|
||||
ALTER TABLE :tableName:
|
||||
ADD CONSTRAINT field_exclusion_exclusion_constraint
|
||||
EXCLUDE USING gist (field_exclusion WITH =);
|
||||
`,
|
||||
{
|
||||
tableName,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
await knex(tableName).delete();
|
||||
await knex(foreignTableName).delete();
|
||||
}
|
||||
|
||||
public static async dropPostgresTable(knex: Knex): Promise<void> {
|
||||
const tableName = this.getCompanionDefinition().entityConfiguration.tableName;
|
||||
const hasTable = await knex.schema.hasTable(tableName);
|
||||
if (hasTable) {
|
||||
await knex.schema.dropTable(tableName);
|
||||
}
|
||||
|
||||
const hasForeignTable = await knex.schema.hasTable(foreignTableName);
|
||||
if (hasForeignTable) {
|
||||
await knex.schema.dropTable(foreignTableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ErrorsTestEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity
|
||||
> {
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
ErrorsTestEntityFields,
|
||||
number,
|
||||
ViewerContext,
|
||||
ErrorsTestEntity
|
||||
>(),
|
||||
];
|
||||
}
|
||||
|
||||
export const ErrorsTestEntityConfiguration = new EntityConfiguration<ErrorsTestEntityFields>({
|
||||
idField: 'id',
|
||||
tableName: 'postgres_test_entities',
|
||||
schema: {
|
||||
id: new NumberField({
|
||||
columnName: 'id',
|
||||
}),
|
||||
fieldNonNull: new StringField({
|
||||
columnName: 'field_non_null',
|
||||
}),
|
||||
fieldForeignKey: new NumberField({
|
||||
columnName: 'field_foreign_key',
|
||||
}),
|
||||
fieldUnique: new StringField({
|
||||
columnName: 'field_unique',
|
||||
}),
|
||||
checkLessThan5: new NumberField({
|
||||
columnName: 'check_less_than_5',
|
||||
}),
|
||||
fieldExclusion: new StringField({
|
||||
columnName: 'field_exclusion',
|
||||
}),
|
||||
nonExistentColumn: new StringField({
|
||||
columnName: 'non_existent_column',
|
||||
}),
|
||||
},
|
||||
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
|
||||
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
|
||||
});
|
||||
|
||||
const errorsTestEntityCompanionDefinition = new EntityCompanionDefinition({
|
||||
entityClass: ErrorsTestEntity,
|
||||
entityConfiguration: ErrorsTestEntityConfiguration,
|
||||
privacyPolicyClass: ErrorsTestEntityPrivacyPolicy,
|
||||
});
|
||||
@@ -3,6 +3,17 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove any cast from EntityLoader ([#100](https://github.com/expo/entity/issues/100)) ([b8e07f9](https://github.com/expo/entity/commit/b8e07f9ddc4077768980c000d61f5ddcc824e2e3))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@expo/entity-example",
|
||||
"private": true,
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.0",
|
||||
"description": "An example integration of the @expo/entity framework",
|
||||
"scripts": {
|
||||
"tsc": "tsc",
|
||||
@@ -22,7 +22,7 @@
|
||||
"author": "Expo",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@expo/entity": "^0.10.0",
|
||||
"@expo/entity": "^0.11.0",
|
||||
"apollo-server-koa": "^2.16.1",
|
||||
"graphql": "^15.3.0",
|
||||
"koa": "^2.13.0",
|
||||
|
||||
@@ -23,8 +23,9 @@ import { ExampleViewerContext } from '../viewerContexts';
|
||||
*/
|
||||
export default class AllowIfUserOwnerPrivacyRule<
|
||||
TFields,
|
||||
TID,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, ExampleViewerContext>
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, ExampleViewerContext>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends PrivacyPolicyRule<TFields, TID, ExampleViewerContext, TEntity> {
|
||||
constructor(private readonly entityOwnerField: keyof TFields) {
|
||||
super();
|
||||
|
||||
@@ -3,6 +3,17 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove any cast from EntityLoader ([#100](https://github.com/expo/entity/issues/100)) ([b8e07f9](https://github.com/expo/entity/commit/b8e07f9ddc4077768980c000d61f5ddcc824e2e3))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@expo/entity-full-integration-tests",
|
||||
"private": true,
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.0",
|
||||
"description": "Full redis and knex integration tests for the entity framework",
|
||||
"scripts": {
|
||||
"tsc": "tsc",
|
||||
@@ -26,8 +26,8 @@
|
||||
"@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"
|
||||
"@expo/entity": "^0.11.0",
|
||||
"@expo/entity-cache-adapter-redis": "^0.11.0",
|
||||
"@expo/entity-database-adapter-knex": "^0.11.0"
|
||||
}
|
||||
}
|
||||
+12
-4
@@ -28,10 +28,18 @@ class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
ViewerContext,
|
||||
TestEntity
|
||||
> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<TestFields, string, ViewerContext, TestEntity>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<TestFields, string, ViewerContext, TestEntity>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<TestFields, string, ViewerContext, TestEntity>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<TestFields, string, ViewerContext, TestEntity>(),
|
||||
];
|
||||
}
|
||||
|
||||
class TestEntity extends Entity<TestFields, string, ViewerContext> {
|
||||
|
||||
+12
-4
@@ -29,10 +29,18 @@ interface OtherFields {
|
||||
}
|
||||
|
||||
class PrivacyPolicy extends EntityPrivacyPolicy<any, string, ViewerContext, any> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
|
||||
+12
-4
@@ -19,10 +19,18 @@ interface ChildFields {
|
||||
}
|
||||
|
||||
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()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
export default class ChildEntity extends Entity<ChildFields, string, ViewerContext> {
|
||||
|
||||
+12
-4
@@ -17,10 +17,18 @@ interface ParentFields {
|
||||
}
|
||||
|
||||
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()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
export default class ParentEntity extends Entity<ParentFields, string, ViewerContext> {
|
||||
|
||||
@@ -3,6 +3,25 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.11.0](https://github.com/expo/entity/compare/v0.10.0...v0.11.0) (2020-12-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add type generic default in test utils ([#104](https://github.com/expo/entity/issues/104)) ([fe2917e](https://github.com/expo/entity/commit/fe2917e6b5af808f166aa354d476e168c41e6d21))
|
||||
* maps utils invariants for collections containing undefined values ([#103](https://github.com/expo/entity/issues/103)) ([29c1a43](https://github.com/expo/entity/commit/29c1a434ab6cd8d30e9f0e7c742065c8d46d8d8a))
|
||||
* remove any cast from EntityLoader ([#100](https://github.com/expo/entity/issues/100)) ([b8e07f9](https://github.com/expo/entity/commit/b8e07f9ddc4077768980c000d61f5ddcc824e2e3))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* better cache adapter error handling ([#102](https://github.com/expo/entity/issues/102)) ([15546aa](https://github.com/expo/entity/commit/15546aad97734ab9ac67f1c012485f096a0e94f1))
|
||||
* better database adapter error handling ([#101](https://github.com/expo/entity/issues/101)) ([5208aee](https://github.com/expo/entity/commit/5208aeedb0192f0460ba3d575770a04175f5c3aa))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# [0.10.0](https://github.com/expo/entity/compare/v0.9.1...v0.10.0) (2020-11-17)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@expo/entity",
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.0",
|
||||
"description": "A privacy-first data model",
|
||||
"files": [
|
||||
"build",
|
||||
|
||||
@@ -11,7 +11,7 @@ import { mapMap } from './utils/collections/maps';
|
||||
*/
|
||||
export default class EnforcingEntityLoader<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -27,7 +27,7 @@ import ViewerContext from './ViewerContext';
|
||||
*/
|
||||
export default abstract class Entity<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields> {
|
||||
@@ -39,7 +39,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static creator<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMViewerContext2 extends TMViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
@@ -80,7 +80,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static updater<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -121,7 +121,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static deleteAsync<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -163,7 +163,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static enforceDeleteAsync<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -216,7 +216,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static async canViewerUpdateAsync<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -265,7 +265,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
static async canViewerDeleteAsync<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -309,7 +309,7 @@ export default abstract class Entity<
|
||||
*/
|
||||
export interface IEntityClass<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -8,7 +8,7 @@ import ViewerContext from './ViewerContext';
|
||||
|
||||
export default class EntityAssociationLoader<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields
|
||||
@@ -25,7 +25,7 @@ export default class EntityAssociationLoader<
|
||||
async loadAssociatedEntityAsync<
|
||||
TIdentifyingField extends keyof Pick<TFields, TSelectedFields>,
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
@@ -87,7 +87,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadManyAssociatedEntitiesAsync<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
@@ -140,7 +140,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadAssociatedEntityByFieldEqualingAsync<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
@@ -197,7 +197,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadManyAssociatedEntitiesByFieldEqualingAsync<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
@@ -253,7 +253,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadAssociatedEntityThroughAsync<
|
||||
TFields2,
|
||||
TID2,
|
||||
TID2 extends NonNullable<TFields2[TSelectedFields2]>,
|
||||
TEntity2 extends ReadonlyEntity<TFields2, TID2, TViewerContext, TSelectedFields2>,
|
||||
TPrivacyPolicy2 extends EntityPrivacyPolicy<
|
||||
TFields2,
|
||||
@@ -287,7 +287,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadAssociatedEntityThroughAsync<
|
||||
TFields2,
|
||||
TID2,
|
||||
TID2 extends NonNullable<TFields2[TSelectedFields2]>,
|
||||
TEntity2 extends ReadonlyEntity<TFields2, TID2, TViewerContext, TSelectedFields2>,
|
||||
TPrivacyPolicy2 extends EntityPrivacyPolicy<
|
||||
TFields2,
|
||||
@@ -297,7 +297,7 @@ export default class EntityAssociationLoader<
|
||||
TSelectedFields2
|
||||
>,
|
||||
TFields3,
|
||||
TID3,
|
||||
TID3 extends NonNullable<TFields3[TSelectedFields3]>,
|
||||
TEntity3 extends ReadonlyEntity<TFields3, TID3, TViewerContext, TSelectedFields3>,
|
||||
TPrivacyPolicy3 extends EntityPrivacyPolicy<
|
||||
TFields3,
|
||||
@@ -342,7 +342,7 @@ export default class EntityAssociationLoader<
|
||||
*/
|
||||
async loadAssociatedEntityThroughAsync<
|
||||
TFields2,
|
||||
TID2,
|
||||
TID2 extends NonNullable<TFields2[TSelectedFields2]>,
|
||||
TEntity2 extends ReadonlyEntity<TFields2, TID2, TViewerContext, TSelectedFields2>,
|
||||
TPrivacyPolicy2 extends EntityPrivacyPolicy<
|
||||
TFields2,
|
||||
@@ -352,7 +352,7 @@ export default class EntityAssociationLoader<
|
||||
TSelectedFields2
|
||||
>,
|
||||
TFields3,
|
||||
TID3,
|
||||
TID3 extends NonNullable<TFields3[TSelectedFields3]>,
|
||||
TEntity3 extends ReadonlyEntity<TFields3, TID3, TViewerContext, TSelectedFields3>,
|
||||
TPrivacyPolicy3 extends EntityPrivacyPolicy<
|
||||
TFields3,
|
||||
@@ -362,7 +362,7 @@ export default class EntityAssociationLoader<
|
||||
TSelectedFields3
|
||||
>,
|
||||
TFields4,
|
||||
TID4,
|
||||
TID4 extends NonNullable<TFields4[TSelectedFields4]>,
|
||||
TEntity4 extends ReadonlyEntity<TFields4, TID4, TViewerContext, TSelectedFields4>,
|
||||
TPrivacyPolicy4 extends EntityPrivacyPolicy<
|
||||
TFields4,
|
||||
@@ -478,7 +478,7 @@ export interface EntityLoadThroughDirective<
|
||||
TViewerContext extends ViewerContext,
|
||||
TFields,
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
|
||||
@@ -19,7 +19,7 @@ export interface IPrivacyPolicyClass<TPrivacyPolicy> {
|
||||
*/
|
||||
export default class EntityCompanion<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -60,7 +60,7 @@ export interface CacheAdapterFlavorDefinition {
|
||||
*/
|
||||
export class EntityCompanionDefinition<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -181,7 +181,7 @@ export default class EntityCompanionProvider {
|
||||
*/
|
||||
getCompanionForEntity<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import ES6Error from 'es6-error';
|
||||
|
||||
import { IEntityClass } from './Entity';
|
||||
import EntityPrivacyPolicy, { EntityAuthorizationAction } from './EntityPrivacyPolicy';
|
||||
import ReadonlyEntity from './ReadonlyEntity';
|
||||
import ViewerContext from './ViewerContext';
|
||||
|
||||
export abstract class EntityError extends ES6Error {}
|
||||
|
||||
export class EntityNotFoundError<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TSelectedFields
|
||||
>,
|
||||
N extends keyof TFields,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends EntityError {
|
||||
constructor(
|
||||
entityClass: IEntityClass<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TPrivacyPolicy,
|
||||
TSelectedFields
|
||||
>,
|
||||
fieldName: N,
|
||||
fieldValue: TFields[N]
|
||||
) {
|
||||
super(`Entity not found: ${entityClass.name} (${fieldName} = ${fieldValue})`);
|
||||
}
|
||||
}
|
||||
|
||||
export class EntityNotAuthorizedError<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends EntityError {
|
||||
public readonly entityClassName: string;
|
||||
|
||||
constructor(
|
||||
entity: TEntity,
|
||||
viewerContext: TViewerContext,
|
||||
action: EntityAuthorizationAction,
|
||||
ruleIndex: number
|
||||
) {
|
||||
super(
|
||||
`Entity not authorized: ${entity} (viewer = ${viewerContext}, action = ${EntityAuthorizationAction[action]}, ruleIndex = ${ruleIndex})`
|
||||
);
|
||||
this.entityClassName = entity.constructor.name;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ export enum EntityEdgeDeletionBehavior {
|
||||
export interface EntityAssociationDefinition<
|
||||
TViewerContext extends ViewerContext,
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>,
|
||||
TAssociatedEntity extends ReadonlyEntity<
|
||||
TAssociatedFields,
|
||||
TAssociatedID,
|
||||
|
||||
@@ -4,11 +4,11 @@ import invariant from 'invariant';
|
||||
import EnforcingEntityLoader from './EnforcingEntityLoader';
|
||||
import { IEntityClass } from './Entity';
|
||||
import { FieldEqualityCondition, QuerySelectionModifiers } from './EntityDatabaseAdapter';
|
||||
import { EntityNotFoundError } from './EntityErrors';
|
||||
import EntityPrivacyPolicy from './EntityPrivacyPolicy';
|
||||
import { EntityQueryContext } from './EntityQueryContext';
|
||||
import ReadonlyEntity from './ReadonlyEntity';
|
||||
import ViewerContext from './ViewerContext';
|
||||
import EntityNotFoundError from './errors/EntityNotFoundError';
|
||||
import EntityDataManager from './internal/EntityDataManager';
|
||||
import { mapMap, mapMapAsync } from './utils/collections/maps';
|
||||
|
||||
@@ -18,7 +18,7 @@ import { mapMap, mapMapAsync } from './utils/collections/maps';
|
||||
*/
|
||||
export default class EntityLoader<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -150,7 +150,7 @@ export default class EntityLoader<
|
||||
const entityResults = await this.loadManyByIDsAsync([id]);
|
||||
const entityResult = entityResults.get(id);
|
||||
if (entityResult === undefined) {
|
||||
return result(new EntityNotFoundError(this.entityClass, this.idField, id as any));
|
||||
return result(new EntityNotFoundError(this.entityClass, this.idField, id));
|
||||
}
|
||||
return entityResult;
|
||||
}
|
||||
@@ -161,7 +161,7 @@ export default class EntityLoader<
|
||||
* @returns entity result for matching ID, or null if no entity exists for ID.
|
||||
*/
|
||||
async loadByIDNullableAsync(id: TID): Promise<Result<TEntity> | null> {
|
||||
return await this.loadByFieldEqualingAsync(this.idField, id as any);
|
||||
return await this.loadByFieldEqualingAsync(this.idField, id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,15 +171,13 @@ export default class EntityLoader<
|
||||
* @returns map from ID to corresponding entity result, where result error can be UnauthorizedError or EntityNotFoundError.
|
||||
*/
|
||||
async loadManyByIDsAsync(ids: readonly TID[]): Promise<ReadonlyMap<TID, Result<TEntity>>> {
|
||||
const entityResults = ((await this.loadManyByFieldEqualingManyAsync(
|
||||
const entityResults = (await this.loadManyByFieldEqualingManyAsync(
|
||||
this.idField,
|
||||
ids as any
|
||||
)) as any) as ReadonlyMap<TID, readonly Result<TEntity>[]>;
|
||||
ids
|
||||
)) as ReadonlyMap<TID, readonly Result<TEntity>[]>;
|
||||
return mapMap(entityResults, (entityResultsForId, id) => {
|
||||
const entityResult = entityResultsForId[0];
|
||||
return (
|
||||
entityResult ?? result(new EntityNotFoundError(this.entityClass, this.idField, id as any))
|
||||
);
|
||||
return entityResult ?? result(new EntityNotFoundError(this.entityClass, this.idField, id));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -284,13 +282,6 @@ export default class EntityLoader<
|
||||
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.
|
||||
|
||||
@@ -11,7 +11,7 @@ import EntityDataManager from './internal/EntityDataManager';
|
||||
*/
|
||||
export default class EntityLoaderFactory<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -7,7 +7,7 @@ import ViewerContext from './ViewerContext';
|
||||
*/
|
||||
export default interface EntityMutationTriggerConfiguration<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
@@ -70,7 +70,7 @@ export default interface EntityMutationTriggerConfiguration<
|
||||
*/
|
||||
export abstract class EntityMutationTrigger<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
@@ -88,7 +88,7 @@ export abstract class EntityMutationTrigger<
|
||||
*/
|
||||
export abstract class EntityNonTransactionalMutationTrigger<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -8,7 +8,7 @@ import ViewerContext from './ViewerContext';
|
||||
*/
|
||||
export default abstract class EntityMutationValidator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -21,7 +21,7 @@ import { mapMapAsync } from './utils/collections/maps';
|
||||
|
||||
abstract class BaseMutator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends Entity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -114,7 +114,7 @@ abstract class BaseMutator<
|
||||
*/
|
||||
export class CreateMutator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends Entity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -205,7 +205,6 @@ export class CreateMutator<
|
||||
queryContext.appendPostCommitCallback(
|
||||
entityLoader.invalidateFieldsAsync.bind(entityLoader, insertResult)
|
||||
);
|
||||
await entityLoader.invalidateFieldsTransactionalAsync(insertResult);
|
||||
|
||||
const unauthorizedEntityAfterInsert = new this.entityClass(this.viewerContext, insertResult);
|
||||
const newEntity = await entityLoader
|
||||
@@ -240,7 +239,7 @@ export class CreateMutator<
|
||||
*/
|
||||
export class UpdateMutator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends Entity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -396,11 +395,6 @@ export class UpdateMutator<
|
||||
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
|
||||
.enforcing()
|
||||
@@ -434,7 +428,7 @@ export class UpdateMutator<
|
||||
*/
|
||||
export class DeleteMutator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends Entity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -543,7 +537,7 @@ export class DeleteMutator<
|
||||
return authorizeDeleteResult;
|
||||
}
|
||||
|
||||
await DeleteMutator.processEntityDeletionForInboundEdgesAsync(
|
||||
await this.processEntityDeletionForInboundEdgesAsync(
|
||||
this.entity,
|
||||
queryContext,
|
||||
processedEntityIdentifiersFromTransitiveDeletions
|
||||
@@ -572,7 +566,6 @@ export class DeleteMutator<
|
||||
queryContext.appendPostCommitCallback(
|
||||
entityLoader.invalidateFieldsAsync.bind(entityLoader, this.entity.getAllDatabaseFields())
|
||||
);
|
||||
await entityLoader.invalidateFieldsTransactionalAsync(this.entity.getAllDatabaseFields());
|
||||
|
||||
await this.executeMutationTriggersOrValidatorsAsync(
|
||||
this.mutationTriggers.afterDelete,
|
||||
@@ -610,14 +603,8 @@ export class DeleteMutator<
|
||||
*
|
||||
* @param entity - entity to find all references to
|
||||
*/
|
||||
private static async processEntityDeletionForInboundEdgesAsync<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMSelectedFields extends keyof TMFields
|
||||
>(
|
||||
entity: TMEntity,
|
||||
private async processEntityDeletionForInboundEdgesAsync(
|
||||
entity: TEntity,
|
||||
queryContext: EntityTransactionalQueryContext,
|
||||
processedEntityIdentifiers: Set<string>
|
||||
): Promise<void> {
|
||||
@@ -628,12 +615,12 @@ export class DeleteMutator<
|
||||
processedEntityIdentifiers.add(entity.getUniqueIdentifier());
|
||||
|
||||
const companionDefinition = (entity.constructor as any).getCompanionDefinition() as EntityCompanionDefinition<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMViewerContext,
|
||||
TMEntity,
|
||||
EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity, TMSelectedFields>,
|
||||
TMSelectedFields
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TPrivacyPolicy,
|
||||
TSelectedFields
|
||||
>;
|
||||
const entityConfiguration = companionDefinition.entityConfiguration;
|
||||
const inboundEdges = entityConfiguration.getInboundEdges();
|
||||
|
||||
@@ -15,7 +15,7 @@ import IEntityMetricsAdapter from './metrics/IEntityMetricsAdapter';
|
||||
*/
|
||||
export default class EntityMutatorFactory<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends Entity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { EntityNotAuthorizedError } from './EntityErrors';
|
||||
import { EntityQueryContext } from './EntityQueryContext';
|
||||
import ReadonlyEntity from './ReadonlyEntity';
|
||||
import ViewerContext from './ViewerContext';
|
||||
import EntityNotAuthorizedError from './errors/EntityNotAuthorizedError';
|
||||
import PrivacyPolicyRule, { RuleEvaluationResult } from './rules/PrivacyPolicyRule';
|
||||
|
||||
export enum EntityPrivacyPolicyEvaluationMode {
|
||||
@@ -12,7 +12,7 @@ export enum EntityPrivacyPolicyEvaluationMode {
|
||||
|
||||
export type EntityPrivacyPolicyEvaluator<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
@@ -63,7 +63,7 @@ export enum EntityAuthorizationAction {
|
||||
*/
|
||||
export default abstract class EntityPrivacyPolicy<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
@@ -265,7 +265,13 @@ export default abstract class EntityPrivacyPolicy<
|
||||
const ruleEvaluationResult = await rule.evaluateAsync(viewerContext, queryContext, entity);
|
||||
switch (ruleEvaluationResult) {
|
||||
case RuleEvaluationResult.DENY:
|
||||
throw new EntityNotAuthorizedError(entity, viewerContext, action, i);
|
||||
throw new EntityNotAuthorizedError<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TSelectedFields
|
||||
>(entity, viewerContext, action, i);
|
||||
case RuleEvaluationResult.SKIP:
|
||||
continue;
|
||||
case RuleEvaluationResult.ALLOW:
|
||||
@@ -275,6 +281,11 @@ export default abstract class EntityPrivacyPolicy<
|
||||
}
|
||||
}
|
||||
|
||||
throw new EntityNotAuthorizedError(entity, viewerContext, action, -1);
|
||||
throw new EntityNotAuthorizedError<TFields, TID, TViewerContext, TEntity, TSelectedFields>(
|
||||
entity,
|
||||
viewerContext,
|
||||
action,
|
||||
-1
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import EntityQueryContextProvider from './EntityQueryContextProvider';
|
||||
|
||||
export type PostCommitCallback = (...args: any) => Promise<any>;
|
||||
@@ -14,7 +12,7 @@ export type PostCommitCallback = (...args: any) => Promise<any>;
|
||||
export abstract class EntityQueryContext {
|
||||
constructor(private readonly queryInterface: any) {}
|
||||
|
||||
abstract isInTransaction(): this is EntityTransactionalQueryContext;
|
||||
abstract isInTransaction(): boolean;
|
||||
|
||||
getQueryInterface(): any {
|
||||
return this.queryInterface;
|
||||
@@ -33,7 +31,7 @@ export class EntityNonTransactionalQueryContext extends EntityQueryContext {
|
||||
super(queryInterface);
|
||||
}
|
||||
|
||||
isInTransaction(): this is EntityTransactionalQueryContext {
|
||||
isInTransaction(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -45,8 +43,6 @@ export class EntityNonTransactionalQueryContext extends EntityQueryContext {
|
||||
}
|
||||
|
||||
export class EntityTransactionalQueryContext extends EntityQueryContext {
|
||||
public readonly transactionID = uuidv4();
|
||||
|
||||
private readonly postCommitCallbacks: PostCommitCallback[] = [];
|
||||
|
||||
public appendPostCommitCallback(callback: PostCommitCallback): void {
|
||||
@@ -59,7 +55,7 @@ export class EntityTransactionalQueryContext extends EntityQueryContext {
|
||||
await Promise.all(callbacks.map((callback) => callback()));
|
||||
}
|
||||
|
||||
isInTransaction(): this is EntityTransactionalQueryContext {
|
||||
isInTransaction(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import { pick } from './entityUtils';
|
||||
*/
|
||||
export default abstract class ReadonlyEntity<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> {
|
||||
@@ -124,7 +124,7 @@ export default abstract class ReadonlyEntity<
|
||||
*/
|
||||
static getQueryContext<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMViewerContext2 extends TMViewerContext,
|
||||
TMEntity extends ReadonlyEntity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
@@ -161,7 +161,7 @@ export default abstract class ReadonlyEntity<
|
||||
static async runInTransactionAsync<
|
||||
TResult,
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMViewerContext2 extends TMViewerContext,
|
||||
TMEntity extends ReadonlyEntity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
@@ -199,7 +199,7 @@ export default abstract class ReadonlyEntity<
|
||||
*/
|
||||
static loader<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMViewerContext2 extends TMViewerContext,
|
||||
TMEntity extends ReadonlyEntity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
|
||||
@@ -27,7 +27,7 @@ export default class ViewerContext {
|
||||
|
||||
getViewerScopedEntityCompanionForClass<
|
||||
TMFields,
|
||||
TMID,
|
||||
TMID extends NonNullable<TMFields[TMSelectedFields]>,
|
||||
TMViewerContext extends ViewerContext,
|
||||
TMEntity extends ReadonlyEntity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
|
||||
TMPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -12,7 +12,7 @@ import ViewerScopedEntityMutatorFactory from './ViewerScopedEntityMutatorFactory
|
||||
*/
|
||||
export default class ViewerScopedEntityCompanion<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -23,7 +23,7 @@ export default class ViewerScopedEntityCompanionProvider {
|
||||
*/
|
||||
getViewerScopedCompanionForEntity<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -10,7 +10,7 @@ import ViewerContext from './ViewerContext';
|
||||
*/
|
||||
export default class ViewerScopedEntityLoaderFactory<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -10,7 +10,7 @@ import ViewerContext from './ViewerContext';
|
||||
*/
|
||||
export default class ViewerScopedEntityMutatorFactory<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
|
||||
@@ -287,7 +287,6 @@ describe(EnforcingEntityLoader, () => {
|
||||
'enforcing',
|
||||
'invalidateFieldsAsync',
|
||||
'invalidateEntityAsync',
|
||||
'invalidateFieldsTransactionalAsync',
|
||||
'tryConstructEntities',
|
||||
];
|
||||
expect(loaderProperties).toEqual(expect.arrayContaining(knownLoaderOnlyDifferences));
|
||||
|
||||
@@ -106,10 +106,38 @@ class SimpleTestDenyUpdateEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
ViewerContext,
|
||||
SimpleTestDenyUpdateEntity
|
||||
> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysDenyPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyUpdateEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyUpdateEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysDenyPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyUpdateEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyUpdateEntity
|
||||
>(),
|
||||
];
|
||||
}
|
||||
|
||||
class SimpleTestDenyDeleteEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
@@ -118,10 +146,38 @@ class SimpleTestDenyDeleteEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
ViewerContext,
|
||||
SimpleTestDenyDeleteEntity
|
||||
> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysDenyPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyDeleteEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyDeleteEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyDeleteEntity
|
||||
>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysDenyPrivacyPolicyRule<
|
||||
TestEntityFields,
|
||||
string,
|
||||
ViewerContext,
|
||||
SimpleTestDenyDeleteEntity
|
||||
>(),
|
||||
];
|
||||
}
|
||||
|
||||
class SimpleTestDenyUpdateEntity extends Entity<TestEntityFields, string, ViewerContext> {
|
||||
|
||||
@@ -7,12 +7,12 @@ import EntityCompanionProvider, {
|
||||
EntityCompanionDefinition,
|
||||
} from '../EntityCompanionProvider';
|
||||
import EntityConfiguration from '../EntityConfiguration';
|
||||
import { EntityNotAuthorizedError } from '../EntityErrors';
|
||||
import { UUIDField } from '../EntityFields';
|
||||
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
|
||||
import { EntityQueryContext } from '../EntityQueryContext';
|
||||
import ViewerContext from '../ViewerContext';
|
||||
import { enforceResultsAsync } from '../entityUtils';
|
||||
import EntityNotAuthorizedError from '../errors/EntityNotAuthorizedError';
|
||||
import AlwaysAllowPrivacyPolicyRule from '../rules/AlwaysAllowPrivacyPolicyRule';
|
||||
import AlwaysDenyPrivacyPolicyRule from '../rules/AlwaysDenyPrivacyPolicyRule';
|
||||
import PrivacyPolicyRule, { RuleEvaluationResult } from '../rules/PrivacyPolicyRule';
|
||||
|
||||
@@ -34,10 +34,18 @@ class TestEntityPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
any,
|
||||
any
|
||||
> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
CacheAdapterFlavor,
|
||||
} from '../EntityCompanionProvider';
|
||||
import EntityConfiguration from '../EntityConfiguration';
|
||||
import { EntityNotAuthorizedError } from '../EntityErrors';
|
||||
import { UUIDField } from '../EntityFields';
|
||||
import EntityPrivacyPolicy, {
|
||||
EntityPrivacyPolicyEvaluator,
|
||||
@@ -15,6 +14,7 @@ import EntityPrivacyPolicy, {
|
||||
} from '../EntityPrivacyPolicy';
|
||||
import { EntityQueryContext } from '../EntityQueryContext';
|
||||
import ViewerContext from '../ViewerContext';
|
||||
import EntityNotAuthorizedError from '../errors/EntityNotAuthorizedError';
|
||||
import AlwaysAllowPrivacyPolicyRule from '../rules/AlwaysAllowPrivacyPolicyRule';
|
||||
import AlwaysDenyPrivacyPolicyRule from '../rules/AlwaysDenyPrivacyPolicyRule';
|
||||
import AlwaysSkipPrivacyPolicyRule from '../rules/AlwaysSkipPrivacyPolicyRule';
|
||||
|
||||
@@ -25,10 +25,18 @@ class CategoryPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
any,
|
||||
any
|
||||
> {
|
||||
protected readonly readRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly createRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly updateRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly deleteRules = [new AlwaysAllowPrivacyPolicyRule()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<CategoryFields, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<CategoryFields, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<CategoryFields, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<CategoryFields, string, TestViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
|
||||
@@ -111,10 +111,18 @@ const testEntityConfiguration = new EntityConfiguration<TestFields>({
|
||||
});
|
||||
|
||||
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()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
class OneTestEntity extends Entity<TestFields, string, ViewerContext, OneTestFields> {
|
||||
|
||||
@@ -119,10 +119,18 @@ const testEntityConfiguration = new EntityConfiguration<TestFields>({
|
||||
});
|
||||
|
||||
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()];
|
||||
protected readonly readRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly createRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly updateRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
protected readonly deleteRules = [
|
||||
new AlwaysAllowPrivacyPolicyRule<any, string, ViewerContext, any, any>(),
|
||||
];
|
||||
}
|
||||
|
||||
class OneTestEntity extends Entity<TestFields, string, ViewerContext, OneTestFields> {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import EntityError, { EntityErrorCode, EntityErrorState } from './EntityError';
|
||||
|
||||
export default abstract class EntityCacheAdapterError extends EntityError {}
|
||||
|
||||
export class EntityCacheAdapterTransientError extends EntityCacheAdapterError {
|
||||
public readonly state = EntityErrorState.TRANSIENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_CACHE_ADAPTER_TRANSIENT;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import EntityError, { EntityErrorCode, EntityErrorState } from './EntityError';
|
||||
|
||||
export default abstract class EntityDatabaseAdapterError extends EntityError {}
|
||||
|
||||
export class EntityDatabaseAdapterTransientError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.TRANSIENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_TRANSIENT;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterUnknownError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.UNKNOWN;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_UNKNOWN;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterCheckConstraintError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_CHECK_CONSTRAINT;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterExclusionConstraintError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_EXCLUSION_CONSTRAINT;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterForeignKeyConstraintError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_FOREIGN_KEY_CONSTRAINT;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterNotNullConstraintError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_NOT_NULL_CONSTRAINT;
|
||||
}
|
||||
|
||||
export class EntityDatabaseAdapterUniqueConstraintError extends EntityDatabaseAdapterError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_DATABASE_ADAPTER_UNIQUE_CONSTRAINT;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import ES6Error from 'es6-error';
|
||||
|
||||
export enum EntityErrorState {
|
||||
UNKNOWN,
|
||||
TRANSIENT,
|
||||
PERMANENT,
|
||||
}
|
||||
|
||||
export enum EntityErrorCode {
|
||||
ERR_ENTITY_NOT_AUTHORIZED = 'ERR_ENTITY_NOT_AUTHORIZED',
|
||||
ERR_ENTITY_NOT_FOUND = 'ERR_ENTITY_NOT_FOUND',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_TRANSIENT = 'ERR_ENTITY_DATABASE_ADAPTER_TRANSIENT',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_UNKNOWN = 'ERR_ENTITY_DATABASE_ADAPTER_UNKNOWN',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_CHECK_CONSTRAINT = 'ERR_ENTITY_DATABASE_ADAPTER_CHECK_CONSTRAINT',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_EXCLUSION_CONSTRAINT = 'ERR_ENTITY_DATABASE_ADAPTER_EXCLUSION_CONSTRAINT',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_FOREIGN_KEY_CONSTRAINT = 'ERR_ENTITY_DATABASE_ADAPTER_FOREIGN_KEY_CONSTRAINT',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_NOT_NULL_CONSTRAINT = 'ERR_ENTITY_DATABASE_ADAPTER_NOT_NULL_CONSTRAINT',
|
||||
ERR_ENTITY_DATABASE_ADAPTER_UNIQUE_CONSTRAINT = 'ERR_ENTITY_DATABASE_ADAPTER_UNIQUE_CONSTRAINT',
|
||||
ERR_ENTITY_CACHE_ADAPTER_TRANSIENT = 'ERR_ENTITY_CACHE_ADAPTER_TRANSIENT',
|
||||
}
|
||||
|
||||
export default abstract class EntityError extends ES6Error {
|
||||
public abstract readonly state: EntityErrorState;
|
||||
public abstract readonly code: EntityErrorCode;
|
||||
|
||||
constructor(message: string, public readonly cause?: Error) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { EntityAuthorizationAction } from '../EntityPrivacyPolicy';
|
||||
import ReadonlyEntity from '../ReadonlyEntity';
|
||||
import ViewerContext from '../ViewerContext';
|
||||
import EntityError, { EntityErrorCode, EntityErrorState } from './EntityError';
|
||||
|
||||
export default class EntityNotAuthorizedError<
|
||||
TFields,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends EntityError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_NOT_AUTHORIZED;
|
||||
|
||||
public readonly entityClassName: string;
|
||||
|
||||
constructor(
|
||||
entity: TEntity,
|
||||
viewerContext: TViewerContext,
|
||||
action: EntityAuthorizationAction,
|
||||
ruleIndex: number
|
||||
) {
|
||||
super(
|
||||
`Entity not authorized: ${entity} (viewer = ${viewerContext}, action = ${EntityAuthorizationAction[action]}, ruleIndex = ${ruleIndex})`
|
||||
);
|
||||
this.entityClassName = entity.constructor.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { IEntityClass } from '../Entity';
|
||||
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
|
||||
import ReadonlyEntity from '../ReadonlyEntity';
|
||||
import ViewerContext from '../ViewerContext';
|
||||
import EntityError, { EntityErrorCode, EntityErrorState } from './EntityError';
|
||||
|
||||
export default class EntityNotFoundError<
|
||||
TFields,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TPrivacyPolicy extends EntityPrivacyPolicy<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TSelectedFields
|
||||
>,
|
||||
N extends keyof TFields,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
> extends EntityError {
|
||||
public readonly state = EntityErrorState.PERMANENT;
|
||||
public readonly code = EntityErrorCode.ERR_ENTITY_NOT_FOUND;
|
||||
|
||||
constructor(
|
||||
entityClass: IEntityClass<
|
||||
TFields,
|
||||
TID,
|
||||
TViewerContext,
|
||||
TEntity,
|
||||
TPrivacyPolicy,
|
||||
TSelectedFields
|
||||
>,
|
||||
fieldName: N,
|
||||
fieldValue: TFields[N]
|
||||
) {
|
||||
super(`Entity not found: ${entityClass.name} (${fieldName} = ${fieldValue})`);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,14 @@ export * from './EntityCompanionProvider';
|
||||
export { default as EntityConfiguration } from './EntityConfiguration';
|
||||
export { default as EntityDatabaseAdapter } from './EntityDatabaseAdapter';
|
||||
export * from './EntityDatabaseAdapter';
|
||||
export * from './EntityErrors';
|
||||
export { default as EntityDatabaseAdapterError } from './errors/EntityDatabaseAdapterError';
|
||||
export * from './errors/EntityDatabaseAdapterError';
|
||||
export { default as EntityCacheAdapterError } from './errors/EntityCacheAdapterError';
|
||||
export * from './errors/EntityCacheAdapterError';
|
||||
export * from './errors/EntityError';
|
||||
export { default as EntityError } from './errors/EntityError';
|
||||
export { default as EntityNotAuthorizedError } from './errors/EntityNotAuthorizedError';
|
||||
export { default as EntityNotFoundError } from './errors/EntityNotFoundError';
|
||||
export * from './EntityFields';
|
||||
export { default as EntityLoader } from './EntityLoader';
|
||||
export { default as EntityLoaderFactory } from './EntityLoaderFactory';
|
||||
|
||||
@@ -4,7 +4,7 @@ import EntityDatabaseAdapter, {
|
||||
FieldEqualityCondition,
|
||||
QuerySelectionModifiers,
|
||||
} from '../EntityDatabaseAdapter';
|
||||
import { EntityQueryContext, EntityTransactionalQueryContext } from '../EntityQueryContext';
|
||||
import { EntityQueryContext } from '../EntityQueryContext';
|
||||
import EntityQueryContextProvider from '../EntityQueryContextProvider';
|
||||
import { partitionErrors } from '../entityUtils';
|
||||
import {
|
||||
@@ -15,13 +15,6 @@ 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}.
|
||||
@@ -29,11 +22,9 @@ type FieldDataLoadersMap<TFields> = Map<keyof TFields, FieldDataLoader<TFields>>
|
||||
* It is also responsible for invalidating all sources of data when mutated using {@link EntityMutator}.
|
||||
*/
|
||||
export default class EntityDataManager<TFields> {
|
||||
private readonly regularFieldDataLoaders: FieldDataLoadersMap<TFields> = new Map();
|
||||
|
||||
private readonly transactionalFieldDataLoaders: Map<
|
||||
string,
|
||||
FieldDataLoadersMap<TFields>
|
||||
private readonly fieldDataLoaders: Map<
|
||||
keyof TFields,
|
||||
DataLoader<NonNullable<TFields[keyof TFields]>, readonly Readonly<TFields>[]>
|
||||
> = new Map();
|
||||
|
||||
constructor(
|
||||
@@ -44,10 +35,10 @@ export default class EntityDataManager<TFields> {
|
||||
private readonly entityClassName: string
|
||||
) {}
|
||||
|
||||
private getRegularFieldDataLoderForFieldName<N extends keyof TFields>(
|
||||
private getFieldDataLoaderForFieldName<N extends keyof TFields>(
|
||||
fieldName: N
|
||||
): FieldDataLoader<TFields> {
|
||||
return computeIfAbsent(this.regularFieldDataLoaders, fieldName, () => {
|
||||
): DataLoader<NonNullable<TFields[N]>, readonly Readonly<TFields>[]> {
|
||||
return computeIfAbsent(this.fieldDataLoaders, fieldName, () => {
|
||||
return new DataLoader(
|
||||
async (
|
||||
fieldValues: readonly NonNullable<TFields[N]>[]
|
||||
@@ -62,43 +53,6 @@ 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]>[]
|
||||
@@ -107,11 +61,10 @@ export default class EntityDataManager<TFields> {
|
||||
fieldValueCount: fieldValues.length,
|
||||
entityClassName: this.entityClassName,
|
||||
});
|
||||
|
||||
return await this.entityCache.readManyThroughAsync(
|
||||
fieldName,
|
||||
fieldValues,
|
||||
async (fetcherValues: readonly NonNullable<TFields[N]>[]) => {
|
||||
async (fetcherValues) => {
|
||||
this.metricsAdapter.incrementDataManagerDatabaseLoadCount({
|
||||
fieldValueCount: fieldValues.length,
|
||||
entityClassName: this.entityClassName,
|
||||
@@ -159,13 +112,16 @@ export default class EntityDataManager<TFields> {
|
||||
);
|
||||
}
|
||||
|
||||
// don't cache when in transaction, as rollbacks complicate things significantly
|
||||
if (queryContext.isInTransaction()) {
|
||||
return await this.databaseAdapter.fetchManyWhereAsync(queryContext, fieldName, fieldValues);
|
||||
}
|
||||
|
||||
this.metricsAdapter.incrementDataManagerDataloaderLoadCount({
|
||||
fieldValueCount: fieldValues.length,
|
||||
entityClassName: this.entityClassName,
|
||||
});
|
||||
const dataLoader = queryContext.isInTransaction()
|
||||
? this.getTransactionalFieldDataLoaderForFieldName(queryContext, fieldName)
|
||||
: this.getRegularFieldDataLoderForFieldName(fieldName);
|
||||
const dataLoader = this.getFieldDataLoaderForFieldName(fieldName);
|
||||
const results = await dataLoader.loadMany(fieldValues);
|
||||
const [values, errors] = partitionErrors(results);
|
||||
if (errors.length > 0) {
|
||||
@@ -236,20 +192,7 @@ export default class EntityDataManager<TFields> {
|
||||
fieldValues: readonly NonNullable<TFields[N]>[]
|
||||
): Promise<void> {
|
||||
await this.entityCache.invalidateManyAsync(fieldName, fieldValues);
|
||||
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);
|
||||
const dataLoader = this.getFieldDataLoaderForFieldName(fieldName);
|
||||
fieldValues.forEach((fieldValue) => dataLoader.clear(fieldValue));
|
||||
}
|
||||
|
||||
@@ -272,21 +215,4 @@ 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]>,
|
||||
]);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import PrivacyPolicyRule, { RuleEvaluationResult } from './PrivacyPolicyRule';
|
||||
*/
|
||||
export default class AlwaysAllowPrivacyPolicyRule<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -8,7 +8,7 @@ import PrivacyPolicyRule, { RuleEvaluationResult } from './PrivacyPolicyRule';
|
||||
*/
|
||||
export default class AlwaysDenyPrivacyPolicyRule<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -8,7 +8,7 @@ import PrivacyPolicyRule, { RuleEvaluationResult } from './PrivacyPolicyRule';
|
||||
*/
|
||||
export default class AlwaysSkipPrivacyPolicyRule<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -37,7 +37,7 @@ export enum RuleEvaluationResult {
|
||||
*/
|
||||
export default abstract class PrivacyPolicyRule<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
|
||||
@@ -9,9 +9,7 @@ export const computeIfAbsent = <K, V>(
|
||||
const value = mappingFunction(key);
|
||||
map.set(key, value);
|
||||
}
|
||||
const value = map.get(key);
|
||||
invariant(value !== undefined, 'should always be present');
|
||||
return value!;
|
||||
return map.get(key)!;
|
||||
};
|
||||
|
||||
export const mapMap = <K, V, M>(
|
||||
@@ -32,8 +30,8 @@ export const mapMapAsync = async function <K, V, M>(
|
||||
const resultingMap: Map<K, M> = new Map();
|
||||
await Promise.all(
|
||||
Array.from(map.keys()).map(async (k) => {
|
||||
const initialValue = map.get(k) ?? invariant(false, 'cannot be undefined here');
|
||||
const result = await mapper(initialValue!, k);
|
||||
const initialValue = map.get(k) as V;
|
||||
const result = await mapper(initialValue, k);
|
||||
resultingMap.set(k, result);
|
||||
})
|
||||
);
|
||||
|
||||
@@ -5,7 +5,7 @@ import PrivacyPolicyRule, { RuleEvaluationResult } from '../../rules/PrivacyPoli
|
||||
|
||||
export interface Case<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields
|
||||
@@ -17,7 +17,7 @@ export interface Case<
|
||||
|
||||
type CaseMap<
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields
|
||||
@@ -28,10 +28,10 @@ type CaseMap<
|
||||
*/
|
||||
export const describePrivacyPolicyRuleWithAsyncTestCase = <
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
>(
|
||||
privacyPolicyRule: PrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>,
|
||||
{
|
||||
@@ -85,10 +85,10 @@ export const describePrivacyPolicyRuleWithAsyncTestCase = <
|
||||
*/
|
||||
export const describePrivacyPolicyRule = <
|
||||
TFields,
|
||||
TID,
|
||||
TID extends NonNullable<TFields[TSelectedFields]>,
|
||||
TViewerContext extends ViewerContext,
|
||||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
|
||||
TSelectedFields extends keyof TFields
|
||||
TSelectedFields extends keyof TFields = keyof TFields
|
||||
>(
|
||||
privacyPolicyRule: PrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>,
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user