Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/noorzaie/sqema
- Owner: noorzaie
- Created: 2021-12-05T17:45:08.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2021-12-05T17:46:00.000Z (almost 3 years ago)
- Last Synced: 2024-10-10T11:22:47.956Z (27 days ago)
- Topics: json-schema, postgresql, schema, sql, validation
- Language: TypeScript
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.