convex
Version:
Client for the Convex Cloud
83 lines (82 loc) • 2.6 kB
JavaScript
;
import axios from "axios";
import chalk from "chalk";
import ora from "ora";
import path from "path";
import { bundleSchema } from "../../bundler/index.js";
import { version } from "../../index.js";
import { poll, fatalServerErr, deprecationCheckWarning } from "./utils.js";
function stringifyIndex(index) {
return `${index.table}.${index.name} ${JSON.stringify(index.fields)}`;
}
function diffIndexes(indexes) {
let indexDiff = "";
if (indexes.dropped.length > 0) {
indexDiff += "Delete the following indexes:\n";
for (const index of indexes.dropped) {
indexDiff += `[-] ${stringifyIndex(index)}
`;
}
}
if (indexes.added.length > 0) {
indexDiff += "Add the following indexes:\n";
for (const index of indexes.added) {
indexDiff += `[+] ${stringifyIndex(index)}
`;
}
}
return indexDiff;
}
export async function buildIndexes(ctx, origin, adminKey, schemaDir, dryRun) {
if (!ctx.fs.exists(path.resolve(schemaDir, "schema.ts"))) {
return;
}
const bundles = await bundleSchema(ctx.fs, schemaDir);
const spinner = ctx.spinner = ora({
text: "Checking for changed table indexes...",
stream: process.stdout
});
if (!dryRun) {
spinner.start();
}
try {
const res = await axios.post(`${origin}/api/${version}/build_indexes`, {
bundle: bundles[0],
adminKey,
dryRun
});
deprecationCheckWarning(ctx, res);
const indexDiff = diffIndexes(res.data);
if (indexDiff !== "") {
console.log(
chalk.bold(
`
Indexes ${dryRun ? "would" : "will"} be overwritten with the following changes:`
)
);
console.log(indexDiff);
}
if (dryRun) {
return;
}
spinner.text = "Waiting for all table indexes to be backfilled...";
await waitForIndexesToBuild(origin, adminKey);
res.data.added.length > 0 ? spinner.succeed(chalk.green("Successfully backfilled table indexes.")) : res.data.dropped.length > 0 ? spinner.succeed(
chalk.green("Successfully dropped deleted table indexes.")
) : spinner.stop();
} catch (err) {
spinner.fail(chalk.red("Error: Unable to build indexes on", origin));
return await fatalServerErr(ctx, err);
}
}
async function waitForIndexesToBuild(origin, adminKey) {
const fetch = () => axios.get(
`${origin}/api/${version}/get_indexes`,
{
headers: { Authorization: `Convex ${adminKey}` }
}
);
const validate = (result) => result.data.indexes.every((index) => index.backfill.state === "done");
await poll(fetch, validate);
}
//# sourceMappingURL=indexes.js.map