typeorm#FindConditions TypeScript Examples

The following examples show how to use typeorm#FindConditions. 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: polymorphic.repository.ts    From typeorm-polymorphic with MIT License 6 votes vote down vote up
public async find(
    optionsOrConditions?: FindConditions<E> | FindManyOptions<E>,
  ): Promise<E[]> {
    const results = await super.find(optionsOrConditions);

    if (!this.isPolymorph()) {
      return results;
    }

    const metadata = this.getPolymorphicMetadata();

    return Promise.all(
      results.map((entity) => this.hydratePolymorphs(entity, metadata)),
    );
  }
Example #2
Source File: comments.ts    From Corsace with MIT License 5 votes vote down vote up
commentsRouter.get("/", async (ctx) => {
    if (!ctx.query.user)
        return ctx.body = {
            error: "No user ID provided!",
        };

    const userId = parseInt(parseQueryParam(ctx.query.user) || "");
    const year = parseInt(parseQueryParam(ctx.query.year) || "") || new Date().getUTCFullYear();
    const modeString: string = parseQueryParam(ctx.query.mode) || "standard";
    const modeID = ModeDivisionType[modeString];

    if (year === 2020) {
        ctx.body = {
            error: "MCA 2020 is not running comments for AYIM. Sorry for the inconvenience.",
        };
        return;
    }

    const mca = await MCA.findOneOrFail({
        year,
    });

    let query: FindConditions<UserComment> | FindConditions<UserComment>[] = {
        targetID: userId,
        year: year,
        mode: modeID,
        commenter: ctx.state.user,
    };

    // Show all comments if mca results are out
    if (new Date() >= mca?.results) {
        query = [
            query,
            {
                targetID: userId,
                year: year,
                mode: modeID,
                isValid: true,
            },
        ];
    }

    const [target, comments] = await Promise.all([
        User.findOneOrFail(userId),

        UserComment.find({
            where: query,
            relations: ["commenter"],
            order: {
                updatedAt: "DESC",
            },
        }),
    ]);

    if (!isEligibleFor(target, modeID, year)) {
        return ctx.body = {
            error: `User wasn't active for the selected mode`,
        };
    }

    ctx.body = {
        user: target,
        comments,
    };
});
Example #3
Source File: polymorphic.repository.ts    From typeorm-polymorphic with MIT License 5 votes vote down vote up
find(conditions?: FindConditions<E>): Promise<E[]>;
Example #4
Source File: polymorphic.repository.ts    From typeorm-polymorphic with MIT License 5 votes vote down vote up
findOne(
    conditions?: FindConditions<E>,
    options?: FindOneOptions<E>,
  ): Promise<E | undefined>;
Example #5
Source File: polymorphic.repository.ts    From typeorm-polymorphic with MIT License 5 votes vote down vote up
public async findOne(
    idOrOptionsOrConditions?:
      | string
      | number
      | Date
      | ObjectID
      | FindConditions<E>
      | FindOneOptions<E>,
    optionsOrConditions?: FindConditions<E> | FindOneOptions<E>,
  ): Promise<E | undefined> {
    const polymorphicMetadata = this.getPolymorphicMetadata();

    if (Object.keys(polymorphicMetadata).length === 0) {
      return idOrOptionsOrConditions &&
        (typeof idOrOptionsOrConditions === 'string' ||
          typeof idOrOptionsOrConditions === 'number' ||
          typeof idOrOptionsOrConditions === 'object') &&
        optionsOrConditions
        ? super.findOne(
            idOrOptionsOrConditions as number | string | ObjectID | Date,
            optionsOrConditions as FindConditions<E> | FindOneOptions<E>,
          )
        : super.findOne(
            idOrOptionsOrConditions as FindConditions<E> | FindOneOptions<E>,
          );
    }

    const entity =
      idOrOptionsOrConditions &&
      (typeof idOrOptionsOrConditions === 'string' ||
        typeof idOrOptionsOrConditions === 'number' ||
        typeof idOrOptionsOrConditions === 'object') &&
      optionsOrConditions
        ? await super.findOne(
            idOrOptionsOrConditions as number | string | ObjectID | Date,
            optionsOrConditions as FindConditions<E> | FindOneOptions<E>,
          )
        : await super.findOne(
            idOrOptionsOrConditions as FindConditions<E> | FindOneOptions<E>,
          );

    if (!entity) {
      return entity;
    }

    return this.hydratePolymorphs(entity, polymorphicMetadata);
  }
Example #6
Source File: BaseClass.ts    From fosscord-server with GNU Affero General Public License v3.0 5 votes vote down vote up
static increment<T extends BaseClass>(conditions: FindConditions<T>, propertyPath: string, value: number | string) {
		const repository = this.getRepository();
		return repository.increment(conditions as T, propertyPath, value);
	}
Example #7
Source File: BaseClass.ts    From fosscord-server with GNU Affero General Public License v3.0 5 votes vote down vote up
static decrement<T extends BaseClass>(conditions: FindConditions<T>, propertyPath: string, value: number | string) {
		const repository = this.getRepository();
		return repository.decrement(conditions as T, propertyPath, value);
	}