# graph-handler

> An easy to use framework to build rest api service with [koa-neo4j](https://github.com/assister-ai/koa-neo4j-starter-kit),data models are fully declarative by [json-schema](http://json-schema.org/)

Latest version **1.0.0-beta.6** (published 2022-09-26) · 0 weekly downloads

## Install

```sh
npm install graph-handler
pnpm add graph-handler
yarn add graph-handler
bun add graph-handler
```

Provides the command `scirichon-crud-api`.

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.6 |
| Published | 2022-09-26 |
| First published | 2021-12-18 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 21 |
| Unpacked size | 51.9 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | ronyang |

## Links

- npm: https://www.npmjs.com/package/graph-handler
- npm.io page: https://npm.io/package/graph-handler

## Dependencies (21)

- [uuid](https://npm.io/package/uuid.md) ^3.4.0
- [kcors](https://npm.io/package/kcors.md) ^2.2.2
- [config](https://npm.io/package/config.md) ^3.3.7
- [dotenv](https://npm.io/package/dotenv.md) ^8.6.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [mkdirp](https://npm.io/package/mkdirp.md) ^1.0.4
- [moment](https://npm.io/package/moment.md) ^2.29.1
- [koa-body](https://npm.io/package/koa-body.md) ^4.2.0
- [koa-neo4j](https://npm.io/package/koa-neo4j.md) 2.0.0-beta.3
- [koa-compose](https://npm.io/package/koa-compose.md) ^4.1.0
- [jsonpath-plus](https://npm.io/package/jsonpath-plus.md) ^6.0.1
- [license-helper](https://npm.io/package/license-helper.md) 1.0.7-beta.3
- [scirichon-cache](https://npm.io/package/scirichon-cache.md) 1.0.11-beta.12
- [scirichon-common](https://npm.io/package/scirichon-common.md) 1.1.0-beta.1
- [scirichon-search](https://npm.io/package/scirichon-search.md) 1.1.0-beta.6
- [scirichon-json-schema](https://npm.io/package/scirichon-json-schema.md) 1.0.3-beta.7
- [log4js-wrapper-advanced](https://npm.io/package/log4js-wrapper-advanced.md) ^1.0.5
- [scirichon-authenticator](https://npm.io/package/scirichon-authenticator.md) 1.0.0-beta.4
- [scirichon-response-mapper](https://npm.io/package/scirichon-response-mapper.md) 1.0.7-beta.13
- [scirichon-json-schema-init](https://npm.io/package/scirichon-json-schema-init.md) 1.1.0-beta.9
- [scirichon-response-wrapper](https://npm.io/package/scirichon-response-wrapper.md) 1.0.2-beta.4

## Recent versions

- 1.0.0-beta.6 (latest) — 2022-09-26
- 1.0.0-beta.5 — 2022-09-26
- 1.0.0-beta.4 — 2022-03-10
- 1.0.0-beta.3 — 2021-12-26
- 1.0.0-beta.2 — 2021-12-26
- 1.0.0-beta.1 — 2021-12-26
- 1.0.0 — 2021-12-18

## README

An easy to use framework to build rest api service with [koa-neo4j](https://github.com/assister-ai/koa-neo4j-starter-kit),data models are fully declarative by [json-schema](http://json-schema.org/)

## features

* fully declarative koa routes,relationship in neo4j　by json schema

## data modeling based on json schema extension attributes

### basic model

```
{
  "id": "User",
  "type": "object",
  "properties": {
    "alias": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "lang": {
      "type": "string"
    },
    "userid":{
      "type":"integer"
    },
    "passwd":{
      "type":"string"
    }
  },
  "route":"/users"
}
```

* first each data model is a valid json schema,so model 'User' will be validated with [ajv](https://github.com/epoberezkin/ajv) as json object with fields and related data types as above

* data model with attribute `"route":"/users"`  will generate restful api interface with route `/users`

```
POST /users

PUT  /users/:uuid

DELETE /users/:uuid

GET /users/:uuid

GET /users
```

* `"id":"User"` is not only the id of the json schema but also the label of the node stored in neo4j

### model reference others

```
{
  "id": "ConfigurationItem",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "responsibility":{
        "type": "integer",
        "schema":"User",
        "relationship":{"name":"RESPONSIBLE_FOR","reverse":true}
    },
    ...
  },
  "required": ["name"],
  "route": "/cfgItems",
  "search":{"index":"cmdb"}
}
```

* `schema` means field `responsibility` in model `ConfigurationItem` reference model `User` and will generate relationship in neo4j as following

    (:ConfigurationItem)<-[:RESPONSIBLE_FOR]-(:User)

* `search` means instance of `ConfigurationItem` will also stored in elasticsearch with `cmdb` as index name

## Search

* query interfaces which use cypher and elasticsearch dsl(will I called eql) directly

```cypher
api/searchByCypher
{
	"category":"ITService",
	"search":["email","pop3"],
	"cypher":"OPTIONAL MATCH (s1:ITService) WHERE s1.uuid IN {search} or s1.group IN {search} WITH COLLECT(distinct(s1.uuid)) as services_byIds UNWIND {search} as keyword OPTIONAL MATCH (s1:ITService)-[:BelongsTo]->(sg:ITServiceGroup) WHERE s1.name = keyword or sg.name = keyword WITH services_byIds+collect(distinct(s1.uuid)) as services UNWIND services AS service RETURN COLLECT(distinct service)"
}
```

`category` is id of the model,`cypher` is the raw cypher query, other fields are required parameters in cypher query

```eql
api/searchByEql
{
  "category":"ConfigurationItem",
  "body":
  {
      "query": {
      	"bool":{
      		"must":[
      			{"match": {"category": "Router"}},
      			{"match":{"status.status":"In_Use"}},
      			{"match":{"it_service":"{{service_email_id}}"}}
      		]
      	}

      },
      "sort" : [
          { "product_date" : {"order" : "desc"}}]
  }
}
```

`category` is id of the model,`body` is the raw eql


## Deploy

1. install db server

 [neo4j](http://neo4j.com/docs/operations-manual/current/installation/)

 [elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/master/_installation.html)

 [redis](https://redis.io/topics/quickstart)

2. install npm dependencies

    npm install

3. configuration

    modify value in config/default.json to match db configuration

    ```
      "neo4j": {
        "host": "localhost",
        "port": 7687,
        "http_port":7474,
        "user": "neo4j",
        "password": "neo4j"
      },
      "elasticsearch":{
        "host": "localhost",
        "port": 9200,
        "requestTimeout":3000,
        "mode": "strict"
      },
      "redis": {
        "host": "localhost",
        "port": 6379
      },
    ```


4. init Schema

    npm run init

5. start

    npm start
    

6. run integration test cases with [postman](https://www.getpostman.com/docs/)

    npm test

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