书架查询更新 table
Bookshelf query to update table
我尝试搜索但没有提示与此书架查询等同
update <Table Name> set <Column> x = y where z = a;
谢谢。
Bookshelf save()
documentation 有一个例子。
只需在模型上使用 where()
来指定哪些行和 save()
属性列表以及 { patch: true }
选项。
如果我们有一个 table users(id,name,email) 它将是这样的:
var knex = require('knex')({
client: 'sqlite3',
connection: {filename: 'data.sqlite3'},
debug: true}); // <- so you can see the generated query
var bookshelf = require('bookshelf')(knex);
var User = bookshelf.Model.extend({
tableName: 'users',
});
(function() {
User
.where({name:'amy'})
.save({email: 'amysnewemail@example.com'},{patch:true})
.then(function(x) {
console.log(x.toJSON());
});
})();
上面的代码产生输出:
{ method: 'update',
options: {},
bindings: [ 'amysnewemail@example.com', 'amy' ],
sql: 'update "users" set "email" = ? where "name" = ?' }
{ email: 'amysnewemail@example.com' }
我尝试搜索但没有提示与此书架查询等同
update <Table Name> set <Column> x = y where z = a;
谢谢。
Bookshelf save()
documentation 有一个例子。
只需在模型上使用 where()
来指定哪些行和 save()
属性列表以及 { patch: true }
选项。
如果我们有一个 table users(id,name,email) 它将是这样的:
var knex = require('knex')({
client: 'sqlite3',
connection: {filename: 'data.sqlite3'},
debug: true}); // <- so you can see the generated query
var bookshelf = require('bookshelf')(knex);
var User = bookshelf.Model.extend({
tableName: 'users',
});
(function() {
User
.where({name:'amy'})
.save({email: 'amysnewemail@example.com'},{patch:true})
.then(function(x) {
console.log(x.toJSON());
});
})();
上面的代码产生输出:
{ method: 'update',
options: {},
bindings: [ 'amysnewemail@example.com', 'amy' ],
sql: 'update "users" set "email" = ? where "name" = ?' }
{ email: 'amysnewemail@example.com' }