LoginSignup
0
0

More than 1 year has passed since last update.

Firestoreのコレクションやドキュメントへのパス(文字列)を取得する関数を自動生成するコマンド

Last updated at Posted at 2022-07-19

はじめに

Firestoreにアクセスするときに、/some_collection/some_doc_id/other_collection/other_doc_id みたいにコレクションやドキュメントのパスを書くのって少し面倒じゃないですか? 特にWebとAndroidの両方でFirestore使っているときなんかは、TypescriptとKotlinで似たような文字列をコード中に定義することになるので、筆者は少し億劫になってしまいます。(Firestoreは書き方の自由度が高いので、そもそもこのように書かないぜって人も沢山いると思いますが、、、)

ということで、コレクションの構造を定義したファイルをインプットに取って、各言語ごとに、コレクションやドキュメントのパス(文字列)を取得する関数やメソッドをはきだすコマンドを作成しました。

今回作ったコマンドはこちらに置いてあります。(主なコードはシェルスクリプトで書きました。)

Usage

前提

Firestoreのコレクションの構造が下記のようになっているとします。
image.png

①下のようなファイルを用意します

input.txt
# Collection Name      Collection Structure
restaurant             /restaurant
review                 /restaurant/reviews
order                  /restaurant/orders
payment                /restaurant/payments

②次のコマンドを叩きます(Javascriptの場合)

$ firepath --language==js input_file > path.js

すると次のようなファイルが自動生成されます(長いので折りたたんでいます。)

path.js
path.js
/*
* This file is automatically generated by 'firepath' command.
* You can see more details here.
* https://github.com/ppdx999/firestore
*/

export function restaurantCollectionPath() {
  return  "/restaurant";
}

export function restaurantDocumentPath(restaurantId) {
  return "/restaurant/" + restaurantId;
}

export function reviewCollectionPath(restaurantId) {
  return "/restaurant/" + restaurantId +  "/reviews";
}

export function reviewDocumentPath(restaurantId, reviewsId) {
  return "/restaurant/" + restaurantId + "/reviews/" + reviewsId;
}

export function orderCollectionPath(restaurantId) {
  return "/restaurant/" + restaurantId +  "/orders";
}

export function orderDocumentPath(restaurantId, ordersId) {
  return "/restaurant/" + restaurantId + "/orders/" + ordersId;
}

export function paymentCollectionPath(restaurantId) {
  return "/restaurant/" + restaurantId +  "/payments";
}

export function paymentDocumentPath(restaurantId, paymentsId) {
  return "/restaurant/" + restaurantId + "/payments/" + paymentsId;
}

③コード中で次のように使用します。

example.js

const paymentYYYYPath = paymentDocumentPath("restauarnt_XXXX_id", "payment_YYYY_id")
// paymentYYYYPath = restaurant/restaurant_XXXX_id/payments/payment_YYYY_id

const firestore = getFirestore('')
const docRef = doc(firestore,paymentYYYYPath)
const paymentYYYY = await getDoc(docRef)

補足

Javaのコードを自動生成する場合は、次のコマンドを叩くと

$ firepath --language==java input_file > Firepath.java

下のようなファイルが自動生成されます(こちらも長いので折りたたんでいます。)

Firepath.java
Firepath.java
/*
* This file is automatically generated by 'firepath' command.
* You can see more details here.
* https://github.com/ppdx999/firestore
*/

public class Firepath {

  public static String restaurantCollectionPath() {
    return  "/restaurant";
  }

  public static String restaurantDocumentPath(String restaurantId) {
    return "/restaurant/" + restaurantId;
  }

  public static String reviewCollectionPath(String restaurantId) {
    return "/restaurant/" + restaurantId +  "/reviews";
  }

  public static String reviewDocumentPath(String restaurantId, String reviewsId) {
    return "/restaurant/" + restaurantId + "/reviews/" + reviewsId;
  }

  public static String orderCollectionPath(String restaurantId) {
    return "/restaurant/" + restaurantId +  "/orders";
  }

  public static String orderDocumentPath(String restaurantId, String ordersId) {
    return "/restaurant/" + restaurantId + "/orders/" + ordersId;
  }

  public static String paymentCollectionPath(String restaurantId) {
    return "/restaurant/" + restaurantId +  "/payments";
  }

  public static String paymentDocumentPath(String restaurantId, String paymentsId) {
    return "/restaurant/" + restaurantId + "/payments/" + paymentsId;
  }

}

※現在は、Javascript,Typescript,Java,Kotlinに対応しています。

おわりに

ぷちメタプログラミング楽しかった。

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