如何使用 RxJS、MySQL 和 NodeJS [英] How to use RxJS, MySQL and NodeJS
问题描述
我需要在 NodeJS 中使用 RxJS for MySQL.有人可以给我一个选择的例子吗?
在前端,我将使用 Angular2.
就我而言,我使用的是 MySQL 桌面应用程序中的 npm 包,使用 Electron 和 角度.
但是,通过安装和导入 rxjs,即使在普通的 NodeJS 应用程序上也可以使用相同的方法.><小时>
我首先安装了 mysql
和 @types/mysql
包:
npm install --saved-dev mysql @types/mysql
然后我创建了一个 MySQL 服务:
import { Injectable } from '@angular/core';从 'rxjs' 导入 { Observable };import { Connection, ConnectionConfig, FieldInfo, MysqlError } from 'mysql';const mysql = require('mysql');@Injectable({提供在:'根'})导出类 MysqlService {私人连接:连接;构造函数(){}创建连接(配置:ConnectionConfig){this.connection = mysql.createConnection(config);}query(queryString: string, values?: string[]): Observable<{results?: Object[], fields?: FieldInfo[]}>{返回新的 Observable(观察者 => {this.connection.query(queryString, values, (err: MysqlError, results?: Object[], fields?: FieldInfo[]) => {如果(错误){观察者.错误(错误);} 别的 {观察者.下一个({结果,字段});}观察者.完成();});});}}
现在我可以在任何其他服务或组件中使用我的 MysqlService
连接到 mysql 数据库并执行查询.
例如:
import { Component, OnInit } from '@angular/core';从'../../services/mysql.service'导入{MysqlService};@成分({选择器:'app-home',templateUrl: './home.component.html',styleUrls: ['./home.component.scss']})导出类 HomeComponent 实现 OnInit {构造函数(私有 mysqlService:MysqlService,) { }ngOnInit() {this.mysqlService.createConnection({主机:'127.0.0.1',用户:'root',密码:'my_password',数据库:'my_database',});this.mysqlService.query(`SELECT * FROM my_table WHERE name = 'Francesco'`).subscribe((data) => {控制台日志(数据);})}}
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.
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);
})
}
}
这篇关于如何使用 RxJS、MySQL 和 NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!