2022-12-22 01:27:14 -08:00
|
|
|
import { deepCopy, IDataObject, INodeExecutionData, assert } from 'n8n-workflow';
|
2021-07-17 05:14:12 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
AdjustedPutItem,
|
|
|
|
AttributeValueType,
|
|
|
|
EAttributeValueType,
|
|
|
|
IAttributeNameUi,
|
|
|
|
IAttributeValue,
|
|
|
|
IAttributeValueUi,
|
|
|
|
IAttributeValueValue,
|
|
|
|
PutItemUi,
|
|
|
|
} from './types';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const addColon = (attribute: string) =>
|
|
|
|
(attribute = attribute.charAt(0) === ':' ? attribute : `:${attribute}`);
|
2021-07-17 05:14:12 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const addPound = (key: string) => (key = key.charAt(0) === '#' ? key : `#${key}`);
|
2021-07-17 05:14:12 -07:00
|
|
|
|
|
|
|
export function adjustExpressionAttributeValues(eavUi: IAttributeValueUi[]) {
|
|
|
|
const eav: IAttributeValue = {};
|
|
|
|
|
|
|
|
eavUi.forEach(({ attribute, type, value }) => {
|
|
|
|
eav[addColon(attribute)] = { [type]: value } as IAttributeValueValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return eav;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function adjustExpressionAttributeName(eanUi: IAttributeNameUi[]) {
|
2022-07-24 09:13:11 -07:00
|
|
|
const ean: { [key: string]: string } = {};
|
2021-07-17 05:14:12 -07:00
|
|
|
|
|
|
|
eanUi.forEach(({ key, value }) => {
|
2022-07-24 09:13:11 -07:00
|
|
|
ean[addPound(key)] = value;
|
2021-07-17 05:14:12 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return ean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function adjustPutItem(putItemUi: PutItemUi) {
|
|
|
|
const adjustedPutItem: AdjustedPutItem = {};
|
|
|
|
|
|
|
|
Object.entries(putItemUi).forEach(([attribute, value]) => {
|
|
|
|
let type: string;
|
|
|
|
|
|
|
|
if (typeof value === 'boolean') {
|
|
|
|
type = 'BOOL';
|
|
|
|
} else if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
|
|
|
|
type = 'M';
|
|
|
|
// @ts-ignore
|
|
|
|
} else if (isNaN(value)) {
|
|
|
|
type = 'S';
|
|
|
|
} else {
|
|
|
|
type = 'N';
|
|
|
|
}
|
|
|
|
|
|
|
|
adjustedPutItem[attribute] = { [type]: value.toString() };
|
|
|
|
});
|
|
|
|
|
|
|
|
return adjustedPutItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function simplify(item: IAttributeValue): IDataObject {
|
|
|
|
const output: IDataObject = {};
|
|
|
|
|
|
|
|
for (const [attribute, value] of Object.entries(item)) {
|
|
|
|
const [type, content] = Object.entries(value)[0] as [AttributeValueType, string];
|
|
|
|
output[attribute] = decodeAttribute(type, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-12-22 00:40:53 -08:00
|
|
|
function decodeAttribute(type: AttributeValueType, attribute: string | IAttributeValue) {
|
2021-07-17 05:14:12 -07:00
|
|
|
switch (type) {
|
|
|
|
case 'BOOL':
|
|
|
|
return Boolean(attribute);
|
|
|
|
case 'N':
|
|
|
|
return Number(attribute);
|
|
|
|
case 'S':
|
|
|
|
return String(attribute);
|
|
|
|
case 'SS':
|
|
|
|
case 'NS':
|
|
|
|
return attribute;
|
2022-12-21 20:36:49 -08:00
|
|
|
case 'M':
|
2022-12-22 01:27:14 -08:00
|
|
|
assert(
|
|
|
|
typeof attribute === 'object' && !Array.isArray(attribute) && attribute !== null,
|
|
|
|
'Attribute must be an object',
|
|
|
|
);
|
|
|
|
return simplify(attribute);
|
2021-07-17 05:14:12 -07:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validateJSON(input: any): object {
|
|
|
|
try {
|
|
|
|
return JSON.parse(input);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Items must be a valid JSON');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function copyInputItem(item: INodeExecutionData, properties: string[]): IDataObject {
|
|
|
|
// Prepare the data to insert and copy it to be returned
|
2022-12-02 12:54:28 -08:00
|
|
|
const newItem: IDataObject = {};
|
2021-07-17 05:14:12 -07:00
|
|
|
for (const property of properties) {
|
|
|
|
if (item.json[property] === undefined) {
|
|
|
|
newItem[property] = null;
|
|
|
|
} else {
|
2022-10-21 08:24:58 -07:00
|
|
|
newItem[property] = deepCopy(item.json[property]);
|
2021-07-17 05:14:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mapToAttributeValues(item: IDataObject): void {
|
|
|
|
for (const key of Object.keys(item)) {
|
|
|
|
if (!key.startsWith(':')) {
|
|
|
|
item[`:${key}`] = item[key];
|
|
|
|
delete item[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decodeItem(item: IAttributeValue): IDataObject {
|
|
|
|
const _item: IDataObject = {};
|
|
|
|
for (const entry of Object.entries(item)) {
|
|
|
|
const [attribute, value]: [string, object] = entry;
|
|
|
|
const [type, content]: [string, object] = Object.entries(value)[0];
|
|
|
|
_item[attribute] = decodeAttribute(type as EAttributeValueType, content as unknown as string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _item;
|
|
|
|
}
|