如何在 Node.js 应用程序中从 Mamp DB 迁移到 Cloudant DB

How to migrate from Mamp DB to Cloudant DB in Node.js application

我是 Cloudant 数据库的新手。我需要从 Mamp 数据库迁移到 Cloudant。我正在获取表单数据并将其插入到数据库中,但是对于 Cloudant,我无法理解插入命令,因为它的工作方式不同(对象基础保存)。如何建立 Db 连接?

插入代码如下:

const mysql = require("mysql");
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const { promisify } = require ('util');

// const db = mysql.createConnection({
//     host: process.env.DATABASE_HOST,
//     port: process.env.DATABASE_PORT,
//     user: process.env.DATABASE_USER,
//     password: process.env.DATABSE_PASSWORD,
//     database: process.env.DATABASE
// });
Cloudant = require('@cloudant/cloudant');    
    cloudAntConfig = {
      url: process.env.CLOUD_ANT_URL,
      apiKey: {iamauth: {iamApiKey: process.env.CLOUD_ANT_APIKEY}},
      database: process.env.CLOUD_ANT_DB
      };
exports.assessment = (req,res) => {
    console.log(req.body);
    // console.log(user.User_ID );
    const { TeamName, question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11, question12, question13, question14, question15, question16, question17, question18, question19, question20, question21, question22, question23, question24, question25, question26, question27, question28, question29, question30, question31, question32, question33, question34, question35 } = req.body;
    // console.log(parseInt(question3)+parseInt(question4));
    // some code about data goes here
    db.query('INSERT INTO assessment_result SET ?',{User_Id : 1,Team_Name : TeamName, Dimension_1_Score  : (dime_1_total/25)*100, Dimension_2_Score  : (dime_2_total/50)*100, Dimension_3_Score  : (dime_3_total/50)*100, Dimension_4_Score  : (dime_4_total/50)*100, Total_Score  : final_total, Total_Score_Persentage  : (final_total/175)*100 },(error,results)=>{
        // res.render('register');
        })
}

Getting Started section of the @cloudant/cloudant README 展示了如何将文档插入 Cloudant 数据库。

const Cloudant = require('@cloudant/cloudant');    
cloudAntConfig = {
  url: process.env.CLOUD_ANT_URL,
  plugins: { iamauth: { iamApiKey: process.env.CLOUD_ANT_APIKEY } }
}
const cloudant = Cloudant(cloudAntConfig);
const db = cloudant.use(process.env.CLOUD_ANT_DB)
// ...
db.insert({User_Id: 1, /* etc the rest of your document content */}, (err, data) => {})

虽然 @cloudant/cloudant 已弃用,但可能值得注意,如果您正在编写新代码,则应考虑使用 @ibm-cloud/cloudant.