ngx-mysql v1.1.1
NgxMysql
Project ini adalah modul Angular 2 yang berfungsi untuk memudahkan integrasi antara angular 2, electron dan database mysql.
Pendahuluan
Modul ini mengintegrasikan library mysql dengan Angular 2 + Electron. Sebelumnya saya asumsikan anda sudah berhasil mengintegrasikan Angular dengan Electron. Jika belum anda dapat mengikuti tutorial berikut Membuat aplikasi desktop dengan Angular dan Electron. Berikut adalah contoh bagaimana menggunakan modul ini :
Langkah 1. Deklarasikan variable remote
pada src/index.html
<html>
<head>
<title>Angular 2 App</title>
</head>
<body>
<app-root></app-root>
<script>
var remote = require('electron').remote();
</script>
</body>
</html>
Langkah 2. Buka app.module.ts dan deklarasikan remote sebagai variable global Angular
import { NgxElectronMysqlModule } from 'ngx-electron-mysql';
//another imports
import {...} from ...
declare var remote : any;
@NgModule({
declarations : [...]
imports : [
NgxElectronMysqlModule.forRoot(remote)
],
providers : [...]
})
export class AppModule {}
Langkah 3. Buka app.component.ts
import { Component } from '@angular/core';
import { Mysql } from '../../lib';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
private mysql : Mysql
){
this.mysql.open({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'test'
}).then(res => {
this.mysql.query('SELECT*FROM users').then(res => {
console.log(res);
})
})
}
}
result
result(nama_tabel) digunakan untuk mendapatkan baris lebih dari satu
this.mysql.result('users').then(res => {
console.log(res);
});
row
row(nama_tabel) digunakan untuk mendapatkan satu baris berdasarkan filter yang diterapkan
this.mysql.row('users').then(res => {
console.log(res);
})
where
where(filter) digunakan untuk menyaring hasil sebelum memanggil row atau result
this.mysql.where({ id_user : 1 }).then(res => {
this.mysql.row('users').then(res => {
console.log(res);
})
});
select
select(column_string) digunakan untuk memilih field mana saja untuk ditampilkan
this.mysql.where({ id_user : 1 }).then(res => {
this.mysql.select('id_user, nm_user').then(res => {
this.mysql.row('users').then(res => {
console.log(res);
})
})
});
inner_join
inner_join(nama_tabel, relasi) digunakan untuk mendapatkan hasil join dari 2 tabel
this.mysql.inner_join('activities', 'activities.id_user = users.id_user').then(res => {
this.mysql.result('users').then(res => {
console.log(res);
})
})
insert
insert(nama_tabel, data) digunakan untuk menambahkan data pada tabel
this.mysql.insert('users',{
nm_pengguna : this.nm_pengguna,
username : this.username
}).then(res => {
console.log(res);
})
update
update(nama_tabel, data) digunakan untuk mengupdate data pada tabel
this.mysql.where({id_user : 1}).then(res => {
this.mysql.update('users',{
nm_pengguna : this.nm_pengguna,
username : this.username
}).then(res => {
console.log(res);
})
})
delete
delete(nama_tabel) digunakan untuk menghapus data pada tabel
this.mysql.where({id_user : 1}).then(res => {
this.mysql.delete('users').then(res => {
console.log(res);
})
})
Berkontribusi
Anda dapat mengkontribusi dengan mengkloning project ini dan menjalankan perintah
npm install
selanjutnya ketikkan perintah berikut untuk menjalankan aplikasi testing
terminal> ng build --watch
terminal> electron main