-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
116 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
test.ts | ||
test.js | ||
node_modules | ||
output | ||
.idea/ | ||
.idea | ||
*.js | ||
*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
.idea | ||
*.ts | ||
!*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import SQLToJsonSchemaConvertor from '../index'; | ||
|
||
const convertor = new SQLToJsonSchemaConvertor('postgres', { host: 'localhost', port: 5432, database: 'test_db', username: 'postgres', password: 'postgres'}); | ||
|
||
// Generate all possible outputs and write them in output directory | ||
convertor.generateJsonSchemas() | ||
.then(() => { | ||
const granularities = [ 'single-file', 'schema', 'table', 'field' ]; | ||
const outputFormats = [ 'json', 'js', 'ts' ]; | ||
for (const granularity of granularities) { | ||
for (const format of outputFormats) { | ||
// @ts-ignore | ||
convertor.writeJsonSchemas(`output/${granularity}/${format}`, granularity, format); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
{ | ||
"name": "sqema", | ||
"version": "1.0.0", | ||
"description": "Convert relational databases to json schemas", | ||
"version": "1.0.2", | ||
"description": "Convert relational databases to json schema format", | ||
"keywords": [ "json-schema", "sql", "postgresql", "schema", "validation" ], | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "AzizAhmad Noorzaie" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/noorzaie/sqema" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ts-node test.ts", | ||
"build": "tsc" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"dependencies": { | ||
"commander": "^8.2.0", | ||
"pg-structure": "^7.12.1", | ||
|
@@ -21,5 +29,6 @@ | |
"ts-node": "^10.2.1", | ||
"typescript": "^4.4.3" | ||
}, | ||
"bin": "cli/cli.js" | ||
"bin": "cli/cli.js", | ||
"types": "index.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters