n8n/packages/nodes-base/nodes/Cockpit/CollectionFunctions.ts

78 lines
2 KiB
TypeScript
Raw Normal View History

2020-04-04 07:04:25 -07:00
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions
} from 'n8n-core';
import { IDataObject } 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> { // tslint:disable-line:no-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,
...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> { // tslint:disable-line:no-any
2020-04-04 07:04:25 -07:00
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 = JSON.parse(options.filter.toString());
2020-04-04 07:04:25 -07:00
}
if (options.limit) {
body.limit = options.limit as number;
2020-04-04 07:04:25 -07:00
}
if (options.skip) {
body.skip = options.skip as number;
2020-04-04 07:04:25 -07:00
}
if (options.sort) {
body.sort = JSON.parse(options.sort.toString());
2020-04-04 07:04:25 -07:00
}
if (options.populate) {
body.populate = options.populate as boolean;
2020-04-04 07:04:25 -07:00
}
body.simple = true;
if (options.rawData) {
body.simple = !options.rawData as boolean;
2020-04-04 07:04:25 -07:00
}
if (options.language) {
body.lang = options.language as string;
2020-04-04 07:04:25 -07:00
}
return cockpitApiRequest.call(this, 'post', `/collections/get/${resourceName}`, body);
2020-04-04 07:04:25 -07:00
}
export async function getAllCollectionNames(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions): Promise<string[]> {
return cockpitApiRequest.call(this, 'GET', `/collections/listCollections`, {});
}