firebase-rest-firestore 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -361,5 +361,21 @@ export declare class WriteResult {
361
361
  * 新しいFirestoreクライアントインスタンスを作成
362
362
  * @param config Firestore設定オブジェクト
363
363
  * @returns FirestoreClientインスタンス
364
+ *
365
+ * @example
366
+ * // デフォルトデータベースに接続
367
+ * const db = createFirestoreClient({
368
+ * projectId: 'your-project-id',
369
+ * privateKey: 'your-private-key',
370
+ * clientEmail: 'your-client-email'
371
+ * });
372
+ *
373
+ * // 異なる名前のデータベースに接続
374
+ * const customDb = createFirestoreClient({
375
+ * projectId: 'your-project-id',
376
+ * privateKey: 'your-private-key',
377
+ * clientEmail: 'your-client-email',
378
+ * databaseId: 'your-database-id'
379
+ * });
364
380
  */
365
381
  export declare function createFirestoreClient(config: FirestoreConfig): FirestoreClient;
package/dist/client.js CHANGED
@@ -98,7 +98,7 @@ class FirestoreClient {
98
98
  async add(collectionName, data) {
99
99
  // 操作前に設定をチェック
100
100
  this.checkConfig();
101
- const url = (0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName);
101
+ const url = (0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName, this.config.databaseId);
102
102
  const firestoreData = (0, converter_1.convertToFirestoreDocument)(data);
103
103
  const token = await this.getToken();
104
104
  const response = await fetch(url, {
@@ -124,7 +124,7 @@ class FirestoreClient {
124
124
  async get(collectionName, documentId) {
125
125
  // 操作前に設定をチェック
126
126
  this.checkConfig();
127
- const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName)}/${documentId}`;
127
+ const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName, this.config.databaseId)}/${documentId}`;
128
128
  const token = await this.getToken();
129
129
  const response = await fetch(url, {
130
130
  method: "GET",
@@ -151,7 +151,12 @@ class FirestoreClient {
151
151
  async update(collectionName, documentId, data) {
152
152
  // 操作前に設定をチェック
153
153
  this.checkConfig();
154
- const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName)}/${documentId}`;
154
+ const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName, this.config.databaseId)}/${documentId}`;
155
+ // 既存のドキュメントを取得してからマージする
156
+ const existingDoc = await this.get(collectionName, documentId);
157
+ if (existingDoc) {
158
+ data = { ...existingDoc, ...data };
159
+ }
155
160
  const firestoreData = (0, converter_1.convertToFirestoreDocument)(data);
156
161
  const token = await this.getToken();
157
162
  const response = await fetch(url, {
@@ -177,7 +182,7 @@ class FirestoreClient {
177
182
  async delete(collectionName, documentId) {
178
183
  // 操作前に設定をチェック
179
184
  this.checkConfig();
180
- const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName)}/${documentId}`;
185
+ const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, collectionName, this.config.databaseId)}/${documentId}`;
181
186
  const token = await this.getToken();
182
187
  const response = await fetch(url, {
183
188
  method: "DELETE",
@@ -205,7 +210,7 @@ class FirestoreClient {
205
210
  // e.g) パス形式: collectionPath = "/users/123/items" の場合は、collectionPaths = ["users", "123", "items"], collectionName = "items", basePath += "/users/123"
206
211
  // e.g) 単一形式: collectionPath = "items" の場合は、collectionPaths = ["items"], collectionName = "items", basePath += ""
207
212
  const collectionPaths = collectionPath.split("/");
208
- let basePath = `https://firestore.googleapis.com/v1/projects/${this.config.projectId}/databases/(default)/documents`;
213
+ let basePath = `https://firestore.googleapis.com/v1/projects/${this.config.projectId}/databases/${this.config.databaseId || "(default)"}/documents`;
209
214
  let collectionName = collectionPath;
210
215
  if (collectionPaths.length > 1) {
211
216
  basePath = `${basePath}/${collectionPaths.slice(0, -1).join("/")}`;
@@ -844,6 +849,22 @@ exports.WriteResult = WriteResult;
844
849
  * 新しいFirestoreクライアントインスタンスを作成
845
850
  * @param config Firestore設定オブジェクト
846
851
  * @returns FirestoreClientインスタンス
852
+ *
853
+ * @example
854
+ * // デフォルトデータベースに接続
855
+ * const db = createFirestoreClient({
856
+ * projectId: 'your-project-id',
857
+ * privateKey: 'your-private-key',
858
+ * clientEmail: 'your-client-email'
859
+ * });
860
+ *
861
+ * // 異なる名前のデータベースに接続
862
+ * const customDb = createFirestoreClient({
863
+ * projectId: 'your-project-id',
864
+ * privateKey: 'your-private-key',
865
+ * clientEmail: 'your-client-email',
866
+ * databaseId: 'your-database-id'
867
+ * });
847
868
  */
848
869
  function createFirestoreClient(config) {
849
870
  // 秘密鍵のフォーマットを確認
package/dist/types.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface FirestoreConfig {
5
5
  projectId: string;
6
6
  privateKey: string;
7
7
  clientEmail: string;
8
+ databaseId?: string;
8
9
  }
9
10
  /**
10
11
  * Firestoreの値型定義
@@ -2,9 +2,10 @@
2
2
  * Firestoreのベースパスを取得
3
3
  * @param projectId プロジェクトID
4
4
  * @param collectionName コレクション名(省略可)
5
+ * @param databaseId データベースID(省略時は'(default)')
5
6
  * @returns Firestoreのベースパス
6
7
  */
7
- export declare function getFirestoreBasePath(projectId: string, collectionName?: string): string;
8
+ export declare function getFirestoreBasePath(projectId: string, collectionName?: string, databaseId?: string): string;
8
9
  /**
9
10
  * ドキュメントパスからIDを抽出
10
11
  * @param path ドキュメントパス
@@ -6,10 +6,12 @@ exports.getDocumentId = getDocumentId;
6
6
  * Firestoreのベースパスを取得
7
7
  * @param projectId プロジェクトID
8
8
  * @param collectionName コレクション名(省略可)
9
+ * @param databaseId データベースID(省略時は'(default)')
9
10
  * @returns Firestoreのベースパス
10
11
  */
11
- function getFirestoreBasePath(projectId, collectionName) {
12
- const basePath = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default)/documents`;
12
+ function getFirestoreBasePath(projectId, collectionName, databaseId) {
13
+ const dbId = databaseId || "(default)";
14
+ const basePath = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/${dbId}/documents`;
13
15
  return collectionName ? `${basePath}/${collectionName}` : basePath;
14
16
  }
15
17
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "firebase-rest-firestore",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Firebase Firestore REST API client for Edge runtime environments",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",