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

Add 'isLatest' field to reduce array-contains uses #1364

Merged
merged 1 commit into from
Oct 13, 2022
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
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