Skip to content

Instantly share code, notes, and snippets.

@ryumtym
Last active September 30, 2022 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryumtym/6b00d180652144473bf978428ef7883a to your computer and use it in GitHub Desktop.
Save ryumtym/6b00d180652144473bf978428ef7883a to your computer and use it in GitHub Desktop.
FHIRサーバー概要

FHIR SERVER

This is a document for the IPCI practical session at the M conference on 2022/09/03

Screen-Recording-2022-08-30-at-03 20 05 61-PM

概略

  • MongoDB,Node.jsを使用してFHIRサーバーを構築する。
  • 本マニュアルではDocker上にMongoDBを構築する。
  • Release4, PatientResourceをメインに解説。

環境

  • windows10(ubuntu20.04動作確認済)

使用するソフトウェア(ユーザー側でインストール)

  • MongoDB(ver5.09 or later)
  • Git

使用リポジトリ

# clone
git clone https://github.com/ryumtym/dokcer_mongo.git
git clone https://github.com/ryumtym/fhirServer.git

Gitがない場合はDownload ZIPから
19d24d19c20d1929b527f31fa6d39a1c


MongoDBを立ち上げる

先ほどクローンしたryumtym/docker_mongoのディレクトリに移動後、下記コマンドを入力することで、Docker上にMongoDB(port27017)とMongoExpress(port8081)が立ち上がる。

コンテナの起動例

# ディレクトリに移動
PS C:\Users\user\Desktop> cd docker_mongo

# 起動
PS C:\Users\user\Desktop\docker_mongo> docker-compose up -d

以下のコマンドでmongoDBコンテナの起動を確認

# 動いているコンテナを確認
PS C:\Users\user\Desktop> docker container ls

CONTAINER ID   IMAGE                  COMMAND                  CREATED       STATUS             PORTS                      NAMES
faa8ded75a9c   mongo-express:latest   "tini -- /docker-ent…"   3 weeks ago   Up About an hour   0.0.0.0:8081->8081/tcp     mongo_docker-mongo-express-1
3b342f1c80d0   mongo:latest           "docker-entrypoint.s…"   3 weeks ago   Up About an hour   0.0.0.0:27017->27017/tcp   mongo_docker-mongo-1

FHIRサーバーを立ち上げる

ryumtym/fhirServerは、bluehalo/node-fhir-server-mongo からFHIR準拠に対応するためにforkした現在開発中のリポジトリ

issue#75, issue#101

fhirServerディレクトリ直下のenv.jsonMONGO_HOSTNAME.default値がMongoDBアドレス&ポートと合致しているか確認後、以下のコマンド入力。
http://localhost:3000/4_0_0/metadataにアクセスできれば起動している。

# ディレクトリ移動
PS C:\Users\user\Desktop> cd fhirServer

# moduleインストール
PS C:\Users\user\Desktop\fhirServer> npm install

# 起動
PS C:\Users\user\Desktop\fhirServer> npm start

Postman DesktopにCOLLECTIONをインポートする

RESTでのFHIR準拠でのcall方法に正しく則った手順と設定をPostman Collectionsで作成した。

  1. Postman Desktop左上のImportボタン(下図赤枠箇所)をクリック
  2. Linkタブを選択し、https://www.getpostman.com/collections/39b5e9d6f37b86a56600 をペースト、Continueをクリック
  3. Importボタンをクリックし完了

Screen Recording 2022-08-30 at 04 03 22 24 PM


実践

今回の動き

sequenceDiagram
   %%{init:{'theme':'base'}}%%
   autonumber
   participant client
   participant fhirServer(PORT3000)
   participant mongoDB
   client->>fhirServer(PORT3000) : RestAPI形式で患者IDをリクエスト<br>[GET]https://example.com/{id}
   fhirServer(PORT3000)->>mongoDB: リクエスト内容をもとにQueryを作成し、mongoDBへアクセス
   mongoDB->>fhirServer(PORT3000): status:200<br>リクエストされた患者IDをレスポンス
   fhirServer(PORT3000)->>client: status:200<br>リクエストされた患者IDをレスポンス

認可サーバー込みの場合

sequenceDiagram
   %%{init:{'theme':'base'}}%%
   autonumber
   participant client
   participant fhirServer(PORT3000)
   participant Authz
   participant mongoDB
   client->>fhirServer(PORT3000) : RestAPI形式でリクエスト<br>[GET]https://example.com/{id}<br>Token付き
   fhirServer(PORT3000) ->>Authz:Tokenチェック
   Authz-->>fhirServer(PORT3000):チェック結果の返却
   fhirServer(PORT3000)->>mongoDB: リクエスト内容をもとにQueryを作成し、mongoDBへアクセス
   mongoDB-->>fhirServer(PORT3000): status:200<br>リクエストされた患者IDをレスポンス
   fhirServer(PORT3000)-->>client: status:200<br>リクエストされた患者IDをレスポンス
メソッド 概要 URI
POST 登録 http://localhost:3000/4_0_0/Patient
GET 取得 http://localhost:3000/4_0_0/Patient/{id}
UPDATE 更新 http://localhost:3000/4_0_0/Patient/{id}
DELETE 削除 http://localhost:3000/4_0_0/Patient/{id}

POST

Bodyにexampleデータを入れて試す Screen-Recording-2022-08-30-at-03 42 30 51-PM-1

GET

ブラウザ上でも可能 Screen-Recording-2022-08-30-at-03 43 35 50-PM

データの更新について

Screen-Recording-2022-09-02-at-04 54 00 90-PM

データの削除について

インポートしたPostmanCollectionからDELETE DATAを選択、{id}を先ほどコンソールに表示された値に変え、Sendでデータ削除が完了

クエリ検索について

元版のbluehalo/node-fhir-server-mongoは、クエリ検索に未対応箇所がある為、hl7/QueryParamsを基に作成中 ✔️は対応済みのTypeとmodifier
各リソースの編集・追加はsrc/service内で行う

追加機能(2022/8/16現在)

  • buildStu3 -> buildRelease4
  • Date/DateTime -> 年月日検索, 日付範囲検索
  • String -> 完全一致検索, 前方一致検索, 部分一致検索
  • Token -> 否定検索

例: 性別が男性以外 & 姓にChiangを含む & 1965/07/01から1999までに生まれた & 最終更新日が2022/08の患者データ一覧を取得

http://localhost:3000/4_0_0/patient?gender:not=male&family:contains=Chiang&birthdate=gt1965-07-01&birthdate=lt1999&_lastUpdated=2022-08

例: 住所(line/city/district/state/country/postalCodeのいずれか)が1600023 & 名前(text/family/given//prefixのいずれか)が山田から始まる & 記録が有効でない患者データ一覧を取得

http://localhost:3000/4_0_0/Patient?address=1600023&name=山田&active:not=true

Common search params

Summary modif ✔️ example
_id ?_id=12345
_lastUpdated eq gt lt ?_lastUpdated=2021

Patient search params

Summary modif ✔️ example
active :not ?active:not=true
address :contains :exact ?address:contains=ja
address.City :contains :exact ?address-city=PleasantVille
birthDate eq gt lt ?birthdate=gt2001&birthdate=lt2022-06-12
deathDate eq gt lt ?death-date=2015-01-01T00:00:00+00:00
deceased :not ?deceased:not=true
gender :not ?gender:not=unknown
general_practitioner
identifier :text ?identifier=example.com
link ?link=pat2
name :contains :exact ?name:exact=thebausffs
name.family :contains :exact ?family=chiang
name.given :contains :exact ?given=ted
organization ?organization=1
telecom ?telecom=phone|070-1234-5678

Observation search params

Summary modif ✔️ example
code ?code=55233-1
component-code-value-quantity ?component-code-value-quantity=8462-4$gt80
date eq gt lt ?date=gt2013&date=lt2015
encounter ?encounter=1568820

参考にしたページ


fhirServerのディレクトリ構成

.
├─public
├─src
│  ├─capabilty.js
│  ├─constants.js
│  ├─global.js
│  ├─lib
│  ├─utils
│  ├─service       -> 各resourceファイルを置くフォルダ
│  ├─strategies    -> 認可サーバーへの接続関連フォルダ
│  ├─config.js     -> fhirサーバーの設定をまとめるファイル
│  ├─index.js      -> config.jsの呼び出し・fhirサーバーの起動等を行うファイル
└─env.json         -> 環境変数ファイル

認可サーバー周り(SMART ON FHIRへの接続とブラウザアプリへのデータ表示までは確認したが、仕様確認中)

@ryumtym
Copy link
Author

ryumtym commented Aug 18, 2022

D%27Pengu_Emote

@Raja-web-developer
Copy link

D%27Pengu_Emote

Hi ryumtym

How to make a oauth2.0 configuration. please share me
i need endpoint url.

@ryumtym
Copy link
Author

ryumtym commented Sep 27, 2022

Hey @Raja-web-developer .
The Authz server can be external or, if you are familiar with nodejs, can be added within the fhir server.
This repository is what I used as a reference in learning how an authorization server works.
However, the specifications of the Authz server and fhir(SMART) are very complex and I'm still learning.

After cloning the above url repository, enter each command

# https://github.com/ryumtym/node-oidc-working-sample
npm i 
npm start

Next, change some of the settings on the fhir server.
First, edit env.json like this for this environment

  "AUTH_SERVER_URI": {
    "type": "string",
    "default": "http://localhost:3001"
  },
  "WHITELIST": {
    "type": "string",
    "default": "*"
  },
  "CLIENT_ID": {
    "type": "string",
    "default":"testid"
  },
  "CLIENT_SECRET": {
    "type": "string",
    "default":"secret"
  },
  "INTROSPECTION_URL": {
    "type": "string",
    "default":"http://localhost:3001/token/introspection"
  }

Next, edit fhirServerConfig.auth and fhirServerConfig.security in src/config.js

  auth: { //fhirServerConfig.auth
    // This servers URI
    resourceServer: env.RESOURCE_SERVER,
    strategy: {
    	name: 'bearer',
    	useSession: false,
    	service: './src/strategies/bearer.strategy.js'
    },
  },
  security: [ //fhirServerConfig.security
    {
      url: 'authorize',
      valueUri: `${env.AUTH_SERVER_URI}/auth`,
    },
    {
      url: 'token',
      valueUri: `${env.AUTH_SERVER_URI}/token`,
    },
    // optional - registration
  ],

Now the fhir server is currently at 3000 port and the Authz server is at 3001 port
The endpoint url can be found in the http://localhost:3001/.well-known/openid-configuration.
These can be used to test the movement of postman and smart on fhir to include the Authz server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment