Skip to content

Commit 52c4f37

Browse files
authored
Pass tablesDatabaseId and tablesDatabaseToken as server context to OpenOps tables operations (#1671)
Fixes OPS-3022, OPS-3023, OPS-3024, OPS-3025. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Improved internal server context handling for table operations to enhance system reliability and consistency across database interactions. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 0571656 commit 52c4f37

File tree

16 files changed

+258
-61
lines changed

16 files changed

+258
-61
lines changed

packages/blocks/openops-tables/src/actions/delete-record-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const deleteRecordAction = createAction({
3333
const tableId = await cacheWrapper.getOrAdd(
3434
tableCacheKey,
3535
getTableIdByTableName,
36-
[tableName],
36+
[tableName, context.server],
3737
);
3838

3939
const tokenOrResolver = await resolveTokenProvider(context.server);

packages/blocks/openops-tables/src/actions/get-records-action.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ export const getRecordsAction = createAction({
4141
displayName: '',
4242
required: true,
4343
refreshers: ['tableName'],
44-
props: async ({ tableName }) => {
44+
props: async ({ tableName }, ctx) => {
4545
if (!tableName) {
4646
return {};
4747
}
4848
const properties: { [key: string]: any } = {};
4949

5050
const tableFields = await getTableFields(
5151
tableName as unknown as string,
52+
ctx.server,
5253
);
5354

5455
properties['filters'] = Property.Array({
@@ -119,7 +120,7 @@ export const getRecordsAction = createAction({
119120
const tableId = await cacheWrapper.getOrAdd(
120121
tableCacheKey,
121122
getTableIdByTableName,
122-
[tableName],
123+
[tableName, context.server],
123124
);
124125

125126
const filtersProps = context.propsValue.filters['filters'] as unknown as {

packages/blocks/openops-tables/src/actions/get-table-url-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const getTableUrlAction = createAction({
1818
},
1919
async run(context) {
2020
const tableName = context.propsValue.tableName as unknown as string;
21-
const tableId = await getTableIdByTableName(tableName);
21+
const tableId = await getTableIdByTableName(tableName, context.server);
2222
const baseUrl = system.getOrThrow(SharedSystemProp.FRONTEND_URL);
2323

2424
return (

packages/blocks/openops-tables/src/actions/update-record-action.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ export const updateRecordAction = createAction({
3030
'The primary key value of the row to update. If the row does not exist, a new row will be created.',
3131
required: true,
3232
refreshers: ['tableName'],
33-
props: async ({ tableName }) => {
33+
props: async ({ tableName }, context) => {
3434
if (!tableName) {
3535
return {};
3636
}
3737

38-
const fields = await getTableFields(tableName as unknown as string);
38+
const fields = await getTableFields(
39+
tableName as unknown as string,
40+
context.server,
41+
);
3942

4043
const primaryKeyField = getPrimaryKeyFieldFromFields(fields);
4144

@@ -69,6 +72,7 @@ export const updateRecordAction = createAction({
6972

7073
const tableFields = await getTableFields(
7174
tableName as unknown as string,
75+
context.server,
7276
);
7377

7478
const properties: { [key: string]: any } = {};
@@ -129,7 +133,7 @@ export const updateRecordAction = createAction({
129133
const tableId = await cacheWrapper.getOrAdd(
130134
tableCacheKey,
131135
getTableIdByTableName,
132-
[tableName],
136+
[tableName, context.server],
133137
);
134138

135139
const fieldsCacheKey = `${context.run.id}-${tableId}-fields`;

packages/blocks/openops-tables/test/actions/delete-record-action.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,16 @@ describe('deleteRecordAction', () => {
236236
});
237237

238238
function validateWrapperCall(context: any) {
239+
const expectedServerContext = {
240+
tablesDatabaseId: context.server.tablesDatabaseId,
241+
tablesDatabaseToken: context.server.tablesDatabaseToken,
242+
};
239243
expect(cacheWrapperMock.getOrAdd).toHaveBeenCalledTimes(2);
240244
expect(cacheWrapperMock.getOrAdd).toHaveBeenNthCalledWith(
241245
1,
242246
`${context.run.id}-table-${context.propsValue.tableName}`,
243247
getTableIdByTableName,
244-
[context.propsValue.tableName],
248+
[context.propsValue.tableName, expectedServerContext],
245249
);
246250
expect(cacheWrapperMock.getOrAdd).toHaveBeenNthCalledWith(
247251
2,

packages/blocks/openops-tables/test/actions/get-records-action.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ describe('getRecordsAction test', () => {
100100
expect(openopsCommonMock.getTableFields).toHaveBeenCalledTimes(1);
101101
expect(openopsCommonMock.getTableFields).toHaveBeenCalledWith(
102102
'Opportunity',
103+
{
104+
tablesDatabaseId: 1,
105+
tablesDatabaseToken: 'token',
106+
},
103107
);
104108
});
105109
});
@@ -313,12 +317,16 @@ describe('getRecordsAction test', () => {
313317
});
314318

315319
function validateWrapperCall(context: any) {
320+
const expectedServerContext = {
321+
tablesDatabaseId: context.server.tablesDatabaseId,
322+
tablesDatabaseToken: context.server.tablesDatabaseToken,
323+
};
316324
expect(cacheWrapperMock.getOrAdd).toHaveBeenCalledTimes(1);
317325
expect(cacheWrapperMock.getOrAdd).toHaveBeenNthCalledWith(
318326
1,
319327
`${context.run.id}-table-${context.propsValue.tableName}`,
320328
getTableIdByTableName,
321-
[context.propsValue.tableName],
329+
[context.propsValue.tableName, expectedServerContext],
322330
);
323331
}
324332

packages/blocks/openops-tables/test/actions/get-table-url-action.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('getTableUrlAction', () => {
6565
expect(openopsCommonMock.getTableIdByTableName).toHaveBeenCalledTimes(1);
6666
expect(openopsCommonMock.getTableIdByTableName).toHaveBeenCalledWith(
6767
'my table',
68+
{
69+
tablesDatabaseId: 1,
70+
tablesDatabaseToken: 'token',
71+
},
6872
);
6973
expect(systemMock.getOrThrow).toHaveBeenCalledTimes(1);
7074
expect(systemMock.getOrThrow).toHaveBeenCalledWith('FRONTEND_URL');

packages/blocks/openops-tables/test/actions/update-record-action.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ describe('fieldsProperties property', () => {
373373
expect(openopsCommonMock.getTableFields).toHaveBeenCalledTimes(1);
374374
expect(openopsCommonMock.getTableFields).toHaveBeenCalledWith(
375375
'Opportunity',
376+
{
377+
tablesDatabaseId: 1,
378+
tablesDatabaseToken: 'token',
379+
},
376380
);
377381
});
378382
});
@@ -383,7 +387,13 @@ function validateWrapperCall(context: any) {
383387
1,
384388
`${context.run.id}-table-${context.propsValue.tableName}`,
385389
getTableIdByTableName,
386-
[context.propsValue.tableName],
390+
[
391+
context.propsValue.tableName,
392+
{
393+
tablesDatabaseId: context.server.tablesDatabaseId,
394+
tablesDatabaseToken: context.server.tablesDatabaseToken,
395+
},
396+
],
387397
);
388398
expect(cacheWrapperMock.getOrAdd).toHaveBeenNthCalledWith(
389399
2,

packages/openops/src/lib/openops-tables/openops-tables-common-properties.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Property, Validators } from '@openops/blocks-framework';
2-
import { IAxiosRetryConfig } from 'axios-retry';
3-
import { authenticateDefaultUserInOpenOpsTables } from './auth-user';
2+
import { resolveTokenProvider, TablesServerContext } from './context-helpers';
43
import {
54
DateOpenOpsField,
65
DurationOpenOpsField,
@@ -35,13 +34,11 @@ export function openopsTablesDropdownProperty(): any {
3534

3635
export async function getTableFields(
3736
tableName: string,
38-
axiosRetryConfig?: IAxiosRetryConfig,
37+
serverContext: TablesServerContext,
3938
): Promise<OpenOpsField[]> {
40-
const { token } = await authenticateDefaultUserInOpenOpsTables();
41-
42-
const tableId = await getTableIdByTableName(tableName as unknown as string);
43-
44-
return await getFields(tableId, token, false, axiosRetryConfig);
39+
const tableId = await getTableIdByTableName(tableName, serverContext);
40+
const tokenOrResolver = await resolveTokenProvider(serverContext);
41+
return await getFields(tableId, tokenOrResolver, false);
4542
}
4643

4744
// https://api.baserow.io/api/redoc/#tag/Database-table-fields/operation/get_database_table_field

packages/openops/src/lib/openops-tables/tables.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { makeOpenOpsTablesGet } from '../openops-tables/requests-helpers';
22
import { getDefaultDatabaseId } from './applications-service';
33
import { authenticateDefaultUserInOpenOpsTables } from './auth-user';
4+
import { TablesServerContext } from './context-helpers';
45
import { createAxiosHeaders } from './create-axios-headers';
56

67
async function getTables(
@@ -17,6 +18,7 @@ async function getTables(
1718

1819
export async function getTableIdByTableName(
1920
tableName: string,
21+
_tablesServerContext: TablesServerContext,
2022
): Promise<number> {
2123
const table = await getTableByName(tableName);
2224

0 commit comments

Comments
 (0)