Skip to content

Instantly share code, notes, and snippets.

@devarajchidambaram
Created February 28, 2019 06:17
Show Gist options
  • Save devarajchidambaram/fb9db03f99e25e2b561961e1243a77a5 to your computer and use it in GitHub Desktop.
Save devarajchidambaram/fb9db03f99e25e2b561961e1243a77a5 to your computer and use it in GitHub Desktop.
knex tutorial
var Knex = require('knex')
var knex = Knex({
client: 'mysql',
connection: {
host: '192.168.8.221',
user: 'user_name',
password: 'some_pass',
database: 'vehicle'
}
});
knex.raw("select * from customer").then(
(version) => console.log((version[0][0]))
).catch((err) => {
console.log(err);
throw err
})
.finally(() => {
knex.destroy();
});
knex.schema.createTable('cars', (table) => {
console.log("table", table)
table.increments('id')
table.string('name')
table.integer('price')
}).then(() => console.log("table created"))
.catch((err) => {
console.log(err);
throw err
})
.finally(() => {
knex.destroy();
});
const cars = [{
name: 'Audi',
price: 52642
},
{
name: 'Mercedes',
price: 57127
},
{
name: 'Skoda',
price: 9000
},
{
name: 'Volvo',
price: 29000
},
{
name: 'Bentley',
price: 350000
},
{
name: 'Citroen',
price: 21000
},
{
name: 'Hummer',
price: 41400
},
{
name: 'Volkswagen',
price: 21600
},
]
knex('cars').insert(cars).then(() => console.log("data inserted"))
.catch((err) => { console.log(err); throw err })
.finally(() => {
knex.destroy();
});
knex.from('cars').select('*').then(function(rows){
console.log('Rows ==',rows)
}).catch(function(e){
console.log("err", e)
})
knex.from('cars').select("name", "price").where('price', '>', '9000').orderBy('price','desc').then(function (rows) {
// console.log('Rows ==', rows)
for (row of rows) {
console.log("=====", `${row['name']} ${row['price']}`)
}
}).catch(function (e) {
console.log("err", e)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment