Skip to content

Commit

Permalink
feat: support entity fields subset of db fields (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman authored Jul 1, 2020
1 parent ccbdbc4 commit 4e40b2e
Show file tree
Hide file tree
Showing 50 changed files with 1,300 additions and 376 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"rules": {
"no-restricted-imports": ["error",{ "paths": [".", "..", "../.."] }],
"tsdoc/syntax": "warn"
"tsdoc/syntax": "warn",
"no-console": "warn"
},
"overrides": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(RedisCacheAdapter, () => {
const cacheAdapter = vc1.entityCompanionProvider.getCompanionForEntity(
RedisTestEntity,
RedisTestEntity.getCompanionDefinition()
)['cacheAdapter'];
)['tableDataCoordinator']['cacheAdapter'];
const cacheKeyMaker = cacheAdapter['makeCacheKey'].bind(cacheAdapter);

// creating an entity should put it in cache (loader loads it after creation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CacheStatus, UUIDField, EntityConfiguration } from '@expo/entity';
import {
CacheStatus,
UUIDField,
EntityConfiguration,
DatabaseAdapterFlavor,
CacheAdapterFlavor,
} from '@expo/entity';
import { Redis, Pipeline } from 'ioredis';
import { mock, when, instance, anything, verify } from 'ts-mockito';

Expand All @@ -14,6 +20,8 @@ const entityConfiguration = new EntityConfiguration<BlahFields>({
schema: {
id: new UUIDField({ columnName: 'id', cache: true }),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});

describe(RedisCacheAdapter, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export const redisTestEntityConfiguration = new EntityConfiguration<RedisTestEnt
columnName: 'date_field',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});

const redisTestEntityCompanionDefinition = {
const redisTestEntityCompanionDefinition = new EntityCompanionDefinition({
entityClass: RedisTestEntity,
entityConfiguration: redisTestEntityConfiguration,
databaseAdaptorFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdaptorFlavor: CacheAdapterFlavor.REDIS,
privacyPolicyClass: RedisTestEntityPrivacyPolicy,
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ export const invalidTestEntityConfiguration = new EntityConfiguration<InvalidTes
columnName: 'name',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});

const invalidTestEntityCompanionDefinition = {
const invalidTestEntityCompanionDefinition = new EntityCompanionDefinition({
entityClass: InvalidTestEntity,
entityConfiguration: invalidTestEntityConfiguration,
databaseAdaptorFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdaptorFlavor: CacheAdapterFlavor.REDIS,
privacyPolicyClass: InvalidTestEntityPrivacyPolicy,
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ export const postgresTestEntityConfiguration = new EntityConfiguration<PostgresT
columnName: 'maybe_json_array_field',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
});

const postgresTestEntityCompanionDefinition = {
const postgresTestEntityCompanionDefinition = new EntityCompanionDefinition({
entityClass: PostgresTestEntity,
entityConfiguration: postgresTestEntityConfiguration,
databaseAdaptorFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdaptorFlavor: CacheAdapterFlavor.REDIS,
privacyPolicyClass: PostgresTestEntityPrivacyPolicy,
};
});
8 changes: 4 additions & 4 deletions packages/entity-example/src/entities/NoteEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class NoteEntity extends Entity<NoteFields, string, ExampleViewer
* of entity. In some languages, this would be representable as "abstract" static members
* of the class itself, but TypeScript disallows static generic and abstract methods.
*/
export const noteEntityCompanion = {
export const noteEntityCompanion = new EntityCompanionDefinition({
entityClass: NoteEntity,
entityConfiguration: new EntityConfiguration<NoteFields>({
idField: 'id',
Expand All @@ -57,8 +57,8 @@ export const noteEntityCompanion = {
columnName: 'body',
}),
},
databaseAdapterFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdapterFlavor: CacheAdapterFlavor.REDIS,
}),
databaseAdaptorFlavor: DatabaseAdapterFlavor.POSTGRES,
cacheAdaptorFlavor: CacheAdapterFlavor.REDIS,
privacyPolicyClass: NotePrivacyPolicy,
};
});
14 changes: 11 additions & 3 deletions packages/entity/src/EnforcingEntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ export default class EnforcingEntityLoader<
TFields,
TID,
TViewerContext extends ViewerContext,
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext>,
TPrivacyPolicy extends EntityPrivacyPolicy<TFields, TID, TViewerContext, TEntity>
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
TPrivacyPolicy extends EntityPrivacyPolicy<
TFields,
TID,
TViewerContext,
TEntity,
TSelectedFields
>,
TSelectedFields extends keyof TFields = keyof TFields
> {
constructor(
private readonly entityLoader: EntityLoader<
TFields,
TID,
TViewerContext,
TEntity,
TPrivacyPolicy
TPrivacyPolicy,
TSelectedFields
>
) {}

Expand Down
143 changes: 118 additions & 25 deletions packages/entity/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import ViewerContext from './ViewerContext';
export default abstract class Entity<
TFields,
TID,
TViewerContext extends ViewerContext
> extends ReadonlyEntity<TFields, TID, TViewerContext> {
TViewerContext extends ViewerContext,
TSelectedFields extends keyof TFields = keyof TFields
> extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields> {
/**
* Vend mutator for creating a new entity in given query context.
* @param viewerContext - viewer context of creating user
Expand All @@ -41,16 +42,30 @@ export default abstract class Entity<
TMID,
TMViewerContext extends ViewerContext,
TMViewerContext2 extends TMViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
viewerContext: TMViewerContext2,
queryContext: EntityQueryContext = viewerContext
.getViewerScopedEntityCompanionForClass(this)
.getQueryContextProvider()
.getRegularEntityQueryContext()
): CreateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy> {
): CreateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields> {
return viewerContext
.getViewerScopedEntityCompanionForClass(this)
.getMutatorFactory()
Expand All @@ -67,17 +82,31 @@ export default abstract class Entity<
TMFields,
TMID,
TMViewerContext extends ViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
existingEntity: TMEntity,
queryContext: EntityQueryContext = existingEntity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(this)
.getQueryContextProvider()
.getRegularEntityQueryContext()
): UpdateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy> {
): UpdateMutator<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy, TMSelectedFields> {
return existingEntity
.getViewerContext()
.getViewerScopedEntityCompanionForClass(this)
Expand All @@ -94,10 +123,24 @@ export default abstract class Entity<
TMFields,
TMID,
TMViewerContext extends ViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
existingEntity: TMEntity,
queryContext: EntityQueryContext = existingEntity
.getViewerContext()
Expand All @@ -122,10 +165,24 @@ export default abstract class Entity<
TMFields,
TMID,
TMViewerContext extends ViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
existingEntity: TMEntity,
queryContext: EntityQueryContext = existingEntity
.getViewerContext()
Expand Down Expand Up @@ -161,10 +218,24 @@ export default abstract class Entity<
TMFields,
TMID,
TMViewerContext extends ViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
existingEntity: TMEntity,
queryContext: EntityQueryContext = existingEntity
.getViewerContext()
Expand Down Expand Up @@ -196,10 +267,24 @@ export default abstract class Entity<
TMFields,
TMID,
TMViewerContext extends ViewerContext,
TMEntity extends Entity<TMFields, TMID, TMViewerContext>,
TMPrivacyPolicy extends EntityPrivacyPolicy<TMFields, TMID, TMViewerContext, TMEntity>
TMEntity extends Entity<TMFields, TMID, TMViewerContext, TMSelectedFields>,
TMPrivacyPolicy extends EntityPrivacyPolicy<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMSelectedFields
>,
TMSelectedFields extends keyof TMFields = keyof TMFields
>(
this: IEntityClass<TMFields, TMID, TMViewerContext, TMEntity, TMPrivacyPolicy>,
this: IEntityClass<
TMFields,
TMID,
TMViewerContext,
TMEntity,
TMPrivacyPolicy,
TMSelectedFields
>,
existingEntity: TMEntity,
queryContext: EntityQueryContext = existingEntity
.getViewerContext()
Expand All @@ -226,15 +311,23 @@ export interface IEntityClass<
TFields,
TID,
TViewerContext extends ViewerContext,
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext>,
TPrivacyPolicy extends EntityPrivacyPolicy<TFields, TID, TViewerContext, TEntity>
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>,
TPrivacyPolicy extends EntityPrivacyPolicy<
TFields,
TID,
TViewerContext,
TEntity,
TSelectedFields
>,
TSelectedFields extends keyof TFields = keyof TFields
> {
new (viewerContext: TViewerContext, obj: Readonly<TFields>): TEntity;
getCompanionDefinition(): EntityCompanionDefinition<
TFields,
TID,
TViewerContext,
TEntity,
TPrivacyPolicy
TPrivacyPolicy,
TSelectedFields
>;
}
Loading

0 comments on commit 4e40b2e

Please sign in to comment.