# Sails.js as the backend

**URL:** <https://discuss.emberjs.com/t/sails-js-as-the-backend/5233>\
**Category:** Uncategorized\
**Created:** [May 13, 2014, 8:20am UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233 "2014-05-13T08:20:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 8:20am UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/1 "2014-05-13T08:20:02Z")

</div>

Hi everyone,

Any thoughts/tips/examples on using sails as the backend API for ember? Particularly, what is the best why to make the default sails rest format confirm to what ember expects? Is it maybe better to make ember accept sails format?

Thanks!

---

<div class="post-metadata">

**Author:** ![m19](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@m19](https://discuss.emberjs.com/u/m19)\
**Post date:** [May 13, 2014, 3:55pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/2 "2014-05-13T15:55:41Z")

</div>

I use Sails version 0.10@rc-7 for a realtime Ember app. I started with [this adapter](https://github.com/bmac/ember-data-sails-adapter) and [this serializer](http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/) and “hacked” them together. I’ve been thinking for a while now to make my versions open source so their quality can be improved a lot.

If there is more interest in this let me know.

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 5:35pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/3 "2014-05-13T17:35:52Z")

</div>

Thanks! So you do it with no change to the sails blueprint API?

I’m **very** interested in this. I’m surprised that sails doesn’t have an ember-adapter (does it?) I think sails could be an excellent backend, mostly because of its easy support for sockets.

It would have been really cool though if the integration were easier.

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 6:02pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/4 "2014-05-13T18:02:20Z")

</div>

Looking at [https://github.com/bmac/ember-data-sails-adapter](https://github.com/bmac/ember-data-sails-adapter) again, I can’t find how it deals with the problem of ember expecting a root-element in the json:

Ember: { posts: [{…},{…}] }

Sails: [{…},{…}]

Did I miss something?

---

<div class="post-metadata">

**Author:** ![m19](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@m19](https://discuss.emberjs.com/u/m19)\
**Post date:** [May 13, 2014, 6:14pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/5 "2014-05-13T18:14:47Z")

</div>

Yes, the blueprint api that Sails offers is untouched in my app.

You can find my version of the adapter [here](https://gist.github.com/m19/31606a34f1ac89ed612d) and my serializer [here](https://gist.github.com/m19/929475e8d26411abc091).

The serializer transforms the Sails payload to the expected Ember payload. This is the preferred way (imo) since your API might be used for other things too, editing your whole api makes no sense.

Please note that these versions are edited on the fly while developing my own app, so I wouldn’t recommend using it in production. But it should definitely help you start.

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 6:23pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/6 "2014-05-13T18:23:37Z")

</div>

Thanks a lot!!! I will check it out now and see if I can make it work.

Are you using the SailsSocketAdapter? How is that working for you?

Thanks again!

---

<div class="post-metadata">

**Author:** ![m19](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@m19](https://discuss.emberjs.com/u/m19)\
**Post date:** [May 13, 2014, 6:25pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/7 "2014-05-13T18:25:33Z")

</div>

Yes I use the socket adapter. Since I wanted real time functionality.

`App.ApplicationAdapter = DS.SailsSocketAdapter.extend({ namespace: '/api', log: true });`

I use the namespace /api in my sails config, so ignore that if you don’t.

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 6:29pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/8 "2014-05-13T18:29:54Z")

</div>

Great!

I hope I’ll be able to make it work today. Thanks!

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 7:44pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/9 "2014-05-13T19:44:00Z")

</div>

OK, plain REST now works 😄

I’m very happy no change to the sails blueprint needed to be made. Only “pluralize: true” in the blueprint configuration, right?

Sockets is next 😄

Thank you so much!

---

<div class="post-metadata">

**Author:** ![m19](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@m19](https://discuss.emberjs.com/u/m19)\
**Post date:** [May 13, 2014, 7:51pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/10 "2014-05-13T19:51:16Z")

</div>

I have `pluralize: false`, maybe both work. No idea if I’m honest.

For example I have this route:

````auto
App.GameRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.find('game', params.game_id);
    }
});
```
It makes a request to `127.0.0.1:1337/api/game/1` when I go to `127.0.0.1:1337/#/game/1`.

Same for `Games` route

````

App.GamesRoute = Ember.Route.extend({ model: function () { return this.store.find(‘game’); } });

```auto

Makes a request to `127.0.0.1:1337/api/game/`, so singular, not plural.

These requests are made over sockets by default when using the `SailsSocketAdapter`.
```

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 8:48pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/11 "2014-05-13T20:48:57Z")

</div>

ok, using the SailsRESTAdapter and this.store.find(‘user’) I got a call to /users (plural).

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [May 13, 2014, 9:39pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/12 "2014-05-13T21:39:12Z")

</div>

Hi @ofridagan. There is an unfortunate bug currently with the Ember Sails Adapter where the `SailsSocketAdapter` uses singular model names in its routes and the the `SailsRESTAdapter ` uses plural model names in its routes. There is a pr that fixes that issue where I’ve been waiting on some feedback but I can merge that now and push out a new release.

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [May 13, 2014, 9:52pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/13 "2014-05-13T21:52:44Z")

</div>

I’ve been using Ember Data’s built in [JSONSerializer](http://emberjs.com/api/data/classes/DS.JSONSerializer.html) to work around Sails’s lack of a root-element in its response payload. This requires defining your relationships as ‘[async](http://emberjs.com/api/data/#method_belongsTo)’ which have some annoying edge cases in Ember Data at the moment. As a result I’d defiantly suggest go down the path of using @m19’s [serializer](https://gist.github.com/m19/929475e8d26411abc091).

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 13, 2014, 10:02pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/14 "2014-05-13T22:02:26Z")

</div>

Thank you @bmac!

If I understand correctly the fix adds the option to use plural for SailsSocketAdapter. There is still no option to **not** use plural with SailsRESTAdapter, right?

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [May 13, 2014, 10:16pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/15 "2014-05-13T22:16:11Z")

</div>

The fix makes it so the `SailsRESTAdapter` uses singular route names by default (`SailsSocketAdapter` was already working this way). This is in line with the default sails.js behavior.

If you would like to use plural model names in your route you will need to [extend](https://github.com/bmac/ember-data-sails-adapter#pluralised-routes) the `SailsRESTAdapter` and set pluralize: true" in the blueprint config.

---

<div class="post-metadata">

**Author:** ![GlobeCoder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/globecoder/32/14356_2.png) [@GlobeCoder](https://discuss.emberjs.com/u/GlobeCoder)\
**Post date:** [May 14, 2014, 3:03am UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/16 "2014-05-14T03:03:56Z")

</div>

I am very interested in using Sails.js as the backend as well and will probably try it for one of the next projects. Good to see that there is some more interested. Even though I have seen the ember-sails-adapter my strategy would probably to create a blueprint to change it on the server-side (don’t like doing that kind of stuff on the client-side if I have control over the API).

---

<div class="post-metadata">

**Author:** ![ofridagan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ofridagan/32/15760_2.png) [@ofridagan](https://discuss.emberjs.com/u/ofridagan)\
**Post date:** [May 14, 2014, 8:10am UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/17 "2014-05-14T08:10:36Z")

</div>

That’s interesting. I was also thinking about the best way to go.

I think I would also like to keep Ember as it is because it is more compatible with [http://jsonapi.org/](http://jsonapi.org/) It’s a shame jsonapi is not yet the standard.

---

<div class="post-metadata">

**Author:** ![GlobeCoder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/globecoder/32/14356_2.png) [@GlobeCoder](https://discuss.emberjs.com/u/GlobeCoder)\
**Post date:** [June 12, 2014, 10:48pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/18 "2014-06-12T22:48:12Z")

</div>

Just starting to work with Sails 0.10 and it is really simple to override the default JSON response using blueprints. Might push it to a repository so other people can save the grunt work.

---

<div class="post-metadata">

**Author:** ![m19](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@m19](https://discuss.emberjs.com/u/m19)\
**Post date:** [June 13, 2014, 10:23am UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/19 "2014-06-13T10:23:24Z")

</div>

@GlobeCoder, really curious what you’ve made.

---

<div class="post-metadata">

**Author:** ![marcus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/marcus/32/5737_2.png) [@marcus](https://discuss.emberjs.com/u/marcus)\
**Post date:** [June 18, 2014, 3:52pm UTC](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233/20 "2014-06-18T15:52:07Z")

</div>

@GlobeCoder Also curious about your Sails blueprints for Ember, since I’m more or less half way through writing/modifying my own blueprints and I also thought about releasing them open source. Maybe a good idea to do this together?

[Next page](https://discuss.emberjs.com/t/sails-js-as-the-backend/5233.md?page=2)
