JSPM

  • Created
  • Published
  • Downloads 875
  • Score
    100M100P100Q102406F
  • License MIT

Generate typescript interface definitions from (postgres) SQL database schema

Package Exports

  • schemats

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (schemats) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

schemats

npm GitHub tag CircleCI Coverage Status

Generate typescript interface definitions from (postgres) SQL database schema

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

The command above 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':

schemats generate -c postgres://postgres@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

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.usersFields.email,
    creation_time: osm.usersFields.creation_time
}> = await db.query("SELECT (email, creation_time) FROM users WHERE creation_time >= '2013-01-01'");

// osm.usersFields.name is just a type alias to string
// and osm.usersFields.creation_time is just a type alias to Date
// Hence the one below also works

let emailOfUsersCreatedAfter2013: Array<{
    email: string,
    creation_time: Date
}> = 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.

demo 1

demo 2

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.

Upgrading to v1.0

Deprecation of Namespace

Version 1.0 deprecates generating schema typescript files with namespace.

Instead of generating schema typescript files with

schemats generate -c postgres://postgres@localhost/db -n yournamespace -o db.ts

and import them with

import {yournamespace} from './db'

It is now encouraged to generate without namespace

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

and import them with

import * as yournamespace from './db'
// or
import {table_a, table_b} from './db'

As TypeScript's documentation describes, having a top level namespace is needless. This was discussed in #25.

Generating schema typescript files with namespace still works in v1.0, but it is discouraged and subjected to removal in the future.

Support Strict Null-Checking

Version 1.0 supports strict null-checking and reflects the NOT NULL constraint defined in PostgreSQL schema.

Contributing

👍🎉 First off, thanks for taking the time to contribute! 🎉👍

Steps to contribute:

  • Make your awesome changes
  • Run npm run lint
  • Optionally, run DATABASE_URL="postgres://youruser@localhost/anyemptytestdatabase" npm test
  • Submit pull request

Our project runs npm test automatically on pull requests via CircleCI.