The library provides a flexible and powerful set of tools for querying and manipulating collections of objects in TypeScript. Whether you need to filter, paginate, or perform complex queries, this library offers an intuitive interface and extensive features.
Features
- Construct queries with a variety of comparison operators.
- Easily manipulate and filter object collections based on specific criteria.
- Perform data manipulation operations on collections.
- Calculate sums, select specific properties, and group elements by key.
- Implement pagination with methods to limit and offset the collection.
- Benefit from type safety and enhanced code completion when using TypeScript.
You can install the library using npm.
npm install @njs-lib/select-query --saveFor more information on using npm check out the docs here.
The SelectQuery provides utility methods for querying and manipulating collections of objects.
Creates a new instance of SelectQuery with the provided collection.
- @param
collection: An array of objects to be used for querying.
Gets the first element of the collection.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).first();
// RESULT { id: 1, name: 'Jak', age: 30 };Gets the last element of the collection.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).last();
// RESULT { id: 4, name: 'Sam', age: 20 };Gets all elements in the collection.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).last();
/* RESULT
[
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
*/Gets the number of elements in the collection.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).count();
// RESULT 4Limits the number of objects in the collection to a specified maximum.
- @param
limitThe maximum number of objects to include in the collection
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).limit(2).get();
/* RESULT
[
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
]
*/Offsets the collection by a specified number of objects.
- @param
offsetThe number of objects to skip from the beginning of the collection.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).offset(2).get();
/* RESULT
[
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
]
*/Paginates the collection based on a page number and page size.
- @param
numThe page number (1-based). - @param
sizeThe number of objects per page.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).paginate(2, 2).get();
/* RESULT
[
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
]
*/Calculates the sum of the values for a specified property in the collection.
- @param
keyThe property key whose values are to be summed up.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).where('age', '===', 20).sum('age');
// RESULT 40Selects specific properties from each object in the collection and returns a new SelectQuery instance.
- @param
keysThe keys (properties) to select.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).select('name').get();
/* RESULT
[
{ name: 'Jak' },
{ name: 'Bob' },
{ name: 'Tom' },
{ name: 'Sam' },
];
*/Groups elements in a collection based on a specified key.
- @param
keyThe key (property name) by which to group elements.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).keyBy('id');
/* RESULT
{
1: { id: 1, name: 'Jak', age: 30 },
2: { id: 2, name: 'Bob', age: 25 },
3: { id: 3, name: 'Tom', age: 20 },
4: { id: 4, name: 'Sam', age: 20 },
};
*/Filters the collection based on a specified condition.
- @param
keyThe key (property name) to compare. - @param
operatorThe comparison operator. - @param
valueThe value to compare against.
const data = [
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Tom', age: 20 },
{ id: 4, name: 'Sam', age: 20 },
];
const result = new SelectQuery(data).where('id', '===', 1).get();
// RESULT [{ id: 1, name: 'Jak', age: 30 }];
const result = new SelectQuery(data).where('age', '>', 20).get();
/* RESULT
[
{ id: 1, name: 'Jak', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
];
*/Returns an array of valid operators for comparisons.
new SelectQuery.getOperators();
// RESULT ['===', '!==', '<', '<=', '>', '>=', 'like', '^like', 'like$'];| Value | Description |
|---|---|
=== |
Checks if the specified property of an item is equal to a given value. |
!== |
Checks if the specified property of an item is not equal to a given value. |
< |
Checks if the specified property of an item is less than a given value. |
<= |
Checks if the specified property of an item is less than or equal to a given value. |
> |
Checks if the specified property of an item is greater than a given value. |
>= |
Checks if the specified property of an item is greater than or equal to a given value. |
like |
Checks if the specified property of value contains the given value |
^like |
Checks if the specified property of value starts with the given value. |
like$ |
Checks if the specified property of value ends with the given value. |
Released under the terms of the MIT License.