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

ts2sql

v0.9.0

Published

Transformer and helper for typescript tot SQL conversion

Downloads

4

Readme

ts2sql

Generate

Generates files with SQL create queries out of provided typescript files.

Usage: ts2sql mysql --sql-out ../db/sql ./src/User.ts

Arguments

| Argument | Description | | ------------------------------- | ------------------------------------------------------------------ | | first argument, type | Databse type:mysql, postgres, hana, sqlite | | second argument, input, file, i | Typescript input file | | sql-out | SQL files output directory. Default ./sql | | ts-out | Typescript files output directory. Deaults to input file directory | | database, db | Schema or database name in SQL files | | version | Adds a comment with version to the top of the files | | skip-views | Skips generating views | | test | Test parameter skips execution |

Annotations

Table |Name|Description| |----|-----------| |name|Table name| |database|Database name| |charset|Default character set. Default utf8| |collate|Default collate. Default utf8_bin|

Column |Name|Description| |----|-----------| |name|Column name| |type|Column typeDefault: number -> INT(32); string -> VARCHAR(256); boolean -> INT(1); Date -> DATETIME| |primary|Generates PRIMARY KEY| |autoincrement|Generates AUTO_INCREMENT| |unique|Genarates UNIQUE| |key|Generates KEY| |null|Generates NULL. Optional [?] members also generate NULL. Default NOT NULL| |default|Generates DEFAULT 'value'| |serialize|Generates type BLOB and serializes to JSON| |virtual|Member will be ignored and is not reflected in the database table| |hide|Member is hidden in typescript and in select statement but is reflected in the database table| |value|Value for member in insert statement. Default ?|

Example

/**
 * @database test
 */
export interface User {
  /**
   * @primary
   * @autoincrement
   */
  id: number;
  /**
   * @unique
   */
  email: string;
  /**
   * @hide
   */
  password: string;
  /**
   * @null
   * @type DATE
   */
  birthday: Date;
  /**
   * @virtual
   */
  weekday: string;
  /**
   * @sertialize
   */
  roles: Role[];
  /**
   * @default NOW()
   * @value default
   */
  created: Date;
  active?: boolean;
}

creates

create_table_user.gen.sql

CREATE TABLE IF NOT EXISTS test.user (
    id INT(32) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'ts id: number',
    email VARCHAR(256) NOT NULL UNIQUE COMMENT 'ts email: string',
    password VARCHAR(256) NOT NULL COMMENT 'ts password: string',
    birthday DATE NULL COMMENT 'ts birthday: Date',
    roles BLOB NOT NULL COMMENT 'ts roles: Role[]',
    created DATETIME NOT NULL DEFAULT NOW() COMMENT 'ts created: Date',
    active INT(1) NULL COMMENT 'ts active: boolean'
) DEFAULT CHARSET utf8 DEFAULT COLLATE utf8_bin;
UserDatabase.gen.ts;

import { User } from './User.ts';

export interface UserResult {
  id: number;
  email: string;
  birthday: string;
  roles: string;
  created: string;
  active: number;
}

export function transformUserResult(result: UserResult): User {
  return {
    id: result.id,
    email: result.email,
    birthday: new Date(result.birthday),
    weekday: null,
    roles: JSON.parse(result.roles),
    created: new Date(result.created),
    active: !!result.active,
  };
}

export type UserResultSet = UserResult[];

export type UserInsert = [number, string, string, Date, string, Date, boolean];

export const selectUserSql: string = 'SELECT id, email, birthday, roles, created, active FROM test.user';

export const insertUserSql: string =
  'INSERT INTO test.user (id, email, password, birthday, roles, created, active) VALUES (default, ?, ?, ?, ?, default, ?)';

Notice

  • Member with type of a class are ignored and transformed to null
  • Member with type of an enum are transformed as ENUM
  • Member with type of an alias are transformed in their original type
  • Member with multiple type are transformed as their first type
  • Member with fixed types are transformed as ENUM
  • Member with array types of interfaces are transformed into SQL views
  • Member with other array types are ignored