{"id":13683697,"url":"https://github.com/SweetIQ/schemats","last_synced_at":"2025-04-30T13:31:45.632Z","repository":{"id":38408388,"uuid":"65752808","full_name":"SweetIQ/schemats","owner":"SweetIQ","description":" Generate typescript interface definitions from SQL database schema","archived":false,"fork":false,"pushed_at":"2022-01-06T15:13:34.000Z","size":1192,"stargazers_count":1048,"open_issues_count":49,"forks_count":106,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-22T18:12:07.206Z","etag":null,"topics":["automation","mysql","postgres","postgresql","reflection","schema","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/schemats","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SweetIQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-15T17:54:26.000Z","updated_at":"2025-04-14T11:18:18.000Z","dependencies_parsed_at":"2022-08-21T05:30:30.025Z","dependency_job_id":null,"html_url":"https://github.com/SweetIQ/schemats","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SweetIQ%2Fschemats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SweetIQ%2Fschemats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SweetIQ%2Fschemats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SweetIQ%2Fschemats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SweetIQ","download_url":"https://codeload.github.com/SweetIQ/schemats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251712896,"owners_count":21631461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","mysql","postgres","postgresql","reflection","schema","typescript"],"created_at":"2024-08-02T13:02:24.115Z","updated_at":"2025-04-30T13:31:43.892Z","avatar_url":"https://github.com/SweetIQ.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Tools"],"sub_categories":["IDE"],"readme":"# Schemats\n\n[![npm](https://img.shields.io/npm/v/schemats.svg)](https://www.npmjs.com/package/schemats)\n[![GitHub tag](https://img.shields.io/github/tag/SweetIQ/schemats.svg)](https://github.com/SweetIQ/schemats)\n[![TravisCI Build Status](https://travis-ci.org/SweetIQ/schemats.svg?branch=master)](https://travis-ci.org/SweetIQ/schemats)\n[![Coverage Status](https://coveralls.io/repos/github/SweetIQ/schemats/badge.svg?branch=coverage)](https://coveralls.io/github/SweetIQ/schemats?branch=coverage)\n\nUsing Schemats, you can generate TypeScript interface definitions from (Postgres, MySQL) SQL database schema automatically.\n\nStart with a database schema: \n\n\u003ctable\u003e\n\u003ctr\u003e\u003cth colspan=\"2\"\u003eUsers\u003c/th\u003e\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003eid\u003c/td\u003e\u003ctd\u003eSERIAL\u003c/td\u003e\n\u003c/tr\u003e\u003ctr\u003e\n\u003ctd\u003eusername\u003c/td\u003e\u003ctd\u003eVARCHAR\u003c/td\u003e\n\u003c/tr\u003e\u003ctr\u003e\n\u003ctd\u003epassword\u003c/td\u003e\u003ctd\u003eVARCHAR\u003c/td\u003e\n\u003c/tr\u003e\u003ctr\u003e\n\u003ctd\u003elast_logon\u003c/td\u003e\u003ctd\u003eTIMESTAMP\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\nAutomatically have the following TypesScript Interface generated\n\n```typescript\ninterface Users {\n    id: number;\n    username: string;\n    password: string;\n    last_logon: Date;\n}\n```\n\n\nFor an overview on the motivation and rational behind this project, please take a look at [Statically typed PostgreSQL queries in Typescript\n](http://cs.mcgill.ca/~mxia3/2016/11/18/Statically-typed-PostgreSQL-queries-and-typescript-schemats/).\n\n## Quick Start\n\n### Installing Schemats\n\n```\nnpm install -g schemats\n```\n\n### Generating the type definition from schema\n\n```\nschemats generate -c postgres://postgres@localhost/osm -t users -o osm.ts\nschemats generate -c mysql://mysql@localhost/osm -t users -o osm.ts\n```\n\n\nThe above commands will generate typescript interfaces for [`osm`](test/osm_schema.sql) database \nwith table [`users`](test/osm_schema.sql#L18). The resulting file is stored as [`osm.ts`](test/example/osm.ts).\n\n### Generating the type definition for all the tables in a postgres schema\n\nTo generate all type definitions for all the tables within the schema 'public': \n\n*Note: MySQL does not have a default public schema, but should it have a schema named public, this will still work.*\n\n```\nschemats generate -c postgres://postgres@localhost/osm -s public -o osm.ts\nschemats generate -c mysql://mysql@localhost/osm -s public -o osm.ts\n```\n\nIf 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:\n\n```\nschemats generate -c postgres://postgres@localhost/osm -o osm.ts\nschemats generate -c mysql://mysql@localhost/osm -o osm.ts\n```\n\n### Using schemats.json config file \n\nSchemats 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.\n\nFor example, if a `schemats.json` exists in the current working directory with the following content:\n\n```json\n{\n    \"conn\": \"postgres://postgres@localhost/osm\",\n    \"table\": [\"users\"]\n}\n```\n\nRunning `schemats generate` here is equivalent to running `schemats generate -c postgres://postgres@localhost/osm -t users -o osm.ts`. \n\n### Writing code with typed schema\n\nWe can import `osm.ts` directly\n\n```typescript\n\n// imports the _osm_ namespace from ./osm.ts\n\nimport * as osm from './osm'\n\n\n// Now query with pg-promise and have a completely typed return value\n  \nlet usersCreatedAfter2013: Array\u003cosm.users\u003e\n   = await db.query(\"SELECT * FROM users WHERE creation_time \u003e= '2013-01-01'\");\n\n// We can decide to only get selected fields\n\nlet emailOfUsersCreatedAfter2013: Array\u003c{\n    email: osm.users['email'],\n    creation_time: osm.users['creation_time']\n}\u003e = await db.query(\"SELECT (email, creation_time) FROM users WHERE creation_time \u003e= '2013-01-01'\");\n```\n\nWith generated type definition for our database schema, we can write code with autocompletion and static type checks.\n\n\u003cp align=\"center\"\u003e\n\u003cimg align=\"center\" src=\"https://github.com/SweetIQ/schemats/raw/master/demo.gif\" width=\"100%\" alt=\"demo 1\"/\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003cimg align=\"center\" src=\"https://github.com/SweetIQ/schemats/raw/master/demo2.gif\" width=\"100%\" alt=\"demo 2\"/\u003e\n\u003c/p\u003e\n\n### Using schemats as a library\n\nSchemats 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.\n\n### Upgrading to v1.0\n\n#### Deprecation of Namespace\nVersion 1.0 deprecates generating schema typescript files with namespace. \n\nInstead of generating schema typescript files with\n\n```bash\nschemats generate -c postgres://postgres@localhost/db -n yournamespace -o db.ts\n```\n\nand import them with \n```typescript\nimport {yournamespace} from './db'\n```\n\nIt is now encouraged to generate without namespace\n```bash\nschemats generate -c postgres://postgres@localhost/db -o db.ts\n```\nand import them with \n```typescript\nimport * as yournamespace from './db'\n// or\nimport {table_a, table_b} from './db'\n```\n\nAs [TypeScript's documentation](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html) describes,\nhaving a top level namespace is needless. This was discussed in [#25](https://github.com/SweetIQ/schemats/issues/25).\n\nGenerating schema typescript files with namespace still works in v1.0, but it is discouraged and subjected to \nremoval in the future.\n\n#### Support Strict Null-Checking\n\nVersion 1.0 [supports](https://github.com/SweetIQ/schemats/issues/19)\n [strict null-checking](https://github.com/Microsoft/TypeScript/pull/7140)\nand reflects the _NOT NULL_ constraint defined in PostgreSQL schema.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSweetIQ%2Fschemats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSweetIQ%2Fschemats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSweetIQ%2Fschemats/lists"}