firebase-rest-firestore 1.5.0 → 1.6.0
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/README.md +37 -0
- package/dist/cjs/client.d.ts +37 -0
- package/dist/cjs/client.js +121 -7
- package/dist/cjs/field-value.d.ts +22 -0
- package/dist/cjs/field-value.js +31 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/types.d.ts +78 -1
- package/dist/cjs/types.js +73 -0
- package/dist/cjs/utils/converter.d.ts +31 -1
- package/dist/cjs/utils/converter.js +114 -3
- package/dist/esm/client.d.ts +37 -0
- package/dist/esm/client.js +122 -8
- package/dist/esm/field-value.d.ts +22 -0
- package/dist/esm/field-value.js +27 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/types.d.ts +78 -1
- package/dist/esm/types.js +70 -1
- package/dist/esm/utils/converter.d.ts +31 -1
- package/dist/esm/utils/converter.js +112 -3
- package/dist/types/client.d.ts +37 -0
- package/dist/types/field-value.d.ts +22 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types.d.ts +78 -1
- package/dist/types/utils/converter.d.ts +31 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -134,6 +134,43 @@ Parameters:
|
|
|
134
134
|
|
|
135
135
|
Returns: The added document with auto-generated ID.
|
|
136
136
|
|
|
137
|
+
### FieldValue
|
|
138
|
+
|
|
139
|
+
Sentinels for special write behaviors, mirroring the native Firebase SDK.
|
|
140
|
+
|
|
141
|
+
#### FieldValue.serverTimestamp()
|
|
142
|
+
|
|
143
|
+
Sets the field to the server's request timestamp at write time (rather than an
|
|
144
|
+
unreliable client clock). Works with `add`, `update`, `set`, and `createWithId`,
|
|
145
|
+
including nested fields. The returned document contains the resolved `Date`.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { createFirestoreClient, FieldValue } from "firebase-rest-firestore";
|
|
149
|
+
|
|
150
|
+
const client = createFirestoreClient(config);
|
|
151
|
+
|
|
152
|
+
// On create (auto-generated ID)
|
|
153
|
+
const post = await client.add("posts", {
|
|
154
|
+
title: "Hello",
|
|
155
|
+
createdAt: FieldValue.serverTimestamp(),
|
|
156
|
+
});
|
|
157
|
+
console.log(post.createdAt); // Date, set by the server
|
|
158
|
+
|
|
159
|
+
// On update
|
|
160
|
+
await client.update("posts", post.id, {
|
|
161
|
+
updatedAt: FieldValue.serverTimestamp(),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Nested fields are supported too
|
|
165
|
+
await client.collection("posts").doc(post.id).set({
|
|
166
|
+
meta: { touchedAt: FieldValue.serverTimestamp() },
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
> Writes containing a `FieldValue` are sent through the Firestore `commit`
|
|
171
|
+
> endpoint so the transform is applied server-side. A `FieldValue` cannot be
|
|
172
|
+
> used inside an array.
|
|
173
|
+
|
|
137
174
|
## Error Handling
|
|
138
175
|
|
|
139
176
|
Firebase REST Firestore throws exceptions with appropriate error messages when API requests fail. Here's an example of error handling:
|
package/dist/cjs/client.d.ts
CHANGED
|
@@ -30,6 +30,30 @@ export declare class FirestoreClient {
|
|
|
30
30
|
* @private
|
|
31
31
|
*/
|
|
32
32
|
private prepareHeaders;
|
|
33
|
+
/**
|
|
34
|
+
* Build the fully-qualified Firestore reference value for a document path,
|
|
35
|
+
* e.g. `projects/{projectId}/databases/{databaseId}/documents/{path}`.
|
|
36
|
+
* Used to encode a DocumentReference without exposing internal path helpers.
|
|
37
|
+
* @param documentPath Document path (ex: "users/uid/posts/postId")
|
|
38
|
+
*/
|
|
39
|
+
getReferenceValue(documentPath: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Apply a single write through the `documents:commit` endpoint. This is the
|
|
42
|
+
* only REST path that supports field transforms (e.g. server timestamps).
|
|
43
|
+
* @param collectionName Collection path
|
|
44
|
+
* @param documentId Document ID
|
|
45
|
+
* @param fields Already-converted Firestore field values
|
|
46
|
+
* @param transforms Field transforms to apply after the update
|
|
47
|
+
* @param currentDocument Optional precondition (e.g. `{ exists: false }`)
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
private commit;
|
|
51
|
+
/**
|
|
52
|
+
* Commit a write and read the document back, so the resolved transform
|
|
53
|
+
* values (e.g. the server timestamp) are returned to the caller.
|
|
54
|
+
* @private
|
|
55
|
+
*/
|
|
56
|
+
private commitAndRead;
|
|
33
57
|
/**
|
|
34
58
|
* Get collection reference
|
|
35
59
|
* @param path Collection path
|
|
@@ -57,6 +81,14 @@ export declare class FirestoreClient {
|
|
|
57
81
|
add(collectionName: string, data: Record<string, any>): Promise<Record<string, any> & {
|
|
58
82
|
id: string;
|
|
59
83
|
}>;
|
|
84
|
+
/**
|
|
85
|
+
* Create a document that contains field transforms (e.g. serverTimestamp)
|
|
86
|
+
* with an auto-generated ID. Uses the commit endpoint with an
|
|
87
|
+
* `exists: false` precondition and retries on the (astronomically unlikely)
|
|
88
|
+
* ID collision, mirroring the uniqueness of server-generated IDs.
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
private addWithTransforms;
|
|
60
92
|
/**
|
|
61
93
|
* Get document
|
|
62
94
|
* @param collectionName Collection name
|
|
@@ -186,6 +218,11 @@ export declare class DocumentReference {
|
|
|
186
218
|
* Get document path
|
|
187
219
|
*/
|
|
188
220
|
get path(): string;
|
|
221
|
+
/**
|
|
222
|
+
* Fully-qualified Firestore reference value for this document, e.g.
|
|
223
|
+
* `projects/{projectId}/databases/{databaseId}/documents/{path}`.
|
|
224
|
+
*/
|
|
225
|
+
get referenceValue(): string;
|
|
189
226
|
/**
|
|
190
227
|
* Get parent collection reference
|
|
191
228
|
*/
|
package/dist/cjs/client.js
CHANGED
|
@@ -7,6 +7,17 @@ const converter_1 = require("./utils/converter");
|
|
|
7
7
|
const path_1 = require("./utils/path");
|
|
8
8
|
const config_1 = require("./utils/config");
|
|
9
9
|
const path_2 = require("./utils/path");
|
|
10
|
+
/**
|
|
11
|
+
* Generate a random 20-character document ID (same alphabet as the Firebase SDKs).
|
|
12
|
+
*/
|
|
13
|
+
function generateAutoId() {
|
|
14
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
15
|
+
let id = "";
|
|
16
|
+
for (let i = 0; i < 20; i++) {
|
|
17
|
+
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
18
|
+
}
|
|
19
|
+
return id;
|
|
20
|
+
}
|
|
10
21
|
/**
|
|
11
22
|
* Firestore client class
|
|
12
23
|
*/
|
|
@@ -94,6 +105,63 @@ class FirestoreClient {
|
|
|
94
105
|
}
|
|
95
106
|
return headers;
|
|
96
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Build the fully-qualified Firestore reference value for a document path,
|
|
110
|
+
* e.g. `projects/{projectId}/databases/{databaseId}/documents/{path}`.
|
|
111
|
+
* Used to encode a DocumentReference without exposing internal path helpers.
|
|
112
|
+
* @param documentPath Document path (ex: "users/uid/posts/postId")
|
|
113
|
+
*/
|
|
114
|
+
getReferenceValue(documentPath) {
|
|
115
|
+
return this.pathUtil.getParentReference(documentPath);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Apply a single write through the `documents:commit` endpoint. This is the
|
|
119
|
+
* only REST path that supports field transforms (e.g. server timestamps).
|
|
120
|
+
* @param collectionName Collection path
|
|
121
|
+
* @param documentId Document ID
|
|
122
|
+
* @param fields Already-converted Firestore field values
|
|
123
|
+
* @param transforms Field transforms to apply after the update
|
|
124
|
+
* @param currentDocument Optional precondition (e.g. `{ exists: false }`)
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
async commit(collectionName, documentId, fields, transforms, currentDocument) {
|
|
128
|
+
const documentName = this.pathUtil.getParentReference(`${collectionName}/${documentId}`);
|
|
129
|
+
const write = (0, converter_1.buildCommitWrite)(documentName, fields, transforms, currentDocument);
|
|
130
|
+
const url = `${this.pathUtil.getBasePath()}:commit`;
|
|
131
|
+
if (this.debug) {
|
|
132
|
+
console.log(`Committing write to: ${url}`, JSON.stringify(write));
|
|
133
|
+
}
|
|
134
|
+
const headers = await this.prepareHeaders();
|
|
135
|
+
const response = await fetch(url, {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers,
|
|
138
|
+
body: JSON.stringify({ writes: [write] }),
|
|
139
|
+
});
|
|
140
|
+
if (!response.ok) {
|
|
141
|
+
const errorText = await response.text();
|
|
142
|
+
if (this.debug) {
|
|
143
|
+
console.error(`Error response: ${errorText}`);
|
|
144
|
+
}
|
|
145
|
+
const error = new Error(`Firestore API error: ${response.statusText || response.status} - ${errorText}`);
|
|
146
|
+
error.status = response.status;
|
|
147
|
+
error.alreadyExists =
|
|
148
|
+
response.status === 409 || /ALREADY_EXISTS/.test(errorText);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Commit a write and read the document back, so the resolved transform
|
|
154
|
+
* values (e.g. the server timestamp) are returned to the caller.
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
async commitAndRead(collectionName, documentId, fields, transforms, currentDocument) {
|
|
158
|
+
await this.commit(collectionName, documentId, fields, transforms, currentDocument);
|
|
159
|
+
const saved = await this.get(collectionName, documentId);
|
|
160
|
+
if (!saved) {
|
|
161
|
+
throw new Error(`Document ${collectionName}/${documentId} could not be read back after commit`);
|
|
162
|
+
}
|
|
163
|
+
return saved;
|
|
164
|
+
}
|
|
97
165
|
/**
|
|
98
166
|
* Get collection reference
|
|
99
167
|
* @param path Collection path
|
|
@@ -138,6 +206,12 @@ class FirestoreClient {
|
|
|
138
206
|
if (this.debug) {
|
|
139
207
|
console.log(`Adding document to collection: ${collectionName}`, data);
|
|
140
208
|
}
|
|
209
|
+
// When the data contains field transforms (e.g. serverTimestamp), the
|
|
210
|
+
// create must go through the commit endpoint with a client-generated ID.
|
|
211
|
+
const { fields: plainData, transforms } = (0, converter_1.extractFieldTransforms)(data);
|
|
212
|
+
if (transforms.length > 0) {
|
|
213
|
+
return this.addWithTransforms(collectionName, plainData, transforms);
|
|
214
|
+
}
|
|
141
215
|
const url = this.pathUtil.getCollectionPath(collectionName);
|
|
142
216
|
const firestoreData = (0, converter_1.convertToFirestoreDocument)(data);
|
|
143
217
|
if (this.debug) {
|
|
@@ -162,6 +236,31 @@ class FirestoreClient {
|
|
|
162
236
|
const result = (await response.json());
|
|
163
237
|
return (0, converter_1.convertFromFirestoreDocument)(result);
|
|
164
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Create a document that contains field transforms (e.g. serverTimestamp)
|
|
241
|
+
* with an auto-generated ID. Uses the commit endpoint with an
|
|
242
|
+
* `exists: false` precondition and retries on the (astronomically unlikely)
|
|
243
|
+
* ID collision, mirroring the uniqueness of server-generated IDs.
|
|
244
|
+
* @private
|
|
245
|
+
*/
|
|
246
|
+
async addWithTransforms(collectionName, plainData, transforms) {
|
|
247
|
+
const fields = (0, converter_1.convertToFirestoreDocument)(plainData).fields;
|
|
248
|
+
const maxAttempts = 5;
|
|
249
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
250
|
+
const documentId = generateAutoId();
|
|
251
|
+
try {
|
|
252
|
+
return await this.commitAndRead(collectionName, documentId, fields, transforms, { exists: false });
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
const collided = error?.alreadyExists;
|
|
256
|
+
if (collided && attempt < maxAttempts - 1) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
throw new Error("Failed to generate a unique document ID after multiple attempts");
|
|
263
|
+
}
|
|
165
264
|
/**
|
|
166
265
|
* Get document
|
|
167
266
|
* @param collectionName Collection name
|
|
@@ -266,6 +365,13 @@ class FirestoreClient {
|
|
|
266
365
|
data = { ...existingDoc, ...data };
|
|
267
366
|
}
|
|
268
367
|
}
|
|
368
|
+
// Route writes that contain field transforms (e.g. serverTimestamp)
|
|
369
|
+
// through the commit endpoint; the merged document is sent as the update.
|
|
370
|
+
const { fields: plainData, transforms } = (0, converter_1.extractFieldTransforms)(data);
|
|
371
|
+
if (transforms.length > 0) {
|
|
372
|
+
const fields = (0, converter_1.convertToFirestoreDocument)(plainData).fields;
|
|
373
|
+
return this.commitAndRead(collectionName, documentId, fields, transforms);
|
|
374
|
+
}
|
|
269
375
|
const firestoreData = (0, converter_1.convertToFirestoreDocument)(data);
|
|
270
376
|
const headers = await this.prepareHeaders();
|
|
271
377
|
const response = await fetch(url, {
|
|
@@ -469,6 +575,13 @@ class FirestoreClient {
|
|
|
469
575
|
async createWithId(collectionName, documentId, data) {
|
|
470
576
|
// 操作前に設定をチェック
|
|
471
577
|
this.checkConfig();
|
|
578
|
+
// Route writes that contain field transforms (e.g. serverTimestamp)
|
|
579
|
+
// through the commit endpoint (which creates the document if absent).
|
|
580
|
+
const { fields: plainData, transforms } = (0, converter_1.extractFieldTransforms)(data);
|
|
581
|
+
if (transforms.length > 0) {
|
|
582
|
+
const fields = (0, converter_1.convertToFirestoreDocument)(plainData).fields;
|
|
583
|
+
return this.commitAndRead(collectionName, documentId, fields, transforms);
|
|
584
|
+
}
|
|
472
585
|
const url = `${(0, path_1.getFirestoreBasePath)(this.config.projectId, this.config.databaseId, this.config)}/${collectionName}/${documentId}`;
|
|
473
586
|
const firestoreData = (0, converter_1.convertToFirestoreDocument)(data);
|
|
474
587
|
const token = await this.getToken();
|
|
@@ -636,13 +749,7 @@ class CollectionReference {
|
|
|
636
749
|
* @returns Random ID
|
|
637
750
|
*/
|
|
638
751
|
_generateId() {
|
|
639
|
-
|
|
640
|
-
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
641
|
-
let id = "";
|
|
642
|
-
for (let i = 0; i < 20; i++) {
|
|
643
|
-
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
644
|
-
}
|
|
645
|
-
return id;
|
|
752
|
+
return generateAutoId();
|
|
646
753
|
}
|
|
647
754
|
}
|
|
648
755
|
exports.CollectionReference = CollectionReference;
|
|
@@ -667,6 +774,13 @@ class DocumentReference {
|
|
|
667
774
|
get path() {
|
|
668
775
|
return `${this.collectionPath}/${this.docId}`;
|
|
669
776
|
}
|
|
777
|
+
/**
|
|
778
|
+
* Fully-qualified Firestore reference value for this document, e.g.
|
|
779
|
+
* `projects/{projectId}/databases/{databaseId}/documents/{path}`.
|
|
780
|
+
*/
|
|
781
|
+
get referenceValue() {
|
|
782
|
+
return this.client.getReferenceValue(this.path);
|
|
783
|
+
}
|
|
670
784
|
/**
|
|
671
785
|
* Get parent collection reference
|
|
672
786
|
*/
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentinel values for special write behaviors, mirroring the native Firebase
|
|
3
|
+
* SDK's `FieldValue`. Currently only `serverTimestamp()` is supported.
|
|
4
|
+
*
|
|
5
|
+
* A `FieldValue` is not a real field value: when used as a field in a write it
|
|
6
|
+
* is translated into a Firestore field transform (applied server-side) rather
|
|
7
|
+
* than serialized as data. Using one anywhere else (e.g. inside an array) is an
|
|
8
|
+
* error.
|
|
9
|
+
*/
|
|
10
|
+
export declare class FieldValue {
|
|
11
|
+
readonly methodName: "serverTimestamp";
|
|
12
|
+
private constructor();
|
|
13
|
+
/**
|
|
14
|
+
* Returns a sentinel that sets the field to the server's request timestamp at
|
|
15
|
+
* write time, e.g. `client.add("posts", { createdAt: FieldValue.serverTimestamp() })`.
|
|
16
|
+
*/
|
|
17
|
+
static serverTimestamp(): FieldValue;
|
|
18
|
+
/**
|
|
19
|
+
* Whether this sentinel represents the same transform as another.
|
|
20
|
+
*/
|
|
21
|
+
isEqual(other: FieldValue): boolean;
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FieldValue = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Sentinel values for special write behaviors, mirroring the native Firebase
|
|
6
|
+
* SDK's `FieldValue`. Currently only `serverTimestamp()` is supported.
|
|
7
|
+
*
|
|
8
|
+
* A `FieldValue` is not a real field value: when used as a field in a write it
|
|
9
|
+
* is translated into a Firestore field transform (applied server-side) rather
|
|
10
|
+
* than serialized as data. Using one anywhere else (e.g. inside an array) is an
|
|
11
|
+
* error.
|
|
12
|
+
*/
|
|
13
|
+
class FieldValue {
|
|
14
|
+
constructor(methodName) {
|
|
15
|
+
this.methodName = methodName;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns a sentinel that sets the field to the server's request timestamp at
|
|
19
|
+
* write time, e.g. `client.add("posts", { createdAt: FieldValue.serverTimestamp() })`.
|
|
20
|
+
*/
|
|
21
|
+
static serverTimestamp() {
|
|
22
|
+
return new FieldValue("serverTimestamp");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether this sentinel represents the same transform as another.
|
|
26
|
+
*/
|
|
27
|
+
isEqual(other) {
|
|
28
|
+
return other instanceof FieldValue && other.methodName === this.methodName;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.FieldValue = FieldValue;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./types";
|
|
2
2
|
import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult } from "./client";
|
|
3
|
+
export { FieldValue } from "./field-value";
|
|
3
4
|
export { getFirestoreToken } from "./utils/auth";
|
|
4
5
|
export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
|
|
5
6
|
export { getFirestoreBasePath, getDocumentId } from "./utils/path";
|
package/dist/cjs/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.WriteResult = exports.DocumentSnapshot = exports.QuerySnapshot = exports.Query = exports.CollectionGroup = exports.DocumentReference = exports.CollectionReference = exports.createFirestoreClient = exports.FirestoreClient = exports.formatPrivateKey = exports.getDocumentId = exports.getFirestoreBasePath = exports.convertFromFirestoreDocument = exports.convertToFirestoreDocument = exports.convertFromFirestoreValue = exports.convertToFirestoreValue = exports.getFirestoreToken = void 0;
|
|
17
|
+
exports.WriteResult = exports.DocumentSnapshot = exports.QuerySnapshot = exports.Query = exports.CollectionGroup = exports.DocumentReference = exports.CollectionReference = exports.createFirestoreClient = exports.FirestoreClient = exports.formatPrivateKey = exports.getDocumentId = exports.getFirestoreBasePath = exports.convertFromFirestoreDocument = exports.convertToFirestoreDocument = exports.convertFromFirestoreValue = exports.convertToFirestoreValue = exports.getFirestoreToken = exports.FieldValue = void 0;
|
|
18
18
|
// 型定義のエクスポート
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
20
|
// クライアントのエクスポート
|
|
@@ -28,6 +28,9 @@ Object.defineProperty(exports, "Query", { enumerable: true, get: function () { r
|
|
|
28
28
|
Object.defineProperty(exports, "QuerySnapshot", { enumerable: true, get: function () { return client_1.QuerySnapshot; } });
|
|
29
29
|
Object.defineProperty(exports, "DocumentSnapshot", { enumerable: true, get: function () { return client_1.DocumentSnapshot; } });
|
|
30
30
|
Object.defineProperty(exports, "WriteResult", { enumerable: true, get: function () { return client_1.WriteResult; } });
|
|
31
|
+
// FieldValue センチネルのエクスポート
|
|
32
|
+
var field_value_1 = require("./field-value");
|
|
33
|
+
Object.defineProperty(exports, "FieldValue", { enumerable: true, get: function () { return field_value_1.FieldValue; } });
|
|
31
34
|
// ユーティリティ関数のエクスポート
|
|
32
35
|
var auth_1 = require("./utils/auth");
|
|
33
36
|
Object.defineProperty(exports, "getFirestoreToken", { enumerable: true, get: function () { return auth_1.getFirestoreToken; } });
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -11,8 +11,63 @@ export interface FirestoreConfig {
|
|
|
11
11
|
emulatorHost?: string;
|
|
12
12
|
emulatorPort?: number;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* A reference to a document. For example: `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
|
|
16
|
+
* Used to represent document references globally and not connected to any particular client.
|
|
17
|
+
*/
|
|
18
|
+
export declare class LiteralDocumentReference {
|
|
19
|
+
referenceValue: string;
|
|
20
|
+
constructor(options: Pick<LiteralDocumentReference, "referenceValue">);
|
|
21
|
+
/**
|
|
22
|
+
* Globally unique Firestore document reference paths look like:
|
|
23
|
+
* projects/{project_id}/databases/{database_id}/documents/{document_path}
|
|
24
|
+
* The database id (e.g. `(default)`) never contains a slash, while the
|
|
25
|
+
* document path may contain many. A single anchored regex parses this
|
|
26
|
+
* without pulling in a URLPattern polyfill.
|
|
27
|
+
*/
|
|
28
|
+
private static readonly pattern;
|
|
29
|
+
private parse;
|
|
30
|
+
/**
|
|
31
|
+
* Get Project ID
|
|
32
|
+
*/
|
|
33
|
+
get project_id(): string;
|
|
34
|
+
/**
|
|
35
|
+
* Get Database ID
|
|
36
|
+
* Ex: `(default)`
|
|
37
|
+
*/
|
|
38
|
+
get database_id(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Get document ID
|
|
41
|
+
*/
|
|
42
|
+
get id(): string;
|
|
43
|
+
/**
|
|
44
|
+
* Get the collection ID
|
|
45
|
+
*/
|
|
46
|
+
get collectionPath(): string;
|
|
47
|
+
/**
|
|
48
|
+
* Get document path
|
|
49
|
+
*/
|
|
50
|
+
get path(): string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A geo point value representing a point on the surface of Earth.
|
|
54
|
+
*/
|
|
55
|
+
export declare class LiteralGeoPointValue {
|
|
56
|
+
geoPointValue: {
|
|
57
|
+
/**
|
|
58
|
+
* The latitude in degrees. It must be in the range [-90.0, +90.0].
|
|
59
|
+
*/
|
|
60
|
+
latitude: number;
|
|
61
|
+
/**
|
|
62
|
+
* The longitude in degrees. It must be in the range [-180.0, +180.0].
|
|
63
|
+
*/
|
|
64
|
+
longitude: number;
|
|
65
|
+
};
|
|
66
|
+
constructor(options: Pick<LiteralGeoPointValue, "geoPointValue">);
|
|
67
|
+
}
|
|
14
68
|
/**
|
|
15
69
|
* Firestoreの値型定義
|
|
70
|
+
* See: https://github.com/googleapis/google-api-nodejs-client/blob/5870dfe31f4885eebc82c19f7471c50403308f26/src/apis/firestore/v1.ts#L2246
|
|
16
71
|
*/
|
|
17
72
|
export type FirestoreFieldValue = {
|
|
18
73
|
stringValue: string;
|
|
@@ -26,7 +81,7 @@ export type FirestoreFieldValue = {
|
|
|
26
81
|
nullValue: null;
|
|
27
82
|
} | {
|
|
28
83
|
timestampValue: string;
|
|
29
|
-
} | {
|
|
84
|
+
} | Pick<LiteralGeoPointValue, 'geoPointValue'> | Pick<LiteralDocumentReference, 'referenceValue'> | {
|
|
30
85
|
mapValue: {
|
|
31
86
|
fields: Record<string, FirestoreFieldValue>;
|
|
32
87
|
};
|
|
@@ -35,6 +90,28 @@ export type FirestoreFieldValue = {
|
|
|
35
90
|
values: FirestoreFieldValue[];
|
|
36
91
|
};
|
|
37
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* A Firestore field transform applied server-side during a commit write.
|
|
95
|
+
* See: https://firebase.google.com/docs/firestore/reference/rest/v1/Write#FieldTransform
|
|
96
|
+
*/
|
|
97
|
+
export interface FieldTransform {
|
|
98
|
+
fieldPath: string;
|
|
99
|
+
setToServerValue: "REQUEST_TIME";
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A single write in a `documents:commit` request.
|
|
103
|
+
*/
|
|
104
|
+
export interface CommitWrite {
|
|
105
|
+
update: {
|
|
106
|
+
name: string;
|
|
107
|
+
fields: Record<string, FirestoreFieldValue>;
|
|
108
|
+
};
|
|
109
|
+
updateTransforms?: FieldTransform[];
|
|
110
|
+
currentDocument?: {
|
|
111
|
+
exists?: boolean;
|
|
112
|
+
updateTime?: string;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
38
115
|
/**
|
|
39
116
|
* Firestoreドキュメント型
|
|
40
117
|
*/
|
package/dist/cjs/types.js
CHANGED
|
@@ -1,2 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LiteralGeoPointValue = exports.LiteralDocumentReference = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A reference to a document. For example: `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
|
|
6
|
+
* Used to represent document references globally and not connected to any particular client.
|
|
7
|
+
*/
|
|
8
|
+
class LiteralDocumentReference {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.referenceValue = options.referenceValue;
|
|
11
|
+
}
|
|
12
|
+
parse() {
|
|
13
|
+
const match = LiteralDocumentReference.pattern.exec(this.referenceValue);
|
|
14
|
+
if (!match) {
|
|
15
|
+
throw new Error("Invalid document path. Path does not match pattern.");
|
|
16
|
+
}
|
|
17
|
+
const [, project_id, database_id, document_path] = match;
|
|
18
|
+
return { project_id, database_id, document_path };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get Project ID
|
|
22
|
+
*/
|
|
23
|
+
get project_id() {
|
|
24
|
+
return this.parse().project_id;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get Database ID
|
|
28
|
+
* Ex: `(default)`
|
|
29
|
+
*/
|
|
30
|
+
get database_id() {
|
|
31
|
+
return this.parse().database_id;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get document ID
|
|
35
|
+
*/
|
|
36
|
+
get id() {
|
|
37
|
+
const path = this.parse().document_path;
|
|
38
|
+
const parts = path.split("/");
|
|
39
|
+
const docId = parts[parts.length - 1];
|
|
40
|
+
return docId;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the collection ID
|
|
44
|
+
*/
|
|
45
|
+
get collectionPath() {
|
|
46
|
+
const path = this.parse().document_path;
|
|
47
|
+
const parts = path.split("/");
|
|
48
|
+
const collectionPath = parts.slice(0, parts.length - 1).join("/");
|
|
49
|
+
return collectionPath;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get document path
|
|
53
|
+
*/
|
|
54
|
+
get path() {
|
|
55
|
+
return this.parse().document_path;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.LiteralDocumentReference = LiteralDocumentReference;
|
|
59
|
+
/**
|
|
60
|
+
* Globally unique Firestore document reference paths look like:
|
|
61
|
+
* projects/{project_id}/databases/{database_id}/documents/{document_path}
|
|
62
|
+
* The database id (e.g. `(default)`) never contains a slash, while the
|
|
63
|
+
* document path may contain many. A single anchored regex parses this
|
|
64
|
+
* without pulling in a URLPattern polyfill.
|
|
65
|
+
*/
|
|
66
|
+
LiteralDocumentReference.pattern = /^projects\/([^/]+)\/databases\/([^/]+)\/documents\/(.+)$/;
|
|
67
|
+
/**
|
|
68
|
+
* A geo point value representing a point on the surface of Earth.
|
|
69
|
+
*/
|
|
70
|
+
class LiteralGeoPointValue {
|
|
71
|
+
constructor(options) {
|
|
72
|
+
this.geoPointValue = options.geoPointValue;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.LiteralGeoPointValue = LiteralGeoPointValue;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FirestoreDocument, FirestoreFieldValue, FirestoreResponse } from "../types";
|
|
1
|
+
import { CommitWrite, FieldTransform, FirestoreDocument, FirestoreFieldValue, FirestoreResponse } from "../types";
|
|
2
2
|
/**
|
|
3
3
|
* JSの値をFirestore形式に変換する
|
|
4
4
|
* @param value 変換する値
|
|
@@ -17,6 +17,36 @@ export declare function convertFromFirestoreValue(firestoreValue: FirestoreField
|
|
|
17
17
|
* @returns Firestoreドキュメント
|
|
18
18
|
*/
|
|
19
19
|
export declare function convertToFirestoreDocument(data: Record<string, any>): FirestoreDocument;
|
|
20
|
+
/**
|
|
21
|
+
* Split write data into plain field values and Firestore field transforms.
|
|
22
|
+
*
|
|
23
|
+
* `FieldValue` sentinels (e.g. `serverTimestamp()`) are pulled out into
|
|
24
|
+
* transforms keyed by their (escaped, dot-separated) field path; everything
|
|
25
|
+
* else is left untouched in `fields`. Recursion only descends into plain
|
|
26
|
+
* objects, so class instances (Date / references / geo points) are treated as
|
|
27
|
+
* leaves.
|
|
28
|
+
*
|
|
29
|
+
* @param data Write data (JS values, may contain FieldValue sentinels)
|
|
30
|
+
* @param prefix Field-path prefix used while recursing (internal, pre-escaped)
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractFieldTransforms(data: Record<string, any>, prefix?: string): {
|
|
33
|
+
fields: Record<string, any>;
|
|
34
|
+
transforms: FieldTransform[];
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Build a single `documents:commit` write that updates a document and applies
|
|
38
|
+
* field transforms. `updateTransforms` / `currentDocument` are only included
|
|
39
|
+
* when relevant.
|
|
40
|
+
*
|
|
41
|
+
* @param documentName Full resource name (projects/.../documents/<path>)
|
|
42
|
+
* @param fields Already-converted Firestore field values
|
|
43
|
+
* @param transforms Field transforms to apply after the update
|
|
44
|
+
* @param currentDocument Optional precondition (e.g. `{ exists: false }`)
|
|
45
|
+
*/
|
|
46
|
+
export declare function buildCommitWrite(documentName: string, fields: Record<string, FirestoreFieldValue>, transforms: FieldTransform[], currentDocument?: {
|
|
47
|
+
exists?: boolean;
|
|
48
|
+
updateTime?: string;
|
|
49
|
+
}): CommitWrite;
|
|
20
50
|
/**
|
|
21
51
|
* Firestoreドキュメントをオブジェクトに変換
|
|
22
52
|
* @param doc Firestoreレスポンス
|