sveltekit-sync
Version:
Local-first sync engine for SvelteKit
56 lines (55 loc) • 2.46 kB
JavaScript
import { eq, ne, gt, gte, lt, lte, inArray, notInArray, contains, startsWith, endsWith, between, isNull, isNotNull } from './operators.js';
export const FIELD_SYMBOL = Symbol('field-reference');
export function isFieldCondition(value) {
return value !== null && typeof value === 'object' && FIELD_SYMBOL in value && 'operator' in value;
}
export function isOrderByCondition(value) {
return value !== null && typeof value === 'object' && FIELD_SYMBOL in value && 'direction' in value;
}
export function isFieldReference(value) {
return value !== null && typeof value === 'object' && FIELD_SYMBOL in value;
}
function createFieldCondition(fieldName, operator) {
return { [FIELD_SYMBOL]: true, fieldName, operator };
}
function createOrderByCondition(fieldName, direction) {
return { [FIELD_SYMBOL]: true, fieldName, direction };
}
function createFieldReference(fieldName) {
return {
[FIELD_SYMBOL]: true,
fieldName,
// Comparison
eq: (value) => createFieldCondition(fieldName, eq(value)),
ne: (value) => createFieldCondition(fieldName, ne(value)),
gt: (value) => createFieldCondition(fieldName, gt(value)),
gte: (value) => createFieldCondition(fieldName, gte(value)),
lt: (value) => createFieldCondition(fieldName, lt(value)),
lte: (value) => createFieldCondition(fieldName, lte(value)),
// Array
in: (values) => createFieldCondition(fieldName, inArray(values)),
notIn: (values) => createFieldCondition(fieldName, notInArray(values)),
// String
contains: (value) => createFieldCondition(fieldName, contains(value)),
startsWith: (value) => createFieldCondition(fieldName, startsWith(value)),
endsWith: (value) => createFieldCondition(fieldName, endsWith(value)),
// Range
between: (min, max) => createFieldCondition(fieldName, between(min, max)),
// Null
isNull: () => createFieldCondition(fieldName, isNull()),
isNotNull: () => createFieldCondition(fieldName, isNotNull()),
// Ordering
asc: () => createOrderByCondition(fieldName, 'asc'),
desc: () => createOrderByCondition(fieldName, 'desc'),
};
}
/**
* Creates a proxy that returns typed field references for any property access
*/
export function createFieldsProxy() {
return new Proxy({}, {
get(_target, prop) {
return createFieldReference(prop);
}
});
}