Skip to content

karmi/ember-data-elasticsearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ember Data Adapter for elasticsearch

This library provides an adapter for the Ember Data persistence framework for Ember.js, allowing to store application models as JSON documents in elasticsearch.

It handles the full model lifecycle (create/read/update/delete), in the same way as the DS.RESTAdapter bundled with Ember Data.

It does not provide support for any Rails-like has_many associations and probably never will.

It does not provide support for the bulk Ember Data API, yet.

Usage

First, load the ember-data/lib/adapters/elasticsearch_adapter.js file in your application.

Use the adapter in your application's store:

var App = Em.Application.create();

App.store = DS.Store.create({
  revision: 4,
  adapter: DS.ElasticSearchAdapter.create({url: 'http://localhost:9200'})
});

To define a model in your application, use the standard Ember Data API:

App.Person = DS.Model.extend({
  name: DS.attr('string')
});

Define an index and type for the adapter as the model's url property:

App.Person.reopenClass({
  url: 'people/person'
});

To create a new record:

App.Person.createRecord({ id: 1, name: "John" });
App.Person.createRecord({ id: 2, name: "Mary" });
store.commit();

To load models from the store, use the find method:

var people = App.Person.find();
people.toArray().map( function(person) { return person.get("name") } );
// => ["John", "Mary"]

To load a single model by ID:

var person = App.Person.find( 1 );
person.get("name");
// => "John"

To load multiple models by their IDs, pass them as an Array:

var people = App.Person.find( [2, 1] );
people.toArray().map( function(person) { return person.get("name") } );
// => ["Mary", "John"]

To load models by an elasticsearch query, pass it as an Object:

var people = App.Person.find( {query: { query_string: { query: "john" } }} );
people.get("length");
// => 1

To persist model changes to the store, use the store's commit method:

person.set("name", "Frank");
store.commit();

To remove the record from the store:

person.deleteRecord();
store.commit();

Note, that all the methods are asynchronous, so the returned object is empty; bindings and observers take care of updating the object with loaded data in an Ember.js application.

See the Ember Data documentation for more information.

Example Application

The library includes an example “todos” application, as required by law.

Tests

The library comes with a QUnit-based integration test suite in ember-data/test/elasticsearch_adapter_tests.js.

Apart from running it in the browser, you can run it on the command line with the PhantomJS JavaScript runtime via a Rake task:

rake test

Karel Minarik and contributors

Releases

No releases published

Packages

No packages published