All Projects → SweetIQ → Schemats

SweetIQ / Schemats

Licence: mit
Generate typescript interface definitions from SQL database schema

Programming Languages

typescript
32286 projects
reflection
70 projects

Projects that are alternatives of or similar to Schemats

Pg chameleon
MySQL to PostgreSQL replica system
Stars: ✭ 274 (-65.71%)
Mutual labels:  mysql, postgresql, postgres
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (-7.26%)
Mutual labels:  mysql, postgresql, postgres
Dbq
Zero boilerplate database operations for Go
Stars: ✭ 273 (-65.83%)
Mutual labels:  mysql, postgresql, postgres
E Commerce Db
Database schema for e-commerce (webstores) sites.
Stars: ✭ 245 (-69.34%)
Mutual labels:  schema, mysql, postgresql
With advisory lock
Advisory locking for ActiveRecord
Stars: ✭ 409 (-48.81%)
Mutual labels:  mysql, postgresql, postgres
Auto App
Crie um aplicativo com todas as tabelas de um dos seus bancos sem uma linha de código.
Stars: ✭ 18 (-97.75%)
Mutual labels:  automation, mysql, postgresql
Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (-53.32%)
Mutual labels:  mysql, postgresql, postgres
Sql Lint
An SQL linter
Stars: ✭ 243 (-69.59%)
Mutual labels:  mysql, postgresql, postgres
Sqlboiler
Generate a Go ORM tailored to your database schema.
Stars: ✭ 4,497 (+462.83%)
Mutual labels:  mysql, postgresql, postgres
Deno Nessie
A modular Deno library for PostgreSQL, MySQL, MariaDB and SQLite migrations
Stars: ✭ 381 (-52.32%)
Mutual labels:  mysql, postgresql, postgres
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+164.33%)
Mutual labels:  schema, mysql, postgres
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (-4.01%)
Mutual labels:  schema, mysql, postgresql
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+1272.59%)
Mutual labels:  schema, postgresql, postgres
Metabase
The simplest, fastest way to get business intelligence and analytics to everyone in your company 😋
Stars: ✭ 26,803 (+3254.57%)
Mutual labels:  mysql, postgresql, postgres
Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-92.87%)
Mutual labels:  schema, postgresql, postgres
Yiigo
🔥 Go 轻量级开发通用库 🚀🚀🚀
Stars: ✭ 304 (-61.95%)
Mutual labels:  mysql, postgresql, postgres
Condenser
Condenser is a database subsetting tool
Stars: ✭ 189 (-76.35%)
Mutual labels:  mysql, postgresql, postgres
Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (-71.09%)
Mutual labels:  mysql, postgresql, postgres
Sqlx
🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL.
Stars: ✭ 5,039 (+530.66%)
Mutual labels:  mysql, postgresql, postgres
Blog
Everything about database,business.(Most for PostgreSQL).
Stars: ✭ 6,330 (+692.24%)
Mutual labels:  mysql, postgresql, postgres

Schemats

npm GitHub tag TravisCI Build Status Coverage Status

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

Start with a database schema:

Users
id SERIAL
username VARCHAR
password VARCHAR
last_logon TIMESTAMP

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://[email protected]/osm -t users -o osm.ts
schemats generate -c mysql://[email protected]/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://[email protected]/osm -s public -o osm.ts
schemats generate -c mysql://[email protected]/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://[email protected]/osm -o osm.ts
schemats generate -c mysql://[email protected]/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://[email protected]/osm",
    "table": ["users"]
}

Running schemats generate here is equivalent to running schemats generate -c postgres://[email protected]/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.

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://[email protected]/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://[email protected]/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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].