typeorm#ObjectLiteral TypeScript Examples

The following examples show how to use typeorm#ObjectLiteral. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: typeorm.ts    From type-graphql-dataloader with MIT License 6 votes vote down vote up
coalesceTypenames = (objects: ObjectLiteral[]): typename => {
  const typename = objects
    .map((a) => a.__typename)
    .reduce((a, b) => (a === b ? a : null));
  if (typename == null) {
    throw Error("typename mismatch");
  }
  return typename;
}
Example #2
Source File: typeorm.ts    From type-graphql-dataloader with MIT License 5 votes vote down vote up
verify = async <Entity extends ObjectLiteral>(
  objectOrObjects: ObjectLiteral | ObjectLiteral[],
  entityOrEntities: Entity | Entity[]
) => {
  if (Array.isArray(objectOrObjects)) {
    if (!Array.isArray(entityOrEntities)) {
      throw Error("entityOrEntities type mismatch");
    }
    const entities = entityOrEntities;

    const objects = objectOrObjects;
    expect(objects.length).toEqual(entities.length);
    if (objects.length === 0) {
      return;
    }
    coalesceTypenames(objects);

    await Promise.all(
      objects.map((object) => {
        const entity = entities.find((entity) => entity.name === object.name);
        if (entity == null) {
          throw Error("Corresponding entity was not found");
        }
        return verify(object, entity);
      })
    );
  } else {
    if (Array.isArray(entityOrEntities)) {
      throw Error("entityOrEntities type mismatch");
    }
    const obj = objectOrObjects;
    const entity = entityOrEntities;
    expect(obj.name).toEqual(entity.name);

    await Promise.all(
      Object.keys(obj).map(async (k) => {
        const nextObj = obj[k];
        const getSelfEntity = async () =>
          (await getRepository(
            objectTypes[obj.__typename as typename]
          ).findOneOrFail({
            where: { name: obj.name },
            relations: [k],
          })) as any;

        if (Array.isArray(nextObj)) {
          // ToMany field
          const nextEntities = await (await getSelfEntity())[k];
          return verify(nextObj, nextEntities);
        } else {
          // ToOne field (null)
          if (nextObj == null) {
            expect(await (await getSelfEntity())[k]).toBeNull();
            return;
          }
          // Column field
          const typename = nextObj.__typename as typename;
          if (typename == null) {
            return;
          }
          // ToOne field
          const nextEntity = await (await getSelfEntity())[k];
          return verify(nextObj, nextEntity);
        }
      })
    );
  }
}
Example #3
Source File: FakeSelectQueryBuilder.ts    From typeorm-extension with MIT License 5 votes vote down vote up
leftJoinAndSelect(entityOrProperty: Function|string|((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>), alias: string, condition: string = "", parameters?: ObjectLiteral): this {
        return this;
    }