Thursday 2 September 2021

ArangoDB: Enforce schema at collection level

ArangoDB is schema less by default, But it allows you enforce schema at collection level.

 

How to define a schema?

Schema is defined in json schema format (https://json-schema.org/).

 

For example,

var schema = {
  "rule": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "firstName": {
        "type": "string"
      },
      "lastName": {
        "type": "string"
      },
      "hobbies": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    },
    "required": ["id", "firstName", "lastName"],
    "additionalProperties": false
  },
  "level": "moderate",
  "message": "Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes."

}

 

Above schema defines how the document in a collection should looks like. Schema define that a document has 4 attributes.

a.   id

b.   firstName

c.    lastName

d.   hobbies

 

In these 4 attributes id, firstName and lastName are mandatory and hobbies is optional. As per this schema document shouldn’t contain any additional attributes.

 

Level control

Level controls when the validation should be triggered. Below table summarizes possible values associated with level attribute.

 

Value

Description

none

Validation is turned off

new

Validation is applicable to only newly inserted documents.

moderate

New and modified documents must pass validation except for modified documents where the OLD value did not pass validation already.

strict

All new and modified document must strictly pass validation.

 

Error message

When the schema validation fails, ArangoDB throws an error. You can customize this error message using ‘ message’ attribute.

 

Create a collection using this schema

db._create("person", {"schema": schema})

127.0.0.1:8529@abc_org> var schema = {
...>   "rule": {
...>     "properties": {
...>       "id": {
...>         "type": "integer"
...>       },
...>       "firstName": {
...>         "type": "string"
...>       },
...>       "lastName": {
...>         "type": "string"
...>       },
...>       "hobbies": {
...>         "type": "array",
...>         "items": {
...>           "type": "string"
...>         }
...>       }
...>     },
...>     "required": ["id", "firstName", "lastName"],
...>     "additionalProperties": false
...>   },
...>   "level": "moderate",
...>   "message": "Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes."
...> 
...> }

127.0.0.1:8529@abc_org> db._create("person", {"schema": schema})
[ArangoCollection 37662, "person" (type document, status loaded)]

 

Try to insert documents which are not comply with the schema.

127.0.0.1:8529@abc_org> db.person.save({"id" : 1})
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1620: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
!      throw error;
!      ^
stacktrace: ArangoError: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.save.ArangoCollection.insert (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:965:14)
    at <shell command>:1:11

127.0.0.1:8529@abc_org> db.person.save({"id" : 1, "firstName": "Krishna", "lastName": "Gurram", "a" : 1})
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1620: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
!      throw error;
!      ^
stacktrace: ArangoError: Valid attributes are id, firstName, lastName and hobbies. id, firstName and lastName are mandatory attributes.
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.save.ArangoCollection.insert (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:965:14)
    at <shell command>:1:11

 

Let’s insert a document that is comply with the schema.

127.0.0.1:8529@abc_org> db.person.save({"id" : 1, "firstName": "Krishna", "lastName": "Gurram"})
{ 
  "_id" : "person/37705", 
  "_key" : "37705", 
  "_rev" : "_cS_cpIC---" 
}

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment