An easy to use framework to quick build rest api service from json-schema data models
{
"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 nameduser
-
"responsibility":{ "type": "string", "schema":"User" }
means fieldresponsibility
in modelConfigurationItem
reference modelUser
which generates 1-1 relation -
"maintainers": { "type": "array", "items": { "type": "string", "schema":"User"}, "uniqueItems": true }
means fieldmaintainers
in modelConfigurationItem
is an array each reference modelUser
which generates 1-n relation -
"search":{"index":"cfgItem"}
means instance ofConfigurationItem
will also stored into elasticsearch withcfgItem
as index name
- 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
- install db server
-
install npm dependencies
npm install
-
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 },
-
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"
}
}
}
]
}
}
}
`
-
start
npm start
-
run integration test cases with postman
npm test