|
| 1 | +import { |
| 2 | + EXTENSIONS_BUCKET, |
| 3 | + EXTENSIONS_DETAILS_TABLE, |
| 4 | + POPULARITY_FILE, |
| 5 | + REGISTRY_FILE, |
| 6 | + REGISTRY_VERSION_FILE |
| 7 | +} from "../constants.js"; |
| 8 | +import db from "../db.js"; |
| 9 | +import {S3} from "../s3.js"; |
| 10 | + |
| 11 | +export async function syncRegistryDBToS3JSON() { |
| 12 | + console.log("syncing non synced extension data in db to s3 extension.json"); |
| 13 | + let pending = await db.query(EXTENSIONS_DETAILS_TABLE, "$.syncPending='Y'"); |
| 14 | + if(!pending.isSuccess){ |
| 15 | + // unexpected error |
| 16 | + throw new Error("Error getting syncPending extensions from: " + EXTENSIONS_DETAILS_TABLE); |
| 17 | + } |
| 18 | + if(!pending.documents.length){ |
| 19 | + console.log("nothing to sync from db to s3 registry.json"); |
| 20 | + return; |
| 21 | + } |
| 22 | + console.log("getting registry file from S3"); |
| 23 | + let registry = JSON.parse(await S3.getObject(EXTENSIONS_BUCKET, REGISTRY_FILE)); |
| 24 | + let popularity = JSON.parse(await S3.getObject(EXTENSIONS_BUCKET, POPULARITY_FILE)); |
| 25 | + for(let document of pending.documents){ |
| 26 | + // remove internal variables |
| 27 | + let newDoc = structuredClone(document); |
| 28 | + delete newDoc.documentId; |
| 29 | + delete newDoc.syncPending; |
| 30 | + console.log("Updating Registry entry with[existing, new]: ", registry[newDoc.metadata.name], newDoc); |
| 31 | + registry[newDoc.metadata.name] = newDoc; |
| 32 | + popularity[newDoc.metadata.name]= { |
| 33 | + "totalDownloads": newDoc.totalDownloads || 0, |
| 34 | + "gihubStars": newDoc.gihubStars || 0 // should have been gitHubStars, refactor in phcode tech debt. |
| 35 | + }; |
| 36 | + } |
| 37 | + // now update all jsons in registry |
| 38 | + console.log("Writing main registry file: ", REGISTRY_FILE); |
| 39 | + await S3.putObject(EXTENSIONS_BUCKET, REGISTRY_FILE, JSON.stringify(registry)); |
| 40 | + let registryVersion = JSON.parse(await S3.getObject(EXTENSIONS_BUCKET, REGISTRY_VERSION_FILE)); |
| 41 | + registryVersion.version = registryVersion.version + 1; |
| 42 | + console.log("Writing registry version file version: ", registryVersion.version, REGISTRY_VERSION_FILE); |
| 43 | + await S3.putObject(EXTENSIONS_BUCKET, REGISTRY_VERSION_FILE, JSON.stringify(registryVersion)); |
| 44 | + console.log("Writing registry popularity file version: ", POPULARITY_FILE); |
| 45 | + await S3.putObject(EXTENSIONS_BUCKET, POPULARITY_FILE, JSON.stringify(popularity)); |
| 46 | + |
| 47 | + // now update all syncPending flags |
| 48 | + let updatePromises = []; |
| 49 | + for(let document of pending.documents){ |
| 50 | + // remove internal variables |
| 51 | + let documentID = document.documentId; |
| 52 | + delete document.documentId; |
| 53 | + delete document.syncPending; |
| 54 | + console.log("Setting syncPending for: ", document.metadata.name, documentID); |
| 55 | + // conditional update to make sure than no new release happened while we were updating this release |
| 56 | + updatePromises.push(db.update(EXTENSIONS_DETAILS_TABLE, documentID, document, |
| 57 | + `$.metadata.version='${document.metadata.version}'`)); |
| 58 | + } |
| 59 | + console.log("syncPending status updated in db for: ", await Promise.all(updatePromises)); |
| 60 | +} |
0 commit comments