寻求适用于DynamoDB的JavaScript JSON压缩及分类数据编解码工具
Great question! It sounds like you need a tool that handles bidirectional mapping between enum-like index arrays and human-readable labels, supports deep nested structures, lets you target specific fields via wildcard paths, and ideally compresses key names for DynamoDB efficiency. Let’s break down your options:
Why You Might Not Have Found Existing Tools
Most off-the-shelf libraries focus on either generic JSON transformation or DynamoDB type conversion, rather than the specific combination of wildcard path targeting + bidirectional enum mapping + nested structure support. The right keywords to search for are things like "schema-based value transformer", "enum mapper with path matching", or "DynamoDB nested data encoder".
Recommended Tools & Workarounds
If you want to avoid building everything from scratch, here are some paths to explore:
1. Extend General-Purpose Transformation Libraries
Many flexible libraries can be adapted to your needs with minimal custom code:
- Lodash: Use
_.get()and_.set()to target fields via paths (you can add wildcard support by parsing paths into regex or using_.matches()for pattern matching). Pair this with your mapping arrays to handle the index ↔ label conversion. It’s lightweight and perfect for quick custom implementations. - Joi: While primarily a validation library, its schema traversal capabilities let you define custom rules for specific fields. You can add custom validators/transformers that handle the enum mapping for nested or wildcard-targeted paths.
- JSON Schema Transformers: Libraries like
json-schema-transformerlet you define transformation rules based on JSON Schema. You can extend these to support bidirectional mapping and wildcard path targeting.
2. DynamoDB-Specific Optimization Tools
For key name compression (a huge win for DynamoDB’s storage costs), combine the above with:
- Custom key mapping: Maintain a simple lookup object (e.g.,
{ "yourfriendsare": "yfa" }) to compress long keys during encoding and expand them during decoding. dynamodb-encoder: A lightweight library that handles DynamoDB type conversions, and can be extended to include your enum mapping and key compression logic.
Custom Implementation (If No Tool Fits)
If you end up building your own, the core logic is straightforward. Here’s a quick JavaScript example using Lodash to handle decoding (easily adapted for encoding):
const _ = require('lodash'); // Define your rules: path → mapping array (supports simple wildcards with custom parsing) const transformationRules = [ { path: 'yourfriendsare', mapping: ["Religious", "Atheist", "Other"] }, { path: '*.beliefs', mapping: ["Christian", "Muslim", "Atheist", "Other"] } // Wildcard example ]; // Helper to resolve wildcard paths (simplified version) function getMatchingPaths(obj, wildcardPath) { const regex = new RegExp(`^${wildcardPath.replace(/\*/g, '[^.]+')}$`); const paths = []; function traverse(currentObj, currentPath) { _.forEach(currentObj, (value, key) => { const fullPath = currentPath ? `${currentPath}.${key}` : key; if (regex.test(fullPath)) paths.push(fullPath); if (typeof value === 'object' && value !== null) traverse(value, fullPath); }); } traverse(obj, ''); return paths; } function decodeItem(item) { const decoded = _.cloneDeep(item); transformationRules.forEach(rule => { const targetPaths = rule.path.includes('*') ? getMatchingPaths(decoded, rule.path) : [rule.path]; targetPaths.forEach(path => { const value = _.get(decoded, path); if (Array.isArray(value)) { const mappedValues = value .map(index => rule.mapping[index - 1]) // Adjust if your indices are 1-based .filter(Boolean); _.set(decoded, path, mappedValues); } }); }); return decoded; } // Example usage const encodedItem = { yourfriendsare: [1, 3], user: { beliefs: [2, 4] } }; console.log(decodeItem(encodedItem)); // Output: { yourfriendsare: ["Religious", "Other"], user: { beliefs: ["Muslim", "Other"] } }
Key Tips for Your Implementation
- Wildcard Paths: Use
glob-to-regexpto convert wildcard paths (like**/interests) into regex for easier traversal. - Mapping Centralization: Store your enum mappings in a separate config or DynamoDB table so you can update them without changing code, and share them across multi-language clients.
- Performance: Since DynamoDB operations are usually per-item or small batches, even a custom implementation will be fast enough. Cache regex patterns and mapping arrays to optimize repeated calls.
内容的提问来源于stack exchange,提问作者user2856949

