How to use the @fullstack-one/db.EventSubscriber function in @fullstack-one/db

To help you get started, we’ve selected a few @fullstack-one/db examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fullstack-build / fullstack-one / packages / schema-builder / lib / decorators / computedColumn.ts View on Github external
import { expressions } from "../gql-schema-builder/expressions/defineExpression";
import { ExpressionCompiler } from "../gql-schema-builder/expressions/ExpressionCompiler";

interface IOptions {
  name: string;
  params?: TParams;
  gqlType: string;
}

interface IObjectLiteral {
  [key: string]: any;
}

const resolveComputedColumnPrefix = "_resolve_computed_";

@EventSubscriber()
export class AfterLoadForComputedColumnsSubscriber implements EntitySubscriberInterface {
  public afterLoad(entity: IObjectLiteral, event: LoadEvent): Promise {
    const resolveComputedFns = getResolveComputedFns(entity);
    const promises = resolveComputedFns.map((fn) => fn(entity, event.connection));
    return Promise.all(promises);
  }
}

function getResolveComputedFns(entity: IObjectLiteral): Array<(entity: IObjectLiteral, connection: Connection) => Promise> {
  return Object.entries(entity.constructor)
    .filter(([key]) => key.startsWith(resolveComputedColumnPrefix))
    .map(([key, value]) => value as (entity: IObjectLiteral) => Promise);
}

// tslint:disable-next-line:function-name
export function Computed(options: IOptions) {