# querifier

> Mongo query implementation for objects

Latest version **0.6.2** (published 2019-01-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install querifier
pnpm add querifier
yarn add querifier
bun add querifier
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2019-01-01 |
| First published | 2018-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 104.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | LastShadowJS |
| Maintainers | lastshadowpl |
| Keywords | Mongo, Query, Node |

## Links

- npm: https://www.npmjs.com/package/querifier
- Repository: https://github.com/LastShadowPL/querify
- Homepage: https://github.com/LastShadowPL/querify#readme
- Issues: https://github.com/LastShadowPL/querify/issues
- npm.io page: https://npm.io/package/querifier

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.6.2 (latest) — 2019-01-01
- 0.6.1 — 2018-12-31
- 0.6.0 — 2018-12-31
- 0.5.1 — 2018-12-30
- 0.5.0 — 2018-12-29
- 0.4.8 — 2018-12-28
- 0.4.7 — 2018-12-28
- 0.4.6 — 2018-12-27
- 0.4.5 — 2018-12-27
- 0.4.4 — 2018-12-27
- 0.4.3 — 2018-12-27
- 0.4.2 — 2018-12-26
- 0.4.1 — 2018-12-25
- 0.4.0 — 2018-12-25
- 0.3.1 — 2018-12-23
- … 4 more at https://npm.io/package/querifier/versions

## README

# Querifier
Mongo-like query execution on javascript objects

## Currently available 
1. [Update](#update)
2. [Get](#get)
3. [Operators](#operators)
4. [Conditionals](#conditionals)

# Features
## <div id="update">Update</div>
Performes a not mutating action on an object 
>Example
```typescript
  const object = {
    n: 2
  }

  update(object, {n: 4})
  // -> { n: 4 }
  
```
Bisides accepting a classical ```key: value``` pairs, it accepts special operators, just like MongoDB queries.
## <div id="get">Get</div>
### Basics
#### Calling with conditions
Performes a search operation on specified collections of an object. Accepts an object, `ConditionQuery` and `options`
>Example
```typescript
  const object = {
    evens: [0, 2, 4, 6, 8],
    odds: [1, 3, 5, 7, 9]
  }

  get(object, {
    evens: {
      $lt: 6
    },
    odds: {
      $lt: 7
    }
  }, {
    $sort: "asc"
  })
  // -> [0, 1, 2, 3, 4, 5]
``` 
#### Calling without conditions
If called without any conditions, but with a collection name, returns an arrayified version of the said collection. 
But if no collections are specified, returns an empty array.
>Example
```typescript
  const object = {
    evens: [0, 2, 4, 6, 8],
    odds: [1, 3, 5, 7, 9]
  }

  get(object, {
    evens: {},
    odds: {}
  }) // -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  get(object, {}) // -> []
  get(object)     // -> []
```  
### <div id="operators">Operators:</div>
#### $set
>Sets a new value of object's property **only** if their types match
```typescript
const object = { number: 2 }
update(object, { $set: { number: "newValue" } }) ❌
// -> { number: 2 }

update(object, { $set: { number: 21 } }) ✔️
// -> { number: 21 }
```


#### $inc
>Increments object's value by some number. It accepts both positive and negative numbers
```typescript
const object = { number: 2 }

update(object, { $inc: { number: "2" } }) ❌
// -> { number: 2 }

update(object, { $inc: { number: -2 } }) ✔️
// -> { number: 0 }
```


#### $min 
> Changes the value when it's smaller than the current one
```typescript
const object = { number: 20 }
update(object, { $min: { number: 30 } }) ❌
// -> { number: 20 }

update(object, { $min: { number: 10 } }) ✔️
// -> { number: 10 }
```


#### $max
> Changes the value when it's bigger than the current one
```typescript
const object = { number: 20 }
update(object, { $max: { number: 10 } }) ❌
// -> { number: 20 }

update(object, { $max: { number: 30 } }) ✔️
// -> { number: 30 }
```

#### $mul
> Multiplies object's value. If it's undefined, ```update``` assigns 0 to the key
 ```typescript
 const object = { number: 2 }

 update(object, { $mul: { number: 4 } })
 // -> { number: 8 }

 update(object, { $mul: { x: 2 } })
 // -> {number: 2, x: 0}
 ```

#### $rename
> Renames object's key. Accepts: ```symbol, string, number```
 ```typescript
 const object = { oldKey: 2 }
 
 update(object, { $rename: { oldKey: () => {} } }) ❌
 // -> { oldKey: 2 }
 
 update(object, { $rename: { oldKey: "newKey" } }) ✔️
 // -> { newKey: 2 }
 ```

#### $unset
> Deletes object's property
 ```typescript
 const object = { dontNeedThat: 2 }

 update(object, { $unset: { dontNeedThat: "Anything you want" } })
 // -> {}
 ```

#### $addToSet
> Adds an item literal to the array. If you want to concatinate an array, checkout the [$push](#push) operator
 ```typescript
 const object = { array: [1, 2, 3, 4] }

 update(object, { $addToSet: { array: 5 } })
 // -> { array: [1, 2, 3, 4, 5] }

 update(object, { $addToSet: { array: [5, 6] } })
 // -> { array: [1, 2, 3, 4, [5, 6]] }
 ```

#### $pull
> Pulls out matching elements from an array. Accepts elements and [conditional operators](#conditionals)
```typescript
  const object = { array: [1, 2, 3, 4, 5] }

  update(object, { $pull: { array: 3 } })
  // -> { array: [1, 2, 4, 5] }

  update(object, { $pull: { array: { $in: [1, 2, 3] } } })
  // -> { array: [4, 5] }
```

### $pop
> Removes the first element of an array when called with `-1` and the last element when called with `1`. Doesn't remove anything when called with something else
```typescript
  const object = { array: [1, 2, 3] }

  update(object, { $pop: { array: 543 } }) ❌
  update(object, { $pop: { array: "String" } }) ❌

  update(object, { $pop: { array: 1 } }) ✔️
  // -> { array: [1, 2] }
```

### $push
> Pushes items to the array. Besides elements, also accepts `$each` operator, which unwraps the input
```typescript
  const object = { array: [1, 2, 3] }

  update(object, { $push: { array: 4 } })
  // -> { array: [1, 2, 3, 4] }

  update(object, { $push: { array: [4, 5] } }) ❌
  // -> { array: [1, 2, 3, [4, 5]] }

  update(object, { $push: { array: { $each: [4, 5] } } }) ✔️
  // -> { array: [1, 2, 3, 4, 5] }
```

## <div id="conditionals">Conditional operators</div>
Example
```typescript
  const object = {array: [1, 2, 3, 4]}

  update(object, { $pull: { array: { $eq: 4 } } }) 
  // -> { array: [1, 2, 3] }

  update(object, { $pull: { array: { $in: [1, 2] } } }) 
  // -> { array: [3, 4] }

  get(object, { array: { $gte: 3 } })
  // -> [3, 4]
```
### $eq
> Matches values that are equal to a specified value
### $ne
> Matches values that aren't equal to a specified value
### $gt
> Matches values that are greater than a specified value
### $gte
> Matches values that are greater than or equal to a specified value
### $lt
> Matches values that are less than a specified value
### $lte
> Matches values that are less than or equal to a specified value
### $in
> Matches values that are members of a specified value
### $nin
> Matches values that aren't members of a specified value
### $and
> Joins query elements with the logical `and` operator. Accepts and array of conditionals
```typescript
const object = { array: [1, 2, 3, 4] }

update(object, {$pull:{array: { $and: [
  { $gt: 2 },
  { $lt: 5 },
  { $in: [3, 4, 5] }
] }}})
// -> { array: [1, 2, 5] }
```
### $or
> Joins query elements with the logical `or` operator. Accepts and array of conditionals
```typescript
const object = { array: [1, 2, 3, 4] }

update(object, {$pull:{array: { $or: [
  { $eq: 2 },
  { $eq: 4 }
] }}})
// -> { array: [1, 3] }
```
### $not 
> Performs the logical `not` operation with the value
```typescript
const object = { array: [1, 2, 3] }

update(object, {
  $pull: {
    array: {
      $not: {
        $eq: 3
      }
    }
  }
}) // -> { array: [3] }

update(object, {
  $pull: {
    array: {
      $not: 2
    }
  }
}) // -> { array: [2] }
``` 

### $type
> Returns true if type matches
```typescript
const object = {array: [1, 2, "Dog"]}

update(object, {
  $pull: {
    array: {
      $type: "string"
    }
  }
}) // -> { array: [1, 2] }
``` 
### $match
> Returns a result of `Regexp.test`
```typescript
const object = {
  files: [
    "index.ts",
    "get.ts",
    "update.ts",
    "lame.js"
  ],
  people: [
    "Jon Snow",
    "Ann Snow",
    "Lil Snow",
    "Lil Notsnow",
  ]
}

update(object, {
  $pull: {
    files: {
      $match: /\.js$/
    }
  }
}) // -> { people: [...], files: ["index.ts", "get.ts", "update.ts"] }

get(object, {
  people: {
    $match: /\w*\sSnow/
  }
}) // -> ["Jon Snow", "Ann Snow", "Lil Snow"]
``` 
### $exec
> Returns a result of the given function of type: ``` <T>(item: T) => boolean```
```typescript
const object = {
  people: [
    {
      name: "Jon Snow",
      age: 21
    },
    {
      name: "Ann Snow",
      age: 20
    },
    {
      name: "Lil Snow",
      age: 8
    }
  ]
}

get(object, {
  people: {
    $exec(person) {
      return person.age >= 18
    }
  }
}) // -> [{name: "Jon Snow", age: 21}, {name: "Ann Snow", age: 20}]
```

---
_Source: https://npm.io/package/querifier · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
