convex
Version:
Client for the Convex Cloud
111 lines (110 loc) • 4.09 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var indexes_exports = {};
__export(indexes_exports, {
buildIndexes: () => buildIndexes
});
module.exports = __toCommonJS(indexes_exports);
var import_axios = __toESM(require("axios"));
var import_chalk = __toESM(require("chalk"));
var import_ora = __toESM(require("ora"));
var import_path = __toESM(require("path"));
var import_bundler = require("../../bundler/index.js");
var import__ = require("../../index.js");
var import_utils = require("./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;
}
async function buildIndexes(ctx, origin, adminKey, schemaDir, dryRun) {
if (!ctx.fs.exists(import_path.default.resolve(schemaDir, "schema.ts"))) {
return;
}
const bundles = await (0, import_bundler.bundleSchema)(ctx.fs, schemaDir);
const spinner = ctx.spinner = (0, import_ora.default)({
text: "Checking for changed table indexes...",
stream: process.stdout
});
if (!dryRun) {
spinner.start();
}
try {
const res = await import_axios.default.post(`${origin}/api/${import__.version}/build_indexes`, {
bundle: bundles[0],
adminKey,
dryRun
});
(0, import_utils.deprecationCheckWarning)(ctx, res);
const indexDiff = diffIndexes(res.data);
if (indexDiff !== "") {
console.log(
import_chalk.default.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(import_chalk.default.green("Successfully backfilled table indexes.")) : res.data.dropped.length > 0 ? spinner.succeed(
import_chalk.default.green("Successfully dropped deleted table indexes.")
) : spinner.stop();
} catch (err) {
spinner.fail(import_chalk.default.red("Error: Unable to build indexes on", origin));
return await (0, import_utils.fatalServerErr)(ctx, err);
}
}
async function waitForIndexesToBuild(origin, adminKey) {
const fetch = () => import_axios.default.get(
`${origin}/api/${import__.version}/get_indexes`,
{
headers: { Authorization: `Convex ${adminKey}` }
}
);
const validate = (result) => result.data.indexes.every((index) => index.backfill.state === "done");
await (0, import_utils.poll)(fetch, validate);
}
//# sourceMappingURL=indexes.js.map