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

@tgriesser/schemats

v9.0.1

Published

Fork of schemats - Generate typescript interface definitions from (postgres) SQL database schema

Downloads

7,324

Readme

Schemats

Upstream fork of https://github.com/SweetIQ/schemats, with new features / options:

  • forInsert: defines any nullable / columns with defaults optional ?:, default false
  • forInsertNull: use with forInsert, makes any null columns required, default false
  • customTypes: define a mapping of custom types, e.g. for emulating enum behavior
  • customHeader: allows adding a custom string header to the generated document
  • prettierConfig: specify a path to prettier to auto-format the output
  • tableNamespaces: whether to add extra namespaces for columns, default false
  • enumManifest: whether to include a lookup of all enums, if a string is provided, the name of the interface
  • tableManifest: whether to include a lookup of all tables, if a string is provided, it's the name of the interface
yarn install @tgriesser/schemats

Using Schemats, you can generate TypeScript interface definitions from (Postgres, MySQL) SQL database schema automatically.

Start with a database schema:

Automatically have the following TypesScript Interface generated

interface Users {
    id: number
    username: string
    password: string
    last_logon: Date
}

For an overview on the motivation and rational behind this project, please take a look at Statically typed PostgreSQL queries in Typescript .

Quick Start

Installing Schemats

npm install -g schemats

Generating the type definition from schema

schemats generate -c postgres://postgres@localhost/osm -t users -o osm.ts
schemats generate -c mysql://mysql@localhost/osm -t users -o osm.ts

The above commands will generate typescript interfaces for osm database with table users. The resulting file is stored as osm.ts.

Generating the type definition for all the tables in a postgres schema

To generate all type definitions for all the tables within the schema 'public':

Note: MySQL does not have a default public schema, but should it have a schema named public, this will still work.

schemats generate -c postgres://postgres@localhost/osm -s public -o osm.ts
schemats generate -c mysql://mysql@localhost/osm -s public -o osm.ts

If neither the table parameter nor the schema parameter is provided, all tables in schema 'public' will be generated, so the command above is equivalent to:

schemats generate -c postgres://postgres@localhost/osm -o osm.ts
schemats generate -c mysql://mysql@localhost/osm -o osm.ts

Using schemats.json config file

Schemats supports reading configuration from a json config file (defaults to schemats.json). Instead of passing configuration via commandline parameter like done above, it is also possible to supply the configuration through a config file. The config file supports the same parameters as the commandline arguments.

For example, if a schemats.json exists in the current working directory with the following content:

{
    "conn": "postgres://postgres@localhost/osm",
    "table": ["users"]
}

Running schemats generate here is equivalent to running schemats generate -c postgres://postgres@localhost/osm -t users -o osm.ts.

Writing code with typed schema

We can import osm.ts directly

// imports the _osm_ namespace from ./osm.ts

import * as osm from './osm'

// Now query with pg-promise and have a completely typed return value

let usersCreatedAfter2013: Array<osm.users> = await db.query(
    "SELECT * FROM users WHERE creation_time >= '2013-01-01'"
)

// We can decide to only get selected fields

let emailOfUsersCreatedAfter2013: Array<{
    email: osm.users['email']
    creation_time: osm.users['creation_time']
}> = await db.query(
    "SELECT (email, creation_time) FROM users WHERE creation_time >= '2013-01-01'"
)

With generated type definition for our database schema, we can write code with autocompletion and static type checks.

Using schemats as a library

Schemats exposes two high-level functions for generating typescript definition from a database schema. They can be used by a build tool such as grunt and gulp.