How to connect the database to ember

Hi! I am new to ember. I have lot of doubts on ember data like how to connect the database to ember and how to run the code with database. is it necessary to run any back end servers with ember server for working with back end.

Hi @Santhoshi, hopefully I can help clear things up for you. Ember is a front-end framework only, and the ember serve command is meant only for development purposes. AFAIK any modern front-end framework will work like this, meaning it will require some sort of API or backend layer. Ember is not a framework intended for developing server-side code. It is, however, highly customizable so you can fit it onto whatever backend you need.

A production ember app will essentially require two servers, or at least a server that serves two different things:

  1. the Ember app itself (an html file, some js, css files and assets)
  2. the API endpoints that the Ember app uses for data

So the ‘ember serve’ command is meant only to fulfill item 1 above, and only for development purposes. To connect a database you’d either want to write your own server API or use something like Firebase or maybe PouchDB (@broerse could probably tell you more about that).

When you actually deploy a production ember app you would typically deploy to a static file service like Amazon S3 or use any other standard webserver with configuration for service a webapp, but again you would never use ember serve in production. It is merely a convenience for serving the webapp locally during development.

1 Like

@dknutsen Thanks! @Santhoshi Yes GitHub - pouchdb-community/ember-pouch: PouchDB/CouchDB adapter for Ember Data will let you talk to an in browser PouchDB server. This server can be synced with a CouchDB server like IBM cloudant or your own CouchDB. For testing you can just run PouchDB without a server. The Ember App can be deployed to the CouchDB database so you don’t need a web server. https://bloggr.exmer.com is deployed to a CouchDB server this way. (We are in the process of switching from martinic.cloudant.com to martinic.couchcluster.com so it can break in the next week)

Source for Bloggr Example GitHub - broerse/ember-cli-blog: Tom Dale's blog example updated for the Ember CLI

@dknutsen thank you for reply.I got some idea on database but i need to know api server.Actually i working on mysql database.Is there any format for writing the api path to connect the database.

Hi @Santhoshi, in order to manage the data you want to save you’ll need to use a backend framework like rails, phoenix or nodejs. These will let you save the information from the ember app to the database.

GoRails have a series which should walk you through this process. You can find it here.

Hi @James-Byrne, I already installed nodejs framework but i don’t know how to connect the database.

Ahh sorry @Santhoshi, in that case heres a tutorial on connecting a node app to a mysql db.

@James-Byrne Thanks, can you provide tutorial on simple user login system using ember-php-my sql with REST adapters.

@Santhoshi if you want to work with ember without having to worry about the whole server-side end of things, look into ember-cli-mirage

1 Like

Hi!.. Im working on my ember training project where it is a login form… I need to use java servelts to get the input text field from the login form(username,password)and store it in mysql database. How to get the input values to servlets program

Hello and welcome :wave:t2: Generally with login forms you will need to post your data (using fetch or another Ajax library) to an endpoint that your Java pages expose. Your Java code will then save the data to your MySQL database. Does that help?

can you please elaborate on how to use fetch or ajax library with an example? if possible please explain the control flow.

@Sri_vijay_kalki sure, you can see examples in the guides here: Specifying a Route's Model - Routing - Ember Guides

Basically, an Ember route has an optional model hook that can either return static data or handle any type of dynamic querying. If you query your backend in the model hook, a lot of the complexity of handling async is handled for you automatically by Ember. If you want more control over how the screen loads and feels beyond what Ember handles out of the box with the model hook and loading states, then it’s worth looking at ember-concurrency as an upgrade.

A good write up on some of the pros/cons on that can be found here: Readers' Questions - "Is it bad to load data in components?" - #8 by Parry

1 Like

Great discussion. I have the similar problem. I’m trying to solve it now, thanks.

1 Like

@Nelly can you share any more specifics of what stack you’re using and details about what you’re trying to do and what you’re stuck on? It might be worth starting a new thread.

@dknutsen As a “young” developer that I am, I faced a lot of problems. Mostly I see that this is just a misunderstanding of mine. So, following this instruction, I found a great guide on how to connect node application to mysql db. This is the moment when every contribution counts.

hi @James-Byrne ,To create login page and registration page, How to connect ember js with mysql db using Java servlet.

Hi @KARTHIK_S generally speak a database does not provide an HTTP layer so you cannot connect directly from a front-end browser app to a database. You need some sort of layer in-between, usually an API layer such as Node.js, Ruby on Rails, Python with Django, or in your case it sounds like you’d want a Java-based API server.

These layers can be extremely simple if you wish, just providing basic CRUD (Create Read Update Delete) endpoints for your Ember app to reach out to. Although in your case if you want login/registration you’ll also need some sort of authentication mechanism. Any API framework should have an authentication plugin available, or include it by default. But a database alone will not provide any of that functionality.

So in summary, you’ll want to:

  • choose an API framework or library (not sure what you’re already using in your Java setup)
  • use it to implement some basic CRUD endpoints for reading/writing data that your Ember app will be using
  • add authentication and registrattion related endpoints
  • add login and registration pages in your Ember app that reach out to these endpoints
1 Like

hi @dknutsen I want to create registration form and login form using Ember js (front-end) and java servlet as backend then server is mysql database.

How to pass username ,password to servlet program and check details with database and give access to user.Is there any api needed or we directly call servlet program from .hbs file?

You’ll need some sort of API with or within the servlet. The Ember code will run in a web browser and so you’ll have to have the ability, using browser APIs, to send a request to your server to register or to login. In general this is done with a RESTful endpoint of some sort (the specific depend on your API framework of choice). On the Ember side you would probably want to use ember-simple-auth or roll your own solution like it (I’d recommend ESA, it’s extremely popular and works very well). You could make a call to your server using fetch.

For example for registration you’d probably want to define a POST /register endpoint on your server that takes body parameters gathered from inputs in a form on the Ember side. The Ember app would collect the inputs from the form and when the form is submitted make a fetch call to your endpoint. Then it could handle the response appropriately.

Login works a similar way but would tie in with your authentication system to keep authentication token or other state and “block off” your authenticated routes.