Skip to content

Commit

Permalink
Add 'isLatest' field to reduce array-contains uses
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfagnani committed Oct 13, 2022
1 parent 6143977 commit 3e1eb52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/catalog-server/src/lib/firestore/firestore-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,22 @@ export class FirestoreRepository implements Repository {
// collection first.
const elements = await t.get(customElementsRef);

const isLatest = versionDistTags.includes('latest');

// Update the PackageVersion doc
await t.update(versionRef, {
// We remove the converter to fix the types:
// https://github.com/googleapis/nodejs-firestore/issues/1745
await t.update(versionRef.withConverter(null), {
distTags: versionDistTags,
isLatest,
});

// Update all elements
await Promise.all(
elements.docs.map(async (element) => {
await t.update(element.ref, {
await t.update(element.ref.withConverter(null), {
distTags: versionDistTags,
isLatest,
});
})
);
Expand Down Expand Up @@ -284,13 +290,15 @@ export class FirestoreRepository implements Repository {
// Store custom elements data in subcollection
const versionRef = this.getPackageVersionRef(packageName, version);
const customElementsRef = versionRef.collection('customElements');
const isLatest = distTags.includes('latest');
const batch = db.batch();

for (const c of customElements) {
batch.create(customElementsRef.doc(), {
package: packageName,
version,
distTags,
isLatest,
author,
tagName: c.export.name,
className: c.declaration.name,
Expand Down Expand Up @@ -394,10 +402,14 @@ export class FirestoreRepository implements Repository {
}

// Now query for a version that's assigned this dist-tag
const result = await this.getPackageVersionCollectionRef(packageName)
.where('distTags', 'array-contains', versionOrTag)
.limit(1)
.get();
let query: CollectionReference<PackageVersion> | Query<PackageVersion> =
this.getPackageVersionCollectionRef(packageName);
if (versionOrTag === 'latest') {
query = query.where('isLatest', '==', true);
} else {
query = query.where('distTags', 'array-contains', versionOrTag);
}
const result = await query.limit(1).get();
if (result.size !== 0) {
return result.docs[0]!.data();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const packageVersionConverter: FirestoreDataConverter<PackageVersion> = {
customElementsManifest: packageVersion.customElementsManifest,
description: packageVersion.description,
distTags: packageVersion.distTags,
isLatest: packageVersion.distTags.includes('latest'),
homepage: packageVersion.homepage,
lastUpdate: packageVersion.lastUpdate,
status: packageVersion.status,
Expand Down

0 comments on commit 3e1eb52

Please sign in to comment.