From 0b957fd3425564a07cbd8bdc3ca22a16bac55aa5 Mon Sep 17 00:00:00 2001 From: Jeff Mendez Date: Sat, 10 Jul 2021 09:00:02 -0400 Subject: [PATCH] add transaction rollback --- config.ts | 1 - firestore.test.ts | 7 ++----- firestore.ts | 17 +++++++++++++++++ types.ts | 10 ++++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/config.ts b/config.ts index 9312208..577cff3 100644 --- a/config.ts +++ b/config.ts @@ -123,7 +123,6 @@ const setTokenFromEmailPassword = async ( returnSecureToken: true, }; } - const firebase = await fetch(`https://${baseUrl}?key=${key ?? projectkey}`, { headers: { contentType: "application/json", diff --git a/firestore.test.ts b/firestore.test.ts index 69b6c3a..31a72c4 100644 --- a/firestore.test.ts +++ b/firestore.test.ts @@ -23,7 +23,6 @@ Deno.test({ lastname: { stringValue: "Jeff" }, }, }); - if (d.error?.status === "ALREADY_EXISTS") { assertEquals(d.error.code, 409); } else { @@ -107,6 +106,7 @@ Deno.test({ lastname: { stringValue: "Jeff" }, }, }); + assertEquals(d.fields.lastname.stringValue, "Jeff"); }, }); @@ -114,11 +114,8 @@ Deno.test({ // Deno.test({ // name: "firestore should begin and commit transaction", // fn: async () => { -// const d = await firestore.beginTransaction({ -// transaction: "transaction_id", -// }); +// const d = await firestore.beginTransaction({}); // assertEquals(typeof d.id, "string"); - // const c = await firestore.commitTransaction({ // ...body, // transaction: d.id, diff --git a/firestore.ts b/firestore.ts index 10608c6..d90af2b 100644 --- a/firestore.ts +++ b/firestore.ts @@ -10,6 +10,7 @@ import type { GetDocument, FireEvents, RequestInterface, + RollBack, UpdateDocument, } from "./types.ts"; @@ -171,6 +172,16 @@ const fireMethods = { }, }); }, + rollback: async ({ transaction, authorization }: RollBack) => { + return await client.request({ + method: "POST", + url: "documents:rollback", + authorization, + reqBody: { + transaction, + }, + }); + }, }; class FireStore { @@ -235,6 +246,12 @@ class FireStore { const res = await fireMethods.importDocuments(args); await this.log({ ...args, res }); + return res; + } + async rollback(args: RollBack) { + const res = await fireMethods.rollback(args); + await this.log({ ...args, res }); + return res; } } diff --git a/types.ts b/types.ts index 91a9e2a..b936d7a 100644 --- a/types.ts +++ b/types.ts @@ -1,8 +1,8 @@ export interface TransactionOptions { - readOnly: { + readOnly?: { readTime: string; }; - readWrite: { + readWrite?: { retryTransaction: string; }; } @@ -116,6 +116,7 @@ export interface FireResponse { documents: Document[]; fields: MapValue["fields"]; error?: FireError; + id?: string; } export interface RequestInterface extends FireRequest, Partial {} @@ -179,3 +180,8 @@ export type ImportExport = { export type MoveDocuments = Pick & ImportExport; + +export interface RollBack { + transaction: string; + authorization: RequestInterface["authorization"]; +}