mysql - 如何在POST body的基础上创建动态插入语句

标签 mysql node.js mysqljs

body 对象中的属性数量根据值user_type 的不同而不同。

如果 user_typejob_seeker 那么 body 对象具有这些属性 --> username, user_type, name, email, password, resume, resume_date_time, salary_expectation。我创建了这样的硬编码插入语句 -->

插入用户(用户名、用户类型、姓名、电子邮件、密码、简历、resume_date_time、salary_expectation)值(?、?、?、?、?、?、?、?);

如果 user_typeemployer 那么正文对象具有这些属性 --> username, user_type, name, email, password, company_name, company_profile。这种情况下的插入语句是这样的 -->

insert into users (username, user_type, name, email, password, company_name, company_profile) values (?, ?, ?, ?, ?, ?, ?);

那么,如何根据不同的编号创建动态插入查询。 POST 请求正文中的属性?

最佳答案

这是由 mysqljs 模块为您处理的,因此您无需对其进行过度思考或过度设计。将您的插入负载作为一个对象提供,其中的键与您的表字段名称共享相同的命名。无论您提供什么,都将在插入中使用。如果您不提供字段,它将遵循数据库模式中的表默认值(id 将自动填充并递增,如果您以这种方式设置它,created_attimestamp 或其他类似的日期字段,如果某个字段设置为在未提供值时使用默认值,则将使用该默认值。

有关详细信息,请参阅此处的文档:https://github.com/mysqljs/mysql#escaping-query-values

下面是使用您提供的两个场景的示例。

var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'me',
  password: 'secret',
  database: 'my_db'
});

connection.connect();

let job_seeker = {
  username: 'j.smith',
  user_type: 'job_seeker',
  name: 'john smith',
  email: 'j.smith@example.com',
  password: 'keyboard_cat',
  resume: true,
  resume_date_time: '2109-01-01',
  salary_expectation: 100000
};

let employer = {
  username: 'b.burke',
  user_type: 'employer',
  name: 'betty burke',
  email: 'b.burke@example.com',
  password: 'admin_cat',
  company_name: 'Acme Inc.',
  company_profile: 'http://acme.example.com/about_us/'
};


connection.query('INSERT INTO users SET ?', job_seeker, function(error, results, fields) {
  if (error) throw error;
  console.log(results.insertId);
});

connection.query('INSERT INTO users SET ?', employer, function(error, results, fields) {
  if (error) throw error;
  console.log(results.insertId);
});

// if a scenario exists where you might encounter unique key conflicts, consider using a query model that will update on duplicates:

connection.query('INSERT INTO users SET ? ON DUPLICATE KEY UPDATE', employer, function(error, results, fields) {
  if (error) throw error;
  console.log(results.insertId);
});

// this would update every field provided in the employer object. If you didn't want to update every fields, you'd have to list out only the fields you'd like to update:

connection.query('INSERT INTO users SET ? ON DUPLICATE KEY UPDATE name = VALUES(name)', employer, function(error, results, fields) {
  if (error) throw error;
  console.log(results.insertId);
});

// in the above example, only the name field will be updated if a duplicate is found, even though the full employer object is provided.

如果您在将 POST 主体传递到代码中的这一步之前正确构建它,您可以节省一些时间,让它直接流向查询函数,而无需额外的工作或对象构造或操纵。

根据评论编辑

如果您的计划是包含一些查询参数,您可能希望使用 UPDATE 而不是 INSERT。无论哪种方式,我们都可以以此作为示例,说明如何说明 mysqljs 模块提供的动态功能。

每次使用 ? 时,它都是一个占位符,映射到函数中后面的数组索引。

在我链接的文档部分,您可以看到一个示例,其中 4 ? 映射到后面数组中提供的 4 个值。您只需确保数组中值的顺序与其问号对应的正确顺序一致。

如果我们想用一个对象在一个查询中解决所有这些问题,我们可以为我们的 POST 对象添加一些复杂性,以便它包含我们查询所需的所有信息。使用您的示例,考虑以下内容:

// you could define the following object as we have for this example, or it could be coming right from req.body

let update_payload = {
  table_name = 'users',
  query_field = 'email',
  query_value = 'j.smith.old@example.com',
  job_seeker = {
    username: 'j.smith',
    user_type: 'job_seeker',
    name: 'john smith',
    email: 'j.smith.new@example.com',
    password: 'keyboard_cat_new',
    resume: true,
    resume_date_time: '2109-01-01',
    salary_expectation: 100000
  }
};


connection.query('UPDATE ?? SET ? WHERE ?? = ? ', [update_payload.table_name, update_payload.job_seeker, update_payload.query_field, update_payload.query_value], function(error, results, fields) {
  if (error) throw error;
  console.log(results);
});

// expected console output…

OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '(Rows matched: 1  Changed: 1  Warnings: 0',
  protocol41: true,
  changedRows: 1
}

注意:您会看到有时我使用 ?? 而其他时候我使用 ?。这是模块的一个特性,它提供转义以防止 SQL 注入(inject)。根据您的数据库配置、体系结构、字段类型和您的个人安全策略的具体情况,在何时何地使用单数与双数的必要性会有所不同,并且需要使用您自己的代码库进行测试。

关于mysql - 如何在POST body的基础上创建动态插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045121/

相关文章:

node.js - 使用 MySQL 对 NodeJS Express API 进行 Web 打包会在模式 '-p' 而非 '-d' 中引发连接错误

MySQL 计数、求和、然后除法

javascript - 如何检查 Puppeteer 中是否存在选择器?

node.js - Electron 错误错误: Cannot find module 'mysql' Require stack:

javascript - io.emit 不发送 promise 返回

ajax - 通过ajax将嵌套对象发布到nodejs服务器返回未定义

javascript - 访问部分 javascript 数组 (mysqljs)

mysql - 我需要使用 group by 和 rollup 进行查询方面的帮助

mysql - SQL MYSQL 多表查询连接

mysql - 如何将以空格分隔的文本文件导入 MySQL?