Mysql – Angular 2 and MySQL concepts

angularMySQL

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.

Best Answer

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.