Coder Social home page Coder Social logo

koa-neo4j-schema's Introduction

An easy to use framework to build rest api service with koa-neo4j,data models are fully declarative by json-schema

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 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
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

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

elasticsearch

redis

  1. install npm dependencies

    npm install

  2. 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
      },
    
  3. init Schema

    npm run init

  4. start

    npm start

  5. run integration test cases with postman

    npm test

koa-neo4j-schema's People

Contributors

yrong avatar

Stargazers

 avatar Wei Wu avatar

Watchers

James Cloos avatar

Forkers

iamgrewal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.