@kaynooo/cli-db 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/cli.js +214 -25
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ bun install -g @kaynooo/cli-db
|
|
|
23
23
|
| --- | --- |
|
|
24
24
|
| `db "<sql>"` / `db query` | runs one read-only statement, as an argument or on stdin |
|
|
25
25
|
| `db tables` | tables and views with an estimated row count (Postgres: outside the system schemas, MySQL: the connected database) |
|
|
26
|
+
| `db sample <table>` | a few rows of a table (`-n`, `--where`), sensitive columns masked; `db count <table>` the number of rows |
|
|
26
27
|
| `db describe <table>` | columns with type, nullability, default and primary key (`schema.table` to pick a schema, the database on MySQL) |
|
|
27
28
|
| `db dbs` | databases the connection may open, with their size, the one read marked `current` |
|
|
28
29
|
|
package/dist/cli.js
CHANGED
|
@@ -410,8 +410,12 @@ class HelpShown extends Exit {
|
|
|
410
410
|
|
|
411
411
|
class Completing {
|
|
412
412
|
options;
|
|
413
|
-
|
|
413
|
+
positionals;
|
|
414
|
+
candidates;
|
|
415
|
+
constructor(options, positionals = [], candidates = {}) {
|
|
414
416
|
this.options = options;
|
|
417
|
+
this.positionals = positionals;
|
|
418
|
+
this.candidates = candidates;
|
|
415
419
|
}
|
|
416
420
|
}
|
|
417
421
|
var completion = { active: false };
|
|
@@ -454,8 +458,10 @@ function exampleRows(examples, colors = paint(process3.stdout)) {
|
|
|
454
458
|
function optionRows(options, colors = paint(process3.stdout)) {
|
|
455
459
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
456
460
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
457
|
-
const
|
|
458
|
-
|
|
461
|
+
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
462
|
+
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
463
|
+
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
464
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
|
|
459
465
|
});
|
|
460
466
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
461
467
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
@@ -481,14 +487,13 @@ function applyOutput(color, plain, env = process3.env) {
|
|
|
481
487
|
function parse(args, usage, options, settings = {}) {
|
|
482
488
|
const output = Object.fromEntries(Object.entries(OUTPUT_OPTIONS).filter(([name]) => !(name in options)));
|
|
483
489
|
const all = { ...options, ...output, help: { type: "boolean", short: "h", description: "show this help" } };
|
|
490
|
+
const listing = listed(all, settings.allowNegative === true);
|
|
484
491
|
if (completion.active)
|
|
485
|
-
throw new Completing(
|
|
486
|
-
const
|
|
487
|
-
const own = separator === -1 ? args : args.slice(0, separator);
|
|
488
|
-
const rest = separator === -1 ? undefined : args.slice(separator + 1);
|
|
492
|
+
throw new Completing(listing, settings.positionals, settings.candidates);
|
|
493
|
+
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
489
494
|
let parsed;
|
|
490
495
|
try {
|
|
491
|
-
parsed = parseArgs({ args: joinDashValues(own, all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
496
|
+
parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
492
497
|
} catch (error) {
|
|
493
498
|
const message = parseError(error);
|
|
494
499
|
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
@@ -496,12 +501,84 @@ function parse(args, usage, options, settings = {}) {
|
|
|
496
501
|
const values = parsed.values;
|
|
497
502
|
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
498
503
|
if (values.help === true) {
|
|
499
|
-
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage,
|
|
504
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
|
|
500
505
|
throw new HelpShown;
|
|
501
506
|
}
|
|
507
|
+
checkChoices(parsed.values, all);
|
|
508
|
+
checkConflicts(parsed.values, all);
|
|
502
509
|
if (settings.positionals !== undefined)
|
|
503
510
|
checkPositionals(parsed.positionals, settings.positionals);
|
|
504
|
-
return { ...parsed, positionals: parsed.positionals, rest };
|
|
511
|
+
return { ...parsed, values: parsed.values, positionals: parsed.positionals, rest };
|
|
512
|
+
}
|
|
513
|
+
function splitRest(args, passes) {
|
|
514
|
+
const separator = passes ? args.indexOf("--") : -1;
|
|
515
|
+
return { own: separator === -1 ? args : args.slice(0, separator), rest: separator === -1 ? undefined : args.slice(separator + 1) };
|
|
516
|
+
}
|
|
517
|
+
function splitTrailing(args, options, count) {
|
|
518
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
519
|
+
let seen = 0;
|
|
520
|
+
if (count === 0)
|
|
521
|
+
return { own: args, rest: undefined };
|
|
522
|
+
for (let at = 0;at < args.length; at++) {
|
|
523
|
+
const word = args[at];
|
|
524
|
+
if (word === "--")
|
|
525
|
+
break;
|
|
526
|
+
if (word.startsWith("-")) {
|
|
527
|
+
if (takesValue(word))
|
|
528
|
+
at++;
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
seen++;
|
|
532
|
+
if (seen === count)
|
|
533
|
+
return { own: args.slice(0, at + 1), rest: args.slice(at + 1) };
|
|
534
|
+
}
|
|
535
|
+
return { own: args, rest: undefined };
|
|
536
|
+
}
|
|
537
|
+
function listed(options, allowNegative) {
|
|
538
|
+
if (!allowNegative)
|
|
539
|
+
return options;
|
|
540
|
+
return Object.fromEntries(Object.entries(options).flatMap(([name, option]) => {
|
|
541
|
+
if (option.type !== "boolean" || option.negative === undefined)
|
|
542
|
+
return [[name, option]];
|
|
543
|
+
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
544
|
+
}));
|
|
545
|
+
}
|
|
546
|
+
function withBare(args, options) {
|
|
547
|
+
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
548
|
+
if (bare.size === 0)
|
|
549
|
+
return args;
|
|
550
|
+
const separator = args.indexOf("--");
|
|
551
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
552
|
+
return [...own.flatMap((arg, index) => {
|
|
553
|
+
const optional = bare.get(arg);
|
|
554
|
+
const next = own[index + 1];
|
|
555
|
+
const value = next !== undefined && (optional?.accepts === undefined ? !next.startsWith("-") : optional.accepts.test(next));
|
|
556
|
+
return optional === undefined || value ? [arg] : [arg, optional.bare];
|
|
557
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
558
|
+
}
|
|
559
|
+
function checkConflicts(values, options) {
|
|
560
|
+
const given = (name) => values[name] !== undefined && values[name] !== false;
|
|
561
|
+
for (const [name, { conflicts = [] }] of Object.entries(options)) {
|
|
562
|
+
const other = given(name) ? conflicts.find(given) : undefined;
|
|
563
|
+
if (other !== undefined)
|
|
564
|
+
badUsage(`--${name} cannot be used with --${other}`);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function checkChoices(values, options) {
|
|
568
|
+
for (const [name, option] of Object.entries(options)) {
|
|
569
|
+
if (option.choices === undefined)
|
|
570
|
+
continue;
|
|
571
|
+
const given = values[name];
|
|
572
|
+
for (const value of Array.isArray(given) ? given : [given]) {
|
|
573
|
+
if (typeof value === "string")
|
|
574
|
+
oneOf(value, name, option.choices);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function oneOf(value, name, choices) {
|
|
579
|
+
if (!choices.includes(value))
|
|
580
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes one of ${choices.join(", ")}, not ${value}`);
|
|
581
|
+
return value;
|
|
505
582
|
}
|
|
506
583
|
function checkPositionals(given, names) {
|
|
507
584
|
const variadic = names.at(-1)?.replace("]", "").endsWith("...") === true;
|
|
@@ -672,8 +749,9 @@ var LEVEL_OPTIONS = {
|
|
|
672
749
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
673
750
|
};
|
|
674
751
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
675
|
-
async function commandRows(
|
|
676
|
-
const
|
|
752
|
+
async function commandRows(all, colors) {
|
|
753
|
+
const commands = shown(all);
|
|
754
|
+
const width = Math.max(0, ...Object.keys(commands).map((name) => name.length));
|
|
677
755
|
const groups = new Map([["Commands:", []]]);
|
|
678
756
|
const summaries = await Promise.all(Object.values(commands).map(async ({ summary }) => resolveSummary(summary)));
|
|
679
757
|
for (const [index, [name, { group }]] of Object.entries(commands).entries()) {
|
|
@@ -684,6 +762,9 @@ async function commandRows(commands, colors) {
|
|
|
684
762
|
}
|
|
685
763
|
return [...groups].filter(([, rows]) => rows.length > 0).flatMap(([heading, rows], index) => [...index === 0 ? [] : [""], colors.bold(heading), ...rows]);
|
|
686
764
|
}
|
|
765
|
+
function shown(commands) {
|
|
766
|
+
return Object.fromEntries(Object.entries(commands).filter(([, { hidden }]) => hidden !== true));
|
|
767
|
+
}
|
|
687
768
|
function takeOutputFlags(argv) {
|
|
688
769
|
let index = 0;
|
|
689
770
|
for (;index < argv.length; index++) {
|
|
@@ -775,6 +856,14 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
775
856
|
return reportThrown(prefix, error);
|
|
776
857
|
}
|
|
777
858
|
const [name, ...args] = argv;
|
|
859
|
+
const fallback = level.defaultCommand === undefined ? undefined : level.commands[level.defaultCommand];
|
|
860
|
+
if (name === undefined && level.runBare && fallback !== undefined && "run" in fallback) {
|
|
861
|
+
try {
|
|
862
|
+
return await fallback.run([]);
|
|
863
|
+
} catch (error) {
|
|
864
|
+
return reportThrown(`${prefix} ${level.defaultCommand}`, error, redact, false);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
778
867
|
if (name === undefined && level.path.length > 1) {
|
|
779
868
|
console.error(await help(await expand(level, [...level.path.slice(1), ...argv]), process5.stderr));
|
|
780
869
|
return 2;
|
|
@@ -789,6 +878,7 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
789
878
|
}
|
|
790
879
|
let command = level.commands[level.aliases?.[name] ?? name];
|
|
791
880
|
let rest = args;
|
|
881
|
+
let ran = name;
|
|
792
882
|
if (command === undefined && level.lazy.length > 0) {
|
|
793
883
|
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
794
884
|
command = level.commands[level.aliases?.[name] ?? name];
|
|
@@ -796,40 +886,77 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
796
886
|
if (command === undefined && level.defaultCommand !== undefined) {
|
|
797
887
|
command = level.commands[level.defaultCommand];
|
|
798
888
|
rest = argv;
|
|
889
|
+
ran = level.defaultCommand;
|
|
799
890
|
}
|
|
800
891
|
if (command === undefined) {
|
|
801
|
-
const meant = closest(name, Object.keys(level.commands));
|
|
892
|
+
const meant = closest(name, Object.keys(shown(level.commands)));
|
|
802
893
|
printError(prefix, `no command ${name}`, meant === undefined ? `run ${prefix} --help for its commands` : `did you mean ${meant}?`);
|
|
803
894
|
return 2;
|
|
804
895
|
}
|
|
805
896
|
if ("commands" in command) {
|
|
806
897
|
const { summary } = command;
|
|
807
|
-
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
898
|
+
return dispatch({ path: [...level.path, name], header: (colors, text) => `${colors.bold(`${prefix} ${name}`)} ${text}`, summary, ...scopeOf(command.commands, command.extraCommands, command.aliases), defaultCommand: undefined, runBare: false, examples: command.examples, replaces: undefined }, rest, { version, redact });
|
|
808
899
|
}
|
|
809
900
|
try {
|
|
810
901
|
return await command.run(rest);
|
|
811
902
|
} catch (error) {
|
|
812
|
-
return reportThrown(`${prefix} ${
|
|
903
|
+
return reportThrown(`${prefix} ${ran}`, error, redact, wantsJson(rest));
|
|
813
904
|
}
|
|
814
905
|
}
|
|
815
906
|
var COMPLETE = "__complete";
|
|
816
907
|
var FILES = "__files__";
|
|
817
|
-
async function
|
|
908
|
+
async function completingOf(run) {
|
|
818
909
|
completion.active = true;
|
|
819
910
|
try {
|
|
820
911
|
await run([]);
|
|
821
912
|
} catch (error) {
|
|
822
913
|
if (error instanceof Completing)
|
|
823
|
-
return error
|
|
914
|
+
return error;
|
|
824
915
|
} finally {
|
|
825
916
|
completion.active = false;
|
|
826
917
|
}
|
|
827
|
-
return {};
|
|
918
|
+
return new Completing({});
|
|
919
|
+
}
|
|
920
|
+
async function optionsOf(run) {
|
|
921
|
+
return (await completingOf(run)).options;
|
|
922
|
+
}
|
|
923
|
+
function candidateLine(candidate) {
|
|
924
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
925
|
+
}
|
|
926
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
927
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
928
|
+
let index = 0;
|
|
929
|
+
let at = 0;
|
|
930
|
+
while (at < before.length) {
|
|
931
|
+
const word = before[at];
|
|
932
|
+
if (word === "--") {
|
|
933
|
+
index += before.length - at - 1;
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
if (!word.startsWith("-"))
|
|
937
|
+
index++;
|
|
938
|
+
else if (takesValue(word))
|
|
939
|
+
at++;
|
|
940
|
+
at++;
|
|
941
|
+
}
|
|
942
|
+
const last = positionals.at(-1);
|
|
943
|
+
if (index < positionals.length)
|
|
944
|
+
return positionals[index];
|
|
945
|
+
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
828
946
|
}
|
|
829
947
|
async function leafCandidates(run, words) {
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
948
|
+
const completing = await completingOf(run);
|
|
949
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
950
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
951
|
+
const flag = words.at(-2);
|
|
952
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
953
|
+
if (option?.type === "string") {
|
|
954
|
+
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
955
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
956
|
+
}
|
|
957
|
+
const name = positionalBeingTyped(words.slice(0, -1), completing);
|
|
958
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
959
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
833
960
|
}
|
|
834
961
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
835
962
|
const [word = "", ...rest] = words;
|
|
@@ -840,8 +967,13 @@ async function candidates(scope, defaultCommand, words, before = []) {
|
|
|
840
967
|
({ commands, aliases } = await expand(scope, [...before, ...words]));
|
|
841
968
|
command = commands[aliases?.[word] ?? word];
|
|
842
969
|
}
|
|
843
|
-
if (listing)
|
|
844
|
-
|
|
970
|
+
if (listing) {
|
|
971
|
+
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
972
|
+
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
973
|
+
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
974
|
+
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
975
|
+
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
|
|
976
|
+
}
|
|
845
977
|
if (command === undefined) {
|
|
846
978
|
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
847
979
|
return fallback !== undefined && "run" in fallback ? leafCandidates(fallback.run, words) : [];
|
|
@@ -856,7 +988,7 @@ async function printCandidates(found) {
|
|
|
856
988
|
async function runCli(cli, argv = process5.argv.slice(2)) {
|
|
857
989
|
if (argv[0] === COMPLETE)
|
|
858
990
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
859
|
-
return dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(cli.version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, examples: cli.examples, replaces: cli.replaces }, argv, { version: cli.version, redact: cli.redact });
|
|
991
|
+
return dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(cli.version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version: cli.version, redact: cli.redact });
|
|
860
992
|
}
|
|
861
993
|
// ../kit/src/logs.ts
|
|
862
994
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -872,7 +1004,7 @@ var {Glob } = globalThis.Bun;
|
|
|
872
1004
|
var SESSION_FILES = new Glob("*.json");
|
|
873
1005
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
874
1006
|
// package.json
|
|
875
|
-
var version = "0.
|
|
1007
|
+
var version = "0.9.0";
|
|
876
1008
|
|
|
877
1009
|
// src/args.ts
|
|
878
1010
|
var DEFAULT_ENV = "DATABASE_URL";
|
|
@@ -1810,6 +1942,61 @@ order by table_schema, ordinal_position`, params: second === undefined ? [table]
|
|
|
1810
1942
|
});
|
|
1811
1943
|
}
|
|
1812
1944
|
|
|
1945
|
+
// src/commands/table.ts
|
|
1946
|
+
var SAMPLE_LIMIT = 5;
|
|
1947
|
+
var NAME = /^[A-Z_][\w$]*(?:\.[A-Z_][\w$]*)?$/i;
|
|
1948
|
+
var EXITS2 = "Exits 0 when it ran, 1 when the database refused or was unreachable, 2 on a usage error or a host that is not allowed.";
|
|
1949
|
+
function quotedTable(target, dialect) {
|
|
1950
|
+
if (!NAME.test(target))
|
|
1951
|
+
fail(`${target} is not a table name: use name or schema.name`);
|
|
1952
|
+
const quote = dialect === "mysql" ? "`" : '"';
|
|
1953
|
+
return target.split(".").map((part) => `${quote}${part}${quote}`).join(".");
|
|
1954
|
+
}
|
|
1955
|
+
var whereOf = (where) => where === undefined ? "" : ` where ${where}`;
|
|
1956
|
+
function sampleSql(target, dialect, limit, where) {
|
|
1957
|
+
return `select * from ${quotedTable(target, dialect)}${whereOf(where)} limit ${limit}`;
|
|
1958
|
+
}
|
|
1959
|
+
function countSql(target, dialect, where) {
|
|
1960
|
+
return `select count(*) as count from ${quotedTable(target, dialect)}${whereOf(where)}`;
|
|
1961
|
+
}
|
|
1962
|
+
var where = { type: "string", short: "w", description: "only the rows matching this SQL condition" };
|
|
1963
|
+
var sampleOptions = { ...connectionOptions, ...outputOptions, where };
|
|
1964
|
+
var countOptions = { ...connectionOptions, json: outputOptions.json, where };
|
|
1965
|
+
async function run(args, usage, options, build) {
|
|
1966
|
+
const parsed = parse(args, usage, options);
|
|
1967
|
+
const { values, positionals } = parsed;
|
|
1968
|
+
const [target, ...extra] = positionals;
|
|
1969
|
+
if (target === undefined || extra.length > 0)
|
|
1970
|
+
fail("give one table name");
|
|
1971
|
+
const url = await connectionUrl(values);
|
|
1972
|
+
const dialect = dialectOf(new URL(url));
|
|
1973
|
+
const limit = wholeNumber(values.limit, "limit", SAMPLE_LIMIT, 0);
|
|
1974
|
+
const sql = build(target, dialect, { limit, where: values.where });
|
|
1975
|
+
const verdict = inspect(sql, dialect);
|
|
1976
|
+
if (verdict.reasons.length > 0 || verdict.statements > 1)
|
|
1977
|
+
fail(`not a plain read: ${verdict.reasons.join("; ") || "one statement at a time"}`);
|
|
1978
|
+
console.log(render(shape(await readRows(url, sql, [], limit), limit, values.raw === true, false), values.json === true));
|
|
1979
|
+
return 0;
|
|
1980
|
+
}
|
|
1981
|
+
async function sample(args) {
|
|
1982
|
+
return run(args, `db sample <table> [-n ${SAMPLE_LIMIT}] [--where <sql>]
|
|
1983
|
+
|
|
1984
|
+
Prints a few rows of a table (\`schema.table\` to pick a schema, the name is case sensitive on Postgres), sensitive columns masked as in a query.
|
|
1985
|
+
|
|
1986
|
+
${HOSTS_NOTE}
|
|
1987
|
+
|
|
1988
|
+
${EXITS2}`, sampleOptions, (target, dialect, { limit, where }) => sampleSql(target, dialect, limit, where));
|
|
1989
|
+
}
|
|
1990
|
+
async function count(args) {
|
|
1991
|
+
return run(args, `db count <table> [--where <sql>]
|
|
1992
|
+
|
|
1993
|
+
Prints the number of rows of a table (the name is case sensitive on Postgres), or of those matching a condition.
|
|
1994
|
+
|
|
1995
|
+
${HOSTS_NOTE}
|
|
1996
|
+
|
|
1997
|
+
${EXITS2}`, countOptions, (target, dialect, { where }) => countSql(target, dialect, where));
|
|
1998
|
+
}
|
|
1999
|
+
|
|
1813
2000
|
// src/cli.ts
|
|
1814
2001
|
process8.exitCode = await runCli({
|
|
1815
2002
|
name: "db",
|
|
@@ -1821,6 +2008,8 @@ process8.exitCode = await runCli({
|
|
|
1821
2008
|
query: { run: query, summary: 'run one read-only statement (the default: db "<sql>"), a write runs on a _test database and is printed otherwise' },
|
|
1822
2009
|
tables: { run: tables, summary: "list tables and views with an estimated row count" },
|
|
1823
2010
|
describe: { run: describe, summary: "columns of a table with type, nullability, default and primary key" },
|
|
2011
|
+
sample: { run: sample, summary: "a few rows of a table, sensitive columns masked" },
|
|
2012
|
+
count: { run: count, summary: "the number of rows of a table, optionally matching --where" },
|
|
1824
2013
|
dbs: { run: databases, summary: "databases the connection may open, with their size" }
|
|
1825
2014
|
},
|
|
1826
2015
|
defaultCommand: "query"
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-db",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"description": "db: run read-only SQL against Postgres, MySQL and MariaDB from the terminal without ever showing the connection credentials.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"db": "$npm_execpath src/cli.ts"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@kaynooo/cli-kit": "0.
|
|
31
|
+
"@kaynooo/cli-kit": "0.19.0"
|
|
32
32
|
}
|
|
33
33
|
}
|