config.js
module.exports = {
sqlite: {
client: 'sqlite3',
connection: {
filename: "./data.sqlite"
},
pool: {
min: 0,
max: 7
}
}
}
books.js
var books = {},
config = require('../../config/config.js'),
knex = require('knex')(config.sqlite);
books.addBooks = function(bookInfo, callback){
knex.insert(bookInfo,'id').into('t_books').then(function(id) {
callback(null, id);
}).catch(function(err) {
callback(err);
});
};
books.findBooksByCode = function(bookCode, callback) {
knex.raw('select * from t_books where bookCode = ?', [bookCode]).then(function(doc) {
callback(null, doc);
}).catch(function(err) {
callback(err);
});
}
books.findAllBooks = function(callback){
//查询指定字段方式一
knex.select('id','bookName','bookCode','bookPrice').from('t_books').orderBy('created_at','desc').then(function(doc) {
callback(null, doc);
}).catch(function(err) {
callback(err);
});
//查询指定字段方式二
/* knex.column('bookName', 'bookCode', 'bookPrice').select().from('t_books').then(function(doc) {
callback(null, doc);
}).catch(function(err) {
callback(err);
});*/
};
books.updateBookName = function(bookCode,bookName,callback){
knex('t_books').where('bookCode',bookCode).update({
bookName: bookName,
updated_at : new Date(),
bookTime:knex.fn.now()
})
.then(function (value) { callback(null,value); })
.catch(function (reason) { callback(reason,-2); });
};
books.deleteBook = function(bookCode,callback){
knex('t_books').where('bookCode', bookCode).del().then(function (value) { callback(null,value); })
.catch(function (reason) { callback(reason,201); });
};
module.exports=books;
package.json
{
"name": "nwjs-sqlite",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"bluebird": "^3.5.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"knex": "^0.15.2",
"morgan": "~1.9.0",
"pug": "2.0.0-beta11",
"sqlite3": "*"
}
}