@platformatic/sql-json-schema-mapper 0.28.1 → 0.30.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/index.js +14 -4
- package/package.json +2 -2
- package/test/types.test.js +24 -24
package/index.js
CHANGED
|
@@ -106,7 +106,7 @@ function mapSQLEntityToJSONSchema (entity, ignore = {}) {
|
|
|
106
106
|
return res
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
function mapOpenAPItoTypes (obj, opts = {}) {
|
|
109
|
+
function mapOpenAPItoTypes (obj, fieldDefinitions, opts = {}) {
|
|
110
110
|
let { writer, addedProps } = opts
|
|
111
111
|
addedProps ??= new Set()
|
|
112
112
|
writer ??= new CodeBlockWriter()
|
|
@@ -116,13 +116,23 @@ function mapOpenAPItoTypes (obj, opts = {}) {
|
|
|
116
116
|
writer.write(` * ${description}`).newLine()
|
|
117
117
|
writer.write(' */').newLine()
|
|
118
118
|
writer.write(`declare interface ${title}`).block(() => {
|
|
119
|
-
renderProperties(writer, addedProps, properties, additionalProperties, required)
|
|
119
|
+
renderProperties(writer, addedProps, properties, additionalProperties, required, fieldDefinitions)
|
|
120
120
|
})
|
|
121
121
|
return writer.toString()
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
function renderProperties (writer, addedProps, properties = {}, additionalProperties, required = []) {
|
|
125
|
-
|
|
124
|
+
function renderProperties (writer, addedProps, properties = {}, additionalProperties, required = [], fieldDefinitions = {}) {
|
|
125
|
+
// Since Array.prototype.sort is guaranteed to be stable, we can sort by name first, then apply special sorting rules
|
|
126
|
+
const keys = Object.keys(properties)
|
|
127
|
+
.sort()
|
|
128
|
+
.sort((a, b) => {
|
|
129
|
+
// Sort PKs first
|
|
130
|
+
if (fieldDefinitions[a]?.primaryKey === fieldDefinitions[b]?.primaryKey) {
|
|
131
|
+
return 0
|
|
132
|
+
}
|
|
133
|
+
return fieldDefinitions[a]?.primaryKey ? -1 : 1
|
|
134
|
+
})
|
|
135
|
+
for (const name of keys) {
|
|
126
136
|
const localProperty = properties[name]
|
|
127
137
|
const { type, nullable, items } = localProperty
|
|
128
138
|
addedProps.add(name)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-json-schema-mapper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Map SQL entity to JSON schema",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"snazzy": "^9.0.0",
|
|
19
19
|
"standard": "^17.1.0",
|
|
20
20
|
"tap": "^16.3.6",
|
|
21
|
-
"@platformatic/sql-mapper": "0.
|
|
21
|
+
"@platformatic/sql-mapper": "0.30.0"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"code-block-writer": "^12.0.0",
|
package/test/types.test.js
CHANGED
|
@@ -14,7 +14,7 @@ function referenceTest (name, obj, opts = {}) {
|
|
|
14
14
|
test(name, { only }, async t => {
|
|
15
15
|
const reference = await dtsgenerator.default({ contents: [dtsgenerator.parseSchema(structuredClone(obj))] })
|
|
16
16
|
const cloned = structuredClone(obj)
|
|
17
|
-
const ours = mapOpenAPItoTypes(cloned)
|
|
17
|
+
const ours = mapOpenAPItoTypes(cloned, { id: { primaryKey: true } })
|
|
18
18
|
t.not(cloned, obj)
|
|
19
19
|
t.same(cloned, obj)
|
|
20
20
|
t.same(ours.trim(), reference.trim())
|
|
@@ -30,8 +30,9 @@ referenceTest('p1', {
|
|
|
30
30
|
id: {
|
|
31
31
|
type: 'integer'
|
|
32
32
|
},
|
|
33
|
-
|
|
34
|
-
type: 'string'
|
|
33
|
+
description: {
|
|
34
|
+
type: 'string',
|
|
35
|
+
nullable: true
|
|
35
36
|
},
|
|
36
37
|
metadata: {
|
|
37
38
|
type: 'object',
|
|
@@ -42,9 +43,8 @@ referenceTest('p1', {
|
|
|
42
43
|
type: 'number',
|
|
43
44
|
nullable: true
|
|
44
45
|
},
|
|
45
|
-
|
|
46
|
-
type: 'string'
|
|
47
|
-
nullable: true
|
|
46
|
+
title: {
|
|
47
|
+
type: 'string'
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
required: [
|
|
@@ -61,6 +61,10 @@ referenceTest('p2', {
|
|
|
61
61
|
id: {
|
|
62
62
|
type: 'integer'
|
|
63
63
|
},
|
|
64
|
+
description: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
nullable: true
|
|
67
|
+
},
|
|
64
68
|
metadata: {
|
|
65
69
|
type: 'object',
|
|
66
70
|
additionalProperties: true,
|
|
@@ -69,10 +73,6 @@ referenceTest('p2', {
|
|
|
69
73
|
section: {
|
|
70
74
|
type: 'number',
|
|
71
75
|
nullable: true
|
|
72
|
-
},
|
|
73
|
-
description: {
|
|
74
|
-
type: 'string',
|
|
75
|
-
nullable: true
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
78
|
required: []
|
|
@@ -114,8 +114,9 @@ referenceTest('multiple types', {
|
|
|
114
114
|
id: {
|
|
115
115
|
type: ['integer', 'string']
|
|
116
116
|
},
|
|
117
|
-
|
|
118
|
-
type: 'string'
|
|
117
|
+
description: {
|
|
118
|
+
type: 'string',
|
|
119
|
+
nullable: true
|
|
119
120
|
},
|
|
120
121
|
metadata: {
|
|
121
122
|
type: 'object',
|
|
@@ -125,9 +126,8 @@ referenceTest('multiple types', {
|
|
|
125
126
|
section: {
|
|
126
127
|
type: ['number', 'null']
|
|
127
128
|
},
|
|
128
|
-
|
|
129
|
-
type: 'string'
|
|
130
|
-
nullable: true
|
|
129
|
+
title: {
|
|
130
|
+
type: 'string'
|
|
131
131
|
}
|
|
132
132
|
},
|
|
133
133
|
required: [
|
|
@@ -144,14 +144,14 @@ referenceTest('arrays', {
|
|
|
144
144
|
id: {
|
|
145
145
|
type: 'integer'
|
|
146
146
|
},
|
|
147
|
-
title: {
|
|
148
|
-
type: 'string'
|
|
149
|
-
},
|
|
150
147
|
tags: {
|
|
151
148
|
type: 'array',
|
|
152
149
|
items: {
|
|
153
150
|
type: 'string'
|
|
154
151
|
}
|
|
152
|
+
},
|
|
153
|
+
title: {
|
|
154
|
+
type: 'string'
|
|
155
155
|
}
|
|
156
156
|
},
|
|
157
157
|
required: [
|
|
@@ -168,9 +168,6 @@ referenceTest('objects in arrays', {
|
|
|
168
168
|
id: {
|
|
169
169
|
type: 'integer'
|
|
170
170
|
},
|
|
171
|
-
title: {
|
|
172
|
-
type: 'string'
|
|
173
|
-
},
|
|
174
171
|
tags: {
|
|
175
172
|
type: 'array',
|
|
176
173
|
items: {
|
|
@@ -181,6 +178,9 @@ referenceTest('objects in arrays', {
|
|
|
181
178
|
}
|
|
182
179
|
}
|
|
183
180
|
}
|
|
181
|
+
},
|
|
182
|
+
title: {
|
|
183
|
+
type: 'string'
|
|
184
184
|
}
|
|
185
185
|
},
|
|
186
186
|
required: [
|
|
@@ -197,12 +197,12 @@ referenceTest('enums', {
|
|
|
197
197
|
id: {
|
|
198
198
|
type: 'integer'
|
|
199
199
|
},
|
|
200
|
-
title: {
|
|
201
|
-
type: 'string'
|
|
202
|
-
},
|
|
203
200
|
color: {
|
|
204
201
|
type: 'string',
|
|
205
202
|
enum: ['amber', 'green', 'red']
|
|
203
|
+
},
|
|
204
|
+
title: {
|
|
205
|
+
type: 'string'
|
|
206
206
|
}
|
|
207
207
|
},
|
|
208
208
|
required: [
|