2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2022-01-08 01:36:07 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
|
|
|
INodeProperties,
|
|
|
|
NodeApiError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function supabaseApiRequest(
|
|
|
|
this:
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IHookFunctions
|
|
|
|
| IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
): Promise<any> {
|
|
|
|
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,
|
|
|
|
uri: uri || `${credentials.host}/rest/v1${resource}`,
|
|
|
|
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) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2022-11-08 06:28:21 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.helpers.request!(options);
|
2022-06-03 10:23:49 -07:00
|
|
|
}
|