|
| 1 | +--- |
| 2 | +title: Mapping |
| 3 | +description: Mapping protocol schemas |
| 4 | +--- |
| 5 | + |
| 6 | +# Mapping |
| 7 | + |
| 8 | +<Callout type="info"> |
| 9 | +**Source:** `packages/spec/src/data/mapping.zod.ts` |
| 10 | +</Callout> |
| 11 | + |
| 12 | +## TypeScript Usage |
| 13 | + |
| 14 | +```typescript |
| 15 | +import { MappingSchema, FieldMappingSchema, TransformType } from '@objectstack/spec/data'; |
| 16 | +import type { Mapping, FieldMapping } from '@objectstack/spec/data'; |
| 17 | + |
| 18 | +// Validate data |
| 19 | +const result = MappingSchema.parse(data); |
| 20 | +``` |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## FieldMapping |
| 25 | + |
| 26 | +### Properties |
| 27 | + |
| 28 | +| Property | Type | Required | Description | |
| 29 | +| :--- | :--- | :--- | :--- | |
| 30 | +| **source** | `string \| string[]` | ✅ | Source column header(s) | |
| 31 | +| **target** | `string \| string[]` | ✅ | Target object field(s) | |
| 32 | +| **transform** | `Enum<'none' \| 'constant' \| 'lookup' \| 'split' \| 'join' \| 'javascript' \| 'map'>` | optional | Transformation type (default: 'none') | |
| 33 | +| **params** | `object` | optional | Configuration for transform | |
| 34 | + |
| 35 | +### Transform Params Properties |
| 36 | + |
| 37 | +| Property | Type | Required | Description | |
| 38 | +| :--- | :--- | :--- | :--- | |
| 39 | +| **value** | `any` | optional | Value for constant transform | |
| 40 | +| **object** | `string` | optional | Lookup Object for lookup transform | |
| 41 | +| **fromField** | `string` | optional | Match on field (e.g. "name") for lookup | |
| 42 | +| **toField** | `string` | optional | Value to take (e.g. "_id") for lookup | |
| 43 | +| **autoCreate** | `boolean` | optional | Create if missing for lookup | |
| 44 | +| **valueMap** | `Record<string, any>` | optional | Value mapping for map transform (e.g. { "Open": "draft" }) | |
| 45 | +| **separator** | `string` | optional | Separator for split/join transforms | |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## Mapping |
| 50 | + |
| 51 | +Defines a reusable data mapping configuration for ETL operations. |
| 52 | + |
| 53 | +**NAMING CONVENTION:** |
| 54 | +Mapping names are machine identifiers and must be lowercase snake_case. |
| 55 | + |
| 56 | +**Examples of good mapping names:** |
| 57 | +- `salesforce_to_crm` |
| 58 | +- `csv_import_contacts` |
| 59 | +- `api_sync_orders` |
| 60 | + |
| 61 | +**Examples of bad mapping names (will be rejected):** |
| 62 | +- `SalesforceToCRM` (PascalCase) |
| 63 | +- `CSV Import` (spaces) |
| 64 | + |
| 65 | +### Properties |
| 66 | + |
| 67 | +| Property | Type | Required | Description | |
| 68 | +| :--- | :--- | :--- | :--- | |
| 69 | +| **name** | `string` | ✅ | Mapping unique name (lowercase snake_case) | |
| 70 | +| **label** | `string` | optional | Human readable label | |
| 71 | +| **sourceFormat** | `Enum<'csv' \| 'json' \| 'xml' \| 'sql'>` | optional | Source data format (default: 'csv') | |
| 72 | +| **targetObject** | `string` | ✅ | Target Object Name | |
| 73 | +| **fieldMapping** | `object[]` | ✅ | Column Mappings | |
| 74 | +| **mode** | `Enum<'insert' \| 'update' \| 'upsert'>` | optional | Data operation mode (default: 'insert') | |
| 75 | +| **upsertKey** | `string[]` | optional | Fields to match for upsert (e.g. email) | |
| 76 | +| **extractQuery** | `object` | optional | Query to run for export only | |
| 77 | +| **errorPolicy** | `Enum<'skip' \| 'abort' \| 'retry'>` | optional | Error handling strategy (default: 'skip') | |
| 78 | +| **batchSize** | `number` | optional | Batch size for operations (default: 1000) | |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## TransformType |
| 83 | + |
| 84 | +Built-in helpers for converting data during import. |
| 85 | + |
| 86 | +### Allowed Values |
| 87 | + |
| 88 | +* `none` - Direct copy |
| 89 | +* `constant` - Use a hardcoded value |
| 90 | +* `lookup` - Resolve FK (Name → ID) |
| 91 | +* `split` - "John Doe" → ["John", "Doe"] |
| 92 | +* `join` - ["John", "Doe"] → "John Doe" |
| 93 | +* `javascript` - Custom script (Review security!) |
| 94 | +* `map` - Value mapping (e.g. "Active" → "active") |
0 commit comments