From 8482733ae8d6673eb3d42eeadbc8a0f0f2d5e791 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 24 Jul 2017 17:36:54 -0700 Subject: [PATCH 1/2] fix(database): Adding toJSON() method to TransactionResult --- src/database/api/Reference.ts | 2 +- src/database/api/TransactionResult.ts | 9 +++++++++ tests/database/transaction.test.ts | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/database/api/Reference.ts b/src/database/api/Reference.ts index d1e2f890648..f86c4c8ac34 100644 --- a/src/database/api/Reference.ts +++ b/src/database/api/Reference.ts @@ -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 { + applyLocally?: boolean): Promise { validateArgCount('Reference.transaction', 1, 3, arguments.length); validateWritablePath('Reference.transaction', this.path); validateCallback('Reference.transaction', 1, transactionUpdate, false); diff --git a/src/database/api/TransactionResult.ts b/src/database/api/TransactionResult.ts index 147c4789977..81715f1af3f 100644 --- a/src/database/api/TransactionResult.ts +++ b/src/database/api/TransactionResult.ts @@ -15,6 +15,7 @@ */ import { DataSnapshot } from './DataSnapshot'; +import { validateArgCount } from '../../utils/validation'; export class TransactionResult { /** @@ -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 { + validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length); + return { committed: this.committed, snapshot: this.snapshot.toJSON() }; + } + } \ No newline at end of file diff --git a/tests/database/transaction.test.ts b/tests/database/transaction.test.ts index 39db68bdd4d..38bc584d695 100644 --- a/tests/database/transaction.test.ts +++ b/tests/database/transaction.test.ts @@ -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); From 420219912cfe3fc92585f4d54ccf4b8678fd0891 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 25 Jul 2017 09:18:19 -0700 Subject: [PATCH 2/2] fix(database): Making return type of toJSON explicit --- src/database/api/TransactionResult.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/database/api/TransactionResult.ts b/src/database/api/TransactionResult.ts index 81715f1af3f..b5d5608c555 100644 --- a/src/database/api/TransactionResult.ts +++ b/src/database/api/TransactionResult.ts @@ -31,7 +31,7 @@ export class TransactionResult { // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary // for end-users - toJSON(): any { + toJSON(): object { validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length); return { committed: this.committed, snapshot: this.snapshot.toJSON() }; }