2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2022-10-21 11:52:43 -07:00
|
|
|
import { IDataObject, jsonParse } from 'n8n-workflow';
|
2020-04-04 07:04:25 -07:00
|
|
|
import { ICollection } from './CollectionInterface';
|
|
|
|
import { cockpitApiRequest } from './GenericFunctions';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function createCollectionEntry(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
resourceName: string,
|
|
|
|
data: IDataObject,
|
|
|
|
id?: string,
|
|
|
|
): Promise<any> {
|
2020-04-04 07:04:25 -07:00
|
|
|
const body: ICollection = {
|
2020-04-11 11:24:51 -07:00
|
|
|
data,
|
2020-04-04 07:04:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
body.data = {
|
|
|
|
_id: id,
|
2020-10-22 06:46:03 -07:00
|
|
|
...body.data,
|
2020-04-04 07:04:25 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return cockpitApiRequest.call(this, 'post', `/collections/save/${resourceName}`, body);
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getAllCollectionEntries(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
resourceName: string,
|
|
|
|
options: IDataObject,
|
|
|
|
): Promise<any> {
|
2020-04-04 07:04:25 -07:00
|
|
|
const body: ICollection = {};
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.fields) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const fields = (options.fields as string).split(',').map((field) => field.trim());
|
2020-04-12 03:28:36 -07:00
|
|
|
|
|
|
|
const bodyFields = {
|
|
|
|
_id: false,
|
|
|
|
} as IDataObject;
|
|
|
|
for (const field of fields) {
|
|
|
|
bodyFields[field] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.fields = bodyFields;
|
2020-04-05 06:49:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.filter) {
|
2022-10-21 11:52:43 -07:00
|
|
|
body.filter = jsonParse(options.filter.toString(), {
|
|
|
|
errorMessage: "'Filter' option is not valid JSON",
|
|
|
|
});
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.limit) {
|
|
|
|
body.limit = options.limit as number;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.skip) {
|
|
|
|
body.skip = options.skip as number;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.sort) {
|
2022-10-21 11:52:43 -07:00
|
|
|
body.sort = jsonParse(options.sort.toString(), {
|
|
|
|
errorMessage: "'Sort' option is not valid JSON",
|
|
|
|
});
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.populate) {
|
|
|
|
body.populate = options.populate as boolean;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:03:49 -07:00
|
|
|
body.simple = true;
|
|
|
|
if (options.rawData) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.simple = !options.rawData;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
if (options.language) {
|
|
|
|
body.lang = options.language as string;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:03:49 -07:00
|
|
|
return cockpitApiRequest.call(this, 'post', `/collections/get/${resourceName}`, body);
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
2020-04-05 06:49:47 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getAllCollectionNames(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
): Promise<string[]> {
|
2022-12-29 03:20:43 -08:00
|
|
|
return cockpitApiRequest.call(this, 'GET', '/collections/listCollections', {});
|
2020-04-06 01:03:49 -07:00
|
|
|
}
|