Knex.js
- Website
- GitHub
- Knex Query Lab
- (Translates Knex to SQL)
- ...
Select
knex.select("title", "rows").from("tableName").then(function(rows) {
screen.write(rows);
})
.catch(function(err){
console.log(err) // handle error
})
.finally(function(){
knex.destroy(); // release connection
})
or
knex.table("tableName").column(["title", "rating"]).then(function(rows) {
screen.write(rows);
})
.catch(function(err){
console.log(err) // handle error
})
.finally(function(){
knex.destroy(); // release connection
})
or
knex.("tableName").column(["title", "rating"]).then(function(rows) {
screen.write(rows);
})
.catch(function(err){
console.log(err) // handle error
})
.finally(function(){
knex.destroy(); // release connection
})
Raw
var user_id = 1;
var query = knex.raw("SELECT * FROM Posts WHERE userid = ?", [author_id])
run(query, "pretty");
var run = function(knexQuery, mode){
.then(function(rows) {
screen.write(rows);
})
.catch(function(err){
console.log(err) // handle error
})
.finally(function(){
knex.destroy(); // release connection
})
}
Methods
.distinct("")
- will return only unique values
.join()
- well...
.on()
/.orOn()
- Joins
.orderBy("")
- specify a column by which the data should be ordered
.orderByRaw("")
- same
.select("")
- query
knex.raw("")
- Write sql commands directly