Skip to content

mahdiaslami/bare-queryable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

20df675 · Oct 18, 2022
Jun 4, 2022
Jun 19, 2022
Oct 10, 2021
Mar 1, 2022
Oct 9, 2021
Oct 9, 2021
Oct 18, 2022
Mar 2, 2022
May 19, 2022
May 19, 2022
Jun 4, 2022

Repository files navigation

Query on array

Version Downloads

bare-queryable help you to query on an array of objects as SQL way.

In the usual way, you need to create a callback method for javascript array helpers like filter() and etc, But here you only do what you need.

Introduction

import { query } from 'query'

const users = [
  { id: 1, name: 'bare' },
  { id: 1, name: 'bare' },
]

const result = 
  query(users)
    where('name').contain('re')
    .get()

APIs

Select

  • get()

Returns query result.

query([...]).get()

Limit

query([...]).first()
  • first()

Returns first item of query result.

  • last()

Returns last item of query result.

  • count()

Returns count of items in query result.

Where

query([...])
  .where('id').equal(1)
  .get()

column parameter can be nested, for example:

query([...])
  .where('prop.id').equal(1)
  .get()
  • where(column).equal(value)

Filters the array so that the column value is exactly equal to the desired value. We use the === operator for this comparison.

  • where(column).notEqual(value)

Filters the array so that the column value is not equal to the desired value. We use the !== operator for this comparison.

  • where(column).above(value)

Filters the array so that the column value is greater than the desired value.

  • where(column).aboveOrEqual(value)

Filters the array so that the column value is greater than or equal to the desired value.

  • where(column).below(value)

Filters the array so that the column value is lower than the desired value.

  • where(column).belowOrEqual(value)

Filters the array so that the column value is lower than or equal to the desired value.

  • where(column).contain(value)

Filters the array so that the column value contains the desired value.

  • where(column).in(array)

Filters the array so that the column value is inside the desired array.

query([...])
  .where('id').in([1, 2, 3])
  .get()
  • andWhere(column)

Chain conditions with and operator. both ways are ok.

query([...])
  .where('id').equal(1)
  .andWhere('name').contain('foo')
  .get()

query([...])
  .where('id').equal(1)
  .where('name').contain('foo')
  .get()
  • orWhere(column)

Chain conditions with or operator.

query([...])
  .where('id').equal(1)
  .orWhere('id').above(10)
  .get()
  • Compare two columns

If you want to compare two columns you can use the .col attribute.

query([...])
  .where('name').col.notEqual('family')
  .get()

Order by

Sorts columns ascending or descending.

import { NUMBER_COMPARATOR, STRING_COMPARATOR, DATE_COMPARATOR } from 'bare-queryable'

query([...])
  .orderBy('id', NUMBER_COMPARATOR).asc()
  .orderBy('name', STRING_COMPARATOR).desc()
  .orderBy('born_at', DATE_COMPARATOR).desc()
  .get()

column parameter can be nested, for example:

query([...])
  .order('prop.id').equal(1)
  .get()
  • DATE_COMPARATOR

A callback that can compare two Dates.

  • STRING_COMPARATOR

A callback that can compare two strings.

  • NUMBER_COMPARATOR

A callback that can compare two numbers.

About

Query on array.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published