# query-mapper

> Map query to resource and get data what you asked for.

Latest version **0.3.7** (published 2021-11-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install query-mapper
pnpm add query-mapper
yarn add query-mapper
bun add query-mapper
```

## 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.3.7 |
| Published | 2021-11-28 |
| First published | 2020-03-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | chandrahul028 |
| Maintainers | chandrahul028 |
| Keywords | query-mapper |

## Links

- npm: https://www.npmjs.com/package/query-mapper
- Homepage: https://github.com/RahulChand028/query-mapper#readme
- npm.io page: https://npm.io/package/query-mapper

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.3.7 (latest) — 2021-11-28
- 0.3.5 — 2020-11-20
- 0.3.1 — 2020-09-27
- 0.3.0 — 2020-09-27
- 0.0.3 — 2020-03-24
- 0.0.2 — 2020-03-23
- 0.0.1 — 2020-03-22

## README

# query-mapper

`query-mapper` is an alternate solution to `graphql` . define your request in a json format.

It use joi like validator to validate request.

## Install

Install with npm:

```sh
npm i query-mapper --save
```

Install with yarn:

```sh
yarn add query-mapper
```

## Example

Here is a basic setup.

###### index.js

```js
const express = require("express");
const bodyParser = require("body-parser");
const { Query } = require("query-mapper");
const map = require("./map");

const app = express();
app.use(bodyParser.json());

app.post("/v1/api", (request, response) => {
  Query.mapper(map, request.body.request)
    .then(result => {
      res.status(200).json(result);
    })
    .catch(error => {
      res.status(500).json(error);
    });
});

app.listen(3000);
```

###### map.js

```js
let { Users, Blogs } = require("../model/models");

let map = {
  User: {
    resolver: (args, parent) => {
      return parent
        ? await Blogs.find({id:parent.data.id})
        : await Blogs.find({})
    }
  },
  Blog: {
    resolver: (args, parent) => {
      return parent
        ? await Blogs.find({id:parent.data.id})
        : await Blogs.find({})
    }
  }
};
module.exports = map;
```

## Request

- `request object to get just user Id and First Name`

```json
{
  "type": "User",
  "attributes": ["Id", "firstName"]
}
```

- `request object to get just user Id , First Name and Blogs title posted by user (shows relation with users and blogs collection)`

```json
{
  "type": "User",
  "attributes": ["Id", "firstName"],
  "relationships": {
    "type": "Blog",
    "attributes": ["title"]
  }
}
```

- `In a case of user having multiple relation like get details about blogs and product relationships would be an array`

```json
{
  "type": "User",
  "attributes": ["Id", "firstName"],
  "relationships": [
    {
      "type": "Blog",
      "attributes": ["title"]
    },
    {
      "type": "Product",
      "attributes": ["Id"]
    }
  ]
}
```

- `data could be pass with query and will be receive in args in User resolver`

```json
{
  "type": "User",
  "attributes": ["Id", "firstName"],
  "query": { "data": 1 }
}
```

# validation

```js
let { Query, Schema } = require("query-mapper");

let userScehema = {
  Id: Schema.isNumber(),
  firstName: Schema.isAlpha().isOptional(),
  blogIds: Schema.isArray([Schema.isNumber()]),
  blogDetails: Schema.isObject({
    Id: Schema.isNumber(),
    title: Schema.isString()
  })
};
```

<b style='background-color:#2ecc71;color:#fff;padding:4px'>validation success</b>

```js
let user = Query.validate(userScehema, {
  Id: 4,
  blogIds: [23, 45, 67],
  blogDetails: { Id: 4, title: "title of blog" }
});

console.log(user);
```

```json
{
  "Id": 4,
  "blogIds": [23, 45, 67],
  "blogDetails": {
    "Id": 4,
    "title": "title of blog"
  }
}
```

<b style='background-color:#e74c3c;color:#fff;padding:4px'>validation error</b>

```js
let user = Query.validate(userScehema, {
  Id: "four",
  blogIds: [23, 45, 67],
  blogDetails: { Id: 4, title: "title of blog" }
});

console.log(user);
```

```json
{
  "errors": [
    {
      "key": "Id",
      "error": "Invalid Type"
    }
  ],
  "blogIds": [23, 45, 67],
  "blogDetails": {
    "Id": 4,
    "title": "title of blog"
  }
}
```

##### Validating request before passing to resolve

```js
let { Users, Blogs } = require("../model/models");

let map = {
  User: {
    before: (request, args, parent) {
       let userScehema = {
           Id: Schema.isNumber(),
           firstName: Schema.isAlpha().isOptional(),
       };
      let validate = Query.validate(userScehema, request.query);

      return validate.errors
        ? { type: false, errors: validate.errors }
        : { type: true };
    },
    resolver: (args, parent) => {
      return parent
        ? await Blogs.find({id:parent.data.id})
        : await Blogs.find({})
    }
  },
};
module.exports = map;
```

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