首页 > 解决方案 > 如何在 POST 正文的基础上创建动态插入语句

问题描述

body 对象中的属性数量是不同的,它基于 value user_type

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

insert into users (username, user_type, name, email, password, resume, resume_date_time, salary_expectation) values (?, ?, ?, ?, ?, ?, ?, ?);

如果user_type雇主,那么主体对象具有这些属性 --> 用户名、用户类型、姓名、电子邮件、密码、公司名称、公司简介。这种情况下的插入语句是这样的 -->

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

那么,如何在可变编号的基础上创建动态插入查询。POST 请求正文中的属性?

标签: mysqlnode.jsmysqljs

解决方案


这是由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模块提供的动态功能。

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

在我链接的文档部分中,您可以看到一个示例,其中 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 注入。根据您的数据库配置、体系结构、字段类型和您的个人安全策略的具体情况,在何时何地使用单一与双重的必要性会有所不同,并且需要使用您自己的代码库进行测试。


推荐阅读