Skip to content

Commit

Permalink
feat: support index creation in smply cli
Browse files Browse the repository at this point in the history
  • Loading branch information
abrgr committed Oct 24, 2023
1 parent b5f86e5 commit 2c69b22
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@gjsify/esbuild-plugin-deno-loader": "^0.0.3",
"@statebacked/client": "^0.1.38",
"@statebacked/client": "^0.1.39",
"@statebacked/token": "^0.2.0",
"@supabase/supabase-js": "2.26.0",
"commander": "11.0.0",
Expand Down
30 changes: 28 additions & 2 deletions src/commands/machine-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn } from "node:child_process";
import { copyFile, mkdtemp, rmdir, unlink, writeFile } from "node:fs/promises";
import * as path from "node:path";
import * as os from "node:os";
import { Command } from "commander";
import { Command, InvalidArgumentError } from "commander";
import { PaginationOptions, paginateWithCursor } from "../paginator.js";
import {
getStatebackedClient,
Expand Down Expand Up @@ -33,6 +33,10 @@ export function addMachineVersionsCommands(cmd: Command) {
"-n, --node <file>",
"Path to the Node.js entrypoint to use as the machine definition. We will build the file into a single, self-contained ECMAScript module. Exactly one of --js or --node must be specified.",
)
.option(
"-d, --index-selectors <indexSelectors>",
"JSON object mapping index names to JSON path expressions that point into the context for each machine instance to extract the value that will be indexed. Keys must match the indexes created for the machine. Any values that are not specified will default to the existing index selectors for the current version for the machine.",
)
.option(
"-c, --make-current",
"Make this version the current version",
Expand Down Expand Up @@ -127,10 +131,30 @@ async function createMachineVersion(
versionReference: string;
makeCurrent: boolean;
skipValidation: boolean;
indexSelectors?: string;
},
options: Command,
) {
await silencableCreateMachineVersion(opts, options);
const indexSelectors = opts.indexSelectors
? JSON.parse(opts.indexSelectors)
: undefined;

if (
indexSelectors &&
Object.entries(indexSelectors).some(([k, v]) => !k || typeof v !== "string")
) {
throw new InvalidArgumentError(
"indexSelectors must be a JSON object mapping index names to JSON path expressions",
);
}

await silencableCreateMachineVersion(
{
...opts,
indexSelectors,
},
options,
);
}

export async function silencableCreateMachineVersion(
Expand All @@ -139,6 +163,7 @@ export async function silencableCreateMachineVersion(
versionReference: string;
makeCurrent: boolean;
skipValidation: boolean;
indexSelectors?: Record<string, string>;
quiet?: boolean;
},
options: Command,
Expand All @@ -164,6 +189,7 @@ export async function silencableCreateMachineVersion(
clientInfo: opts.versionReference,
makeCurrent: opts.makeCurrent,
gzippedCode,
indexSelectors: opts.indexSelectors,
});

if (!opts.quiet) {
Expand Down
29 changes: 28 additions & 1 deletion src/commands/machines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export function addMachineCommands(cmd: Command) {
return slug;
},
)
.option(
"-i, --index <index...>",
"Names of indexes to create for instances of this machine. If you specify one of --js or --node, use --index-selectors instead to specify the actual index selectors. If specified multiple times, multiple indexes will be created.",
)
.option(
"-d, --index-selectors <indexSelectors>",
"JSON object mapping index names to JSON path expressions that point into the context for each machine instance to extract the value that will be indexed. If you are not specifying --js or --node to create an initial machine version, use --index instead to specify the names of the indexes only.",
)
.option(
"-r, --version-reference <versionReference>",
"Name for the first version of the machine. E.g. git commit sha or semantic version identifier.",
Expand Down Expand Up @@ -93,12 +101,30 @@ async function createMachine(
machine: string;
versionReference?: string;
skipValidation: boolean;
index?: string[];
indexSelectors?: string;
},
options: Command,
) {
const client = await getStatebackedClient(options);

await client.machines.create(opts.machine);
const indexSelectors = opts.indexSelectors
? JSON.parse(opts.indexSelectors)
: undefined;
if (
indexSelectors &&
Object.entries(indexSelectors).some(([k, v]) => !k || typeof v !== "string")
) {
throw new InvalidArgumentError(
"indexSelectors must be a JSON object mapping index names to JSON path expressions",
);
}

const indexes = indexSelectors ? Object.keys(indexSelectors) : opts.index;

await client.machines.create(opts.machine, {
indexes,
});

const output = {
name: opts.machine,
Expand All @@ -114,6 +140,7 @@ async function createMachine(
node: opts.node,
deno: opts.deno,
skipValidation: opts.skipValidation,
indexSelectors,
makeCurrent: true,
quiet: true,
},
Expand Down

0 comments on commit 2c69b22

Please sign in to comment.