feat(Supabase Node): Add support for database schema (#13339)

Co-authored-by: Dana <152518854+dana-gill@users.noreply.github.com>
Co-authored-by: Dana Lee <dana@n8n.io>
This commit is contained in:
Ria Scholz
2025-04-14 17:56:07 +02:00
committed by GitHub
parent de03452631
commit 23f25cefbf
4 changed files with 141 additions and 8 deletions

View File

@@ -14,14 +14,22 @@ import { Supabase } from '../Supabase.node';
describe('Test Supabase Node', () => {
const node = new Supabase();
const input = [{ json: {} }];
const mockRequestWithAuthentication = jest.fn().mockResolvedValue([]);
beforeEach(() => {
jest.clearAllMocks();
});
const createMockExecuteFunction = (
nodeParameters: IDataObject,
continueOnFail: boolean = false,
) => {
const fakeExecuteFunction = {
getCredentials: jest.fn().mockResolvedValue({
host: 'https://api.supabase.io',
serviceRole: 'service_role',
}),
getNodeParameter(
parameterName: string,
itemIndex: number,
@@ -29,13 +37,10 @@ describe('Test Supabase Node', () => {
options?: IGetNodeParameterOptions | undefined,
) {
const parameter = options?.extractValue ? `${parameterName}.value` : parameterName;
const parameterValue = get(nodeParameters, parameter, fallbackValue);
if ((parameterValue as IDataObject)?.nodeOperationError) {
throw new NodeOperationError(mock(), 'Get Options Error', { itemIndex });
}
return parameterValue;
},
getNode() {
@@ -44,6 +49,7 @@ describe('Test Supabase Node', () => {
continueOnFail: () => continueOnFail,
getInputData: () => input,
helpers: {
requestWithAuthentication: mockRequestWithAuthentication,
constructExecutionMetaData: (
_inputData: INodeExecutionData[],
_options: { itemData: IPairedItemData | IPairedItemData[] },
@@ -95,5 +101,102 @@ describe('Test Supabase Node', () => {
offset: 0,
},
);
supabaseApiRequest.mockRestore();
});
it('should not set schema headers if no custom schema is used', async () => {
const fakeExecuteFunction = createMockExecuteFunction({
resource: 'row',
operation: 'getAll',
returnAll: true,
useCustomSchema: false,
schema: 'public',
tableId: 'my_table',
});
await node.execute.call(fakeExecuteFunction);
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
'supabaseApi',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
Prefer: 'return=representation',
}),
uri: 'https://api.supabase.io/rest/v1/my_table',
}),
);
});
it('should set the schema headers for GET calls if custom schema is used', async () => {
const fakeExecuteFunction = createMockExecuteFunction({
resource: 'row',
operation: 'getAll',
returnAll: true,
useCustomSchema: true,
schema: 'custom_schema',
tableId: 'my_table',
});
await node.execute.call(fakeExecuteFunction);
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
'supabaseApi',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
'Accept-Profile': 'custom_schema',
Prefer: 'return=representation',
}),
uri: 'https://api.supabase.io/rest/v1/my_table',
}),
);
});
it('should set the schema headers for POST calls if custom schema is used', async () => {
const fakeExecuteFunction = createMockExecuteFunction({
resource: 'row',
operation: 'create',
returnAll: true,
useCustomSchema: true,
schema: 'custom_schema',
tableId: 'my_table',
});
await node.execute.call(fakeExecuteFunction);
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
'supabaseApi',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Profile': 'custom_schema',
Prefer: 'return=representation',
}),
uri: 'https://api.supabase.io/rest/v1/my_table',
}),
);
});
it('should show descriptive message when error is caught', async () => {
const fakeExecuteFunction = createMockExecuteFunction({
resource: 'row',
operation: 'create',
returnAll: true,
useCustomSchema: true,
schema: '',
tableId: 'my_table',
});
fakeExecuteFunction.helpers.requestWithAuthentication = jest.fn().mockRejectedValue({
description: 'Something when wrong',
message: 'error',
});
await expect(node.execute.call(fakeExecuteFunction)).rejects.toHaveProperty(
'message',
'error: Something when wrong',
);
});
});