nodejs+sqlite3+knex

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": "*"
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值