From 172850ae87f3e8e13a9051aa18affd009feb88e7 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 28 Feb 2022 12:54:32 +0000 Subject: [PATCH] Switching the basic single row read to use the enrich endpoint. --- packages/server/specs/openapi.json | 48 ++++++++++++++++++- packages/server/specs/openapi.yaml | 35 +++++++++++++- packages/server/specs/resources/row.js | 35 ++++++++++++++ .../server/src/api/controllers/public/rows.ts | 2 +- .../src/api/controllers/row/internal.js | 4 ++ packages/server/src/api/routes/public/rows.ts | 6 ++- packages/server/src/db/utils.js | 12 +++++ 7 files changed, 135 insertions(+), 7 deletions(-) diff --git a/packages/server/specs/openapi.json b/packages/server/specs/openapi.json index 3ec5882046..22469b1783 100644 --- a/packages/server/specs/openapi.json +++ b/packages/server/specs/openapi.json @@ -184,6 +184,29 @@ } } }, + "enrichedRow": { + "value": { + "row": { + "_id": "ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4", + "name": "eg", + "tableId": "ta_5b1649e42a5b41dea4ef7742a36a7a70", + "type": "row", + "relationship": [ + { + "_id": "ro_ta_users_us_8f3d717147d74d759d8cef5b6712062f", + "name": "Joe", + "tableId": "ta_users", + "internal": [ + { + "_id": "ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4", + "primaryDisplay": "eg" + } + ] + } + ] + } + } + }, "rows": { "value": { "rows": [ @@ -523,6 +546,16 @@ "row": { "description": "The row to be created/updated, based on the table schema.", "type": "object", + "properties": { + "_id": { + "description": "The ID of the row.", + "type": "string" + }, + "tableId": { + "description": "The ID of the table this row comes from.", + "type": "string" + } + }, "additionalProperties": { "oneOf": [ { @@ -549,6 +582,16 @@ "row": { "description": "The row to be created/updated, based on the table schema.", "type": "object", + "properties": { + "_id": { + "description": "The ID of the row.", + "type": "string" + }, + "tableId": { + "description": "The ID of the table this row comes from.", + "type": "string" + } + }, "additionalProperties": { "oneOf": [ { @@ -1595,6 +1638,7 @@ }, "get": { "summary": "Get a single row from the specified table.", + "description": "This gets a single row, it will be enriched with the full related rows, rather than the squashed \"primaryDisplay\" format returned by the search endpoint.", "tags": [ "rows" ], @@ -1618,8 +1662,8 @@ "$ref": "#/components/schemas/rowOutput" }, "examples": { - "row": { - "$ref": "#/components/examples/row" + "enrichedRow": { + "$ref": "#/components/examples/enrichedRow" } } } diff --git a/packages/server/specs/openapi.yaml b/packages/server/specs/openapi.yaml index 040cbc0b69..6bfd150db0 100644 --- a/packages/server/specs/openapi.yaml +++ b/packages/server/specs/openapi.yaml @@ -137,6 +137,20 @@ components: relationship: - primaryDisplay: Joe _id: ro_ta_... + enrichedRow: + value: + row: + _id: ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4 + name: eg + tableId: ta_5b1649e42a5b41dea4ef7742a36a7a70 + type: row + relationship: + - _id: ro_ta_users_us_8f3d717147d74d759d8cef5b6712062f + name: Joe + tableId: ta_users + internal: + - _id: ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4 + primaryDisplay: eg rows: value: rows: @@ -381,6 +395,13 @@ components: row: description: The row to be created/updated, based on the table schema. type: object + properties: + _id: + description: The ID of the row. + type: string + tableId: + description: The ID of the table this row comes from. + type: string additionalProperties: oneOf: - type: string @@ -394,6 +415,13 @@ components: row: description: The row to be created/updated, based on the table schema. type: object + properties: + _id: + description: The ID of the row. + type: string + tableId: + description: The ID of the table this row comes from. + type: string additionalProperties: oneOf: - type: string @@ -1115,6 +1143,9 @@ paths: $ref: "#/components/examples/row" get: summary: Get a single row from the specified table. + description: This gets a single row, it will be enriched with the full related + rows, rather than the squashed "primaryDisplay" format returned by the + search endpoint. tags: - rows parameters: @@ -1129,8 +1160,8 @@ paths: schema: $ref: "#/components/schemas/rowOutput" examples: - row: - $ref: "#/components/examples/row" + enrichedRow: + $ref: "#/components/examples/enrichedRow" "/tables/{tableId}/rows/search": post: summary: Used to search for rows within a table. diff --git a/packages/server/specs/resources/row.js b/packages/server/specs/resources/row.js index 25e3615d47..f8c3986858 100644 --- a/packages/server/specs/resources/row.js +++ b/packages/server/specs/resources/row.js @@ -24,9 +24,39 @@ const row = { ], } +const enrichedRow = { + _id: "ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4", + name: "eg", + tableId: "ta_5b1649e42a5b41dea4ef7742a36a7a70", + type: "row", + relationship: [ + { + _id: "ro_ta_users_us_8f3d717147d74d759d8cef5b6712062f", + name: "Joe", + tableId: "ta_users", + internal: [ + { + _id: "ro_ta_5b1649e42a5b41dea4ef7742a36a7a70_e6dc7e38cf1343b2b56760265201cda4", + primaryDisplay: "eg", + }, + ], + }, + ], +} + const rowSchema = { description: "The row to be created/updated, based on the table schema.", type: "object", + properties: { + _id: { + description: "The ID of the row.", + type: "string", + }, + tableId: { + description: "The ID of the table this row comes from.", + type: "string", + }, + }, additionalProperties: { oneOf: [ { type: "string" }, @@ -48,6 +78,11 @@ module.exports = new Resource() row: row, }, }, + enrichedRow: { + value: { + row: enrichedRow, + }, + }, rows: { value: { rows: [row], diff --git a/packages/server/src/api/controllers/public/rows.ts b/packages/server/src/api/controllers/public/rows.ts index 91bd2fe2d1..755f0c815c 100644 --- a/packages/server/src/api/controllers/public/rows.ts +++ b/packages/server/src/api/controllers/public/rows.ts @@ -45,7 +45,7 @@ export async function create(ctx: any) { } export async function read(ctx: any) { - await rowController.find(ctx) + await rowController.fetchEnrichedRow(ctx) ctx.body = { row: ctx.body } } diff --git a/packages/server/src/api/controllers/row/internal.js b/packages/server/src/api/controllers/row/internal.js index e1ea32e557..7650ac275f 100644 --- a/packages/server/src/api/controllers/row/internal.js +++ b/packages/server/src/api/controllers/row/internal.js @@ -2,6 +2,7 @@ const linkRows = require("../../../db/linkedRows") const { generateRowID, getRowParams, + getTableIDFromRowID, DocumentTypes, InternalTables, } = require("../../../db/utils") @@ -386,6 +387,9 @@ exports.fetchEnrichedRow = async ctx => { let groups = {}, tables = {} for (let row of response) { + if (!row.tableId) { + row.tableId = getTableIDFromRowID(row._id) + } const linkedTableId = row.tableId if (groups[linkedTableId] == null) { groups[linkedTableId] = [row] diff --git a/packages/server/src/api/routes/public/rows.ts b/packages/server/src/api/routes/public/rows.ts index 92706af613..6dc36e4620 100644 --- a/packages/server/src/api/routes/public/rows.ts +++ b/packages/server/src/api/routes/public/rows.ts @@ -104,6 +104,8 @@ write.push( * /tables/{tableId}/rows/{rowId}: * get: * summary: Get a single row from the specified table. + * description: This gets a single row, it will be enriched with the full related rows, rather than + * the squashed "primaryDisplay" format returned by the search endpoint. * tags: * - rows * parameters: @@ -118,8 +120,8 @@ write.push( * schema: * $ref: '#/components/schemas/rowOutput' * examples: - * row: - * $ref: '#/components/examples/row' + * enrichedRow: + * $ref: '#/components/examples/enrichedRow' */ read.push(new Endpoint("get", "/tables/:tableId/rows/:rowId", controller.read)) diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index e19fed0201..e40773bd0e 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -146,6 +146,18 @@ exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => { return getDocParams(DocumentTypes.ROW, endOfKey, otherProps) } +/** + * Given a row ID this will find the table ID within it (only works for internal tables). + * @param {string} rowId The ID of the row. + * @returns {string} The table ID. + */ +exports.getTableIDFromRowID = rowId => { + const components = rowId + .split(DocumentTypes.TABLE + SEPARATOR)[1] + .split(SEPARATOR) + return `${DocumentTypes.TABLE}${SEPARATOR}${components[0]}` +} + /** * Gets a new row ID for the specified table. * @param {string} tableId The table which the row is being created for.