finsweet / fireworkers Goto Github PK
View Code? Open in Web Editor NEWUse Cloud Firestore inside Cloudflare Workers.
Use Cloud Firestore inside Cloudflare Workers.
I've noticed a discrepancy in the endpoint URL between version v0.1.0 and v0.2.0. The URL structure seems to have changed, leading to potential issues.
In v0.1.0, the endpoint was: https://firestore.googleapis.com/v1/projects/{project_id}/databases/(default)/documents/{path}
In v0.2.0, it has changed to: https://firestore.googleapis.com/v1/projects/{project_id}/databases/(default)/{path}
The endpoint URL should remain consistent across versions, or a change should be clearly documented.
The endpoint URL has changed between v0.1.0 and v0.2.0 without clear documentation.
get
method.get
method.I'm using this with these values:
const db = await Firestore.init({
uid: nanoid(16),
project_id: serviceAccount.project_id,
client_email: serviceAccount.client_email,
private_key: serviceAccount.private_key,
private_key_id: serviceAccount.private_key_id,
claims: {
premium_account: true
}
});
and if i try to create document. I get ReferenceError: CryptoKey is not defined in node_modules/dist/index.js.
To implement this functionality, it may be necessary to transmit the 'transform' property. https://gist.github.com/mikelehen/6c8f67164f18b7b49135231f1cc0cf29
Hi,
I have the need to query all of the items in a collection or sub collection. Firestore.get
only returns a single document, but get can also return an array of documents.
I ended up forking your repo and adding a getCollection to add a loop when parsing the json results.
I'm happy to make a pull request if you think it would be useful addition.
/* eslint-disable @typescript-eslint/no-explicit-any */
import { extract_fields_from_document } from "./fields";
import type * as Firestore from "./types";
import { get_firestore_endpoint } from "./utils";
/**
* Gets a collection of documents from Firestore.
* Reference: {@link https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/get}
*
* @param firestore The DB instance.
* @param collection_path The collection path.
*/
export const getCollection = async <Fields extends Record<string, any>>(
{ jwt, project_id }: Firestore.DB,
...args: string[]
) => {
const endpoint = get_firestore_endpoint(project_id);
const collection_path = args.join("/");
const response = await fetch(`${endpoint}/${collection_path}`, {
headers: {
Authorization: `Bearer ${jwt}`,
},
});
const data: Firestore.GetCollectionResponse = await response.json();
if ("error" in data) throw new Error(data.error.message);
const documents = data.documents.map((doc) =>
extract_fields_from_document<Fields>(doc),
);
return documents;
};
If todos
has fileds: name
, completed
, createdTime
... etc
const updatedTodo = await Firestore.update(db, 'todos', 'aDyjLiTViX1G7HyF74Ax', {
completed: false,
});
executing the above code will clear the name
, createdTime
fields, result in this todo
document has only one field: completed
.
I believe you missed the updateMask.fieldPaths=completed
query parameter
Hi there! Currently the underlying Firebase types (e.g. StructuredQuery
) are not exported by the library, causing difficulties when using TypeScript (for example if I want to put a StructuredQuery
in a variable but still get auto-complete and type checking, by annotating my variable with the type).
Is there any reason behind hiding them, or could this be done?
And thanks for this amazing library!
Hi, thanks for this library! It's helped a lot :)
I noticed however, the JWT's generated by the init
function are invalid, notably that they have an iat
of 0
and exp
of 3600
.
Notably,
const db = await Firestore.init({
uid: 'anything',
project_id: authConfig.projectId,
client_email: serviceUser.client_email,
private_key: serviceUser.private_key,
private_key_id: serviceUser.private_key_id
})
const a = await Firestore.get(db, 'users')
Would return the following error:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
I don't know enough about JOSE to help debug or fix the actual issue, but I was able to get the library working with a JWT I generate myself using @tsndr/cloudflare-worker-jwt
(https://github.com/tsndr/cloudflare-worker-jwt):
const now = new Date().getTime() / 1000
const JWT = await jwt.sign({
iss: serviceUser.client_email,
sub: serviceUser.client_email,
aud: "https://firestore.googleapis.com/",
iat: now,
exp: now + 3600,
}, serviceUser.private_key, {algorithm: "RS256"})
Passing this into my own DB
object with the public key gives me better responses. Putting the two key's into jwt.io side by side reveals:
Firestore.init |
jwt.sign |
---|---|
I'm happy to open a PR that replaces jose
with @tsndr/cloudflare-worker-jwt
if you're okay with the library change.
src/worker.js:1:27: 1 โ import * as Firestore from 'fireworkers'; โต ~~~~~~~~~~~~~ You can mark the path "fireworkers" as external to exclude it from the bundle, which will remove this error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.