# sqema

> Convert relational databases to json schema format

Latest version **1.0.2** (published 2021-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install sqema
pnpm add sqema
yarn add sqema
bun add sqema
```

Provides the command `sqema`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-12-05 |
| First published | 2021-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 43.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | AzizAhmad Noorzaie |
| Maintainers | aanoorzaie |
| Keywords | json-schema, sql, postgresql, schema, validation |

## Links

- npm: https://www.npmjs.com/package/sqema
- Repository: https://github.com/noorzaie/sqema
- Homepage: https://github.com/noorzaie/sqema#readme
- Issues: https://github.com/noorzaie/sqema/issues
- npm.io page: https://npm.io/package/sqema

## Dependencies (3)

- [prompts](https://npm.io/package/prompts.md) ^2.4.2
- [commander](https://npm.io/package/commander.md) ^8.2.0
- [pg-structure](https://npm.io/package/pg-structure.md) ^7.12.1

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-12-05
- 1.0.1 — 2021-12-05

## README

When you create a web application, you need to validate client-side inputs, most of inputs are same as database fields and you need to create validation schema manually. With `sqema` you can automatically export your sql tables to [json-schema](https://json-schema.org/) format.

## Installation
```npm
npm install sqema
```

## Usage
### In scripts
```typescript
import SQLToJsonSchemaConvertor from 'sqema';

const convertor = new SQLToJsonSchemaConvertor(
    'postgres', 
    { host: 'localhost', port: 5432, database: '***', username: '***', password: '***'}
);

convertor.generateJsonSchemas()
    .then(() => {
        // This will write json schemas to output directory
        convertor.writeJsonSchemas('output', 'single-file', 'json');
    });
```

#### `writeJsonSchemas` Options
| Option  | Description | Possible values | Default value |
| ------------- | ------------- | ------------- | ------------- |
| path | Path of a directory to write schemas  | Any string  | output  |
| granularity  | Scale of data to be written  | `single-file`: Write all schemas in single file <br> `schema`: Write each schema in separate files <br> `table`: Write each table in separate file <br> `field`: Write each field in separate file  | single-file  |
| format  | Format of output  | `json` <br> `js` <br> `ts`  | json  |

### Using cli
You can use `sqma` command with following options:
```
Options:
  -d, --dialect <dialect>          database dialect (choices: "postgres", default: "postgres")
  -h, --host <host>                database host
  -port, --port <port>             database port
  -db, --database <database>       database name
  -u, --username <username>        database username
  -p, --path <path>                output folder (default: "output")
  -g, --granularity <granularity>  output files granularity (choices: "single-file", "schema", "table", "field", default: "single-file")
  -f, --format <format>            output format (choices: "json", "js", "ts", default: "json")
  --help                           display help for command
```

## Note
This library currently supports `postgresql` database and also some complex data types not implemented yet.

---
_Source: https://npm.io/package/sqema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
