Select is the only command available in the whole SQL to retrieve data. KNEX provides an implementation of the "Select" Query. The following code illustrates:
knex.select("*").from("depts")
.then(function (dept) {
dept.forEach(function(value){
console.log(value.deptno);
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});
}
The above code has following important observations:
a. Its Selecting data from a table named "Depts". We are selecting all the columns and rows.
b. Once select query has fetched rows, it will run a promise of .then
c. "dept" in the .then function is a collection of all records fetched from the table.
d. We can access individual columns like "value.deptno", "value.dname" within forEach loop.
Happy Coding !!!
knex.select("*").from("depts")
.then(function (dept) {
dept.forEach(function(value){
console.log(value.deptno);
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});
}
The above code has following important observations:
a. Its Selecting data from a table named "Depts". We are selecting all the columns and rows.
b. Once select query has fetched rows, it will run a promise of .then
c. "dept" in the .then function is a collection of all records fetched from the table.
d. We can access individual columns like "value.deptno", "value.dname" within forEach loop.
Happy Coding !!!
Comments