Skip to content

Commit 0bd6b23

Browse files
committed
Customised output for Pongo shell
1 parent 17cd40e commit 0bd6b23

File tree

7 files changed

+73
-51
lines changed

7 files changed

+73
-51
lines changed

samples/simple-ts/package-lock.json

+29-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/simple-ts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"dist"
6464
],
6565
"dependencies": {
66-
"@event-driven-io/pongo": "0.15.0-alpha.1"
66+
"@event-driven-io/pongo": "0.15.0-alpha.2"
6767
}
6868
}

src/package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo-core",
3-
"version": "0.15.0-alpha.2",
3+
"version": "0.15.0",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"engines": {

src/packages/dumbo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/dumbo",
3-
"version": "0.11.0-alpha.2",
3+
"version": "0.11.0",
44
"description": "Dumbo - tools for dealing with PostgreSQL",
55
"type": "module",
66
"scripts": {

src/packages/pongo/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@event-driven-io/pongo",
3-
"version": "0.15.0-alpha.2",
3+
"version": "0.15.0",
44
"description": "Pongo - Mongo with strong consistency on top of Postgres",
55
"type": "module",
66
"scripts": {
@@ -87,7 +87,7 @@
8787
"pongo": "./dist/cli.js"
8888
},
8989
"peerDependencies": {
90-
"@event-driven-io/dumbo": "0.11.0-alpha.2",
90+
"@event-driven-io/dumbo": "0.11.0",
9191
"@types/mongodb": "^4.0.7",
9292
"@types/pg": "^8.11.6",
9393
"@types/uuid": "^10.0.0",

src/packages/pongo/src/commandLine/shell.ts

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { JSONSerializer } from '@event-driven-io/dumbo';
12
import chalk from 'chalk';
23
import Table from 'cli-table3';
34
import { Command } from 'commander';
45
import repl from 'node:repl';
5-
import { pongoClient, pongoSchema } from '../core';
6+
import { pongoClient, pongoSchema, type PongoClient } from '../core';
7+
8+
let pongo: PongoClient;
69

710
const calculateColumnWidths = (
811
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -23,10 +26,16 @@ const calculateColumnWidths = (
2326
};
2427

2528
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26-
const displayResultsAsTable = (results: any[]) => {
29+
const printOutput = (obj: any): string => {
30+
return Array.isArray(obj)
31+
? displayResultsAsTable(obj)
32+
: JSONSerializer.serialize(obj);
33+
};
34+
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
const displayResultsAsTable = (results: any[]): string => {
2737
if (results.length === 0) {
28-
console.log(chalk.yellow('No documents found.'));
29-
return;
38+
return chalk.yellow('No documents found.');
3039
}
3140

3241
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -48,7 +57,7 @@ const displayResultsAsTable = (results: any[]) => {
4857
);
4958
});
5059

51-
console.log(table.toString());
60+
return table.toString();
5261
};
5362

5463
const startRepl = (options: {
@@ -61,6 +70,8 @@ const startRepl = (options: {
6170
const r = repl.start({
6271
prompt: chalk.green('pongo> '),
6372
useGlobal: true,
73+
breakEvalOnSigint: true,
74+
writer: printOutput,
6475
});
6576

6677
const schema =
@@ -72,23 +83,34 @@ const startRepl = (options: {
7283
})
7384
: undefined;
7485

75-
const pongo = pongoClient(options.connectionString, {
86+
pongo = pongoClient(options.connectionString, {
7687
...(schema ? { schema: { definition: schema } } : {}),
7788
});
7889

79-
// Expose the db object to the REPL context
80-
r.context.db = schema ? pongo.database : pongo.db(options.schema.database);
90+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
91+
const db = schema
92+
? // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
93+
(pongo as any).database
94+
: pongo.db(options.schema.database);
8195

82-
// Handle default output formatting
83-
r.context.displayResults = displayResultsAsTable;
96+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
97+
r.context.db = db;
8498

8599
// Intercept REPL output to display results as a table if they are arrays
86-
r.on('exit', () => {
87-
console.log(chalk.yellow('Exiting Pongo Shell...'));
100+
r.on('exit', async () => {
101+
await teardown();
88102
process.exit();
89103
});
90104
};
91105

106+
const teardown = async () => {
107+
console.log(chalk.yellow('Exiting Pongo Shell...'));
108+
await pongo.close();
109+
};
110+
111+
process.on('uncaughtException', teardown);
112+
process.on('SIGINT', teardown);
113+
92114
interface ShellOptions {
93115
database: string;
94116
collection: string[];

0 commit comments

Comments
 (0)