Skip to content

Commit

Permalink
Customised output for Pongo shell
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Oct 1, 2024
1 parent 17cd40e commit 0bd6b23
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 51 deletions.
58 changes: 29 additions & 29 deletions samples/simple-ts/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 samples/simple-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"dist"
],
"dependencies": {
"@event-driven-io/pongo": "0.15.0-alpha.1"
"@event-driven-io/pongo": "0.15.0-alpha.2"
}
}
10 changes: 5 additions & 5 deletions src/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 src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/pongo-core",
"version": "0.15.0-alpha.2",
"version": "0.15.0",
"description": "Pongo - Mongo with strong consistency on top of Postgres",
"type": "module",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/dumbo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/dumbo",
"version": "0.11.0-alpha.2",
"version": "0.11.0",
"description": "Dumbo - tools for dealing with PostgreSQL",
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/pongo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/pongo",
"version": "0.15.0-alpha.2",
"version": "0.15.0",
"description": "Pongo - Mongo with strong consistency on top of Postgres",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -87,7 +87,7 @@
"pongo": "./dist/cli.js"
},
"peerDependencies": {
"@event-driven-io/dumbo": "0.11.0-alpha.2",
"@event-driven-io/dumbo": "0.11.0",
"@types/mongodb": "^4.0.7",
"@types/pg": "^8.11.6",
"@types/uuid": "^10.0.0",
Expand Down
46 changes: 34 additions & 12 deletions src/packages/pongo/src/commandLine/shell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { JSONSerializer } from '@event-driven-io/dumbo';
import chalk from 'chalk';
import Table from 'cli-table3';
import { Command } from 'commander';
import repl from 'node:repl';
import { pongoClient, pongoSchema } from '../core';
import { pongoClient, pongoSchema, type PongoClient } from '../core';

let pongo: PongoClient;

const calculateColumnWidths = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -23,10 +26,16 @@ const calculateColumnWidths = (
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const displayResultsAsTable = (results: any[]) => {
const printOutput = (obj: any): string => {
return Array.isArray(obj)
? displayResultsAsTable(obj)
: JSONSerializer.serialize(obj);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const displayResultsAsTable = (results: any[]): string => {
if (results.length === 0) {
console.log(chalk.yellow('No documents found.'));
return;
return chalk.yellow('No documents found.');
}

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

console.log(table.toString());
return table.toString();
};

const startRepl = (options: {
Expand All @@ -61,6 +70,8 @@ const startRepl = (options: {
const r = repl.start({
prompt: chalk.green('pongo> '),
useGlobal: true,
breakEvalOnSigint: true,
writer: printOutput,
});

const schema =
Expand All @@ -72,23 +83,34 @@ const startRepl = (options: {
})
: undefined;

const pongo = pongoClient(options.connectionString, {
pongo = pongoClient(options.connectionString, {
...(schema ? { schema: { definition: schema } } : {}),
});

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

// Handle default output formatting
r.context.displayResults = displayResultsAsTable;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
r.context.db = db;

// Intercept REPL output to display results as a table if they are arrays
r.on('exit', () => {
console.log(chalk.yellow('Exiting Pongo Shell...'));
r.on('exit', async () => {
await teardown();
process.exit();
});
};

const teardown = async () => {
console.log(chalk.yellow('Exiting Pongo Shell...'));
await pongo.close();
};

process.on('uncaughtException', teardown);
process.on('SIGINT', teardown);

interface ShellOptions {
database: string;
collection: string[];
Expand Down

0 comments on commit 0bd6b23

Please sign in to comment.