Skip to content

Commit db66757

Browse files
committed
feat: use factory class for queries and mutations, minimise getAllRows payload
1 parent 602d732 commit db66757

File tree

15 files changed

+148
-31
lines changed

15 files changed

+148
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('create table mutation', () => {
3030
it('should create a table and with placeholder rows and columns', async () => {
3131
const table = await createTable(
3232
null,
33-
{ input: { name: 'Test Table', isBlank: false } },
33+
{ input: { name: 'Test Table', isBlank: false, databaseType: 'ddb' } },
3434
context,
3535
)
3636
const tableColumnCount = await table.$relatedQuery('columns').resultSize()

packages/backend/src/graphql/__tests__/queries/tiles/get-all-rows.itest.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ describe('get all rows query', () => {
9898
context,
9999
)
100100
cursor = stringifiedCursor
101-
expect(pageRows.length).toBeLessThan(numRowsToInsert)
102101
rows = rows.concat(pageRows)
103102
} while (cursor)
104103
expect(rows.length).toBe(numRowsToInsert)
@@ -138,7 +137,7 @@ describe('get all rows query', () => {
138137
)
139138
const rows = await Promise.all(returnedRows)
140139

141-
expect(Object.keys(rows[0].data).sort()).toEqual(dummyColumnIds.sort())
140+
expect(rows[0].data.split(',').length).toBe(dummyColumnIds.length)
142141
})
143142

144143
it('should allow all collaborators to call this function', async () => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TableCollaborator from '@/models/table-collaborators'
22
import TableMetadata from '@/models/table-metadata'
3-
import { createTableRow } from '@/models/tiles/dynamodb/table-row'
3+
import { getTableOperations } from '@/models/tiles/factory'
44

55
import type { MutationResolvers } from '../../__generated__/types.generated'
66

@@ -19,7 +19,9 @@ const createRow: MutationResolvers['createRow'] = async (
1919
throw new Error('Invalid column id')
2020
}
2121

22-
const row = await createTableRow({ tableId, data })
22+
const tableOperations = getTableOperations(table.db)
23+
24+
const row = await tableOperations.createTableRow({ tableId, data })
2325

2426
return row.rowId
2527
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TableCollaborator from '@/models/table-collaborators'
22
import TableMetadata from '@/models/table-metadata'
3-
import { createTableRows } from '@/models/tiles/dynamodb/table-row'
3+
import { getTableOperations } from '@/models/tiles/factory'
44

55
import type { MutationResolvers } from '../../__generated__/types.generated'
66

@@ -19,7 +19,9 @@ const createRows: MutationResolvers['createRows'] = async (
1919
throw new Error('Invalid column id')
2020
}
2121

22-
await createTableRows({ tableId, dataArray })
22+
const tableOperations = getTableOperations(table.db)
23+
24+
await tableOperations.createTableRows({ tableId, dataArray })
2325

2426
return true
2527
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createTableRows } from '@/models/tiles/dynamodb/table-row'
1+
import { getTableOperations } from '@/models/tiles/factory'
22

33
import type { MutationResolvers } from '../../__generated__/types.generated'
44

@@ -23,20 +23,36 @@ const createTable: MutationResolvers['createTable'] = async (
2323
params,
2424
context,
2525
) => {
26-
const { name: tableName, isBlank: isBlankTable } = params.input
26+
const {
27+
name: tableName,
28+
isBlank: isBlankTable,
29+
databaseType = 'pg',
30+
} = params.input
2731

2832
if (!tableName) {
2933
throw new Error('Table name is required')
3034
}
3135

36+
const tableOperations = getTableOperations(databaseType)
37+
38+
// TODO: should i wrap this in a transaction?
3239
const table = await context.currentUser.$relatedQuery('tables').insertGraph({
3340
name: tableName,
3441
role: 'owner',
42+
db: databaseType,
3543
columns: isBlankTable ? [] : PLACEHOLDER_COLUMNS,
3644
})
3745

46+
await tableOperations.createTable(
47+
table.id,
48+
isBlankTable ? [] : table.columns.map((column) => column.id),
49+
)
50+
3851
if (!isBlankTable) {
39-
await createTableRows({ tableId: table.id, dataArray: PLACEHOLDER_ROWS })
52+
await tableOperations.createTableRows({
53+
tableId: table.id,
54+
dataArray: PLACEHOLDER_ROWS,
55+
})
4056
}
4157

4258
return table

packages/backend/src/graphql/mutations/tiles/delete-rows.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TableCollaborator from '@/models/table-collaborators'
2-
import { deleteTableRows } from '@/models/tiles/dynamodb/table-row'
2+
import TableMetadata from '@/models/table-metadata'
3+
import { getTableOperations } from '@/models/tiles/factory'
34

45
import type { MutationResolvers } from '../../__generated__/types.generated'
56

@@ -12,7 +13,10 @@ const deleteRows: MutationResolvers['deleteRows'] = async (
1213

1314
await TableCollaborator.hasAccess(context.currentUser.id, tableId, 'editor')
1415

15-
await deleteTableRows({ tableId, rowIds })
16+
const table = await TableMetadata.query().findById(tableId).throwIfNotFound()
17+
const tableOperations = getTableOperations(table.db)
18+
19+
await tableOperations.deleteTableRows({ tableId, rowIds })
1620

1721
return rowIds
1822
}

packages/backend/src/graphql/mutations/tiles/update-row.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import TableCollaborator from '@/models/table-collaborators'
22
import TableMetadata from '@/models/table-metadata'
3-
import { updateTableRow } from '@/models/tiles/dynamodb/table-row'
3+
import { getTableOperations } from '@/models/tiles/factory'
44

55
import type { MutationResolvers } from '../../__generated__/types.generated'
66

@@ -19,7 +19,9 @@ const updateRow: MutationResolvers['updateRow'] = async (
1919
throw new Error('Invalid column id')
2020
}
2121

22-
await updateTableRow({ tableId, rowId, data })
22+
const tableOperations = getTableOperations(table.db)
23+
24+
await tableOperations.updateTableRow({ tableId, rowId, data })
2325

2426
return rowId
2527
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { z } from 'zod'
44
import TableCollaborator from '@/models/table-collaborators'
55
import TableColumnMetadata from '@/models/table-column-metadata'
66
import TableMetadata from '@/models/table-metadata'
7+
import { getTableOperations } from '@/models/tiles/factory'
78

89
import type { MutationResolvers } from '../../__generated__/types.generated'
910

@@ -45,6 +46,8 @@ const updateTable: MutationResolvers['updateTable'] = async (
4546

4647
const table = await TableMetadata.query().findById(tableId)
4748

49+
const tableOperations = getTableOperations(table.db)
50+
4851
if (tableName) {
4952
await table.$query().patch({
5053
name: tableName.trim(),
@@ -57,11 +60,20 @@ const updateTable: MutationResolvers['updateTable'] = async (
5760
.$relatedQuery('columns', trx)
5861
.max('position as position') // aliasing for more convenient typing
5962
const maxPosition = results[0].position || 0
60-
await table.$relatedQuery('columns', trx).insert(
61-
addedColumns.map((name, i) => ({
62-
name,
63-
position: maxPosition + i + 1,
64-
})),
63+
const tableMetadataColumns = await table
64+
.$relatedQuery('columns', trx)
65+
.insert(
66+
addedColumns.map((name, i) => ({
67+
name,
68+
position: maxPosition + i + 1,
69+
})),
70+
)
71+
.returning('id')
72+
73+
// If creation of columns fail, we roll back the creation of table column metadata
74+
await tableOperations.createTableColumns(
75+
tableId,
76+
tableMetadataColumns.map((column) => column.id),
6577
)
6678
})
6779
}
@@ -98,6 +110,8 @@ const updateTable: MutationResolvers['updateTable'] = async (
98110
.$relatedQuery('columns')
99111
.delete()
100112
.whereIn('id', deletedColumns)
113+
114+
await tableOperations.deleteTableColumns(tableId, deletedColumns)
101115
})
102116
}
103117

packages/backend/src/graphql/queries/tiles/get-all-rows.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import InvalidTileViewKeyError from '@/errors/invalid-tile-view-key'
66
import logger from '@/helpers/logger'
77
import TableMetadata from '@/models/table-metadata'
88
import { DYNAMODB_THROUGHPUT_EXCEEDED_ERROR_MESSAGE } from '@/models/tiles/dynamodb/helpers'
9-
import { getTableRows } from '@/models/tiles/dynamodb/table-row'
9+
import { getTableOperations } from '@/models/tiles/factory'
1010

1111
import type { QueryResolvers } from '../../__generated__/types.generated'
1212

@@ -15,7 +15,7 @@ const getAllRows: QueryResolvers['getAllRows'] = async (
1515
params,
1616
context,
1717
) => {
18-
const { tableId, stringifiedCursor } = params
18+
const { tableId } = params
1919

2020
try {
2121
const table = context.tilesViewKey
@@ -39,14 +39,24 @@ const getAllRows: QueryResolvers['getAllRows'] = async (
3939
})
4040
}
4141

42+
const tableOperations = getTableOperations(table.db)
43+
4244
const columnIds = table.columns.map((column) => column.id)
4345

44-
return await getTableRows({
46+
const { rows } = await tableOperations.getTableRows({
4547
tableId,
4648
columnIds,
47-
stringifiedCursor: stringifiedCursor ?? 'start',
4849
})
49-
// TODO: remove keys from rows to reduce payload size
50+
51+
// Convert data object to csv to minimize payload size
52+
rows.forEach((row) => {
53+
row.data = columnIds.map((columnId) => row.data[columnId]).join(',')
54+
})
55+
56+
return {
57+
rows,
58+
columnIds,
59+
}
5060
} catch (e) {
5161
logger.error(e)
5262
if (e instanceof ObjectionNotFoundError) {

packages/backend/src/graphql/schema.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,16 @@ type PaginatedExecutionSteps {
743743
pageInfo: PageInfo!
744744
}
745745

746+
enum DatabaseType {
747+
pg
748+
ddb
749+
}
750+
746751
# Tiles types
747752
input CreateTableInput {
748753
name: String!
749754
isBlank: Boolean!
755+
databaseType: DatabaseType
750756
}
751757

752758
input TableColumnConfigInput {
@@ -820,6 +826,7 @@ type PaginatedTables {
820826

821827
type GetTableRowsResult {
822828
rows: Any!
829+
columnIds: [String!]!
823830
stringifiedCursor: String
824831
}
825832

packages/backend/src/helpers/flow-templates.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export async function createFlowFromTemplate(
191191
role: 'owner',
192192
columns: columnsToInsert,
193193
})
194+
194195
tableId = table.id
195196

196197
// obtain a map of column name to the column id for easy replacement
@@ -200,6 +201,8 @@ export async function createFlowFromTemplate(
200201

201202
// replace column names with column ids for each row data and create table rows
202203
const newRowData = replaceColumnNamesWithIds(rowData, columnNameToIdMap)
204+
205+
// TODO: tiles-v2: use table operations to create rows
203206
await createTableRows({ tableId, dataArray: newRowData })
204207
} catch (e) {
205208
logger.error(e)

packages/backend/test/pg-global-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function setup() {
2525
)
2626

2727
const config = await import('../knexfile')
28-
const client = knex(config.default as any)
28+
const client = knex(config.default)
2929

3030
// manually running migrations since the programmatic API doesn't work
3131
// see issue here: https://github.com/knex/knex/issues/5323

packages/frontend/src/graphql/queries/tiles/get-all-rows.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const GET_ALL_ROWS = graphql(`
44
query GetAllRows($tableId: String!, $stringifiedCursor: String) {
55
getAllRows(tableId: $tableId, stringifiedCursor: $stringifiedCursor) {
66
rows
7+
columnIds
78
stringifiedCursor
89
}
910
}

0 commit comments

Comments
 (0)