knex-search v1.0.5
Knex Search
Install
yarn add knex-searchExample
import * as knex from 'knex';
import { Search } from 'knex-search';
const searchObject = new Search({
queryBuilder: knex({ client: 'pg' })('your_table'),
searchableFields: ['name', 'id', 'created_at', 'deleted_at'],
displayableFields: ['name', 'id', 'created_at', 'deleted_at'],
});
const query = {
name__li: 'denver',
orderBy: ['name,desc'],
};
const result = await searchObject.createSearch(query).run();
/*
// result...
{
count: 25,
currentPage: 1,
filters: {
name__li: "denver",
},
firstPage: 1,
lastPage: 2,
nextPage: 1,
orderBy: [{column: "name", direction: "desc"}],
perPage: 25,
prevPage: null,
results: [{id: 1, name: "denver"}, ...],
total: 50,
totalPages: 2,
}
*/Named Query Keys:
page Defines the page of the results to go to.
const query = {
name: 'Denver',
page: 2
};
const results = await searchObject.createSearch(query).run();perPage Defines the number of results per page. Default is 25
const query = {
name: 'Denver',
perPage: 40
};
const results = await searchObject.createSearch(query).run();orderBy Defines the order for the search. format: column,direction.
Valid directions are asc, desc, ASC, DESC, IS NULL, IS NOT NULL, NULLS LAST, NULLS FIRST.
The columns you can order by are limited by the searchableFields passed into the Search constructor.
const query = {
name: 'Denver',
orderBy: 'id,desc'
};
const results = await searchObject.createSearch(query).run();Multiple order bys
const query = {
name: 'Denver',
orderBy: ['id,decs', 'name,asc']
};
const results = await searchObject.createSearch(query).run();columns Defines the columns you want back in the results.
const query = {
name: 'Denver',
columns: ['id', 'name']
};
const results = await searchObject.createSearch(query).run();noPaging If passed in the paging will not be calculated
const query = {
name: 'Denver',
noPaging: true
};
const results = await searchObject.createSearch(query).run();withDeleted If passed in the paging will included deleted records
const query = {
name: 'Denver',
withDeleted: true
};
const results = await searchObject.createSearch(query).run();Search Terms
You can pass any number of search class.
The columns you can search against are limited by the searchableFields passed into the Search constructor.
You can also modify how the search term searches using modifiers.
The Format is column__modifier;
The following modifiers are available:
__liCreates a Like query where wild cards are placed on each side of the search value.column__li: 'fo'__inCreates an In query.column__in: ['foo', 'bar']__ninCreates a Not In query.column__nin: ['foo', 'bar']__gtCreates a Greater Than query.column__gt: '100'__gteCreates a Greater Than Or Equal query.column__gte: '100'__ltCreates a Less Than query.column__lt: '100'__lteCreates a Less Than Or Equal query.column__lte: '100'__btwCreates a Between query.column__btw: '1000|2000'__nCreates a Where NULL query.column__n: true__nnCreates a Where NOT NULL query.column__nn: true__neqCreates a Not Equals query.column__neq: 'foo'__eqCreates an Equals query.column__eq: 'foo'- If no modifer is present it creates an
__eqquery
const query = {
name_li: 'dave',
age_gt: 30,
};
const results = await searchObject.createSearch(query).run();