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.
If you use Angular in front-end, you can use any backend with it to connect to MySQL database, for example: Jersey web service written in Java. Php with mysqli.
Create, Read, Update and Delete (CRUD) operations are the four basic operations that can be performed on a database Collection or Table. In terms of MySQL this means: Create a new entry (insertion or addition) Read entries (queries)
AngularJS SQL. AngularJS is perfect for displaying data from a Database. Just make sure the data is in JSON format.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With