Storing Documents in AppEngine's Datastore
One of the advantages of Google AppEngine's Datastore is that it is schema-less. You can simply store any Entity you wish, as long as you abide the indexes.
In this post, I will mimic the MongoDB document store, where you can store any kind of JSON document in your collection, as long as it has attributes for your collection's indexes.
Objectives
- I would like to access the document store using a generic REST-JSON API, which implements basic CRUD methods plus a query method
- On the consumer side, I would like to map to POJOs, not a map
- I would like to be able to store new kinds of objects without interrupting the service
- I would like to be able to introduce new attributes on my entities without updating any of the existing entities
Source for the json-doc-store
The source code is hosted on BitBucket.
hg clone https://bitbucket.org/f94os/json-doc-store
Binding request from JSON to Map
The intermediate data type when using Spring and jackson JSON mapper is the Map class:
@RequestMapping(value="{kind}", method= RequestMethod.POST)
public ResponseEntity createDocument(
@PathVariable String kind,
@RequestParam String id,
@RequestBody Map props
) {
// Spring and Jackson binds my JSON object to a Map
// Bind to AppEngine Datastore Entities
// persist in datastore
}
Read a document from the datastore
The Spring Controller method to create entities looks like this:
Create by kind only (generate key)
@RequestMapping(value="{kind}/{id}", method= RequestMethod.GET)
public ResponseEntity
// Jackson will serialize from Map to JSON
}
Namespace and Kind
A relational database can be compared to using Namespaces in the Datastore, and a table can be compared to using the Entity Kind.
Admin database
MySQL uses a database with tables for users, user databases, user tables and so on. The same concept is done here, with an
admin Namespace of json-doc-store, and two Kinds, _DBs and _Users.