Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 and MySQL concepts

Tags:

mysql

angular

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.

like image 389
user1840267 Avatar asked Dec 19 '15 08:12

user1840267


People also ask

Can Angular be used with MySQL?

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.

What are the concepts in MySQL?

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)

Which database is used with Angular?

AngularJS SQL. AngularJS is perfect for displaying data from a Database. Just make sure the data is in JSON format.


2 Answers

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.

like image 147
CodeTower Avatar answered Oct 19 '22 15:10

CodeTower


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

like image 1
jjj Avatar answered Oct 19 '22 13:10

jjj