Switching the basic single row read to use the enrich endpoint.

This commit is contained in:
mike12345567 2022-02-28 12:54:32 +00:00
parent 2bc2d17066
commit 172850ae87
7 changed files with 135 additions and 7 deletions

View File

@ -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": { "rows": {
"value": { "value": {
"rows": [ "rows": [
@ -523,6 +546,16 @@
"row": { "row": {
"description": "The row to be created/updated, based on the table schema.", "description": "The row to be created/updated, based on the table schema.",
"type": "object", "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": { "additionalProperties": {
"oneOf": [ "oneOf": [
{ {
@ -549,6 +582,16 @@
"row": { "row": {
"description": "The row to be created/updated, based on the table schema.", "description": "The row to be created/updated, based on the table schema.",
"type": "object", "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": { "additionalProperties": {
"oneOf": [ "oneOf": [
{ {
@ -1595,6 +1638,7 @@
}, },
"get": { "get": {
"summary": "Get a single row from the specified table.", "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": [ "tags": [
"rows" "rows"
], ],
@ -1618,8 +1662,8 @@
"$ref": "#/components/schemas/rowOutput" "$ref": "#/components/schemas/rowOutput"
}, },
"examples": { "examples": {
"row": { "enrichedRow": {
"$ref": "#/components/examples/row" "$ref": "#/components/examples/enrichedRow"
} }
} }
} }

View File

@ -137,6 +137,20 @@ components:
relationship: relationship:
- primaryDisplay: Joe - primaryDisplay: Joe
_id: ro_ta_... _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: rows:
value: value:
rows: rows:
@ -381,6 +395,13 @@ components:
row: row:
description: The row to be created/updated, based on the table schema. description: The row to be created/updated, based on the table schema.
type: object 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: additionalProperties:
oneOf: oneOf:
- type: string - type: string
@ -394,6 +415,13 @@ components:
row: row:
description: The row to be created/updated, based on the table schema. description: The row to be created/updated, based on the table schema.
type: object 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: additionalProperties:
oneOf: oneOf:
- type: string - type: string
@ -1115,6 +1143,9 @@ paths:
$ref: "#/components/examples/row" $ref: "#/components/examples/row"
get: get:
summary: Get a single row from the specified table. 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: tags:
- rows - rows
parameters: parameters:
@ -1129,8 +1160,8 @@ paths:
schema: schema:
$ref: "#/components/schemas/rowOutput" $ref: "#/components/schemas/rowOutput"
examples: examples:
row: enrichedRow:
$ref: "#/components/examples/row" $ref: "#/components/examples/enrichedRow"
"/tables/{tableId}/rows/search": "/tables/{tableId}/rows/search":
post: post:
summary: Used to search for rows within a table. summary: Used to search for rows within a table.

View File

@ -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 = { const rowSchema = {
description: "The row to be created/updated, based on the table schema.", description: "The row to be created/updated, based on the table schema.",
type: "object", 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: { additionalProperties: {
oneOf: [ oneOf: [
{ type: "string" }, { type: "string" },
@ -48,6 +78,11 @@ module.exports = new Resource()
row: row, row: row,
}, },
}, },
enrichedRow: {
value: {
row: enrichedRow,
},
},
rows: { rows: {
value: { value: {
rows: [row], rows: [row],

View File

@ -45,7 +45,7 @@ export async function create(ctx: any) {
} }
export async function read(ctx: any) { export async function read(ctx: any) {
await rowController.find(ctx) await rowController.fetchEnrichedRow(ctx)
ctx.body = { row: ctx.body } ctx.body = { row: ctx.body }
} }

View File

@ -2,6 +2,7 @@ const linkRows = require("../../../db/linkedRows")
const { const {
generateRowID, generateRowID,
getRowParams, getRowParams,
getTableIDFromRowID,
DocumentTypes, DocumentTypes,
InternalTables, InternalTables,
} = require("../../../db/utils") } = require("../../../db/utils")
@ -386,6 +387,9 @@ exports.fetchEnrichedRow = async ctx => {
let groups = {}, let groups = {},
tables = {} tables = {}
for (let row of response) { for (let row of response) {
if (!row.tableId) {
row.tableId = getTableIDFromRowID(row._id)
}
const linkedTableId = row.tableId const linkedTableId = row.tableId
if (groups[linkedTableId] == null) { if (groups[linkedTableId] == null) {
groups[linkedTableId] = [row] groups[linkedTableId] = [row]

View File

@ -104,6 +104,8 @@ write.push(
* /tables/{tableId}/rows/{rowId}: * /tables/{tableId}/rows/{rowId}:
* get: * get:
* summary: Get a single row from the specified table. * 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: * tags:
* - rows * - rows
* parameters: * parameters:
@ -118,8 +120,8 @@ write.push(
* schema: * schema:
* $ref: '#/components/schemas/rowOutput' * $ref: '#/components/schemas/rowOutput'
* examples: * examples:
* row: * enrichedRow:
* $ref: '#/components/examples/row' * $ref: '#/components/examples/enrichedRow'
*/ */
read.push(new Endpoint("get", "/tables/:tableId/rows/:rowId", controller.read)) read.push(new Endpoint("get", "/tables/:tableId/rows/:rowId", controller.read))

View File

@ -146,6 +146,18 @@ exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => {
return getDocParams(DocumentTypes.ROW, endOfKey, 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. * Gets a new row ID for the specified table.
* @param {string} tableId The table which the row is being created for. * @param {string} tableId The table which the row is being created for.