https://github.com/emresandikci/pocketbase-query
A pocketbase query builder
https://github.com/emresandikci/pocketbase-query
pcoketbase query query-builder
Last synced: 3 months ago
JSON representation
A pocketbase query builder
- Host: GitHub
- URL: https://github.com/emresandikci/pocketbase-query
- Owner: emresandikci
- License: mit
- Created: 2025-01-31T11:33:24.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-02-25T21:12:10.000Z (3 months ago)
- Last Synced: 2025-02-25T22:20:16.787Z (3 months ago)
- Topics: pcoketbase, query, query-builder
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@emresandikci/pocketbase-query
- Size: 222 KB
- Stars: 31
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-pocketbase - pocketbase-query - is a TypeScript-based query builder designed to generate complex filter queries for PocketBase. It allows for easy construction of queries using various operators while maintaining a fluent and chainable API.  (TypeScript tools)
README
# Pocketbase Query
## Overview
`@emresandikci/pocketbase-query` is a TypeScript-based query builder designed to generate complex filter queries for PocketBase. It allows for easy construction of queries using various operators while maintaining a fluent and chainable API.
---
## Installation
This library can be used in any TypeScript/JavaScript project. Simply import it as needed:
```bash
npm i @emresandikci/pocketbase-query
``````typescript
import PocketbaseQuery from '@emresandikci/pocketbase-query';
```---
## Usage
### Creating an Instance
The `PocketbaseQuery` class follows the singleton pattern. You should use `getInstance()` to get a query builder instance:
```typescript
const query = PocketbaseQuery.getInstance();
```## Example
```typescript
import PocketbaseQuery from '@emresandikci/pocketbase-query';const query = PocketbaseQuery.getInstance<{ status: string; comments: number }>();
const customFilters = query
.equal('status', 'active')
.and()
.greaterThan('comments', 50)
.build();console.log(customFilters); // Outputs: status='active' && comments>50
await pb.collection('posts').getFullList({
filter: customFilters,
expand: [{ key: 'comments_via_post' }],
})
```---
## API Methods
### Operators Enum
`OperatorEnum` defines a set of operators for use in queries:
```typescript
enum OperatorEnum {
Equal = "=",
NotEqual = "!=",
GreaterThan = ">",
GreaterThanOrEqual = ">=",
LessThan = "<",
LessThanOrEqual = "<=",
Like = "~",
NotLike = "!~",
AnyEqual = "?=",
AnyNotEqual = "?!=",
AnyGreaterThan = "?>",
AnyGreaterThanOrEqual = "?>=",
AnyLessThan = "?<",
AnyLessThanOrEqual = "?<=",
AnyLike = "?~",
AnyNotLike = "?!~",
}
```### Query Builder Methods
#### `equal(field: keyof T, value: string | boolean)`
Adds an equality condition to the query.
```typescript
query.equal("status", "active");
```#### `notEqual(field: keyof T, value: string)`
Adds a not-equal condition to the query.
```typescript
query.notEqual("category", "archived");
```#### `greaterThan(field: keyof T, value: string)`
Adds a greater-than condition.
```typescript
query.greaterThan("age", "18");
```#### `lessThan(field: keyof T, value: string)`
Adds a less-than condition.
```typescript
query.lessThan("price", "100");
```#### `like(field: keyof T, value: string)`
Adds a LIKE condition (partial match).
```typescript
query.like("name", "John");
```#### `notLike(field: keyof T, value: string)`
Adds a NOT LIKE condition.
```typescript
query.notLike("description", "discount");
```#### `anyEqual(field: keyof T, value: string)`
Adds an equality condition for array fields.
```typescript
query.anyEqual("tags", "sale");
```#### `in(field: keyof T, values: any[])`
Adds an OR condition for multiple values.
```typescript
query.in("category", ["electronics", "furniture"]);
```#### `customFilter(filter: string)`
Adds a custom filter string to the query.
```typescript
query.customFilter("status='active' && price>100");
```---
### Logical Operators
#### `and()`
Joins multiple conditions with `&&`.
```typescript
query.equal("status", "active").and().greaterThan("price", "50");
```#### `or()`
Joins multiple conditions with `||`.
```typescript
query.equal("status", "active").or().equal("status", "pending");
```#### `openBracket()` and `closeBracket()`
Groups conditions using parentheses.
```typescript
query.openBracket().equal("status", "active").or().equal("status", "pending").closeBracket().and().greaterThan("price", "50");
```---
### Query Execution
#### `getQuery()`
Returns the current query string.
```typescript
const queryString = query.getQuery();
```#### `build()`
Finalizes and returns the query string while clearing the internal state.
```typescript
const finalQuery = query.build();
```## Notes
- The `PocketbaseQuery` class uses a singleton pattern, meaning a single instance is reused across calls.
- The `.build()` method resets the query, so ensure you store the generated string if you need it.
- The `in()` method applies `OR` between multiple values.---
## License
MIT License