我正在开发一个带有node的api,并考虑将mysql与存储过程结合使用。例如,假设我有post、post\u tag和tag表:
CREATE TABLE post (
id INT NOT NULL,
title VARCHAR(45) NULL,
PRIMARY KEY (id));
CREATE TABLE tag (
id INT NOT NULL,
tag VARCHAR(45) NULL,
PRIMARY KEY (id));
CREATE TABLE post_tag (
post_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (post_id, tag_id),
INDEX fk_post_tag_tag1_idx (tag_id ASC),
INDEX fk_post_tag_post_idx (post_id ASC),
CONSTRAINT fk_post_tag_post
FOREIGN KEY (post_id)
REFERENCES post (id),
CONSTRAINT fk_post_tag_tag1
FOREIGN KEY (tag_id)
REFERENCES tag (id));
在javascript中,我有两篇文章要插入标记:
const myPosts = [
{
title: "post 1",
tags: [
{
id: 1,
tag: "tag 1",
},
{
id: 2,
tag: "tag 2",
},
],
},
{
title: "post 2",
tags: [
{
id: 1,
tag: "tag 1",
},
{
id: 2,
tag: "tag 2",
},
],
},
];
是否可以将这个json作为参数传递给存储过程,并在其中执行插入操作?
-- Stored procedure to insert post and tags
DELIMITER $$
CREATE PROCEDURE insert_post(
IN data JSON
)
BEGIN
-- ...do the inserts here with data param
END $$
DELIMITER ;
var mysql = require("mysql");
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "test_database",
password: "test_database",
database: "test_database",
});
connection.connect();
const sql = `CALL select_posts_tags(${myPosts})`;
connection.query(sql, (error, results) => console.log(results));
connection.end();
谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!