1.6.0 • Published 2 years ago

bare-queryable v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Query on array

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.

1.6.0

2 years ago

1.5.3

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago