Skip to content

数据新增

Update Param DTS

查询条件配置类型描述

查询类型定义

ts
interface UpdateParam {
  /**
   * 表信息
   */
  t: TABLE_TYPE
  /**
   * 更新数据
   */
  params: Record<string, any>
  /**
   * 更新条件
   */
  condition: CONDITION_TYPE
}

Example

注意事项

更新数据前确保数据表结构已经存在。否者执行会抛出 '[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: '主键'
    },
    {
      name: 'name',
      dataType: 'VARCHAR',
      length: 45,
      notNull: true,
      comments: '名称'
    },
    {
      name: 'age',
      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
  .update({
    t: t_user,
    params: {
      name: '张三_UPDATE',
      age: 18,
      update_at: new Date()
    },
    condition: { id: 1 }
  })
  .then(() => {
    // 更新数据成功
  })
  .catch((err) => {
    // 更新数据失败
    console.error('[ERROR]::', err)
  })