sveltekit-sync
Version:
Local-first sync engine for SvelteKit
119 lines (118 loc) • 3.52 kB
TypeScript
import { type QueryOperator, type LogicalOperator } from './operators.js';
import { type FieldCondition, type OrderByCondition, type FieldsProxy } from './field-proxy.js';
type OrderByInput<T> = keyof T | ((fields: FieldsProxy<T>) => OrderByCondition<T>) | OrderByCondition<T>;
type WhereInput<T> = ((item: T) => boolean) | ((fields: FieldsProxy<T>) => FieldCondition<T>) | Partial<{
[K in keyof T]: T[K] | QueryOperator<T[K]>;
}> | FieldCondition<T> | LogicalOperator<T>;
export interface QueryResult<T> {
data: T[];
total: number;
page?: number;
perPage?: number;
totalPages?: number;
hasMore?: boolean;
}
export declare class QueryBuilder<T extends Record<string, any> & {
id: string;
}> {
private collection;
private conditions;
private orderByClauses;
private limitCount;
private offsetCount;
private fieldsProxy;
constructor(collection: {
data: T[];
delete: (id: string) => Promise<void>;
update: (id: string, data: Partial<T>) => Promise<T>;
});
/**
* Add a where condition - supports multiple syntaxes:
*
* 1. Callback: .where(todo => todo.completed === false)
* 2. Object: .where({ completed: false })
* 3. Object with operators: .where({ priority: gte(5) })
* 4. Proxy callback: .where(f => f.completed.eq(false))
* 5. Field condition: .where(fields.completed.eq(false))
*/
where(condition: WhereInput<T>): this;
/**
* Add multiple AND conditions at once
*/
whereAll(...conditions: WhereInput<T>[]): this;
/**
* Add an OR condition group
*/
orWhere(...conditions: WhereInput<T>[]): this;
/**
* Order by field - supports multiple syntaxes:
*
* 1. String: .orderBy('createdAt', 'desc')
* 2. Callback: .orderBy(f => f.createdAt.desc())
* 3. Condition: .orderBy(fields.createdAt.desc())
*/
orderBy(input: OrderByInput<T>, direction?: 'asc' | 'desc'): this;
/**
* Limit results
*/
limit(count: number): this;
/**
* Skip results (for pagination)
*/
offset(count: number): this;
/**
* Alias for offset
*/
skip(count: number): this;
/**
* Execute query and return results
*/
get(): Promise<T[]>;
/**
* Get first matching result
*/
first(): Promise<T | null>;
/**
* Get last matching result
*/
last(): Promise<T | null>;
/**
* Count matching results
*/
count(): Promise<number>;
/**
* Check if any results match
*/
exists(): Promise<boolean>;
/**
* Paginate results
*/
paginate(page: number, perPage?: number): Promise<QueryResult<T>>;
/**
* Delete all matching records
*/
delete(): Promise<number>;
/**
* Update all matching records
*/
update(data: Partial<T>): Promise<number>;
/**
* Get IDs of matching records
*/
pluck<K extends keyof T>(field: K): Promise<T[K][]>;
sum(field: keyof T): Promise<number>;
avg(field: keyof T): Promise<number>;
min<K extends keyof T>(field: K): Promise<T[K] | null>;
max<K extends keyof T>(field: K): Promise<T[K] | null>;
private clone;
private detectConditionType;
private isLogicalOperator;
private applyConditions;
private evaluateCondition;
private evaluateFieldCondition;
private evaluateObjectCondition;
private evaluateLogicalOperator;
private evaluateOperator;
private applyOrdering;
}
export {};