1

I’m new working with Ember.js and I’m confused about a few things. My real issue is working with MySQL and ember-data. I’ve worked through a few tutorials and found this youtube very useful. At this point, I can create an app that runs on top of a Node.js server. The node server communicates with a mysql database and creates a RESTful API that the ember app consumes. I have no problem getting data from the mysql database and displaying it. But I cannot figure out how to save or update data in the MySQL database. I vaguely understand the concept of the ‘store’ and can push objects into it and display them, but that's it. Are there any examples out there of how to write or update a mysql database using ember-data and Node.js?

EDIT: this question is really asking about the structure of an ember-mysql app and what has worked in production for others.

This is an example of pushing into the store in the routes/message.js

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        var self = this;
        setTimeout(function(){
            console.log("done waiting...")

            self.store.push({
                data: [{
                    id: 11,
                    type: 'message',
                    attributes: {
                        message: 'New Message'
                    },
                    relationships: {}
                }]
            });
        },1000);
        return self.store.findAll('message');
    }
});

1 Answer 1

4

From ember-data perspective it doesn't matter if you use mysql or any other database engine. Ember-data only communicates with an API - a RESTful JSON API by default.

So the node.js part of your application should implement endpoints for both reading and saving data. However writing this API from scratch is a waste, it'd be much easier if you used a framework - if you wish to stick with node.js check out Sails.js, if not, give Ruby on Rails a try.

In both of those frameworks it's pretty straightforward to generate a working RESTful API that ember-data could consume.

As for your code example: Store is a layer in ember-data that's responsible for managing record's data in browser memory. Pushing to Store only adds record to memory (technically records are pushed to Store when you read them from the API).

In order to save a model, just call model.save() which sends a POST (when creating) or a PUT (when updating) request to the API - at least with default JSON API Adapter. (Adapter is the layer that understands how your API works and communicates with it.)

If you're just building a prototype, give Firebase a try - that's basically a ready backend for your JavaScrip app. You could than ditch the node.js part at all - at least for the time beeing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.