mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
d9b98fc8be
* ✨ Create rule `no-unneeded-backticks` * 👕 Enable rule * ⚡ Run rule on `cli` * ⚡ Run rule on `core` * ⚡ Run rule on `workflow` * ⚡ Rule rule on `design-system` * ⚡ Run rule on `node-dev` * ⚡ Run rule on `editor-ui` * ⚡ Run rule on `nodes-base`
87 lines
2 KiB
TypeScript
87 lines
2 KiB
TypeScript
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
|
import { IDataObject, jsonParse } from 'n8n-workflow';
|
|
import { ICollection } from './CollectionInterface';
|
|
import { cockpitApiRequest } from './GenericFunctions';
|
|
|
|
export async function createCollectionEntry(
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
resourceName: string,
|
|
data: IDataObject,
|
|
id?: string,
|
|
): Promise<any> {
|
|
const body: ICollection = {
|
|
data,
|
|
};
|
|
|
|
if (id) {
|
|
body.data = {
|
|
_id: id,
|
|
...body.data,
|
|
};
|
|
}
|
|
|
|
return cockpitApiRequest.call(this, 'post', `/collections/save/${resourceName}`, body);
|
|
}
|
|
|
|
export async function getAllCollectionEntries(
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
resourceName: string,
|
|
options: IDataObject,
|
|
): Promise<any> {
|
|
const body: ICollection = {};
|
|
|
|
if (options.fields) {
|
|
const fields = (options.fields as string).split(',').map((field) => field.trim());
|
|
|
|
const bodyFields = {
|
|
_id: false,
|
|
} as IDataObject;
|
|
for (const field of fields) {
|
|
bodyFields[field] = true;
|
|
}
|
|
|
|
body.fields = bodyFields;
|
|
}
|
|
|
|
if (options.filter) {
|
|
body.filter = jsonParse(options.filter.toString(), {
|
|
errorMessage: "'Filter' option is not valid JSON",
|
|
});
|
|
}
|
|
|
|
if (options.limit) {
|
|
body.limit = options.limit as number;
|
|
}
|
|
|
|
if (options.skip) {
|
|
body.skip = options.skip as number;
|
|
}
|
|
|
|
if (options.sort) {
|
|
body.sort = jsonParse(options.sort.toString(), {
|
|
errorMessage: "'Sort' option is not valid JSON",
|
|
});
|
|
}
|
|
|
|
if (options.populate) {
|
|
body.populate = options.populate as boolean;
|
|
}
|
|
|
|
body.simple = true;
|
|
if (options.rawData) {
|
|
body.simple = !options.rawData;
|
|
}
|
|
|
|
if (options.language) {
|
|
body.lang = options.language as string;
|
|
}
|
|
|
|
return cockpitApiRequest.call(this, 'post', `/collections/get/${resourceName}`, body);
|
|
}
|
|
|
|
export async function getAllCollectionNames(
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
): Promise<string[]> {
|
|
return cockpitApiRequest.call(this, 'GET', '/collections/listCollections', {});
|
|
}
|