Reputation: 428
Are there recommended concepts how to access MySQL from an Angular 2 application?
I'm new to Angular and Typescript. I found and installed the node-mysql
package. I would like to SELECT some datasets for computational analysis and some graphical representation (perhaps with d3).
My own concept would be to implement a singleton service encapsulating the connection and SQL calls.
But perhaps I am thinking too old school here? Perhaps there should be a different approach?
On the practical side, I'm uncertain how to import the node-mysql
package in Angular2 / Typescript - by importing the package at the top?
If someone could point me to an example, I'd be glad to learn.
Upvotes: 7
Views: 18104
Reputation: 2672
Take a look at Angular-Meteor. If you were in any framework other than Meteor, you'd need to start implementing a series of REST endpoints to connect the server to the client.But Meteor makes writing distributed client code as simple as talking to a local database. More in this article: https://www.angular-meteor.com/tutorials/socially/angular2/3-way-data-binding
Upvotes: 1
Reputation: 6333
Communication directly from Angular to Mysql is bad practice (in most cases). Angular runs client side and exposing mysql to it allows for anybody to run arbitrary SQL statements.
The solution is to create an intermidate server. It could run nodejs, to which you can then import node-mysql. The nodejs server could expose a REST api which your angular 2 application consumes.
Upvotes: 16