Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload getAll function to allow array destructuring #515

Merged
merged 8 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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>):
cxam marked this conversation as resolved.
Show resolved Hide resolved
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);
cxam marked this conversation as resolved.
Show resolved Hide resolved
});
});

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