Sails.js as the backend

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!

2 Likes

I use Sails version 0.10@rc-7 for a realtime Ember app. I started with this adapter and this serializer 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.

1 Like

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.

Looking at 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?

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

You can find my version of the adapter here and my serializer here.

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.

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!

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.

Great!

I hope Iā€™ll be able to make it work today. Thanks!

OK, plain REST now works :smile:

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 :smile:

Thank you so much!

I have pluralize: false, maybe both work. No idea if Iā€™m honest.

For example I have this route:

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ā€™); } });


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`.

ok, using the SailsRESTAdapter and this.store.find(ā€˜userā€™) I got a call to /users (plural).

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.

Iā€™ve been using Ember Dataā€™s built in JSONSerializer to work around Sailsā€™s lack of a root-element in its response payload. This requires defining your relationships as ā€˜asyncā€™ 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.

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?

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 the SailsRESTAdapter and set pluralize: true" in the blueprint config.

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).

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/ Itā€™s a shame jsonapi is not yet the standard.

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.

@GlobeCoder, really curious what youā€™ve made.

@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?