GithubHelp home page GithubHelp logo

thaitype / data-viewer Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 4.98 MB

Display Node.js data with live-reload in a web-based table viewer

TypeScript 54.68% JavaScript 9.74% EJS 35.58%
data-viewer datagrid datatable dev-tool local-development node-js table live-reload

data-viewer's Introduction

Data-viewer

Display Node.js data with live-reload in a web-based table viewer.

Build npm version npm download

Motivation

When developing Node.js applications, it's often crucial to inspect and visualize data in a tabular format. While tools like console-table-printer or console.table are helpful, they may fall short when dealing with large datasets or multiple columns. This project aims to simplify the process of viewing tabular data by providing an easy-to-use and efficient web-based solution.

Features

  • Live Reload: Automatically updates the displayed data when the server is restarted.
  • Dynamic Table Support: Works seamlessly with Record<string, unknown>[] data type and straightforward headers.
  • Support Custom Server: Integrate with your existing server.
  • Custom Logger: Integrate with your existing log.

Demo

Installation

npm install @thaitype/data-viewer-server

Getting Started

  1. Create a file named main.ts:
import dataViewer from '@thaitype/data-viewer-server';

const getUsers = async () => (await fetch('https://jsonplaceholder.typicode.com/users')).json();
const getPosts = async () => (await fetch('https://jsonplaceholder.typicode.com/posts')).json();

async function main() {
  dataViewer.addHeader('User Table');
  dataViewer.addTable(await getUsers());
  dataViewer.addHeader('Post Table');
  dataViewer.addTable(await getPosts());
  dataViewer.start();
}

main();
  1. Run in watch mode using your preferred tool:
tsx watch main.ts
  1. The view is automatically updated when the server restarts.

Create Data Container

Sometimes, you may want to create a data container and add data to it later.

export function myReport(){
  const container = new Container();
  container.addHeader('My Header');
  return container.get();
}

dataViewer.addContainer(myReport());
dataViewer.start();

Support Custom Server

You can use the registerMiddleware method to integrate with your existing server.

  const getUsers = async () => (await fetch('https://jsonplaceholder.typicode.com/users')).json();

const dataViewer = new DataViewer({
  path: '/viewer',
});

dataViewer.addHeader('User Table');
dataViewer.addTable(await getUsers());

const app = express();
dataViewer.registerMiddleware(app);

app.listen(3000, async () => console.log(`Already servered on http://localhost:3000/viewer`));

Support Custom Logger

You can use the logger option to integrate with your existing log, this example uses pino log

import pino from 'pino';
const logger = pino();

const stringLogger = {
  log: (message: string) => logger.info(message),
  debug: (message: string) => logger.debug(message),
  info: (message: string) => logger.info(message),
  warn: (message: string) => logger.warn(message),
  error: (message: string) => logger.error(message),
};

const dataViewer = new DataViewer({
  path: '/viewer',
  logger: stringLogger,
}).start();

Idea Behind the Scene

Manual

Disable Live Reload

By default, start method enables live reload automatically. You can disable live reload by setting the enableLiveReload option to false.

const dataViewer = new DataViewer({
  enableLiveReload: false,
})
dataViewer.start();

When use use the custom server by DataViewer.registerMiddleware method, you don't need to set enableLiveReload option. Because it's already disabled by default.

Custom Cell Formatter Function

You can define a custom cell formatting function to tailor the appearance of individual cells. This function takes a cell value as input and returns the formatted string to be displayed in the table.

Example:

dataViewer.setOption({
  cellFormatter: cell => {
    if (typeof cell === 'object') {
      return JSON.stringify(cell);
    }
    return String(cell);
  },
});

In this example, the cell formatter checks if the cell value is an object, and if so, it converts it to a JSON string. Otherwise, it converts the cell value to a string.

Setup Log Level

DataViewer.start method has built-in express server and uses the pino package internally. Example: set up debug log level.

dataViewer
  .start({
    loggerOption: {
      level: 'debug',
    }
  });

Setup Port

Example: Set up the server to run on port 5000.

dataViewer
  .start({
    port: 5000,
  });

Setup View (EJS Template) Directory

Example: Specify a custom directory for EJS templates.

dataViewer.setOption({ 
  viewDirectory: __dirname + '/views'
});

By default, the viewDirectory is set to __dirname + '/views'.

Create a New Instance

You can create a new instance.

import { DataViewer } from '@thaitype/data-viewer-server';

const myViewer = new DataViewer();

Acknowledgement

License

This project is licensed under the MIT License.

data-viewer's People

Contributors

github-actions[bot] avatar mildronize avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

data-viewer's Issues

Act like devtool on local

  • add env var to control to run server or not
  • add like global import for all codes. like import '@thaitype/data-viewer-server';

Next Step

  • Deploy Next.js version data table, run express on local, send data payload to the server to render
  • ability to shared data-viewer with data (store data in GET params)

Provide express middleware as external library

import { DataViewer } from '@thaitype/data-viewer-server';

const getUsers = async () => (await fetch('https://jsonplaceholder.typicode.com/users')).json();
const getPosts = async () => (await fetch('https://jsonplaceholder.typicode.com/posts')).json();

const dataViewer = new DataViewer();

dataViewer.addHeader('User Table');
dataViewer.addTable((await getUsers()));
dataViewer.addHeader('Post Table');
dataViewer.addTable(await getPosts());

const app = express();
dataViewer.registerMiddleware(app);
app.listen(3000, async () => console.log(`Already servered on http://localhost:3000/viewer`));

Ability to create an empty container for rendering later

export function myReport(){
  const container = new Container();
  container.addHeader('');
  container.addTable([]);
  
  return container.get();
}

in another file

const result = myReport();
dataViewer.addContainer(result);
dataViewer.start();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.