think-knex

knex for thinkjs 3.x

Usage no npm install needed!

<script type="module">
  import thinkKnex from 'https://cdn.skypack.dev/think-knex';
</script>

README

thinkjs不支持firebird,knex功能强大,集成到一起(未集成到model,单独做一个插件)

安装支持firebird的knex npm install --save knex-firebird

配置, /src/config/adapter.js

exports.knex = {
  type: 'test',
  test: {
    client: 'firebird',
    connection: {
        host: '127.0.0.1',
        user: 'test',
        password: 'test',
        database: '/usr/db/test.fdb'
    }
  }
};

应用到app, /src/config/extend.js

...
const knex = require('think-knex');

module.exports = [
    ...,
    knex(think.app)
];

控制器示例

const Base = require('./base.js');

module.exports = class extends Base {
    async indexAction() {
        let rows = await this.knex.table('test')
            .limit(5)
            .select('id,name,code')
            //.then( rows => {
            //    rows.forEach( row => {
            //        console.log(row);
            //    });
            //})
            ;
        return this.body = rows[0];
        //return this.display();
    }
};

1.05支持同一个系统多个数据库

module.exports = class extend Base {
    __before() {
        this.m = this.knexInstance('在adapter中配置');
    }
    testAction(){
        await this.m('test').select();
    }
}