Skip to content

Commit c8641b1

Browse files
committed
Add Document.path property with Tests. Closes #99
Update Tests to sync with Shields and Github caching. Fix Tests that couldn't fail. Update README and packages.
1 parent 773c237 commit c8641b1

File tree

6 files changed

+200
-90
lines changed

6 files changed

+200
-90
lines changed

.github/README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Firestore for Google Apps Scripts
22

3-
![GitHub release (latest by date)](https://img.shields.io/github/v/release/grahamearley/FirestoreGoogleAppsScript)
3+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/grahamearley/FirestoreGoogleAppsScript)](/grahamearley/FirestoreGoogleAppsScript/releases/latest)
44
[![Google Apps Script](https://img.shields.io/badge/google%20apps%20script-v8-%234285f4)](https://developers.google.com/apps-script/guides/v8-runtime)
55
[![TypeScript](https://img.shields.io/badge/typescript-3.9.5-%23294E80)](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html)
66
[![clasp](https://img.shields.io/badge/built%20with-clasp-4285f4.svg)](/google/clasp)
@@ -16,12 +16,11 @@ This library allows a user (or service account) to authenticate with Firestore a
1616

1717
Read how this project was started [here](http://grahamearley.website/blog/2017/10/18/firestore-in-google-apps-script.html).
1818

19-
As of **v27**, this project has been updated to use the [GAS V8 runtime](https://developers.google.com/apps-script/guides/v8-runtime) with [Typescript](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html)! This introduces a number of [breaking changes](#breaking-changes).
19+
As of **v27**, this project has been updated to use the [GAS V8 runtime](https://developers.google.com/apps-script/guides/v8-runtime) with [Typescript](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html)! This introduces a number of [breaking changes](#breaking-changes). Scripts utilizing the old Rhino runtime must use **v26**.
2020

2121
## Installation
2222
In the Google online script editor, select the `Resources` menu item and choose `Libraries...`. In the "Add a library" input box, enter **`1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw`** and click "Add." Choose the most recent version number.
2323

24-
2524
## Quick start
2625
#### Creating a service account
2726
The easiest way to use this library is to create a Google Service Account for your application and give it read/write access to your datastore. Giving a service account access to your datastore is like giving access to a user's account, but this account is strictly used by your script, not by a person.
@@ -97,7 +96,6 @@ You can retrieve documents by calling the `getDocument` function:
9796

9897
```javascript
9998
const documentWithMetadata = firestore.getDocument("FirstCollection/FirstDocument");
100-
const storedObject = documentWithMetadata.obj;
10199
```
102100

103101
You can also retrieve all documents within a collection by using the `getDocuments` function:
@@ -112,6 +110,19 @@ You can also get specific documents by providing an array of document names
112110
const someDocuments = firestore.getDocuments("FirstCollection", ["Doc1", "Doc2", "Doc3"]);
113111
```
114112

113+
##### Getting Document Properties
114+
You can access various properties of documents from Firestore:
115+
116+
```javascript
117+
const doc = firestore.getDocument("My Collection/My Document");
118+
const originalData = doc.obj // Original database object (your stored data)
119+
const readTime = doc.read // Date Object of the Read time from database
120+
const updateTime = doc.updated // Date Object of the Updated time from database
121+
const createdTime = doc.created // Date Object of the Created time from database
122+
const name = doc.name // Full document path (projects/projName/databases/(default)/documents/My Collection/My Document)
123+
const path = doc.path // Local document path (My Collection/My Document)
124+
```
125+
115126
##### Getting Documents (Advanced method using Query)
116127
If more specific queries need to be performed, you can use the `query` function followed by an `.Execute()` invocation to get that data:
117128

@@ -140,13 +151,23 @@ const documents3_4_5_6 = firestore.query("FirstCollection").Range(3, 7).Execute(
140151

141152
See other library methods and details [in the wiki](/grahamearley/FirestoreGoogleAppsScript/wiki/).
142153

154+
### Frequently Asked Questions
155+
- **I'm getting the following error:**
156+
> Missing ; before statement. at \[unknown function\](Auth:12)
157+
158+
This is because this library has been updated to utilize the new [V8 Engine](https://developers.google.com/apps-script/guides/v8-runtime), and classes are not supported in the Rhino Engine.
159+
You can either:
160+
1. [Migrate your script to use V8](https://developers.google.com/apps-script/guides/v8-runtime/migration), or
161+
1. Use the last Rhino version of this library (**v26**).
162+
163+
143164
### Breaking Changes
144-
* **v27:** Library rewritten with Typescript and Prettier.
145-
* Query function names have been capitalized (`Select`, `Where`, `OrderBy`, `Limit`, `Offset`, `Range`).
146-
* **All functions return `Document` or `Document[]` types directly from Firebase. Use `document.obj` to extract the raw object.**
147-
* Undo breaking change from v23. `document.createTime` and `document.updateTime` will remain as timestamped strings. However `document.created`, `document.updated`, and `document.read` are Date objects.
148-
* **v23:** When retrieving documents the createTime and updateTime document properties are JS Date objects and not Timestamp Strings.
149-
* **v16:** **Removed:** `createDocumentWithId(documentId, path, fields)`
165+
- **v27:** Library rewritten with Typescript and Prettier.
166+
- Query function names have been capitalized (`Select`, `Where`, `OrderBy`, `Limit`, `Offset`, `Range`).
167+
- **All functions return `Document` or `Document[]` types directly from Firebase. Use `document.obj` to extract the raw object.**
168+
- Undo breaking change from v23. `document.createTime` and `document.updateTime` will remain as timestamped strings. However `document.created`, `document.updated`, and `document.read` are Date objects.
169+
- **v23:** When retrieving documents the createTime and updateTime document properties are JS Date objects and not Timestamp Strings.
170+
- **v16:** **Removed:** `createDocumentWithId(documentId, path, fields)`
150171
> Utilize `createDocument(path + '/' + documentId, fields)` instead to create a document with a specific ID.
151172
152173
## Contributions

Document.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class Document implements FirestoreAPI.Document, FirestoreAPI.MapValue {
3939
return this.readTime ? Document.unwrapDate(this.readTime) : new Date();
4040
}
4141

42+
get path() {
43+
return this.name?.match(Util_.regexPath)![1];
44+
}
45+
4246
/**
4347
* Extract fields from a Firestore document.
4448
*

0 commit comments

Comments
 (0)