Skip to content

Commit

Permalink
Fix getAll function signature to allow array destructuring (#515)
Browse files Browse the repository at this point in the history
Fixes #501.
  • Loading branch information
cxam authored and mikelehen committed Jan 9, 2019
1 parent 8c2ccf3 commit 87024b3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
17 changes: 9 additions & 8 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,12 @@ export class Firestore {
/**
* Retrieves multiple documents from Firestore.
*
* @param {DocumentReference} documentRef A `DocumentReference` to receive.
* @param {Array.<DocumentReference|ReadOptions>} moreDocumentRefsOrReadOptions
* Additional `DocumentReferences` to receive, followed by an optional field
* The first argument is required and must be of type `DocumentReference`
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
Expand All @@ -703,14 +706,12 @@ export class Firestore {
* console.log(`Second document: ${JSON.stringify(docs[1])}`);
* });
*/
getAll(
documentRef: DocumentReference,
...moreDocumentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
getAll(...documentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
Promise<DocumentSnapshot[]> {
this._validator.minNumberOfArguments('Firestore.getAll', arguments, 1);

const {documents, fieldMask} = parseGetAllArguments(
this._validator, [documentRef, ...moreDocumentRefsOrReadOptions]);
const {documents, fieldMask} =
parseGetAllArguments(this._validator, documentRefsOrReadOptions);
return this.getAll_(documents, fieldMask, requestTag());
}

Expand Down
17 changes: 9 additions & 8 deletions dev/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ export class Transaction {
* Retrieves multiple documents from Firestore. Holds a pessimistic lock on
* all returned documents.
*
* @param {DocumentReference} documentRef A `DocumentReference` to receive.
* @param {Array.<DocumentReference|ReadOptions>} moreDocumentRefsOrReadOptions
* Additional `DocumentReferences` to receive, followed by an optional field
* The first argument is required and must be of type `DocumentReference`
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
Expand All @@ -157,18 +160,16 @@ export class Transaction {
* });
* });
*/
getAll(
documentRef: DocumentReference,
...moreDocumentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
getAll(...documentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
Promise<DocumentSnapshot[]> {
if (!this._writeBatch.isEmpty) {
throw new Error(READ_AFTER_WRITE_ERROR_MSG);
}

this._validator.minNumberOfArguments('Transaction.getAll', arguments, 1);

const {documents, fieldMask} = parseGetAllArguments(
this._validator, [documentRef, ...moreDocumentRefsOrReadOptions]);
const {documents, fieldMask} =
parseGetAllArguments(this._validator, documentRefsOrReadOptions);

return this._firestore.getAll_(
documents, fieldMask, this._requestTag, this._transactionId);
Expand Down
59 changes: 59 additions & 0 deletions dev/system-test/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ describe('Firestore class', () => {
});
});

it('getAll() supports array destructuring', () => {
const ref1 = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
return Promise.all([ref1.set({foo: 'a'}), ref2.set({foo: 'a'})])
.then(() => {
return firestore.getAll(...[ref1, ref2]);
})
.then(docs => {
expect(docs.length).to.equal(2);
});
});

it('getAll() supports field mask', () => {
const ref1 = randomCol.doc('doc1');
return ref1.set({foo: 'a', bar: 'b'})
Expand All @@ -87,6 +99,19 @@ describe('Firestore class', () => {
expect(docs[0].data()).to.deep.equal({foo: 'a'});
});
});

it('getAll() supports array destructuring with field mask', () => {
const ref1 = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
return Promise.all([ref1.set({f: 'a', b: 'b'}), ref2.set({f: 'a', b: 'b'})])
.then(() => {
return firestore.getAll(...[ref1, ref2], {fieldMask: ['f']});
})
.then(docs => {
expect(docs[0].data()).to.deep.equal({f: 'a'});
expect(docs[1].data()).to.deep.equal({f: 'a'});
});
});
});

describe('CollectionReference class', () => {
Expand Down Expand Up @@ -1383,6 +1408,22 @@ describe('Transaction class', () => {
});
});

it('getAll() supports array destructuring', () => {
const ref1 = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
return Promise.all([ref1.set({}), ref2.set({})])
.then(() => {
return firestore.runTransaction(updateFunction => {
return updateFunction.getAll(...[ref1, ref2]).then(docs => {
return Promise.resolve(docs.length);
});
});
})
.then(res => {
expect(res).to.equal(2);
});
});

it('getAll() supports field mask', () => {
const ref1 = randomCol.doc('doc1');
return ref1.set({foo: 'a', bar: 'b'}).then(() => {
Expand All @@ -1397,6 +1438,24 @@ describe('Transaction class', () => {
});
});

it('getAll() supports array destructuring with field mask', () => {
const ref1 = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
return Promise.all([ref1.set({f: 'a', b: 'b'}), ref2.set({f: 'a', b: 'b'})])
.then(() => {
return firestore
.runTransaction(updateFunction => {
return updateFunction
.getAll(...[ref1, ref2], {fieldMask: ['f']})
.then((docs) => docs);
})
.then(docs => {
expect(docs[0].data()).to.deep.equal({f: 'a'});
expect(docs[1].data()).to.deep.equal({f: 'a'});
});
});
});

it('has get() with query', () => {
const ref = randomCol.doc('doc');
const query = randomCol.where('foo', '==', 'bar');
Expand Down
32 changes: 18 additions & 14 deletions types/firestore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,18 @@ declare namespace FirebaseFirestore {
/**
* Retrieves multiple documents from Firestore.
*
* @param documentRef A `DocumentReference` to receive.
* @param moreDocumentRefsOrReadOptions Additional `DocumentReferences` to
* receive, followed by an optional field mask.
* The first argument is required and must be of type `DocumentReference`
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @return A Promise that resolves with an array of resulting document
* snapshots.
*/
getAll(
documentRef: DocumentReference,
...moreDocumentRefsOrReadOptions: Array<DocumentReference|ReadOptions>
): Promise<DocumentSnapshot[]>;
getAll(...documentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
Promise<DocumentSnapshot[]>;

/**
* Fetches the root collections that are associated with this Firestore
Expand Down Expand Up @@ -260,16 +262,18 @@ declare namespace FirebaseFirestore {
* Retrieves multiple documents from Firestore. Holds a pessimistic lock on
* all returned documents.
*
* @param documentRef A `DocumentReference` to receive.
* @param moreDocumentRefsOrReadOptions Additional `DocumentReferences` to
* receive, followed by an optional field mask.
* The first argument is required and must be of type `DocumentReference`
* followed by any additional `DocumentReference` documents. If used, the
* optional `ReadOptions` must be the last argument.
*
* @param {Array.<DocumentReference|ReadOptions>} documentRefsOrReadOptions
* The `DocumentReferences` to receive, followed by an optional field
* mask.
* @return A Promise that resolves with an array of resulting document
* snapshots.
*/
getAll(
documentRef: DocumentReference,
...moreDocumentRefsOrReadOptions: Array<DocumentReference|ReadOptions>
): Promise<DocumentSnapshot[]>;
getAll(...documentRefsOrReadOptions: Array<DocumentReference|ReadOptions>):
Promise<DocumentSnapshot[]>;

/**
* Create the document referred to by the provided `DocumentReference`.
Expand Down

0 comments on commit 87024b3

Please sign in to comment.