Skip to content

Commit

Permalink
improve prefilter types
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskalmar committed Jun 6, 2022
1 parent 0127b38 commit 7e21ed6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 48 deletions.
38 changes: 14 additions & 24 deletions src/engine/entity/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@ import {
AttributesMapGenerator,
PrimaryAttribute,
} from '../attribute/Attribute';
import { processPreFilters } from '../filter';
import { PreFilterMap, processPreFilters } from '../filter';
import { Context } from '../context/Context';
import { GraphQLResolveInfo, Source } from 'graphql';

interface PreFilterType {
[key: string]: {
resolve: Function;
attributes: unknown;
};
}

export interface StateMap {
[key: string]: number;
}
Expand Down Expand Up @@ -107,7 +100,7 @@ export interface EntitySetup {
states?: StateMap;
preProcessor?: EntityPreProcessor;
postProcessor?: EntityPostProcessor;
preFilters?: PreFilterType | (() => PreFilterType);
preFilters?: PreFilterMap | (() => PreFilterMap);
meta?: {
[key: string]: unknown;
};
Expand All @@ -128,7 +121,7 @@ export class Entity {
states?: StateMap;
preProcessor?: EntityPreProcessor;
postProcessor?: EntityPostProcessor;
preFilters?: PreFilterType;
preFilters?: PreFilterMap;
meta?: {
[key: string]: unknown;
};
Expand All @@ -143,8 +136,8 @@ export class Entity {
descriptionPermissionsFind: string | boolean;
descriptionPermissionsRead: string | boolean;
isFallbackStorageType: boolean;
findOne: Function;
find: Function;
findOne: () => unknown;
find: () => unknown;

constructor(setup: EntitySetup) {
passOrThrow(isMap(setup), () => 'Entity requires a setup object');
Expand Down Expand Up @@ -507,7 +500,8 @@ export class Entity {
);

if (isFunction(attribute.type)) {
const dataTypeBuilder: DataTypeFunction = attribute.type as DataTypeFunction;
const dataTypeBuilder: DataTypeFunction =
attribute.type as DataTypeFunction;
attribute.type = dataTypeBuilder({
setup: attribute as unknown,
entity: this,
Expand Down Expand Up @@ -794,9 +788,8 @@ export class Entity {
const permission = this.permissions.mutations[mutationName];

if (permission) {
const descriptionPermissions = generatePermissionDescription(
permission,
);
const descriptionPermissions =
generatePermissionDescription(permission);
if (descriptionPermissions) {
mutation.description += descriptionPermissions;
}
Expand All @@ -810,9 +803,8 @@ export class Entity {
const permission = this.permissions.subscriptions[subscriptionName];

if (permission) {
const descriptionPermissions = generatePermissionDescription(
permission,
);
const descriptionPermissions =
generatePermissionDescription(permission);
if (descriptionPermissions) {
subscription.description += descriptionPermissions;
}
Expand All @@ -822,10 +814,8 @@ export class Entity {
}
}

_processPreFilters(): PreFilterType {
return this.setup.preFilters
? (processPreFilters(this, this.setup.preFilters) as any)
: null;
_processPreFilters(preFilters?: PreFilterMap): PreFilterMap {
return preFilters ? processPreFilters(this, preFilters) : null;
}

getPreFilters() {
Expand All @@ -837,7 +827,7 @@ export class Entity {
this.setup.preFilters = this.setup.preFilters();
}

this.preFilters = this._processPreFilters();
this.preFilters = this._processPreFilters(this.setup.preFilters);
return this.preFilters;
}

Expand Down
24 changes: 9 additions & 15 deletions src/engine/entity/ViewEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ import {
AttributesSetupMap,
AttributesMapGenerator,
} from '../attribute/Attribute';
import { processPreFilters } from '../filter';
import { PreFilterMap, processPreFilters } from '../filter';
import { Context } from '../context/Context';
import { GraphQLResolveInfo } from 'graphql';
import { Entity } from './Entity';

interface PreFilterType {
[key: string]: {
resolve: (...args) => any;
attributes: any;
};
}

interface ViewEntityPreProcessorResponse {
args?: {
[key: string]: unknown;
Expand Down Expand Up @@ -69,7 +62,7 @@ export interface ViewEntitySetup {
permissions?: PermissionMap;
preProcessor?: ViewEntityPreProcessor;
postProcessor?: ViewEntityPostProcessor;
preFilters?: PreFilterType | (() => PreFilterType);
preFilters?: PreFilterMap | (() => PreFilterMap);
meta?: any;
}

Expand All @@ -82,7 +75,7 @@ export class ViewEntity {
permissions?: PermissionMap;
preProcessor?: ViewEntityPreProcessor;
postProcessor?: ViewEntityPostProcessor;
preFilters?: PreFilterType;
preFilters?: PreFilterMap;
meta?: any;
private readonly _attributesMap: AttributesSetupMap | AttributesMapGenerator;
private _primaryAttribute: Attribute;
Expand All @@ -91,7 +84,7 @@ export class ViewEntity {
private _attributes: AttributesMap;
descriptionPermissionsFind: string | boolean;
descriptionPermissionsRead: string | boolean;
private _preFilters: PreFilterType | (() => PreFilterType);
private _preFilters: PreFilterMap | (() => PreFilterMap);
isFallbackStorageType: any;
findOne: (...arg) => any;
find: (...args) => any;
Expand Down Expand Up @@ -246,7 +239,8 @@ export class ViewEntity {
);

if (isFunction(attribute.type)) {
const dataTypeBuilder: DataTypeFunction = attribute.type as DataTypeFunction;
const dataTypeBuilder: DataTypeFunction =
attribute.type as DataTypeFunction;
attribute.type = dataTypeBuilder({
setup: attribute as unknown,
entity: this,
Expand Down Expand Up @@ -459,8 +453,8 @@ export class ViewEntity {
}
}

_processPreFilters(): PreFilterType {
return this._preFilters ? processPreFilters(this, this._preFilters) : null;
_processPreFilters(preFilters?: PreFilterMap): PreFilterMap {
return preFilters ? processPreFilters(this, preFilters) : null;
}

getPreFilters() {
Expand All @@ -472,7 +466,7 @@ export class ViewEntity {
this._preFilters = this._preFilters();
}

this.preFilters = this._processPreFilters();
this.preFilters = this._processPreFilters(this._preFilters);
return this.preFilters;
}

Expand Down
27 changes: 18 additions & 9 deletions src/engine/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEntity } from './entity/Entity';
import { Entity, isEntity } from './entity/Entity';

import {
passOrThrow,
Expand All @@ -9,12 +9,19 @@ import {
} from './util';
import { isObjectDataType, ObjectDataType } from './datatype/ObjectDataType';
import { DataTypeFunction } from './datatype/DataType';
import { ViewEntity } from './entity/ViewEntity';
import { Context } from './context/Context';

export type PreFilter = {
resolve: (
context: Context,
input: { [key: string]: unknown },
) => number[] | Promise<number[]>;
attributes?: ObjectDataType | DataTypeFunction;
};

type PreFilter = {
[key: string]: {
resolve: () => unknown;
attributes?: ObjectDataType;
};
export type PreFilterMap = {
[key: string]: PreFilter;
};

const logicFilters = ['$and', '$or'];
Expand Down Expand Up @@ -176,8 +183,7 @@ const isPreFilter = (preFilterDefinition: PreFilter) => {
}

if (isFunction(preFilterDefinition.attributes)) {
const dataTypeBuilder =
preFilterDefinition.attributes as DataTypeFunction;
const dataTypeBuilder = preFilterDefinition.attributes;

const attributesType = dataTypeBuilder({
setup: {
Expand All @@ -196,7 +202,10 @@ const isPreFilter = (preFilterDefinition: PreFilter) => {
return false;
};

export const processPreFilters = (entity, preFilters) => {
export const processPreFilters = (
entity: Entity | ViewEntity,
preFilters: PreFilterMap,
) => {
passOrThrow(
isMap(preFilters),
() =>
Expand Down

0 comments on commit 7e21ed6

Please sign in to comment.