Ember and Sails looked like a match made in heaven. With Sails' superfast APIs and Ember's superfast models, perfect!
Not quite. Ember was quite picky about the data received from Sails, and Sails a little tardy on specification compliance (JSONAPI), and doesn't support PATCH out of the box ( ! ).
So, here is the setup I worked out, allowing an Ember frontend to communicate with Sails backend with MariaDB at the rear.
- EmberJS v2.7
- SailsJS v0.12.4
- sails-mysql v0.11.5
- sails-json-api-blueprints v0.12.1
- sails-generate-models v1.1.0
- JSON API v1.0
- node v6.5.0
- npm v3.10.3
- nvm v0.31.6
- PATCH-PUT solution
-
Sails does not support PATCH out of the box, only PUT; so I added an alias for PATCH in routes.js to redirect PATCHs to PUTs.
-
Sails doesn't talk proper JSONAPI (nor REST) so I used a Sails blueprint to format the output to JSONAPI.
-
Ember needs to use it's JSONAPIAdapter.
-
EmberJS 2.7 in it's own folder
-
with JSONAPIAdapter enabled in
app/adapters/application.js
export default DS.JSONAPIAdapter.extend({ host: 'http://localhost:1337', /// 1337 is Sails's default port });
-
-
SailsJS 0.12.4 in it's own folder
npm install --save sails-json-api-blueprints
- and modifications to
-
config/cors.js
:- set allRoutes to
true
- add
PATCH
to the methods attribute
- set allRoutes to
-
config/routes.js
:- then we add a custom route for each Sails api like this:
'PATCH /individuals/:id': 'IndividualsController.update'
- where
Individuals
is an existing model.
-
In addition to setting up the above communication between Ember and Sails, I also created Sails models from a schema I had created earlier in MariaDB (essentially MySQL).
To create Sails models quickly I entered Sails project directory and ran the following:
npm install --save sails-mysql
npm install --save-dev sails-generate-models
Setup sails-mysql
adapter
I updated the database details in config/connections.js
to the MariaDB previously created.
// someMysqlServer: { },
someMysqlServer: {
adapter: 'sails-mysql', // there's the adapter we npm installed
host: 'localhost',
user: 'user',
password: 'password',
database: 'vdi'
},
- and pointed Sails towards it by updating
config/models.js
with:
// connection: 'localDiskDb',
connection: 'someMysqlServer', // the database name from connections.js
Generate Sails Models Fast!
Then in the Sails project folder I ran sails-generate-models
; all my models and controllers are automatically generated using the details i added about in connection.js
. Here's an example output:
# sails-generate-models
- Writing Accounts.js...
- Writing AccountsController.js...
- Writing Headings.js...
- Writing HeadingsController.js...
...
With any change to the database schema I just re-run sails-generate-models
to keep Sails's models uptodate.
Creating models in Ember was simply a matter of calling ember-cli and adding stubs for expected fields from the backend.
Begin with ember g model individuals
.
And update the model file app/models/individuals.js
with fields:
import DS from 'ember-data';
import attr from 'ember-data/attr';
export default DS.Model.extend({
'first-name': attr( ),
'last-name': attr( ),
postcode: attr( ),
address1: attr( ),
address2: attr( ),
role: attr( ),
nationality: attr( ),
});