Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/database/api/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class Reference extends Query {
*/
transaction(transactionUpdate: (a: any) => any,
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
applyLocally?: boolean): Promise<any> {
applyLocally?: boolean): Promise<TransactionResult> {
validateArgCount('Reference.transaction', 1, 3, arguments.length);
validateWritablePath('Reference.transaction', this.path);
validateCallback('Reference.transaction', 1, transactionUpdate, false);
Expand Down
9 changes: 9 additions & 0 deletions src/database/api/TransactionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { DataSnapshot } from './DataSnapshot';
import { validateArgCount } from '../../utils/validation';

export class TransactionResult {
/**
Expand All @@ -27,4 +28,12 @@ export class TransactionResult {
constructor(public committed: boolean, public snapshot: DataSnapshot) {

}

// Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
// for end-users
toJSON(): any {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really strive not to use "any". I think we can use "object" here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
return { committed: this.committed, snapshot: this.snapshot.toJSON() };
}

}
10 changes: 10 additions & 0 deletions tests/database/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('Transaction Tests', function() {
expect(eventHelper.waiter()).to.equal(true);
});

it('Transaction result can be converted to JSON.', function() {
const node = (getRandomNode() as Reference);

return node.transaction(() => {
return 42;
}).then(transactionResult => {
expect(transactionResult.toJSON()).to.deep.equal({ committed: true, snapshot: 42 });
});
});

it('Non-aborted transaction sets committed to true in callback.', function(done) {
const node = (getRandomNode() as Reference);

Expand Down