Ember Rest Adapter
Expose mongoose models via an API compliant with the defaultInstall
$ npm install ember-mongoose
$ component install kelonye/ember-mongoose
Example
Server
To get started, suppose you have a model, Post
:
// server/models/Post.js moduleexports = title: String content: String versionKey: false ;
To expose this model, via an API, first define the CRUD
auth hooks as:
// server/api_hooks/pre/Post.js exports{ var err; if !req && requser // assuming you're using passport.js err = 'not allowed to access this resource'; errstatus = 403; console; ; }; exports{ ;}; exports{ var err = 'not allowed to access this resource'; errstatus = 403; ;}; exports{ var err = 'not allowed to access this resource'; errstatus = 403; ;};
You also need to specify the fields to expose as:
// server/api_fields/Post.js moduleexports = 'title' 'content';
Finally, build and mount the API to your Express app:
var join = join;var express = ;var db = ;var em = ;var app = ; // build api ; ; app;
With this, you can now access the Post
resource e.g.:
$ curl http://localhost:3000/posts
$ curl
-H "Content-Type: application/json"
-X POST
-d '"post": {{"title":"title","content":""}}'
http://localhost:3000/posts
$ curl http://localhost:3000/posts/1
$ curl
-H "Content-Type: application/json"
-X PUT
-d '"post": {{"title":"title","content":""}}'
http://localhost:3000/posts/1
$ curl -X DELETE http://localhost:3000/posts/1
Client
On client side, assuming you're using component package manager, you can set your app's adapter as:
AppApplicationAdapter = ;
or alternatively, use lib/client/index.js
if not.
Finally, you can then use the Ember-Data
store to perfom API calls, e.g.:
AppPost = DSModel; // pass query to mongoose var query = conditions: title: 'title' options: skip: 0 // start row limit: 10 // end row sort: created_at: -1 // order by latest }; this store ; // create example var record = title: 'My First Post' content: ''; this store ;
Get query count
To only retrieve the number of documents in a query, passin the count
option as:
var query = conditions: title: 'title' options: count: true }; this store ;
For more usage example, see test/support
Test
$ make test