Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var DB = (function(){
- var mysql = require('db-mysql');
- var confDB = {
- hostname: 'localhost',
- user: 'root',
- password: 'Ik789Red',
- database: 'smart_tv_feed'
- }
- function all(){
- new mysql.Database(confDB).connect(function(error) {
- if (error) {
- return console.log('CONNECTION error: ' + error);
- }
- this.query().
- select('*').
- from('users').
- execute(function(error, rows, cols) {
- if (error) {
- console.log('ERROR: ' + error);
- return;
- }
- console.log(rows);
- });
- });
- }
- function get(data){
- new mysql.Database(confDB).connect(function(error) {
- if (error) {
- return console.log('CONNECTION error: ' + error);
- }
- this.query().
- select('*').
- from('users').
- where('id = ?', [ data.id ]).
- execute(function(error, rows, cols) {
- if (error) {
- console.log('ERROR: ' + error);
- return;
- }
- console.log(rows);
- });
- });
- }
- function insert(data){
- new mysql.Database(confDB).connect(function(error) {
- if (error) {
- return console.log('CONNECTION error: ' + error);
- }
- this.query().
- insert('tbl_stories',
- ['time', 'id_category', 'visibility', 'sort','subject','lead','text'],
- [data.date, data.id_category, data.visibility, data.id, data.title, data.lead, data.text]
- ).
- execute(function(error, result) {
- if (error) {
- console.log('ERROR: ' + error);
- return;
- }
- console.log('GENERATED id: ' + result.id);
- });
- });
- }
- function update(data){
- new mysql.Database(confDB).connect(function(error) {
- if (error) {
- return console.log('CONNECTION error: ' + error);
- }
- this.query().
- update('users').
- set({ 'name': data.name }).
- where('id = ?', [ data.id ])
- execute(function(error, result) {
- if (error) {
- console.log('ERROR: ' + error);
- return;
- }
- console.log('RESULT: ', result);
- });
- });
- }
- function remove(data){
- new mysql.Database(confDB).connect(function(error) {
- if (error) {
- return console.log('CONNECTION error: ' + error);
- }
- this.query().
- delete().
- from('users')
- where('id = ?', [ data.id ])
- execute(function(error, result) {
- if (error) {
- console.log('ERROR: ' + error);
- return;
- }
- console.log('RESULT: ', result);
- });
- });
- }
- function test(){
- console.log('test');
- }
- return {
- insert: insert,
- update: update,
- remove: remove,
- get: get,
- all: all
- };
- }());
Add Comment
Please, Sign In to add comment