数据批量更新
BatachUpdate Param DTS
查询条件配置类型描述
ts
/**
* 批量更新参数|唯一索引键更新
*/
interface DuplicateUpdateParam {
/**
* 表信息
*/
t: TABLE_TYPE
/**
* 配置参数
*/
data: Record<string, any>[]
/**
* 唯一索引字段名
*/
unique?: string
/**
* 不更新字段
*/
excludeField?: string | string[]
/**
* 更新条件
*/
condition?: CONDITION_TYPE
}
/**
* 批量更新参数|条件更新
*/
interface ConditionBatchUpdateParam {
/**
* 表信息
*/
t: TABLE_TYPE
/**
* 条件字段
*/
conditionField: string
/**
* 数据
*/
data: Record<string, any>[]
}
interface BatchUpdateParam<
T extends DuplicateUpdateParam | ConditionBatchUpdateParam
> {
type: 'DUPLICATE' | 'CASE_WHEN' | 'TEMPORARY'
params: T
}Example
唯一索引 DUPLICATE 更新
注意事项
唯一索引 DUPLICATE 批量更新数据,唯一索引不存在会新增数据,如果不确认业务需求请谨慎使用,请确保表字段存在唯一索引约束否则会导致重复数据插入。更新数据前确保数据表结构已经存在。否者执行会抛出 '[table name]' doesn't exist 异常。
ts
import emysql, { DefineTable } from '@aicblock/emysql'
// 数据库实例化
const mysql = new emysql({
password: '[db登录密码]',
user: '[db登录用户名]',
database: '访问数据库名称'
})
await mysql.init()
// 定义表结构
const t_user = DefineTable({
tableName: 't_user',
columns: [
{
name: 'id',
dataType: 'INT',
primaryKey: true,
autoIncrement: true,
comments: '主键id'
},
{
name: 'name',
dataType: 'VARCHAR',
length: 45,
notNull: true,
comments: '名称'
},
{
name: 'age',
dataType: 'INT',
notNull: true,
comments: '年龄'
},
{
name: 'sex',
dataType: 'INT',
notNull: true,
comments: '性别'
},
{
name: 'create_at',
dataType: 'DATETIME',
notNull: true,
comments: '创建时间'
},
{
name: 'update_at',
dataType: 'DATETIME',
notNull: true,
comments: '更新时间'
}
]
})
await mysql.table.create([t_user])
mysql.change
.batchUpdate({
type: 'DUPLICATE',
params: {
t: t_user,
data: [
{
name: '张三',
age: 15,
sex: 1,
create_at: new Date(),
unionid: 'a'
},
{
name: '李四',
age: 18,
sex: 0,
create_at: new Date(),
unionid: 'b'
},
{
name: '王五',
age: 14,
sex: 1,
create_at: new Date(),
unionid: 'c'
}
],
unique: 'unionid',
excludeField: ['id', 'create_at']
}
})
.then((result) => {
// 插入数据成功 当 unionid 存在更新相关数据, 不存在则新增一条新增数据
})
.catch((err) => {
// 插入数据失败
console.error('[ERROR]::', err)
})CASE WHEN 批量更新
ts
import emysql, { DefineTable } from '@aicblock/emysql'
// 数据库实例化
const mysql = new emysql({
password: '[db登录密码]',
user: '[db登录用户名]',
database: '访问数据库名称'
})
await mysql.init()
// 定义表结构
const t_user = DefineTable({
tableName: 't_user',
columns: [
{
name: 'id',
dataType: 'INT',
primaryKey: true,
autoIncrement: true,
comments: '主键id'
},
{
name: 'name',
dataType: 'VARCHAR',
length: 45,
notNull: true,
comments: '名称'
},
{
name: 'age',
dataType: 'INT',
notNull: true,
comments: '年龄'
},
{
name: 'sex',
dataType: 'INT',
notNull: true,
comments: '性别'
},
{
name: 'create_at',
dataType: 'DATETIME',
notNull: true,
comments: '创建时间'
},
{
name: 'update_at',
dataType: 'DATETIME',
notNull: true,
comments: '更新时间'
}
]
})
await mysql.table.create([t_user])
mysql.change
.batchUpdate({
type: 'CASE_WHEN',
params: {
t: t_user,
data: [
{
name: '张三',
age: 15,
sex: 1,
create_at: new Date(),
unionid: 'a'
},
{
name: '李四',
age: 18,
sex: 0,
create_at: new Date(),
unionid: 'b'
},
{
name: '王五',
age: 14,
sex: 1,
create_at: new Date(),
unionid: 'c'
}
],
conditionField: 'unionid'
}
})
.then((result) => {
// 符合条件批量更新成功
})
.catch((err) => {
// 插入数据失败
console.error('[ERROR]::', err)
})TEMPORARY 临时表批量更新
ts
import emysql from '@aicblock/emysql'
// 数据库实例化
const mysql = new emysql({
password: '[db登录密码]',
user: '[db登录用户名]',
database: '访问数据库名称'
})
await mysql.init()
// 定义表结构
const t_user = DefineTable({
tableName: 't_user',
columns: [
{
name: 'id',
dataType: 'INT',
primaryKey: true,
autoIncrement: true,
comments: '主键id'
},
{
name: 'name',
dataType: 'VARCHAR',
length: 45,
notNull: true,
comments: '名称'
},
{
name: 'age',
dataType: 'INT',
notNull: true,
comments: '年龄'
},
{
name: 'sex',
dataType: 'INT',
notNull: true,
comments: '性别'
},
{
name: 'create_at',
dataType: 'DATETIME',
notNull: true,
comments: '创建时间'
},
{
name: 'update_at',
dataType: 'DATETIME',
notNull: true,
comments: '更新时间'
}
]
})
await mysql.table.create([t_user])
mysql.change
.batchUpdate({
type: 'TEMPORARY',
params: {
t: t_user,
data: [
{
name: '张三',
age: 15,
sex: 1,
create_at: new Date(),
unionid: 'a'
},
{
name: '李四',
age: 18,
sex: 0,
create_at: new Date(),
unionid: 'b'
},
{
name: '王五',
age: 14,
sex: 1,
create_at: new Date(),
unionid: 'c'
}
],
conditionField: 'unionid'
}
})
.then((result) => {
// 符合条件批量更新成功
})
.catch((err) => {
// 插入数据失败
console.error('[ERROR]::', err)
})