0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cosmos DB - Azure - Memo

Last updated at Posted at 2022-10-13

概要

(自分用メモ)
CosmosDBをデプロイし、ローカルのNode.jsアプリから読み書き。
Azureクイックスタート追試

前提

Azureクラウド上のリソース確保

IDなど設定(好きに)
resourceGroupName=gxx
location="westus"
accountName="myDatabaseId"  # 他人と被らないよう
Azure上にリソース確保
az group create -n $resourceGroupName --location $location
az cosmosdb create -g $resourceGroupName -n $accountName --locations regionName=$location --kind MongoDB --capacity-mode Serverless

Mongo API / Serverless プラン

依存モジュール追加

npm install mongodb dotenv
package.json - 記述を確認
  "dependencies": {
    // ...中略...
    "dotenv": "^16.0.3",
    "mongodb": "^4.10.0"
  }
tsconfig.json - ソースコードフォルダ、生成フォルダなど設定
{
  "compilerOptions": {
    // ....
+    ,"sourceMap": true
+    ,"outDir": "./build"
  },
+ ,"include": ["src/**/*"]
}

tsconfig.json

サンプルコード

src/index.ts

サンプルデータをクラウド上のMongoDBに登録し、idとnameで検索する。

サンプルデータ
{
  "name":"Yamba Surfboard-45",
  "category":"gear-surf-surfboards",
  "quantity":12,
  "sale":false
}

ビルドと実行

環境変数にPrimary接続文字列を格納
sudo apt install -y jq  # https://jqlang.github.io/jq/
x=az cosmosdb list-connection-strings --resource-group $resourceGroupName --name $accountName
export COSMOS_CONNECTION_STRING=`echo $x|jq -r '.connectionStrings[]|select(.keyKind=="Primary").connectionString'`
ビルドと実行
npx tsc && node build/index.js

Azure Portal ➜ リソースグループ gxx ➜ myacc ➜ データエクスプローラ ➜
Adventureworks ➜ producs ➜ Document にドキュメントが格納されていることを確認

参照

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?