2

I need a with RxJS for MySQL in NodeJS. Could someone give me an example for one select?

On front-end I will use Angular2.

ggradnig
  • 13,119
  • 2
  • 37
  • 61
Miha Zoubek
  • 65
  • 10
  • You probably find something by googling! Your question doesn't show you looked before asking. I advise reading http://stackoverflow.com/help/how-to-ask – J. Chomel Apr 15 '16 at 06:32
  • I tried but did not find anything useful or some example that would show me how to do it! If I would find i would not post here a queston... – Miha Zoubek Apr 15 '16 at 07:03
  • Why do you want to use Rxjs? – J. Chomel Apr 15 '16 at 07:11
  • 1
    @J.Chomel as I would like to learn it :) Nothing more :) Angular2 somehow push it and that is why I would like to try it on Nodejs side. – Miha Zoubek Apr 15 '16 at 07:20
  • So if you want to use it, there must be a tutorial that you try to follow. Give us more or this will look to broad a question. – J. Chomel Apr 15 '16 at 07:28
  • Google is your friend. Thinking by yourself too. This is not the place where people help you learn things. You learn those things and when you have specific questions, you come and ask, showing what you tried, code sample, and the result of your previous research. There are a ton of tutorial with RxJs, a ton on mySQL, a ton on Angular2 with Rxjs, so you have more than enough resources to write some code and experiment, and see how it goes. If your goal is learning, that's the best way. – user3743222 Apr 15 '16 at 11:22
  • @MihaZoubek I to am trying to find tutorials on this exact stack minus angular. Please share any tutorials you found since asking! – zafrani Apr 28 '16 at 05:20
  • 1
    Unbelievable that such a question gets 4 upvotes, with such a wealth of info available on SO and on the Internet. I mean, its like 10 of encyclopedias full of it. And yes, 3 yrs after asking im still shocked :p – jcuypers Apr 14 '19 at 15:42

1 Answers1

0

In my case, I'm using the MySQL npm package in a desktop application made with Electron and Angular.

However the same should work even on a plain NodeJS application by installing and importing rxjs.


I've first installed mysql and @types/mysql package with:

npm install --saved-dev mysql @types/mysql

Then I've created a MySQL service:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Connection, ConnectionConfig, FieldInfo, MysqlError } from 'mysql';

const mysql = require('mysql');

@Injectable({
  providedIn: 'root'
})
export class MysqlService {

  private connection: Connection;

  constructor() { }

  createConnection(config: ConnectionConfig) {
    this.connection = mysql.createConnection(config);
  }

  query(queryString: string, values?: string[]): Observable<{results?: Object[], fields?: FieldInfo[]}> {
    return new Observable(observer => {
      this.connection.query(queryString, values, (err: MysqlError, results?: Object[], fields?: FieldInfo[]) => {
        if (err) {
          observer.error(err);
        } else {
          observer.next({ results, fields });
        }
        observer.complete();
      });
    });
  }
}

Now I can use my MysqlService in any other service or component to connect to the mysql database and execute queries.

For example:

import { Component, OnInit } from '@angular/core';

import { MysqlService } from '../../services/mysql.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  constructor(
    private mysqlService: MysqlService,
  ) { }

  ngOnInit() {
    this.mysqlService.createConnection({
      host: '127.0.0.1',
      user: 'root',
      password: 'my_password',
      database: 'my_database',
    });

    this.mysqlService.query(`SELECT * FROM my_table WHERE name = 'Francesco'`).subscribe((data) => {
      console.log(data);
    })
  }
}
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252