Coder Social home page Coder Social logo

koa-mongo-schema's Introduction

An easy to use framework to quick build rest api service from json-schema data models

data modeling based on json schema extension attributes

{
  "id": "User",
  "type": "object",
  "properties": {
    "alias": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "lang": {
      "type": "string"
    },
    "userid":{
      "type":"integer"
    },
    "passwd":{
      "type":"string"
    }
  },
  "collection": "user"
  "route":"/users"
}
{
  "id": "ConfigurationItem",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "responsibility":{
        "type": "string",
        "schema":"User"
    },
    "maintainers": {
          "type": "array",
          "items": { "type": "string", "schema":"User"},
          "uniqueItems": true
    },
    ...
  },
  "required": ["name"],
  "route": "/cfgItems",
  "collection": "cfgItem"
  "search":{"index":"cfgItem"}
}
  • first each data model is a valid json schema, and can be validated with ajv

  • data model with attribute "route":"/users" means restful api interface will be generated as following

POST /users

PUT  /users/:uuid

DELETE /users/:uuid

GET /users/:uuid

GET /users
  • "collection": "user" means instance will be stored into mongodb collection named user

  • "responsibility":{ "type": "string", "schema":"User" } means field responsibility in model ConfigurationItem reference model User which generates 1-1 relation

  • "maintainers": { "type": "array", "items": { "type": "string", "schema":"User"}, "uniqueItems": true } means field maintainers in model ConfigurationItem is an array each reference model User which generates 1-n relation

  • "search":{"index":"cfgItem"} means instance of ConfigurationItem will also stored into elasticsearch with cfgItem as index name

Search

  • query interfaces which use mongodb and elasticsearch dsl directly
api/searchByMql
{
    "category":"ITService",
    "body":{
      "name":"email",
      "description":"dns9"
    }
}

category is id of the model,body which is a valid mongodb query filter

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 valid elasticsearch query filter

Deploy

  1. install db server

mongodb

elasticsearch

redis

  1. install npm dependencies

    npm install

  2. configuration

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

      "mongo": {
          "host": "localhost",
          "port": 27017,
          "db": "test"
       },
      "elasticsearch":{
        "host": "localhost",
        "port": 9200,
        "requestTimeout":3000,
        "mode": "strict"
      },
      "redis": {
        "host": "localhost",
        "port": 6379
      },
    
  3. init Schema

    npm run init

mongodb is schemaless but elasticsearch is not,so run initSchema to build schema in elasticsearch with the template as following

` {

"mappings": {
    "doc": {
        "dynamic_templates": [
            {
                "string_as_date": {
                    "match_pattern": "regex",
                    "match":   ".*_date$|.*_time$|created|lastUpdated",
                    "mapping": {
                        "type": "date"
                    }
                }
            },
            {
                "string_as_keyword": {
                    "match_mapping_type": "string",
                    "unmatch": "*_pinyin",
                    "mapping": {
                        "type": "keyword"
                    }
                }
            }
        ]
    }
}

}

`

  1. start

    npm start

  2. run integration test cases with postman

    npm test

koa-mongo-schema's People

Contributors

yrong avatar

Stargazers

 avatar

Watchers

James Cloos avatar

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.