Skip to content

Commit b1913e1

Browse files
committed
feat: update tiles actions to use factory class
1 parent ff98ade commit b1913e1

File tree

8 files changed

+84
-50
lines changed

8 files changed

+84
-50
lines changed

packages/backend/src/apps/tiles/actions/create-row/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IRawAction } from '@plumber/types'
33
import StepError from '@/errors/step'
44
import TableCollaborator from '@/models/table-collaborators'
55
import TableMetadata from '@/models/table-metadata'
6-
import { createTableRow } from '@/models/tiles/dynamodb/table-row/functions'
6+
import { getTableOperations } from '@/models/tiles/factory'
77

88
import { CreateRowOutput } from '../../types'
99

@@ -124,7 +124,12 @@ const action: IRawAction = {
124124
)
125125
}
126126

127-
const newRow = await createTableRow({ tableId, data: rowDataObject })
127+
const tableOperations = getTableOperations(table.db)
128+
129+
const newRow = await tableOperations.createTableRow({
130+
tableId,
131+
data: rowDataObject,
132+
})
128133

129134
$.setActionItem({
130135
raw: {

packages/backend/src/apps/tiles/actions/find-single-row/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import StepError from '@/errors/step'
44
import logger from '@/helpers/logger'
55
import Step from '@/models/step'
66
import TableCollaborator from '@/models/table-collaborators'
7-
import TableColumnMetadata from '@/models/table-column-metadata'
8-
import { getRawRowById, getTableRows } from '@/models/tiles/dynamodb/table-row'
7+
import TableMetadata from '@/models/table-metadata'
8+
import { getTableOperations } from '@/models/tiles/factory'
99
import { TableRowFilter, TableRowFilterOperator } from '@/models/tiles/types'
1010

1111
import { validateFilters } from '../../common/validate-filters'
@@ -150,13 +150,25 @@ const action: IRawAction = {
150150
/**
151151
* Check for columns first, there will not be any columns if the tile has been deleted.
152152
*/
153-
const columns = await TableColumnMetadata.getColumns(tableId, $)
153+
154+
const table = await TableMetadata.query()
155+
.findById(tableId)
156+
.withGraphFetched('columns')
157+
158+
if (!table) {
159+
throw new StepError(
160+
'Tile not found',
161+
'Tile may have been deleted. Please check your tile.',
162+
$.step.position,
163+
$.app.name,
164+
)
165+
}
154166

155167
await TableCollaborator.hasAccess($.user?.id, tableId, 'editor', $)
156168

157169
// Check that filters are valid
158170
try {
159-
validateFilters(filters, columns)
171+
validateFilters(filters, table.columns)
160172
} catch (e) {
161173
logger.error({
162174
message: 'Invalid filters',
@@ -178,7 +190,9 @@ const action: IRawAction = {
178190
const scanLimitRaw = +step.config?.adminOverride?.tileScanLimit
179191
const scanLimit = isNaN(scanLimitRaw) ? undefined : scanLimitRaw
180192

181-
const { rows } = await getTableRows({
193+
const tableOperations = getTableOperations(table.db)
194+
195+
const { rows } = await tableOperations.getTableRows({
182196
tableId,
183197
filters,
184198
order: returnLastRow ? 'desc' : 'asc',
@@ -199,10 +213,10 @@ const action: IRawAction = {
199213
* We use raw row data instead of mapped column names as we want them to
200214
* be distinct in data_out
201215
*/
202-
const rowToReturn = await getRawRowById({
216+
const rowToReturn = await tableOperations.getRawRowById({
203217
tableId,
204218
rowId: rowIdToUse,
205-
columnIds: columns.map((c) => c.id),
219+
columnIds: table.columns.map((c) => c.id),
206220
})
207221

208222
$.setActionItem({

packages/backend/src/apps/tiles/actions/update-row/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { IRawAction } from '@plumber/types'
22

33
import StepError from '@/errors/step'
44
import TableCollaborator from '@/models/table-collaborators'
5-
import TableColumnMetadata from '@/models/table-column-metadata'
5+
import TableMetadata from '@/models/table-metadata'
66
import {
77
autoMarshallNumberStrings,
88
stripInvalidKeys,
99
} from '@/models/tiles/dynamodb/helpers'
10-
import { patchTableRow } from '@/models/tiles/dynamodb/table-row'
10+
import { getTableOperations } from '@/models/tiles/factory'
1111
import { PatchRowInput } from '@/models/tiles/types'
1212

1313
import { UpdateRowOutput } from '../../types'
@@ -136,8 +136,20 @@ const action: IRawAction = {
136136
/**
137137
* Check for columns first, there will not be any columns if the tile has been deleted.
138138
*/
139-
const columns = await TableColumnMetadata.getColumns(tableId, $)
140-
const columnIds = columns.map((c) => c.id)
139+
const table = await TableMetadata.query()
140+
.findById(tableId)
141+
.withGraphFetched('columns')
142+
143+
if (!table) {
144+
throw new StepError(
145+
'Tile not found',
146+
'Tile may have been deleted. Please check your tile.',
147+
$.step.position,
148+
$.app.name,
149+
)
150+
}
151+
152+
const columnIds = table.columns.map((c) => c.id)
141153

142154
await TableCollaborator.hasAccess($.user?.id, tableId, 'editor', $)
143155

@@ -165,6 +177,8 @@ const action: IRawAction = {
165177
}
166178
}
167179

180+
const tableOperations = getTableOperations(table.db)
181+
168182
const patchData = {
169183
...rowData.reduce(
170184
(acc, { columnId, cellValue, operator }) => {
@@ -192,7 +206,7 @@ const action: IRawAction = {
192206
}
193207

194208
try {
195-
const updatedRow = await patchTableRow({
209+
const updatedRow = await tableOperations.patchTableRow({
196210
tableId,
197211
rowId,
198212
patchData,

packages/backend/src/graphql/__tests__/mutations/tiles/create-table.itest.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('create table mutation', () => {
1717
it('should create a blank table', async () => {
1818
const table = await createTable(
1919
null,
20-
{ input: { name: 'Test Table', isBlank: true } },
20+
{ input: { name: 'Test Table', isBlank: true, databaseType: 'ddb' } },
2121
context,
2222
)
2323
const tableColumnCount = await table.$relatedQuery('columns').resultSize()
@@ -43,13 +43,13 @@ describe('create table mutation', () => {
4343
it('should be able create tables with the same name', async () => {
4444
const table = await createTable(
4545
null,
46-
{ input: { name: 'Test Table', isBlank: false } },
46+
{ input: { name: 'Test Table', isBlank: false, databaseType: 'ddb' } },
4747
context,
4848
)
4949

5050
const table2 = await createTable(
5151
null,
52-
{ input: { name: 'Test Table', isBlank: false } },
52+
{ input: { name: 'Test Table', isBlank: false, databaseType: 'ddb' } },
5353
context,
5454
)
5555
expect(table.name).toBe('Test Table')
@@ -58,7 +58,11 @@ describe('create table mutation', () => {
5858

5959
it('should throw an error when table name is empty', async () => {
6060
await expect(
61-
createTable(null, { input: { name: '', isBlank: false } }, context),
61+
createTable(
62+
null,
63+
{ input: { name: '', isBlank: false, databaseType: 'ddb' } },
64+
context,
65+
),
6266
).rejects.toThrow()
6367
})
6468
})

packages/backend/src/graphql/mutations/tiles/create-table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const createTable: MutationResolvers['createTable'] = async (
2727
const {
2828
name: tableName,
2929
isBlank: isBlankTable,
30-
databaseType = 'pg',
30+
databaseType = 'ddb',
3131
} = params.input
3232

3333
if (!tableName) {

packages/backend/src/models/table-column-metadata.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { IGlobalVariable, ITableColumnConfig } from '@plumber/types'
2-
3-
import StepError from '@/errors/step'
1+
import { ITableColumnConfig } from '@plumber/types'
42

53
import Base from './base'
64
import TableMetadata from './table-metadata'
@@ -36,24 +34,6 @@ class TableColumnMetadata extends Base {
3634
},
3735
},
3836
})
39-
40-
static getColumns = async (tableId: string, $?: IGlobalVariable) => {
41-
const columns = await TableColumnMetadata.query()
42-
.where({
43-
table_id: tableId,
44-
})
45-
.orderBy('position')
46-
47-
if (columns.length === 0) {
48-
throw new StepError(
49-
'Tile not found',
50-
'Tile may have been deleted. Please check your tile.',
51-
$.step.position,
52-
$.app.name,
53-
)
54-
}
55-
return columns
56-
}
5737
}
5838

5939
export default TableColumnMetadata

packages/backend/src/models/table-metadata.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ITableCollabRole } from '@plumber/types'
22

33
import Base from './base'
4+
import ExtendedQueryBuilder from './query-builder'
45
import TableCollaborator from './table-collaborators'
56
import TableColumnMetadata from './table-column-metadata'
67
import User from './user'
@@ -55,6 +56,9 @@ class TableMetadata extends Base {
5556
from: `${this.tableName}.id`,
5657
to: `${TableColumnMetadata.tableName}.table_id`,
5758
},
59+
filter(builder: ExtendedQueryBuilder<TableColumnMetadata>) {
60+
builder.orderBy('position', 'asc')
61+
},
5862
},
5963
})
6064

packages/backend/src/models/tiles/pg/table-row-functions.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ import {
1616
UpdateRowInput,
1717
} from '../types'
1818

19+
function formatTableRow(
20+
row: Record<string, string>,
21+
tableId: string,
22+
): TableRowItem | null {
23+
if (!row) {
24+
return null
25+
}
26+
const { rowId, createdAt: _createdAt, updatedAt: _updatedAt, ...rest } = row
27+
return { rowId, data: rest, tableId }
28+
}
29+
1930
/**
2031
* External functions
2132
*/
@@ -32,7 +43,8 @@ export const createTableRow = async ({
3243
rowId: ulid(),
3344
})
3445
.returning('*')
35-
return res[0]
46+
47+
return formatTableRow(res[0], tableId)
3648
} catch (e: unknown) {
3749
logger.error(e)
3850
throw e
@@ -83,12 +95,12 @@ export const updateTableRow = async ({
8395
* This atomically updates the data object for keys that are changed
8496
*/
8597
export const patchTableRow = async ({
86-
rowId,
98+
rowId: rowIdToUse,
8799
tableId,
88100
patchData,
89101
}: PatchRowInput): Promise<TableRowItem> => {
90102
try {
91-
const query = tilesClient(tableId).where({ rowId })
103+
const query = tilesClient(tableId).where({ rowId: rowIdToUse })
92104

93105
Object.entries(patchData.set || {}).forEach(
94106
([key, value]: [string, string]) => {
@@ -131,7 +143,7 @@ export const patchTableRow = async ({
131143
)
132144

133145
const res = await query.update('updatedAt', new Date()).returning('*')
134-
return res[0]
146+
return formatTableRow(res[0], tableId)
135147
} catch (e: unknown) {
136148
logger.error(e)
137149
throw e
@@ -221,9 +233,7 @@ export const getTableRows = async ({
221233
}): Promise<{
222234
rows: TableRowOutput[]
223235
}> => {
224-
const query = tilesClient(tableId).select(
225-
columnIds ? ['rowId', ...columnIds] : ['*'],
226-
)
236+
const query = tilesClient(tableId).select(['rowId', ...(columnIds ?? [])])
227237
if (filters) {
228238
addFiltersToQuery(query, filters)
229239
}
@@ -251,7 +261,7 @@ export const getTableRows = async ({
251261
*/
252262
export const getRawRowById = async ({
253263
tableId,
254-
rowId,
264+
rowId: rowIdToUse,
255265
columnIds,
256266
}: {
257267
tableId: string
@@ -261,11 +271,14 @@ export const getRawRowById = async ({
261271
try {
262272
const res = await tilesClient(tableId)
263273
.where({
264-
rowId,
274+
rowId: rowIdToUse,
265275
})
266276
.select(columnIds ? ['rowId', ...columnIds] : ['*'])
267277
.first()
268-
return res
278+
if (!res) {
279+
return null
280+
}
281+
return formatTableRow(res, tableId)
269282
} catch (e: unknown) {
270283
logger.error(e)
271284
throw e

0 commit comments

Comments
 (0)