@platformatic/sql-openapi
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,1 +0,1 @@ | ||
{"processes":{"42d15934-e8f2-45a1-954f-31efa1455bf6":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/ignore.test.js","children":[]},"48993cf3-72fe-4040-91b5-28d8be85c30b":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/where.test.js","children":[]},"7f01475e-4ecf-4934-94ef-47d32f22e4cd":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/simple.test.js","children":[]},"c2b5c996-0b3a-4aca-b0ee-517845bda2e2":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/order_by.test.js","children":[]},"cdfcaa24-c93d-46da-a6b5-3e787b3a39be":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/hooks.test.js","children":[]},"cf984c92-532d-4ae1-9559-9b4b588f05d5":{"parent":null,"children":["42d15934-e8f2-45a1-954f-31efa1455bf6","48993cf3-72fe-4040-91b5-28d8be85c30b","7f01475e-4ecf-4934-94ef-47d32f22e4cd","c2b5c996-0b3a-4aca-b0ee-517845bda2e2","cdfcaa24-c93d-46da-a6b5-3e787b3a39be","d0d9eae7-77b6-42ef-a376-cd79dab441cb"]},"d0d9eae7-77b6-42ef-a376-cd79dab441cb":{"parent":"cf984c92-532d-4ae1-9559-9b4b588f05d5","externalId":"test/nested.test.js","children":[]}},"files":{"/Users/matteo/Repositories/platformatic/packages/sql-openapi/index.js":["42d15934-e8f2-45a1-954f-31efa1455bf6","48993cf3-72fe-4040-91b5-28d8be85c30b","7f01475e-4ecf-4934-94ef-47d32f22e4cd","c2b5c996-0b3a-4aca-b0ee-517845bda2e2","cdfcaa24-c93d-46da-a6b5-3e787b3a39be","d0d9eae7-77b6-42ef-a376-cd79dab441cb"],"/Users/matteo/Repositories/platformatic/packages/sql-openapi/lib/entity-to-routes.js":["42d15934-e8f2-45a1-954f-31efa1455bf6","48993cf3-72fe-4040-91b5-28d8be85c30b","7f01475e-4ecf-4934-94ef-47d32f22e4cd","c2b5c996-0b3a-4aca-b0ee-517845bda2e2","cdfcaa24-c93d-46da-a6b5-3e787b3a39be","d0d9eae7-77b6-42ef-a376-cd79dab441cb"]},"externalIds":{"test/ignore.test.js":{"root":"42d15934-e8f2-45a1-954f-31efa1455bf6","children":[]},"test/where.test.js":{"root":"48993cf3-72fe-4040-91b5-28d8be85c30b","children":[]},"test/simple.test.js":{"root":"7f01475e-4ecf-4934-94ef-47d32f22e4cd","children":[]},"test/order_by.test.js":{"root":"c2b5c996-0b3a-4aca-b0ee-517845bda2e2","children":[]},"test/hooks.test.js":{"root":"cdfcaa24-c93d-46da-a6b5-3e787b3a39be","children":[]},"test/nested.test.js":{"root":"d0d9eae7-77b6-42ef-a376-cd79dab441cb","children":[]}}} | ||
{"processes":{"02c64bc2-7c9e-450b-b64e-2be73f13f908":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/ignore.test.js","children":[]},"72788fef-7938-417a-ae0d-e72fb9337de4":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/where.test.js","children":[]},"7d2e4109-5720-4d41-80ed-de20126c7ccf":{"parent":null,"children":["02c64bc2-7c9e-450b-b64e-2be73f13f908","72788fef-7938-417a-ae0d-e72fb9337de4","b724455b-410f-4413-a69b-a8d436cbcb85","d2ef0754-cfb2-4b56-877c-d1708e49d260","ed5921da-2893-4558-be1b-129f63c91f2f","f98b59b8-cbce-448c-af60-dc2ef383041f"]},"b724455b-410f-4413-a69b-a8d436cbcb85":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/order_by.test.js","children":[]},"d2ef0754-cfb2-4b56-877c-d1708e49d260":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/simple.test.js","children":[]},"ed5921da-2893-4558-be1b-129f63c91f2f":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/nested.test.js","children":[]},"f98b59b8-cbce-448c-af60-dc2ef383041f":{"parent":"7d2e4109-5720-4d41-80ed-de20126c7ccf","externalId":"test/hooks.test.js","children":[]}},"files":{"/Users/matteo/Repositories/platformatic/packages/sql-openapi/index.js":["02c64bc2-7c9e-450b-b64e-2be73f13f908","72788fef-7938-417a-ae0d-e72fb9337de4","b724455b-410f-4413-a69b-a8d436cbcb85","d2ef0754-cfb2-4b56-877c-d1708e49d260","ed5921da-2893-4558-be1b-129f63c91f2f","f98b59b8-cbce-448c-af60-dc2ef383041f"],"/Users/matteo/Repositories/platformatic/packages/sql-openapi/lib/entity-to-routes.js":["02c64bc2-7c9e-450b-b64e-2be73f13f908","72788fef-7938-417a-ae0d-e72fb9337de4","b724455b-410f-4413-a69b-a8d436cbcb85","d2ef0754-cfb2-4b56-877c-d1708e49d260","ed5921da-2893-4558-be1b-129f63c91f2f","f98b59b8-cbce-448c-af60-dc2ef383041f"]},"externalIds":{"test/ignore.test.js":{"root":"02c64bc2-7c9e-450b-b64e-2be73f13f908","children":[]},"test/where.test.js":{"root":"72788fef-7938-417a-ae0d-e72fb9337de4","children":[]},"test/order_by.test.js":{"root":"b724455b-410f-4413-a69b-a8d436cbcb85","children":[]},"test/simple.test.js":{"root":"d2ef0754-cfb2-4b56-877c-d1708e49d260","children":[]},"test/nested.test.js":{"root":"ed5921da-2893-4558-be1b-129f63c91f2f","children":[]},"test/hooks.test.js":{"root":"f98b59b8-cbce-448c-af60-dc2ef383041f","children":[]}}} |
@@ -37,3 +37,6 @@ 'use strict' | ||
app.register(SwaggerUI, opts) | ||
app.register(SwaggerUI, { | ||
...opts, | ||
prefix: '/documentation' | ||
}) | ||
@@ -40,0 +43,0 @@ for (const entity of Object.values(app.platformatic.entities)) { |
@@ -283,2 +283,4 @@ 'use strict' | ||
const targetForeignKeyCamelcase = camelcase(relation.foreign_column_name) | ||
const targetColumnCamelcase = camelcase(relation.column_name) | ||
const targetRelation = relation.column_name.replace(/_id$/, '') | ||
const targetEntitySchema = { | ||
@@ -290,3 +292,4 @@ $ref: targetEntity.name + '#' | ||
const operationId = `get${capitalize(targetEntity.singularName)}For${capitalize(entity.singularName)}` | ||
app.get(`/:${camelcase(entity.primaryKey)}/${targetEntity.singularName}`, { | ||
// We need to get the relation name from the PK column: | ||
app.get(`/:${camelcase(entity.primaryKey)}/${targetRelation}`, { | ||
schema: { | ||
@@ -311,3 +314,3 @@ operationId, | ||
// check that the entity exists | ||
const resEntity = await entity.count({ | ||
const resEntity = (await entity.find({ | ||
ctx, | ||
@@ -319,5 +322,5 @@ where: { | ||
} | ||
}) | ||
}))[0] | ||
if (resEntity === 0) { | ||
if (!resEntity) { | ||
return reply.callNotFound() | ||
@@ -331,3 +334,3 @@ } | ||
[targetForeignKeyCamelcase]: { | ||
eq: request.params[primaryKeyCamelcase] | ||
eq: resEntity[targetColumnCamelcase] | ||
} | ||
@@ -334,0 +337,0 @@ }, |
{ | ||
"name": "@platformatic/sql-openapi", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Map a SQL database to OpenAPI, for Fastify", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@platformatic/sql-mapper": "0.4.0", | ||
"@platformatic/sql-mapper": "0.5.0", | ||
"fastify": "^4.6.0", | ||
@@ -28,3 +28,3 @@ "mercurius": "^11.0.0", | ||
"dependencies": { | ||
"@platformatic/sql-json-schema-mapper": "0.4.0", | ||
"@platformatic/sql-json-schema-mapper": "0.5.0", | ||
"@fastify/deepmerge": "^1.1.0", | ||
@@ -31,0 +31,0 @@ "@fastify/swagger": "^8.0.0", |
@@ -72,2 +72,7 @@ 'use strict' | ||
} | ||
try { | ||
await db.query(sql`DROP TABLE people`) | ||
} catch (err) { | ||
} | ||
} |
@@ -183,6 +183,6 @@ | ||
method: 'GET', | ||
url: '/posts/1/owner' | ||
url: '/posts/3/owner' | ||
}) | ||
equal(res.statusCode, 200, 'GET /posts/:id/owner status code') | ||
same(res.json().name, owners[0].name, 'GET /posts/:id/owner response') | ||
same(res.json().name, owners[1].name, 'GET /posts/:id/owner response') | ||
} | ||
@@ -218,1 +218,107 @@ | ||
}) | ||
test('nested routes with recursive FK', async (t) => { | ||
const { pass, teardown, same, equal, matchSnapshot } = t | ||
t.snapshotFile = resolve(__dirname, 'tap-snapshots', 'nested-routes-openapi-recursive.cjs') | ||
const app = fastify() | ||
app.register(sqlMapper, { | ||
...connInfo, | ||
async onDatabaseLoad (db, sql) { | ||
pass('onDatabaseLoad called') | ||
await clear(db, sql) | ||
if (isMysql) { | ||
await db.query(sql` | ||
CREATE TABLE people ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
parent_id BIGINT UNSIGNED, | ||
FOREIGN KEY (parent_id) REFERENCES people(id) | ||
); | ||
`) | ||
} else if (isSQLite) { | ||
await db.query(sql` | ||
CREATE TABLE people ( | ||
id INTEGER PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
parent_id BIGINT UNSIGNED, | ||
FOREIGN KEY (parent_id) REFERENCES people(id) | ||
); | ||
`) | ||
} else { | ||
await db.query(sql` | ||
CREATE TABLE people ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
parent_id INTEGER REFERENCES people(id) | ||
); | ||
`) | ||
} | ||
} | ||
}) | ||
app.register(sqlOpenAPI) | ||
teardown(app.close.bind(app)) | ||
await app.ready() | ||
{ | ||
const res = await app.inject({ | ||
method: 'GET', | ||
url: '/documentation/json' | ||
}) | ||
const openapi = res.json() | ||
matchSnapshot(openapi, 'matches expected OpenAPI defs') | ||
} | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/people', | ||
body: { | ||
name: 'Dad' | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'POST /people status code') | ||
const dad = res.json() | ||
const res2 = await app.inject({ | ||
method: 'POST', | ||
url: '/people', | ||
body: { | ||
name: 'Child', | ||
parentId: dad.id | ||
} | ||
}) | ||
equal(res.statusCode, 200, 'POST /people status code') | ||
const child = res2.json() | ||
{ | ||
const res = await app.inject({ | ||
method: 'GET', | ||
url: '/people' | ||
}) | ||
equal(res.statusCode, 200, 'GET /people status code') | ||
same(res.json(), [{ | ||
id: 1, | ||
name: 'Dad', | ||
parentId: null | ||
}, { | ||
id: 2, | ||
name: 'Child', | ||
parentId: 1 | ||
}], 'GET /people response') | ||
} | ||
{ | ||
const res = await app.inject({ | ||
method: 'GET', | ||
url: `/people/${child.id}/parent` | ||
}) | ||
equal(res.statusCode, 200, 'GET /people/:id/parent status code') | ||
same(res.json(), { | ||
id: 1, | ||
name: 'Dad', | ||
parentId: null | ||
}, 'GET /people/:id/parent response') | ||
} | ||
}) |
383530
40
8632
+ Added@platformatic/sql-json-schema-mapper@0.5.0(transitive)
- Removed@platformatic/sql-json-schema-mapper@0.4.0(transitive)