简单的SQL语句构建工具。
A simple SQL query builder.
-
A base SELECT:
const {SelectBuilder} = require('sqlite');
const builder = new SelectBuilder()
.from('users')
.select('name,age,address')
.where('name', '=', 'Jon Snow')
.where('age', '=', 22);
builder.build();
-
SELECT with a simple JOIN:
const {SelectBuilder} = require('sqlie');
const builder = new SelectBuilder()
.from('users', 'u')
.select('*')
.where('name', '=', 'Jon Snow')
.where('age', '=', 22)
.join('hobbies', function(hobbyBuilder) {
hobbyBuilder
.setAlias('h')
.select('hobby')
.onColumn('h.id', 'u.id');
})
.join('colors', function(colorBuilder) {
colorBuilder
.setAlias('c')
.select('favorite')
.onColumn('c.user_id', 'u.id');
});
builder.build();
-
INSERT
const {InsertBuilder} = require('./dist');
const builder = new InsertBuilder()
.into('users')
.set('name', 'Super Girl')
.setSome({
age: 18,
gender: 'female'
});
builder.build();
-
UPDATE
const {UpdateBuilder} = require('./dist');
const builder = new UpdateBuilder()
.from('users')
.set('age', 22)
.setSome({})
.where('name', '=', 'Super Girl');
builder.build();
-
设置SQL执行函数
const {createConnection} = require('mysql')
const {setQueryHandler, execute} = require('sqlie')
const connection = createConnection({});
setQueryHandler(connection.query.bind(connection));
execute('SELECT * FROM tableName').then(function ({rows, fields}) {
console.log(rows);
console.log(fields);
})
-
模型
const {createModel} = require('sqlie');
const user = createModel('my_users', 'uid');
const msg = createModel('im_group_msgs', ['gid', 'uid']);