npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rafikidota/iroh-query-builder

v0.1.0

Published

TypeORM query builder extracted from iroh: type-safe where/order/select/relations from a plain query object.

Readme

@rafikidota/iroh-query-builder

Sometimes, the best way to solve your own problems is to help someone else.

Type-safe query builder for TypeORM: build where / order / select / relations from a plain, serializable query object instead of chaining TypeORM's QueryBuilder API by hand. Extracted from the iroh project.

Useful when a query shape needs to travel across a boundary (HTTP request body, message queue, RPC payload) and get validated/typed before hitting the database.

Install

npm install @rafikidota/iroh-query-builder

Requires typeorm@^1.0.0 as a peer dependency.

Usage

import { QueryBuilder, type QueryForm } from '@rafikidota/iroh-query-builder';
import { dataSource } from './data-source';
import { User } from './user.entity';

const form: QueryForm<User> = {
  relations: { profile: true },
  select: { name: true, profile: { bio: true } },
  where: {
    age: { $gte: 18 },
    profile: { verified: true },
    $or: [{ name: 'john' }, { name: 'jane' }],
  },
  order: { name: 'ASC' },
};

const sqb = dataSource.getRepository(User).createQueryBuilder('u');
const qb = new QueryBuilder(sqb);

qb.addOptions(form);
qb.addLimit(20);
qb.addOffset(0);

const [rows, count] = await qb.getManyAndCount();

QueryBuilder wraps a TypeORM SelectQueryBuilder<T> and reads its root alias from sqb.alias (whatever you passed to createQueryBuilder(...)). Nested relations are namespaced under it as <alias>_<relation>, <alias>_<relation>_<nested>, ...

QueryForm<T>

type QueryForm<T> = {
  select?: SelectOption<T>;
  relations?: RelationOption<T>;
  where?: WhereOption<T>;
  order?: OrderOption<T>;
};

Each option can be applied independently via its own method (addSelect, addRelations, addWhere, addOrder) or all at once via addOptions(form).

  • select — nested object of true flags. If no field is set to true at a given level, that whole alias is selected (addSelect('alias')).
  • relations — nested object of true / nested objects, mirrors TypeORM's FindOptionsRelations. Each relation becomes a LEFT JOIN under a namespaced alias, and any select fields nested under it are merged in automatically.
  • order — nested object of 'ASC' | 'DESC'. Falsy entries are skipped.
  • where — see below.

where operators

A bare value is an implicit $eq. Supported operators:

| Operator | SQL | | --- | --- | | $eq | = | | $ne | <> | | $gt / $gte | > / >= | | $lt / $lte | < / <= | | $in / $nin | IN / NOT IN | | $like / $nlike | LIKE / NOT LIKE | | $ilike / $nilike | ILIKE / NOT ILIKE | | $between / $nbetween | BETWEEN / NOT BETWEEN | | $regexp / $iregexp | ~ / ~* | | $isnull / $notnull | IS NULL / IS NOT NULL |

Top-level fields are ANDed together. Explicit boolean composition is available via $and, $or, $not:

const where: WhereOption<User> = {
  $or: [{ name: 'john' }, { $and: [{ age: { $gte: 18 } }, { age: { $lte: 65 } }] }],
  $not: { deletedAt: { $isnull: true } },
};

Nested relation fields use dotted paths under the hood (profile.age) and resolve to the namespaced alias (alias_profile.age).

Operator helper functions are also available for building conditions programmatically instead of writing raw operator objects:

import { Equal, In, IsNull, Between } from '@rafikidota/iroh-query-builder';

const where = {
  name: Equal('john'),
  id: In([1, 2, 3]),
  deletedAt: IsNull(),
  createdAt: Between(from, to),
};

Terminal methods

QueryBuilder delegates to the underlying SelectQueryBuilder: getMany(), getManyAndCount(), getOne(), getCount(), and the sql getter (getSql()).

Development

pnpm install
pnpm build          # bundle with tsdown -> dist/
pnpm typecheck       # tsc --noEmit
pnpm lint            # eslint --fix
pnpm lint:check      # eslint, no fixes
pnpm format          # prettier --write
pnpm test            # vitest run
pnpm test:watch      # vitest watch mode
pnpm test:types      # vitest run --typecheck.only (*.test-d.ts)

Run a single test file: pnpm vitest run test/query-builder.test.ts.

Node version is pinned in .nvmrc; package manager is pnpm (pnpm-workspace.yaml / .npmrc).

License

MIT