A simple type-safe ORM like query builder for structured arrays
npm install qweery- Typescript first design, ensuring type safety and autocompletion.
- Very familiar API.
- Zero dependencies.
- Fast? Not really.
import { Qweery } from 'qweery';
const qweery = new Qweery([
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 28 },
]);Filters items that match all conditions in the $AND operator. All fields that are not operators are AND'ed together.
console.log(
qweery
// name == 'Alice' && age > 30
.where({
name: 'Alice',
age: { greaterThan: 30 }
})
// can also be written as
.where({
$AND: [
{ name: 'Alice' },
{ age: { greaterThan: 30 } }
]
})
.skip(0)
.take(10)
.all()
);
// Output: []Filters items that match at least one of the conditions in the $OR operator. All conditions in the $OR operator will be OR'ed together.
console.log(
qweery
// name == 'Alice' || age > 30
.where({
$OR: [
{ name: 'Alice' },
{ age: { greaterThan: 30 } }
]
})
.skip(0)
.take(10)
.all()
);
// Output: [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]If an $OR operator is an array, all conditions in the array will be evaluated together, then AND'ed with the previous condition.
console.log(
qweery
// name.includes('a') && (age > 28 || age < 28)
.where({
name: {
includes: 'a'
},
$OR: [
{ age: { greaterThan: 28 } },
{ age: { lessThan: 28 } }
]
})
.skip(0)
.take(10)
.all()
);
// Output: [{ name: 'Charlie', age: 35 }]If an $OR operator is not an array, it will be OR'ed with the previous condition.
console.log(
qweery
// name.includes('a') || age > 30
.where({
name: {
includes: 'a'
},
$OR: {
age: { greaterThan: 30 }
}
})
.skip(0)
.take(10)
.all()
);
// Output: [{ name: 'Charlie', age: 35 }, { name: 'David', age: 28 }]Used to negate a condition. If the condition is false, the item will be returned.
console.log(
qweery
// name != 'Alice'
.where({
$NOT: [
{ name: 'Alice' }
]
})
.skip(0)
.take(10)
.all()
);
// Output: [{ name: 'Bob', age: 25 }, { name: 'Charlie', age: 35 }, { name: 'David', age: 28 }]You can use a function that returns a boolean to filter a values of a field.
console.log(
qweery
// name.toLowerCase().includes('a')
.where({
name: value => value.toLowerCase().includes('a')
})
.skip(0)
.take(10)
.all()
);
// Output: [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }, { name: 'David', age: 28 }]All operators can be nested.
console.log(
qweery
// name == 'Alice' || (name != 'Bob' && (age > 28 || age < 28))
.where({
name: 'Alice',
$OR: [
{
$NOT: [
{ name: 'Bob' }
],
$OR: [
{ age: { greaterThan: 28 } },
{ age: { lessThan: 28 } }
]
}
]
})
);