@platformatic/sql-json-schema-mapper 3.4.1 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint.config.js +2 -2
- package/index.js +25 -17
- package/package.json +15 -11
package/eslint.config.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import neostandard from 'neostandard'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default neostandard({ ts: true })
|
package/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import CodeBlockWriter from 'code-block-writer'
|
|
2
|
+
import { property } from 'safe-identifier'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const { property } = require('safe-identifier')
|
|
5
|
-
|
|
6
|
-
function mapSQLTypeToOpenAPIType (sqlType) {
|
|
4
|
+
export function mapSQLTypeToOpenAPIType (sqlType) {
|
|
7
5
|
// TODO support more types
|
|
8
6
|
/* istanbul ignore next */
|
|
9
7
|
switch (sqlType) {
|
|
@@ -60,7 +58,7 @@ function mapSQLTypeToOpenAPIType (sqlType) {
|
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
60
|
|
|
63
|
-
function mapSQLEntityToJSONSchema (entity, ignore = {}, noRequired = false) {
|
|
61
|
+
export function mapSQLEntityToJSONSchema (entity, ignore = {}, noRequired = false) {
|
|
64
62
|
const fields = entity.fields
|
|
65
63
|
const properties = {}
|
|
66
64
|
const required = []
|
|
@@ -76,13 +74,13 @@ function mapSQLEntityToJSONSchema (entity, ignore = {}, noRequired = false) {
|
|
|
76
74
|
properties[field.camelcase] = {
|
|
77
75
|
type: 'array',
|
|
78
76
|
items: {
|
|
79
|
-
type
|
|
80
|
-
}
|
|
77
|
+
type
|
|
78
|
+
}
|
|
81
79
|
}
|
|
82
80
|
} else if (field.sqlType === 'json' || field.sqlType === 'jsonb') {
|
|
83
81
|
properties[field.camelcase] = {
|
|
84
82
|
type: 'object',
|
|
85
|
-
additionalProperties: true
|
|
83
|
+
additionalProperties: true
|
|
86
84
|
}
|
|
87
85
|
} else {
|
|
88
86
|
properties[field.camelcase] = { type }
|
|
@@ -109,7 +107,7 @@ function mapSQLEntityToJSONSchema (entity, ignore = {}, noRequired = false) {
|
|
|
109
107
|
description: `A ${entity.name}`,
|
|
110
108
|
type: 'object',
|
|
111
109
|
properties,
|
|
112
|
-
additionalProperties: false
|
|
110
|
+
additionalProperties: false
|
|
113
111
|
}
|
|
114
112
|
|
|
115
113
|
if (required.length > 0) {
|
|
@@ -119,10 +117,10 @@ function mapSQLEntityToJSONSchema (entity, ignore = {}, noRequired = false) {
|
|
|
119
117
|
return res
|
|
120
118
|
}
|
|
121
119
|
|
|
122
|
-
function mapOpenAPItoTypes (obj, fieldDefinitions, opts = {}) {
|
|
120
|
+
export function mapOpenAPItoTypes (obj, fieldDefinitions, opts = {}) {
|
|
123
121
|
let { writer, addedProps } = opts
|
|
124
122
|
addedProps ??= new Set()
|
|
125
|
-
writer ??= new CodeBlockWriter()
|
|
123
|
+
writer ??= new CodeBlockWriter({ indentNumberOfSpaces: opts.indentSpaces ?? 4 })
|
|
126
124
|
const { title, description, properties, required, additionalProperties } = obj
|
|
127
125
|
writer.write('/**').newLine()
|
|
128
126
|
writer.write(` * ${title}`).newLine()
|
|
@@ -134,7 +132,14 @@ function mapOpenAPItoTypes (obj, fieldDefinitions, opts = {}) {
|
|
|
134
132
|
return writer.toString()
|
|
135
133
|
}
|
|
136
134
|
|
|
137
|
-
function renderProperties (
|
|
135
|
+
function renderProperties (
|
|
136
|
+
writer,
|
|
137
|
+
addedProps,
|
|
138
|
+
properties = {},
|
|
139
|
+
additionalProperties,
|
|
140
|
+
required = [],
|
|
141
|
+
fieldDefinitions = {}
|
|
142
|
+
) {
|
|
138
143
|
// Since Array.prototype.sort is guaranteed to be stable, we can sort by name first, then apply special sorting rules
|
|
139
144
|
const keys = Object.keys(properties)
|
|
140
145
|
.sort()
|
|
@@ -185,7 +190,7 @@ function renderProperties (writer, addedProps, properties = {}, additionalProper
|
|
|
185
190
|
})
|
|
186
191
|
writer.write('[]')
|
|
187
192
|
break
|
|
188
|
-
|
|
193
|
+
// TODO support arrays in arrays
|
|
189
194
|
default:
|
|
190
195
|
writer.write(`${JSONSchemaToTsType(items.type)}[]`)
|
|
191
196
|
}
|
|
@@ -195,7 +200,12 @@ function renderProperties (writer, addedProps, properties = {}, additionalProper
|
|
|
195
200
|
renderProperties(writer, addedProps, current.properties, current.additionalProperties, current.required)
|
|
196
201
|
})
|
|
197
202
|
} else if (type === 'string' && localProperty.enum) {
|
|
198
|
-
writer.write(
|
|
203
|
+
writer.write(
|
|
204
|
+
localProperty.enum
|
|
205
|
+
.sort()
|
|
206
|
+
.map(v => `"${v}"`)
|
|
207
|
+
.join(' | ')
|
|
208
|
+
)
|
|
199
209
|
} else {
|
|
200
210
|
writer.write(JSONSchemaToTsType(type))
|
|
201
211
|
}
|
|
@@ -227,5 +237,3 @@ function JSONSchemaToTsType (type) {
|
|
|
227
237
|
return 'any'
|
|
228
238
|
}
|
|
229
239
|
}
|
|
230
|
-
|
|
231
|
-
module.exports = { mapSQLTypeToOpenAPIType, mapSQLEntityToJSONSchema, mapOpenAPItoTypes }
|
package/package.json
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-json-schema-mapper",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "Map SQL entity to JSON schema",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
9
10
|
},
|
|
10
|
-
"author": "
|
|
11
|
+
"author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
|
|
11
12
|
"license": "Apache-2.0",
|
|
12
13
|
"bugs": {
|
|
13
14
|
"url": "https://github.com/platformatic/platformatic/issues"
|
|
14
15
|
},
|
|
15
16
|
"homepage": "https://github.com/platformatic/platformatic#readme",
|
|
16
17
|
"devDependencies": {
|
|
17
|
-
"
|
|
18
|
+
"cleaner-spec-reporter": "^0.5.0",
|
|
18
19
|
"dtsgenerator": "^3.19.1",
|
|
19
20
|
"eslint": "9",
|
|
20
21
|
"fastify": "^5.0.0",
|
|
21
|
-
"neostandard": "^0.
|
|
22
|
+
"neostandard": "^0.12.0",
|
|
22
23
|
"typescript": "^5.5.4",
|
|
23
24
|
"why-is-node-running": "^2.2.2",
|
|
24
|
-
"@platformatic/sql-mapper": "3.
|
|
25
|
+
"@platformatic/sql-mapper": "3.5.0"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"code-block-writer": "^13.0.1",
|
|
28
29
|
"safe-identifier": "^0.4.2"
|
|
29
30
|
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22.19.0"
|
|
33
|
+
},
|
|
30
34
|
"scripts": {
|
|
31
35
|
"lint": "eslint",
|
|
32
|
-
"test": "npm run
|
|
33
|
-
"test:postgresql": "DB=postgresql
|
|
34
|
-
"test:mariadb": "DB=mariadb
|
|
35
|
-
"test:mysql": "DB=mysql
|
|
36
|
-
"test:mysql8": "DB=mysql8
|
|
37
|
-
"test:sqlite": "DB=sqlite
|
|
36
|
+
"test": "npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
|
|
37
|
+
"test:postgresql": "DB=postgresql node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000--timeout=1200000 test/*.test.js",
|
|
38
|
+
"test:mariadb": "DB=mariadb node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000--timeout=1200000 test/*.test.js",
|
|
39
|
+
"test:mysql": "DB=mysql node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000--timeout=1200000 test/*.test.js",
|
|
40
|
+
"test:mysql8": "DB=mysql8 node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000--timeout=1200000 test/*.test.js",
|
|
41
|
+
"test:sqlite": "DB=sqlite node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000--timeout=1200000 test/*.test.js"
|
|
38
42
|
}
|
|
39
43
|
}
|