1.1.1 • Published 2 years ago

@samhuk/data-filter v1.1.1

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

data-filter

A package for creating data filters with conversion to an SQL WHERE clause and JSON representation.

data-filter supports both inline values and numbered parameters (as part of a prepared statement).

Usage

npm i @samhuk/data-filter

Simple, single-node filter

import { createDataFilter } from '@samhuk/data-filter'
import { Operator, DataFilterLogic } from '@samhuk/data-filter/types'

const df1 = createDataFilter({
  field: 'username',
  op: Operator.EQUALS,
  val: 'bob',
})
console.log(df1.toSql()) // { sql: (username = $1), values: ['bob'] }
console.log(df1.toSql({ useParameters: false })) // (username = 'bob')

Complex, nested filter:

import { createDataFilter } from '@samhuk/data-filter'
import { Operator, DataFilterLogic } from '@samhuk/data-filter/types'

const df2 = createDataFilter({
  logic: DataFilterLogic.AND,
  nodes: [
    { field: 'id', op: Operator.IN, val: [1, 2, 3] },
    {
      logic: DataFilterLogic.OR,
      nodes: [
        { field: 'email_v1', op: Operator.NOT_EQUALS, val: null },
        { field: 'email_v2', op: Operator.NOT_EQUALS, val: null },
      ],
    },
    { field: 'date_deleted', op: Operator.NOT_EQUALS, val: 'null' },
  ],
})
console.log(df2.toSql({ indentation: 2 }))

Merging filters (using df1 and df2 from above):

import { joinDataFilters } from '@samhuk/data-filter'
df1.addAnd({ field: 'bar', op: Operator.NOT_EQUALS, val: 'b' })
df1.addOr(df2.value)
const df3 = joinDataFilters(DataFilterLogic.AND, df1, df2)

See the JSDocs for more information on the available operators and other options.

Development

npm i

npm start - Start a hot reloading tsc build.

npm run check - Run linting, unit tests, and tsc build.

1.1.1

2 years ago

1.0.10

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago