Skip to content

Commit

Permalink
add transaction rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Jul 10, 2021
1 parent d8d4c3c commit 0b957fd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 0 additions & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ const setTokenFromEmailPassword = async (
returnSecureToken: true,
};
}

const firebase = await fetch(`https://${baseUrl}?key=${key ?? projectkey}`, {
headers: {
contentType: "application/json",
Expand Down
7 changes: 2 additions & 5 deletions firestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Deno.test({
lastname: { stringValue: "Jeff" },
},
});

if (d.error?.status === "ALREADY_EXISTS") {
assertEquals(d.error.code, 409);
} else {
Expand Down Expand Up @@ -107,18 +106,16 @@ Deno.test({
lastname: { stringValue: "Jeff" },
},
});

assertEquals(d.fields.lastname.stringValue, "Jeff");
},
});

// 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,
Expand Down
17 changes: 17 additions & 0 deletions firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
GetDocument,
FireEvents,
RequestInterface,
RollBack,
UpdateDocument,
} from "./types.ts";

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand Down
10 changes: 8 additions & 2 deletions types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface TransactionOptions {
readOnly: {
readOnly?: {
readTime: string;
};
readWrite: {
readWrite?: {
retryTransaction: string;
};
}
Expand Down Expand Up @@ -116,6 +116,7 @@ export interface FireResponse {
documents: Document[];
fields: MapValue["fields"];
error?: FireError;
id?: string;
}

export interface RequestInterface extends FireRequest, Partial<FetchRequest> {}
Expand Down Expand Up @@ -179,3 +180,8 @@ export type ImportExport = {

export type MoveDocuments = Pick<RequestInterface, "authorization"> &
ImportExport;

export interface RollBack {
transaction: string;
authorization: RequestInterface["authorization"];
}

0 comments on commit 0b957fd

Please sign in to comment.