dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
32 lines (31 loc) • 1.06 kB
JavaScript
import { isNumber } from '../../../utils/validation/isNumber.js';
import { Path } from '../utils/path.js';
export const expressPaths = (paths) => {
let ProjectionExpression = '';
const ExpressionAttributeNames = {};
const tokens = {};
let cursor = 1;
paths.forEach((path, index) => {
if (index > 0) {
ProjectionExpression += ', ';
}
new Path(path).arrayPath.forEach((pathPart, index) => {
if (isNumber(pathPart)) {
ProjectionExpression += `[${pathPart}]`;
return;
}
let token = tokens[pathPart];
if (token === undefined) {
token = `#p_${cursor}`;
tokens[pathPart] = token;
ExpressionAttributeNames[token] = pathPart;
cursor++;
}
if (index > 0) {
ProjectionExpression += '.';
}
ProjectionExpression += token;
});
});
return { ProjectionExpression, ExpressionAttributeNames };
};