1.0.2 • Published 2 years ago

@http-query/core v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Http Query

This project contain the http query pattern to filter data in backend by url.

Features:

  • Search
  • Pagination
  • Sorting
  • Limit by page
  • Get filter from url
  • Relations with TypeORM pattern (array)
  • Operators contract

Databases Support

  • MySQL

Coming soon

Features:

  • Relations to prisma pattern (objects with includes)

Support:

  • Postgres Database
  • Mongodb Database
  • Redis Database
  • SQL Server Database
  • Oracle Database

Installation

NPM:

npm install @http-query/core

yarn:

yarn add @http-query/core

Now implement the bind and getAlias code in your repository.

Sample bind method for typeorm

private bind({ query, relations }: IFind) {
  const order = {};

  query.sort.forEach((item) => {
    order[item.property] = item.order;
  });

  const rules = {
    AND: (qb: SelectQueryBuilder<Entity>, whereQuery: string) => {
      qb.andWhere(whereQuery);
    },
    OR: (qb: SelectQueryBuilder<Entity>, whereQuery: string) => {
      qb.orWhere(whereQuery);
    },
  };

  const where = (qb: SelectQueryBuilder<Entity>) => {
    query.q.forEach((item) => {
      const parsedEntity = this.getAlias(item.entity);
      const property = `${parsedEntity}.${item.property}`;
      const operator = operators[item.operator];
      const value =
        item.operator === 'LIKE' ? `'%${item.value}%'` : `'${item.value}'`;

      rules[item.rule](qb, `${property} ${operator} ${value}`);
    });
  };

  return {
    relations,
    take: query.limit,
    skip: (query.page - 1) * query.limit,
    order,
    where,
  };
}

Sample getAlias method for typeorm

private getAlias(entity: string): string {
  const { targetName } = this.orm.metadata;

  const parsedEntity = entity.replace(/\w{1}/, (match) =>
    match.toUpperCase(),
  );

  if (parsedEntity === targetName) return parsedEntity;

  return `${targetName}__${entity}`;
}