jsonapi-server-elasticsearch
is a Elasticsearch backed data store for jsonapi-server
.
This project conforms to the specification laid out in the jsonapi-server handler documentation.
var ElasticsearchStore = require("jsonapi-store-elasticsearch");
jsonApi.define({
resource: "comments",
handlers: new ElasticsearchStore({
host: "localhost:9200"
})
});
- Search, Find, Create, Delete, Update
- Filtering, Sorting, Pagination
Getting this data store to production isn't too bad...
- Bring up your Elasticsearch stack.
- Create an index, give it a sensible name.
- Create an alias for your index to the name "jsonapi".
- Create the mapping for your resource. If you rely on the automatic mapping, you won't be able to sort on any attributes, and thus pagination won't work either.
- Deploy your code.
- Celebrate.
You'll probably want to override this handler's search
functionality in order to achieve high-performance queries. That may look something like this:
var efficientHandler = new ElasticsearchStore({
host: "localhost:9200"
});
// the next function correlates with the jsonapi-server handler documentation:
// https://github.com/holidayextras/jsonapi-server/blob/master/documentation/handlers.md
efficientHandler.search = function(request, callback) {
// within this scope, this._db is the Elasticsearch client
var efficientQuery = buildAwesomeQuery();
this._db.search(efficientQuery, callback);
};
jsonApi.define({
resource: "comments",
handlers: efficientHandler
});
When making schema changes...
If you are only adding new attributes, you can push in a mapping for the new attribute, deploy your code changes and celebrate.
If you are making destructive changes, you'll need to create a new index, add a fresh mapping, migrate data into it and finally swap the "jsonapi" alias to point to your new index. You can read this section on how to switch aliases around: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#indices-aliases