Skip to content
This repository was archived by the owner on Oct 18, 2025. It is now read-only.

Commit 7db2bb1

Browse files
committed
v.1.0.0: createDocument and updateDocument return a parsed document instead of the raw data from Firestore
1 parent 9fa7428 commit 7db2bb1

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ fetching/updating documents.
6969
const document = await firestore.createDocument("my-collection", {
7070
name: "John Doe",
7171
});
72+
73+
// document = {
74+
// _id: 'XXX',
75+
// _path: 'my-collection/XXX',
76+
// name: 'John Doe',
77+
// }
7278
```
7379

80+
Returns the created document with an additional `_id` and `_path` fields, which
81+
are the document ID and path respectively.
82+
7483
### List all documents in a collection
7584

7685
```typescript
@@ -79,17 +88,21 @@ const documents = await firestore.listDocumentsInCollection("my-collection");
7988
// documents = ['document1', 'document2']
8089
```
8190

91+
Returns an array of document IDs.
92+
8293
### Fetch all documents in a collection
8394

84-
Note: The fetched documents will have an additional `_id` field, which is the document ID.
95+
Note: The fetched documents will have an additional `_id` field, which is the
96+
document ID.
8597

8698
```typescript
8799
const collection = await firestore.getDocumentsInCollection("my-collection");
88100
```
89101

90102
### Fetch all documents in a collection with a filter
91103

92-
Note: The fetched documents will have an additional `_id` field, which is the document ID.
104+
Note: The fetched documents will have an additional `_id` field, which is the
105+
document ID.
93106

94107
```typescript
95108
// Import the FirestoreOperator enum
@@ -137,8 +150,8 @@ const document = await firestore.getDocument("my-collection/my-document");
137150

138151
```typescript
139152
await firestore.updateDocument("my-collection/my-document", {
140-
name: "John Doe"
141-
})
153+
name: "John Doe",
154+
});
142155

143156
// ...or with specific update fields
144157

@@ -151,3 +164,25 @@ await firestore.updateDocument("my-collection/my-document", {
151164
},
152165
}, ["name", "address.city"]); // you can use nested fields as well
153166
```
167+
168+
Returns the updated document with an additional `_id` and `_path` fields.
169+
170+
## Using timestamps
171+
172+
Firestore timestamps are not implemented in this library. If you need to use
173+
timestamps, here's how you can do it:
174+
175+
```bash
176+
deno add npm:firebase/firestore
177+
```
178+
179+
...and then use the `Timestamp` class from the `firebase/firestore` package:
180+
181+
```typescript
182+
import { Timestamp } from "npm:firebase/firestore";
183+
184+
// e.g.
185+
const timestamp = Timestamp.now();
186+
```
187+
188+
> Note: The same principle applies to `UnionArray` field operations, etc.

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@koiztech/firestore-admin",
3-
"version": "0.1.1",
3+
"version": "1.0.0",
44
"exports": "./mod.ts",
55
"tasks": {
66
"dev": "deno run --watch --env mod.ts",

mod.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
import * as googleAuth from "google-auth-library";
33

44
export enum FirestoreOperator {
5-
LESS_THAN = 'LESS_THAN',
6-
LESS_THAN_OR_EQUAL = 'LESS_THAN_OR_EQUAL',
7-
GREATER_THAN = 'GREATER_THAN',
8-
GREATER_THAN_OR_EQUAL = 'GREATER_THAN_OR_EQUAL',
9-
EQUAL = 'EQUAL',
10-
NOT_EQUAL = 'NOT_EQUAL',
11-
ARRAY_CONTAINS = 'ARRAY_CONTAINS',
12-
IN = 'IN',
13-
ARRAY_CONTAINS_ANY = 'ARRAY_CONTAINS_ANY',
14-
NOT_IN = 'NOT_IN',
15-
IS_NAN = 'IS_NAN',
16-
IS_NULL = 'IS_NULL',
17-
IS_NOT_NAN = 'IS_NOT_NAN',
18-
IS_NOT_NULL = 'IS_NOT_NULL'
5+
LESS_THAN = "LESS_THAN",
6+
LESS_THAN_OR_EQUAL = "LESS_THAN_OR_EQUAL",
7+
GREATER_THAN = "GREATER_THAN",
8+
GREATER_THAN_OR_EQUAL = "GREATER_THAN_OR_EQUAL",
9+
EQUAL = "EQUAL",
10+
NOT_EQUAL = "NOT_EQUAL",
11+
ARRAY_CONTAINS = "ARRAY_CONTAINS",
12+
IN = "IN",
13+
ARRAY_CONTAINS_ANY = "ARRAY_CONTAINS_ANY",
14+
NOT_IN = "NOT_IN",
15+
IS_NAN = "IS_NAN",
16+
IS_NULL = "IS_NULL",
17+
IS_NOT_NAN = "IS_NOT_NAN",
18+
IS_NOT_NULL = "IS_NOT_NULL",
1919
}
2020

2121
export class FirestoreAdminClient {
@@ -91,7 +91,12 @@ export class FirestoreAdminClient {
9191
if (responseData?.error) {
9292
this.errorHandler(responseData.error, "createDocument");
9393
}
94-
return responseData;
94+
const jsonDocument = this.documentToJson(responseData.fields);
95+
return {
96+
_id: responseData.name.split(`/`).pop(),
97+
_path: responseData.name.split("documents/")[1],
98+
...jsonDocument,
99+
};
95100
}
96101

97102
/**
@@ -155,16 +160,16 @@ export class FirestoreAdminClient {
155160
if (options.where) {
156161
structuredQuery.where = {
157162
compositeFilter: {
158-
op: 'AND',
163+
op: "AND",
159164
filters: options.where.filters.map(([field, op, value]) => ({
160165
fieldFilter: {
161166
field: { fieldPath: field },
162167
op,
163-
value: this.encodeValue(value)
164-
}
165-
}))
166-
}
167-
}
168+
value: this.encodeValue(value),
169+
},
170+
})),
171+
},
172+
};
168173
}
169174

170175
if (options.orderBy) {
@@ -195,8 +200,13 @@ export class FirestoreAdminClient {
195200
const data: any = await response.json();
196201

197202
if (data?.error || data?.[0]?.error) {
198-
this.errorHandler(data.error ?? data?.[0]?.error, `${this.firestoreBaseUrl}/${path}:runQuery`);
199-
console.log({ extendedDetails: data.error?.details ?? data?.[0]?.error?.details });
203+
this.errorHandler(
204+
data.error ?? data?.[0]?.error,
205+
`${this.firestoreBaseUrl}/${path}:runQuery`,
206+
);
207+
console.log({
208+
extendedDetails: data.error?.details ?? data?.[0]?.error?.details,
209+
});
200210
return [];
201211
}
202212

@@ -205,13 +215,11 @@ export class FirestoreAdminClient {
205215
return [];
206216
}
207217

208-
209-
210218
return data.map((doc: any) => {
211219
const docId = doc.document?.name.split(`/`).pop() ?? "unknown";
212-
console.log({docId});
213-
const documentFields = doc.document?.fields || {}
214-
return { ...this.documentToJson(documentFields), _id: docId }
220+
console.log({ docId });
221+
const documentFields = doc.document?.fields || {};
222+
return { ...this.documentToJson(documentFields), _id: docId };
215223
});
216224
} else {
217225
const response = await fetch(`${this.firestoreBaseUrl}/${path}`, {
@@ -226,8 +234,8 @@ export class FirestoreAdminClient {
226234

227235
return data.documents.map((doc: any) => {
228236
const docId = doc.name.split(`/`).pop() ?? "unknown";
229-
const documentFields = doc.fields || {}
230-
return { ...this.documentToJson(documentFields), _id: docId }
237+
const documentFields = doc.fields || {};
238+
return { ...this.documentToJson(documentFields), _id: docId };
231239
});
232240
}
233241
}
@@ -267,7 +275,12 @@ export class FirestoreAdminClient {
267275
if (responseData?.error) {
268276
this.errorHandler(responseData.error, "updateDocument");
269277
}
270-
return responseData;
278+
const jsonDocument = this.documentToJson(responseData.fields);
279+
return {
280+
_id: responseData.name.split(`/`).pop(),
281+
_path: responseData.name.split("documents/")[1],
282+
...jsonDocument,
283+
};
271284
}
272285

273286
private documentToJson(fields: any): any {

0 commit comments

Comments
 (0)