LoginSignup
1
1

More than 1 year has passed since last update.

Cloudant SDKの置き換え

Last updated at Posted at 2022-08-10

あらまし

1年半ほど前にリリースしたアプリにて、@cloudant/cloudant というパッケージを使用していたのですが、いつの間にかdeprecatedになってしまっていたので、後継の@ibm-cloudant/cloudantへ移行したほうが良いかなと思われ、折を見て移行を実施しました。
その際のログを備忘のため残しておきます。

導入

@ibm-cloudant/cloudantのインストール

npm install @ibm-cloudant/cloudant --save

@cloudant/cloudantのアンインストール

npm uninstall @cloudant/cloudant --save

node.jsアプリからDBへの接続

接続情報の記載方法が、以前と微妙に変わっているように思いました。
環境変数に格納する、というやり方になるようですが、当初いまいち良く分からず、こちらの記事を参考にさせていただきました。
シンプルにIAMでの接続を選択し、以下のような形で接続できました。

app.js
const { CloudantV1 } = require( '@ibm-cloud/cloudant' );
const cloudantConfig = require('../../config.cloudant.js');

//settings
const db_service_name = 'CLOUDANT';

//. env values
process.env[db_service_name + '_AUTH_TYPE'] = cloudantConfig.db_auth_type;
process.env[db_service_name + '_URL'] = cloudantConfig.cloudant_url;
process.env[db_service_name + '_APIKEY'] = cloudantConfig.cloudant_iam_apikey;

const cloudant = CloudantV1.newInstance( { serviceName: db_service_name});

DBアクセスの構文

アプリからDBに対しては、insertとfindの2種類のアクションを実行していました。
@cloudant/cloudant から @ibm-cloudant/cloudantに移行するにあたり、構文が変わっており、少し手こずりました。
API Reference を参考にしつつも、どちらのメソッドに置き換えればよいのか迷ったのと、サンプルコードがいまいち良く分からず・・・一旦以下のような感じで書き換えできました。

@cloudant/cloudantの例

// insert
cloudant.use(feedback_db_name).insert(log_data);

// find
const query = {
     "selector": {
        "_id": {"$gt": "0"}
     },
     "limit":2000,
     "sort": [{"feedback_date": "desc"}]
};
cloudant.use(feedback_db_name).find(query,function(err, result){});

@ibm-cloudant/cloudantの例

// insert
cloudant.postDocument({
   db: feedback_db_name,
   document: log_data
})

// find
cloudant.postFind({
   db:feedback_db_name,
   selector: CloudantV1.selector = {
     "_id": {
       "$gt": "0"
     }
   },
   sort:CloudantV1.JsonObject =[{'feedback_date': 'desc'}],
   limit: 2000
}).then(response => {});

おわりに

作ってから1年以上放置していたら、以前何やっていたんだっけ・・・というのを思い出すところから苦労しました。
また1年後くらいの自分に向けて、やったことの備忘録はちゃんと残していこう、いくべきだ、と心を新たにいたしました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1