如何在POST主体的基础上创建动态插入语句

Ger*_*oke 2 mysql node.js mysqljs

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

如果user_typejob_seeker,则正文对象具有以下属性 --> 用户名、用户类型、姓名、电子邮件、密码、简历、简历日期时间、薪水期望。我已经创建了这样的硬编码插入语句 -->

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 请求正文中的属性?

dus*_*nes 5

这是由模块为您处理的mysqljs,因此您无需过度思考或过度设计。将插入有效负载作为对象提供,其中键与表字段名称共享相同的命名。您提供的任何内容都将在插入中使用。如果您不提供字段,它将遵循数据库模式中的表默认值(id如果您已这样设置,则会自动填充并递增,如下所示created_attimestamp或者其他类似的日期字段,如果字段设置为如果未提供值,则使用默认值,将使用该默认值)。

\n\n

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

\n\n

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

\n\n
var mysql = require(\'mysql\');\nvar connection = mysql.createConnection({\n  host: \'localhost\',\n  user: \'me\',\n  password: \'secret\',\n  database: \'my_db\'\n});\n\nconnection.connect();\n\nlet job_seeker = {\n  username: \'j.smith\',\n  user_type: \'job_seeker\',\n  name: \'john smith\',\n  email: \'j.smith@example.com\',\n  password: \'keyboard_cat\',\n  resume: true,\n  resume_date_time: \'2109-01-01\',\n  salary_expectation: 100000\n};\n\nlet employer = {\n  username: \'b.burke\',\n  user_type: \'employer\',\n  name: \'betty burke\',\n  email: \'b.burke@example.com\',\n  password: \'admin_cat\',\n  company_name: \'Acme Inc.\',\n  company_profile: \'http://acme.example.com/about_us/\'\n};\n\n\nconnection.query(\'INSERT INTO users SET ?\', job_seeker, function(error, results, fields) {\n  if (error) throw error;\n  console.log(results.insertId);\n});\n\nconnection.query(\'INSERT INTO users SET ?\', employer, function(error, results, fields) {\n  if (error) throw error;\n  console.log(results.insertId);\n});\n\n// if a scenario exists where you might encounter unique key conflicts, consider using a query model that will update on duplicates:\n\nconnection.query(\'INSERT INTO users SET ? ON DUPLICATE KEY UPDATE\', employer, function(error, results, fields) {\n  if (error) throw error;\n  console.log(results.insertId);\n});\n\n// 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:\n\nconnection.query(\'INSERT INTO users SET ? ON DUPLICATE KEY UPDATE name = VALUES(name)\', employer, function(error, results, fields) {\n  if (error) throw error;\n  console.log(results.insertId);\n});\n\n// in the above example, only the name field will be updated if a duplicate is found, even though the full employer object is provided.\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

根据评论进行编辑

\n\n

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

\n\n

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

\n\n

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

\n\n

如果我们想用一个对象在一个查询中解决所有问题,我们只需向对象添加一些复杂性,POST使其包含查询所需的所有信息。使用您的示例,请考虑以下事项:

\n\n
// you could define the following object as we have for this example, or it could be coming right from req.body\n\nlet update_payload = {\n  table_name = \'users\',\n  query_field = \'email\',\n  query_value = \'j.smith.old@example.com\',\n  job_seeker = {\n    username: \'j.smith\',\n    user_type: \'job_seeker\',\n    name: \'john smith\',\n    email: \'j.smith.new@example.com\',\n    password: \'keyboard_cat_new\',\n    resume: true,\n    resume_date_time: \'2109-01-01\',\n    salary_expectation: 100000\n  }\n};\n\n\nconnection.query(\'UPDATE ?? SET ? WHERE ?? = ? \', [update_payload.table_name, update_payload.job_seeker, update_payload.query_field, update_payload.query_value], function(error, results, fields) {\n  if (error) throw error;\n  console.log(results);\n});\n\n// expected console output\xe2\x80\xa6\n\nOkPacket {\n  fieldCount: 0,\n  affectedRows: 1,\n  insertId: 0,\n  serverStatus: 2,\n  warningCount: 0,\n  message: \'(Rows matched: 1  Changed: 1  Warnings: 0\',\n  protocol41: true,\n  changedRows: 1\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n