Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/noorzaie/sqema

Convert relational databases to json schema format
https://github.com/noorzaie/sqema

json-schema postgresql schema sql validation

Last synced: 6 days ago
JSON representation

Convert relational databases to json schema format

Awesome Lists containing this project

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
`schema`: Write each schema in separate files
`table`: Write each table in separate file
`field`: Write each field in separate file | single-file |
| format | Format of output | `json`
`js`
`ts` | json |

### Using cli
You can use `sqma` command with following options:
```
Options:
-d, --dialect database dialect (choices: "postgres", default: "postgres")
-h, --host database host
-port, --port database port
-db, --database database name
-u, --username database username
-p, --path output folder (default: "output")
-g, --granularity output files granularity (choices: "single-file", "schema", "table", "field", default: "single-file")
-f, --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.