Skip to content

Commit

Permalink
Add Document.path property with Tests. Closes #99
Browse files Browse the repository at this point in the history
Update Tests to sync with Shields and Github caching.
Fix Tests that couldn't fail.
Update README and packages.
  • Loading branch information
LaughDonor committed Jul 9, 2020
1 parent 773c237 commit c8641b1
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 90 deletions.
41 changes: 31 additions & 10 deletions .github/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Firestore for Google Apps Scripts

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

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

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).
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**.

## Installation
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.


## Quick start
#### Creating a service account
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.
Expand Down Expand Up @@ -97,7 +96,6 @@ You can retrieve documents by calling the `getDocument` function:

```javascript
const documentWithMetadata = firestore.getDocument("FirstCollection/FirstDocument");
const storedObject = documentWithMetadata.obj;
```

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

##### Getting Document Properties
You can access various properties of documents from Firestore:

```javascript
const doc = firestore.getDocument("My Collection/My Document");
const originalData = doc.obj // Original database object (your stored data)
const readTime = doc.read // Date Object of the Read time from database
const updateTime = doc.updated // Date Object of the Updated time from database
const createdTime = doc.created // Date Object of the Created time from database
const name = doc.name // Full document path (projects/projName/databases/(default)/documents/My Collection/My Document)
const path = doc.path // Local document path (My Collection/My Document)
```

##### Getting Documents (Advanced method using Query)
If more specific queries need to be performed, you can use the `query` function followed by an `.Execute()` invocation to get that data:

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

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

### Frequently Asked Questions
- **I'm getting the following error:**
> Missing ; before statement. at \[unknown function\](Auth:12)
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.
You can either:
1. [Migrate your script to use V8](https://developers.google.com/apps-script/guides/v8-runtime/migration), or
1. Use the last Rhino version of this library (**v26**).


### Breaking Changes
* **v27:** Library rewritten with Typescript and Prettier.
* Query function names have been capitalized (`Select`, `Where`, `OrderBy`, `Limit`, `Offset`, `Range`).
* **All functions return `Document` or `Document[]` types directly from Firebase. Use `document.obj` to extract the raw object.**
* 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.
* **v23:** When retrieving documents the createTime and updateTime document properties are JS Date objects and not Timestamp Strings.
* **v16:** **Removed:** `createDocumentWithId(documentId, path, fields)`
- **v27:** Library rewritten with Typescript and Prettier.
- Query function names have been capitalized (`Select`, `Where`, `OrderBy`, `Limit`, `Offset`, `Range`).
- **All functions return `Document` or `Document[]` types directly from Firebase. Use `document.obj` to extract the raw object.**
- 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.
- **v23:** When retrieving documents the createTime and updateTime document properties are JS Date objects and not Timestamp Strings.
- **v16:** **Removed:** `createDocumentWithId(documentId, path, fields)`
> Utilize `createDocument(path + '/' + documentId, fields)` instead to create a document with a specific ID.
## Contributions
Expand Down
4 changes: 4 additions & 0 deletions Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Document implements FirestoreAPI.Document, FirestoreAPI.MapValue {
return this.readTime ? Document.unwrapDate(this.readTime) : new Date();
}

get path() {
return this.name?.match(Util_.regexPath)![1];
}

/**
* Extract fields from a Firestore document.
*
Expand Down

7 comments on commit c8641b1

@lahirue
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting this error even after updating everything to V8

TypeError: firestore.query(...).where is not a function

@lahirue
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried Capitalize word as well. This issue occur after 26th version

TypeError: firestore.query(...).Where(...).execute is not a function

@LaughDonor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try firestore.query(...).Where(...).Execute() with capital "Execute"?

@lahirue
Copy link

@lahirue lahirue commented on c8641b1 Jul 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you and it worked !

One more thing its not related to this issue,

How do I update more than one document once. Lets say, I have to update 10 documents. It is not good to loop the given updated query 10 times.
This is a most common use case and firestore has that capability. Can I do that using this library ?

One more suggestion. It would be nice, if there is any place to add public comments 👍

@LaughDonor
Copy link
Collaborator Author

@LaughDonor LaughDonor commented on c8641b1 Jul 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have already mentioned it before your comment as well.

Your last comment specifies "Execute" that is in lowercase. Since Javascript is case-sensitive, I take that into consideration. Since you commented here, you should see lots of Tests that all work. One example of a working test uses your same query.

How do I update more than one document once. Lets say, I have to update 10 documents. It is not good to loop the given updated query 10 times.
This is a most common use case and firestore has that capability. Can I do that using this library ?

You need to use a loop at the moment. Batched Writes (#65) is still an open issue.

One more suggestion. It would be nice, if there is any place to add public comments 👍

You should be opening an issue for a place to talk about any issue/comment/suggestion/bug. 💯

@lahirue
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your quick response.
We are planing to use this library and hope that batch methods will updated soon.
I would like to talk with your personally if you don't mind. Can you provide me any contact ?

@LaughDonor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your quick response.
We are planing to use this library and hope that batch methods will updated soon.
I would like to talk with your personally if you don't mind. Can you provide me any contact ?

That's great news! However, I can't currently dedicate the time to add that feature in myself. As this is a community-driven project, anyone following the guidelines can contribute. And any questions you have can be addressed to the community as a whole (thousands of active users).

Please sign in to comment.