npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-rows

v0.2.3

Published

Extract metadata from mysql tables to produce typescript interfaces for rows results.

Downloads

8

Readme

typed-rows

typed-rows is a little command-line tool to generate type information for mysql tables in form of Typescript Interfaces.

Motivation

Typescript language provides great safety checks and tooling for javascript. If you query the database the rows that you get using the default node-mysql query function are a collection of the any object type.

    connection.query('SELECT * FROM Users;', (err, rows) => {
        rows[0].id; // No type information
    });

You can manually provide the type information for the type of result that you are expecting using an interface:

    interface IUsers {
        id : number;
        name : string;
    }
    connection.query('SELECT * FROM Users;', (err, rows : IUsers[]) => {
        rows[0].id; // Now you have type information!
    });

That works but if you have a big database with a lot of tables and columns could be very tedious to write (and maintain) all these interfaces. So you can use typed-rows to automatically generate a TyepedRows.ts file with all the interfaces generated automatically from your database.

    $ typed-rows --database=phpmyadmin --user=root --outfile=TypedRows.ts
    Password:
    $ cat TypedRows.ts
    export interface IPmaTracking { 
            db_name : string;
            table_name : string;
            version : number;
            date_created : Date;
            date_updated : Date;
            schema_snapshot : string;
            schema_sql? : string;
            data_sql? : string;
            tracking? : any;
            tracking_active : number;
    }
    
    export interface IPmaUserconfig { 
            username : string;
            timevalue : any;
            config_data : string;
    }
    
    export interface IPmaUsergroups { 
            usergroup : string;
            tab : string;
            allowed : 'Y' | 'N';
    }
    
    export interface IPmaUsers { 
            username : string;
            usergroup : string;
    }
    //...

Now you can import this file and use the interfaces to have typed rows.

    import {IPmaUsergroups} from "./TypedRows";
    connection.query('SELECT * FROM pma__usergroups;', (err, rows : IPmaUsergroups[]) => {
        rows[0].allowed = 'Y';
    });

Helpers

If you are using typed-rows could be nice to have a generic function like:

  export interface FunDb<T> {
    (err: mysql.IError, rows: T[]): void;
  }

  function typedRows<T>(q, replaces?, callback? : FunDb<T>) {
     return conn.query(q, replaces, (err, result : T[]) => {
       if (err) {
         console.log('Error Query', q,  err);
       }
       if (callback) {
         callback(err, result);
       }
     });
  }

Then you can use typedRows function with a TypedRows interface:

    import {IPmaUsergroups} from "./TypedRows";
    typedRows<IPmaUsergroups>('SELECT * FROM pma__usergroups;', callback);    

Features

  • Supported mysql fields: text, varchar, int, integer, smalint, tinyint, mediumint, decimal, numeric, double, bigint, float, decimal, enum, datetime.
  • If a mysql data type is not recognized the column would be any.
  • The generated interfaces are compatible with the automatic typecasts do it by node-mysql. Eg. a mysql datetime type would be mapped to a Date class in the typed-rows generated interface.

Installation & usage

npm installation:

  $ npm install -g typed-rows

The installation should give you a global typed-rows command, which you can use directly to extract type information from a mysql database and write the interfaces to the standard output.

  Usage: typed-rows [options]

  Options:

    -h, --help                 output usage information
    -V, --version              output the version number
    -u, --user [user]          Mysql user - Default root
    -p, --password [password]  mysql password
    -d, --database <database>  database name
    -h, --host [host]          database host - Default 127.0.0.1
    -p, --port [port]          database port - Default 3306
    -o, --outfile <outfile>    Out file - Default ./TypedRows.ts

Tests

To run tests you must create an empty database and configure a test/config.json based on test/config_template.json.

{
  "dbTest": {
    "user": "root",
    "password": "",
    "database": "typed_rows_test",
    "host": "127.0.0.1"
  }
}

Then you simply run:

npm test

TODO

  • More databases support (postgresql, sqlite).
  • More datatypes support.
  • More testing.
  • Test windows & mac support.