2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-01-08 01:36:07 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
2022-01-08 01:36:07 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
INodeProperties,
|
2023-01-31 11:39:20 -08:00
|
|
|
IPairedItemData,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2022-01-08 01:36:07 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-01-08 01:36:07 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function supabaseApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject | IDataObject[] = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentials = (await this.getCredentials('supabaseApi')) as {
|
|
|
|
host: string;
|
|
|
|
serviceRole: string;
|
|
|
|
};
|
2022-01-08 01:36:07 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Prefer: 'return=representation',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${credentials.host}/rest/v1${resource}`,
|
2022-01-08 01:36:07 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2022-08-24 01:26:48 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'supabaseApi', options);
|
2022-01-08 01:36:07 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-01-08 01:36:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapOperations: { [key: string]: string } = {
|
2022-08-17 08:50:24 -07:00
|
|
|
create: 'created',
|
|
|
|
update: 'updated',
|
|
|
|
getAll: 'retrieved',
|
|
|
|
delete: 'deleted',
|
2022-01-08 01:36:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
export function getFilters(
|
|
|
|
resources: string[],
|
|
|
|
operations: string[],
|
|
|
|
{
|
|
|
|
includeNoneOption = true,
|
|
|
|
filterTypeDisplayName = 'Filter',
|
|
|
|
filterFixedCollectionDisplayName = 'Filters',
|
2023-08-01 06:32:33 -07:00
|
|
|
|
2022-01-08 01:36:07 -08:00
|
|
|
filterStringDisplayName = 'Filters (String)',
|
|
|
|
mustMatchOptions = [
|
|
|
|
{
|
|
|
|
name: 'Any Filter',
|
|
|
|
value: 'anyFilter',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'All Filters',
|
|
|
|
value: 'allFilters',
|
|
|
|
},
|
|
|
|
],
|
2022-08-17 08:50:24 -07:00
|
|
|
},
|
|
|
|
): INodeProperties[] {
|
2022-01-08 01:36:07 -08:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
displayName: filterTypeDisplayName,
|
|
|
|
name: 'filterType',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
...(includeNoneOption ? [{ name: 'None', value: 'none' }] : []),
|
|
|
|
{
|
|
|
|
name: 'Build Manually',
|
|
|
|
value: 'manual',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'String',
|
|
|
|
value: 'string',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: resources,
|
|
|
|
operation: operations,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'manual',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Must Match',
|
|
|
|
name: 'matchType',
|
|
|
|
type: 'options',
|
|
|
|
options: mustMatchOptions,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: resources,
|
|
|
|
operation: operations,
|
2022-08-17 08:50:24 -07:00
|
|
|
filterType: ['manual'],
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'anyFilter',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: filterFixedCollectionDisplayName,
|
|
|
|
name: 'filters',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: resources,
|
|
|
|
operation: operations,
|
2022-08-17 08:50:24 -07:00
|
|
|
filterType: ['manual'],
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
},
|
2022-04-22 09:29:51 -07:00
|
|
|
default: {},
|
2022-01-08 01:36:07 -08:00
|
|
|
placeholder: 'Add Condition',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Conditions',
|
|
|
|
name: 'conditions',
|
|
|
|
values: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Field Name or ID',
|
2022-01-08 01:36:07 -08:00
|
|
|
name: 'keyName',
|
|
|
|
type: 'options',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2022-01-08 01:36:07 -08:00
|
|
|
typeOptions: {
|
2022-08-17 08:50:24 -07:00
|
|
|
loadOptionsDependsOn: ['tableId'],
|
2022-01-08 01:36:07 -08:00
|
|
|
loadOptionsMethod: 'getTableColumns',
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Condition',
|
|
|
|
name: 'condition',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Equals',
|
|
|
|
value: 'eq',
|
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
{
|
|
|
|
name: 'Full-Text',
|
|
|
|
value: 'fullText',
|
|
|
|
},
|
2022-01-08 01:36:07 -08:00
|
|
|
{
|
|
|
|
name: 'Greater Than',
|
|
|
|
value: 'gt',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Greater Than or Equal',
|
|
|
|
value: 'gte',
|
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
{
|
|
|
|
name: 'ILIKE operator',
|
|
|
|
value: 'ilike',
|
|
|
|
description: 'Use * in place of %',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Is',
|
|
|
|
value: 'is',
|
|
|
|
description: 'Checking for exact equality (null,true,false,unknown)',
|
|
|
|
},
|
2022-01-08 01:36:07 -08:00
|
|
|
{
|
|
|
|
name: 'Less Than',
|
|
|
|
value: 'lt',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Less Than or Equal',
|
|
|
|
value: 'lte',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'LIKE operator',
|
|
|
|
value: 'like',
|
2022-06-03 10:23:49 -07:00
|
|
|
description: 'Use * in place of %',
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Not Equals',
|
|
|
|
value: 'neq',
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Search Function',
|
|
|
|
name: 'searchFunction',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
condition: ['fullText'],
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'to_tsquery',
|
|
|
|
value: 'fts',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'plainto_tsquery',
|
|
|
|
value: 'plfts',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'phraseto_tsquery',
|
|
|
|
value: 'phfts',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'websearch_to_tsquery',
|
|
|
|
value: 'wfts',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Field Value',
|
|
|
|
name: 'keyValue',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2022-12-02 12:54:28 -08:00
|
|
|
description: `Filter to decide which rows get ${mapOperations[operations[0]]}`,
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
{
|
2022-08-17 08:50:24 -07:00
|
|
|
displayName:
|
|
|
|
'See <a href="https://postgrest.org/en/v9.0/api.html#horizontal-filtering-rows" target="_blank">PostgREST guide</a> to creating filters',
|
2022-01-08 01:36:07 -08:00
|
|
|
name: 'jsonNotice',
|
|
|
|
type: 'notice',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: resources,
|
|
|
|
operation: operations,
|
2022-08-17 08:50:24 -07:00
|
|
|
filterType: ['string'],
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Filters (String)',
|
|
|
|
name: 'filterString',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: resources,
|
|
|
|
operation: operations,
|
2022-08-17 08:50:24 -07:00
|
|
|
filterType: ['string'],
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: 'name=eq.jhon',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const buildQuery = (obj: IDataObject, value: IDataObject) => {
|
|
|
|
if (value.condition === 'fullText') {
|
2022-08-17 08:50:24 -07:00
|
|
|
return Object.assign(obj, {
|
|
|
|
[`${value.keyName}`]: `${value.searchFunction}.${value.keyValue}`,
|
|
|
|
});
|
2022-01-08 01:36:07 -08:00
|
|
|
}
|
|
|
|
return Object.assign(obj, { [`${value.keyName}`]: `${value.condition}.${value.keyValue}` });
|
|
|
|
};
|
|
|
|
|
|
|
|
export const buildOrQuery = (key: IDataObject) => {
|
|
|
|
if (key.condition === 'fullText') {
|
|
|
|
return `${key.keyName}.${key.searchFunction}.${key.keyValue}`;
|
|
|
|
}
|
|
|
|
return `${key.keyName}.${key.condition}.${key.keyValue}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const buildGetQuery = (obj: IDataObject, value: IDataObject) => {
|
|
|
|
return Object.assign(obj, { [`${value.keyName}`]: `eq.${value.keyValue}` });
|
|
|
|
};
|
|
|
|
|
2022-03-25 09:41:53 -07:00
|
|
|
export async function validateCredentials(
|
2022-01-08 01:36:07 -08:00
|
|
|
this: ICredentialTestFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
decryptedCredentials: ICredentialDataDecryptedObject,
|
|
|
|
): Promise<any> {
|
2022-01-08 01:36:07 -08:00
|
|
|
const credentials = decryptedCredentials;
|
|
|
|
|
|
|
|
const { serviceRole } = credentials as {
|
2022-08-17 08:50:24 -07:00
|
|
|
serviceRole: string;
|
2022-01-08 01:36:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
apikey: serviceRole,
|
2022-03-25 09:41:53 -07:00
|
|
|
Authorization: 'Bearer ' + serviceRole,
|
2022-01-08 01:36:07 -08:00
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: `${credentials.host}/rest/v1/`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.request(options);
|
2022-06-03 10:23:49 -07:00
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
|
|
|
|
export function mapPairedItemsFrom<T>(iterable: Iterable<T> | ArrayLike<T>): IPairedItemData[] {
|
|
|
|
return Array.from(iterable, (_, i) => i).map((index) => {
|
|
|
|
return {
|
|
|
|
item: index,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|