Wednesday 3 May 2017

What is the proper way to implement querys into an application

I am pretty new to using a SQL database in applications and I am not sure what the proper way to code querys is.

I currently have the following code for a User table. But I am going to add much more models for example: comments, groups, likes, payments etc. etc.

Is this the proper way to make those models or should I make a "universal" query function that every model calls?

exports.create = function(values, done) {
  console.log("create");
  db.get().query('INSERT INTO Users (Email, Role) VALUES(?, ?)', values, function(err, result) {
    if (err) return done(err)
    done(null, result)
  })
}

exports.getAll = function(done) {
  db.get().query('SELECT * FROM Users', function (err, rows) {
    if (err) return done(err)
    done(null, rows)
  })
}

exports.getUser = function(userId, done) {
  db.get().query('SELECT * FROM Users WHERE userID = ?', userId, function (err, rows) {
    if (err) return done(err)
    done(null, rows)
  })
}



via Soundwave

No comments:

Post a Comment