generated from finsweet/developer-starter
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bc2f2d
commit 945dc3c
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
'fireworkers': minor | ||
--- | ||
|
||
**This release deliberately contains backwards-incompatible changes**. To avoid automatically picking up releases like this, you should either be pinning the exact version of `fireworkers` in your `package.json` file (recommended) or be using a version range syntax that only accepts patch upgrades such as `^0.2.0` or `~0.2.0`. See npm's documentation about [semver](https://docs.npmjs.com/cli/v6/using-npm/semver/) for more information. | ||
|
||
- feat: add new `Firestore.set()` method that matches the behavior of the SDK's [setDoc](https://firebase.google.com/docs/reference/js/firestore_.md#setdoc). | ||
- fix: update `Firestore.update()` to match the behavior of the SDK's [updateDoc](https://firebase.google.com/docs/reference/js/firestore_.md#updatedoc): | ||
- Fields will be merged instead of overriding the entire document. | ||
- Operations will fail if the document doesn't exist. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { create_document_from_fields, extract_fields_from_document } from './fields'; | ||
import type * as Firestore from './types'; | ||
import { get_firestore_endpoint } from './utils'; | ||
|
||
type Options = { | ||
merge?: boolean; | ||
}; | ||
|
||
/** | ||
* Writes to a document. If the document does not yet exist, it will be created. | ||
* Similar to the SDK's [setDoc](https://firebase.google.com/docs/reference/js/firestore_.md#setdoc). | ||
* | ||
* Reference: {@link https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/patch} | ||
* | ||
* @param firestore | ||
* @param document_path | ||
* @param fields | ||
*/ | ||
export const set = async <Fields extends Record<string, any>>( | ||
{ jwt, project_id }: Firestore.DB, | ||
...args: [...string[], Fields] | [...string[], Fields, Options] | ||
) => { | ||
let paths; | ||
let fields; | ||
let options; | ||
|
||
if (typeof args.at(-2) === 'object') { | ||
paths = args.slice(0, -2) as string[]; | ||
fields = args.at(-2) as Fields; | ||
options = args.at(-1) as Options; | ||
} else { | ||
paths = args.slice(0, -1) as string[]; | ||
fields = args.at(-1) as Fields; | ||
} | ||
|
||
const payload = create_document_from_fields(fields); | ||
const endpoint = get_firestore_endpoint(project_id, paths); | ||
|
||
if (options?.merge) { | ||
// Ensure that the fields are updated without overwriting the rest of the document | ||
for (const key in fields) { | ||
endpoint.searchParams.append('updateMask.fieldPaths', key); | ||
} | ||
} | ||
|
||
const response = await fetch(endpoint, { | ||
method: 'PATCH', | ||
body: JSON.stringify(payload), | ||
headers: { | ||
Authorization: `Bearer ${jwt}`, | ||
}, | ||
}); | ||
|
||
const data: Firestore.GetResponse = await response.json(); | ||
|
||
if ('error' in data) throw new Error(data.error.message); | ||
|
||
const document = extract_fields_from_document<Fields>(data); | ||
return document; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters