关于typeorm如何精确更新对象的点
展现出js的语言特性远远超过微软EF的框架的DEMO
1、使用 Query Builder 更新
你可以使用QueryBuilder
创建UPDATE
查询。 例如:
import { getConnection } from "typeorm";
await getConnection()
.createQueryBuilder()
.update(User)
.set({ firstName: "Timber", lastName: "Saw" })
.where("id = :id", { id: 1 })
.execute();
2、lodash登场
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.omit(object, ['a', 'c']);
// => { 'b': '2' }
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.omitBy(object, _.isNumber);
// => { 'b': '2' }
const _ = require('lodash'); const myObject = { name: 'John', age: null, address: { city: 'New York', zipCode: undefined, }, hobbies: [], }; const result = _.omitBy(myObject, (value) => value === null || value === undefined || (_.isObject(value) && _.isEmpty(value))); console.log(result); // Output: { name: 'John', address: { city: 'New York' } }
漫思