trustgraph/ts/packages/cli/src/commands/triples.ts

52 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-04-05 22:44:45 -05:00
/**
* Triples query CLI commands.
*
* Query the knowledge graph for subject-predicate-object triples.
*/
import type { Command } from "commander";
import type { Term } from "@trustgraph/client";
import { createSocket, getOpts } from "./util.js";
export function registerTriplesCommands(program: Command): void {
program
.command("triples")
.description("Query knowledge graph triples")
.option("-s, --subject <iri>", "Subject IRI")
.option("-p, --predicate <iri>", "Predicate IRI")
.option("-o, --object <iri>", "Object IRI or literal")
.option("-l, --limit <n>", "Max results", "20")
.option("--collection <name>", "Collection name")
.action(async (cmdOpts, cmd) => {
const opts = getOpts(cmd);
const socket = await createSocket(opts);
try {
const flow = socket.flow(opts.flow);
2026-05-12 08:06:58 -05:00
const subject = cmdOpts.subject as string | undefined;
const predicate = cmdOpts.predicate as string | undefined;
const object = cmdOpts.object as string | undefined;
const s: Term | undefined = subject !== undefined && subject.length > 0
? { t: "i", i: subject }
2026-04-05 22:44:45 -05:00
: undefined;
2026-05-12 08:06:58 -05:00
const p: Term | undefined = predicate !== undefined && predicate.length > 0
? { t: "i", i: predicate }
2026-04-05 22:44:45 -05:00
: undefined;
2026-05-12 08:06:58 -05:00
const o: Term | undefined = object !== undefined && object.length > 0
? { t: "i", i: object }
2026-04-05 22:44:45 -05:00
: undefined;
const triples = await flow.triplesQuery(
s,
p,
o,
parseInt(cmdOpts.limit as string, 10),
cmdOpts.collection as string | undefined,
);
console.log(JSON.stringify(triples, null, 2));
} finally {
socket.close();
}
});
}