Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

schema-typegen

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-typegen

Generate TypeScript type definitions from database schema

  • 2.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

Welcome to schema-typegen 👋

Version Maintenance License: MIT Coverage Status CI

Generate TypeScript type definitions from database schema.

Install

npm install schema-typegen

Usage

For now only Postgres is supported.

Usage: schema-typegen [opts] connection_string

Example:
schema-typegen -o ./entities.ts postgres://username:password@localhost:5432/database

Available options:

  -f/--suffix SUFFIX
        Suffix to append to generated table type, e.g. item -> ItemEntity. Default: 'Entity'.
  -s/--schema SCHEMA
        Schema. Default: 'public'.
  -h/--header HEADER
        Header content. Default: ''.
  -o/--output OUTPUT_PATH
        Output path, e.g. './entities.ts'. Default: outputs to stdout.
  -e/--exclude EXCLUDE
        Excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock  Default: ''.
  --types
        Use type definitions instead of interfaces in generated output. Default: false.
  --noSemi
        Omit semicolons in generated types. Default: false.
  --ssl
        Use ssl. Default: false.
  --optionals
        Use optionals "?" instead of null. Default: false.
  -v/--version
        Print the version number.
  -h/--help
        Print this menu.

Given database table

CREATE TYPE user_state AS ENUM (
  'asleep',
  'awake'
);

CREATE TABLE users (
    id int4 NOT NULL,
    name varchar(255),
    state user_state,
    is_enabled bool NOT NULL DEFAULT FALSE
);

Running schema-typegen -o ./entities.ts postgres://username:password@localhost:5432/database Will generate the following type definitions

enum UserState {
  asleep = 'asleep',
  awake = 'awake'
}

interface UserEntity {
  id: number;
  name: string | null;
  state: UserState | null;
  is_enabled: boolean;
}

Loading database config

from .env file
export $(grep -v '^#' .env | xargs) && schema-typegen -o ./entities.ts postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME
from json file
schema-typegen -o ./entities.ts postgres://$(jq -r '.DB.USERNAME' config.json):$(jq -r '.DB.PASSWORD' config.json)@$(jq -r '.DB.HOST' config.json):$(jq -r '.DB.PORT' config.json)/$(jq -r '.DB.NAME' config.json)

Run tests

docker-compose up -d
npm test

Contributing

Contributions, issues and feature requests are welcome!

License

Copyright © 2020 Aldis Ameriks.
This project is MIT licensed.

Keywords

FAQs

Package last updated on 03 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc