Skip to content

Commit d9e0ce9

Browse files
fix(database): Adding toJSON() method to TransactionResult (#104)
* fix(database): Adding toJSON() method to TransactionResult * fix(database): Making return type of toJSON explicit
1 parent 1c300d9 commit d9e0ce9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/database/api/Reference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export class Reference extends Query {
206206
*/
207207
transaction(transactionUpdate: (a: any) => any,
208208
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
209-
applyLocally?: boolean): Promise<any> {
209+
applyLocally?: boolean): Promise<TransactionResult> {
210210
validateArgCount('Reference.transaction', 1, 3, arguments.length);
211211
validateWritablePath('Reference.transaction', this.path);
212212
validateCallback('Reference.transaction', 1, transactionUpdate, false);

src/database/api/TransactionResult.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { DataSnapshot } from './DataSnapshot';
18+
import { validateArgCount } from '../../utils/validation';
1819

1920
export class TransactionResult {
2021
/**
@@ -27,4 +28,12 @@ export class TransactionResult {
2728
constructor(public committed: boolean, public snapshot: DataSnapshot) {
2829

2930
}
31+
32+
// Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
33+
// for end-users
34+
toJSON(): object {
35+
validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
36+
return { committed: this.committed, snapshot: this.snapshot.toJSON() };
37+
}
38+
3039
}

tests/database/transaction.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ describe('Transaction Tests', function() {
5656
expect(eventHelper.waiter()).to.equal(true);
5757
});
5858

59+
it('Transaction result can be converted to JSON.', function() {
60+
const node = (getRandomNode() as Reference);
61+
62+
return node.transaction(() => {
63+
return 42;
64+
}).then(transactionResult => {
65+
expect(transactionResult.toJSON()).to.deep.equal({ committed: true, snapshot: 42 });
66+
});
67+
});
68+
5969
it('Non-aborted transaction sets committed to true in callback.', function(done) {
6070
const node = (getRandomNode() as Reference);
6171

0 commit comments

Comments
 (0)