UNPKG

@dudousxd/nestjs-filter-drizzle

Version:

Drizzle ORM adapter for @dudousxd/nestjs-filter.

62 lines • 2.55 kB
import { BaseFilter, escapeLike } from '@dudousxd/nestjs-filter'; import { likeCondition } from './operator-resolver.js'; import { columnOf } from './schema-metadata.js'; /** * Base class for Drizzle filters. `this.$query` is a {@link DrizzleQuery} over * the table given to `@Filterable({ entity })`: * * ```ts * @Injectable() * @Filterable({ entity: users }) * export class UserFilter extends DrizzleFilter<typeof users> { * @FilterFor('minAge') * applyMinAge(value: number) { * this.$query.where(gte(users.age, value)); * } * } * ``` * * Build conditions with drizzle's own operators against the imported table — * or through {@link columns} when the same filter also runs as a relation * filter (`@Relations`), where the query targets an ALIAS of the table. */ export class DrizzleFilter extends BaseFilter { /** * The queried table's columns, as seen by the current query (the alias * inside a relation subquery). Prefer this over the imported table object in * filters that are also used through `@Relations`. */ get columns() { return this.$query.columns; } columnOf(field) { const column = columnOf(this.$query.table, field); if (!column) { throw new Error(`Unknown column "${field}" on the filtered table.`); } return column; } /** * Adds a LIKE condition: `field LIKE '%value%'` (value is escaped). Matching * follows the database's LIKE semantics — case-sensitive on Postgres; use * {@link whereILike} for case-insensitive matching on every dialect. */ whereLike(field, value) { this.$query.where(likeCondition(this.columnOf(field), `%${escapeLike(value)}%`, this.$query.dialect)); } /** Case-insensitive contains: `ILIKE` on Postgres, `lower(x) LIKE lower(p)` elsewhere. */ whereILike(field, value) { this.$query.where(likeCondition(this.columnOf(field), `%${escapeLike(value)}%`, this.$query.dialect, { caseInsensitive: true, })); } /** Adds a LIKE condition: `field LIKE 'value%'` (value is escaped). */ whereBeginsWith(field, value) { this.$query.where(likeCondition(this.columnOf(field), `${escapeLike(value)}%`, this.$query.dialect)); } /** Adds a LIKE condition: `field LIKE '%value'` (value is escaped). */ whereEndsWith(field, value) { this.$query.where(likeCondition(this.columnOf(field), `%${escapeLike(value)}`, this.$query.dialect)); } } //# sourceMappingURL=drizzle-filter.js.map