Code Monkey home page Code Monkey logo

ember-data-elasticsearch's Introduction

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

ember-data-elasticsearch's People

Contributors

karmi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.