@type-map-for/dynamodb
v0.1.1
Published
Compile-time TypeScript → DynamoDB field mapping
Readme
@type-map-for/dynamodb
Generate DynamoDB attribute definitions from TypeScript interfaces at compile time.
Installation
npm install @type-map-for/dynamodbUsage
import type { DynamoDBSchema } from '@type-map-for/dynamodb';
interface User {
id: string;
name: string;
age: number;
active: boolean;
tags: string[];
metadata: { source: string };
}
type UserSchema = DynamoDBSchema<User>;
// Result: Array<{
// { AttributeName: 'id'; AttributeType: 'S' }
// { AttributeName: 'name'; AttributeType: 'S' }
// { AttributeName: 'age'; AttributeType: 'N' }
// { AttributeName: 'active'; AttributeType: 'BOOL' }
// { AttributeName: 'tags'; AttributeType: 'L' }
// { AttributeName: 'metadata'; AttributeType: 'M' }
// }>
// Use with schema validation
const userSchema: UserSchema = [
{ AttributeName: 'id', AttributeType: 'S' },
{ AttributeName: 'name', AttributeType: 'S' },
{ AttributeName: 'age', AttributeType: 'N' },
{ AttributeName: 'active', AttributeType: 'BOOL' },
{ AttributeName: 'tags', AttributeType: 'L' },
{ AttributeName: 'metadata', AttributeType: 'M' }
] as const satisfies UserSchema;Type Mapping
| TypeScript | DynamoDB |
|------------|----------|
| string | 'S' (String) |
| number | 'N' (Number) |
| boolean | 'BOOL' (Boolean) |
| Array<T> | 'L' (List) |
| object | 'M' (Map) |
Note: DynamoDB uses coarse-grained types. All arrays become 'L' and all objects become 'M' regardless of their internal structure.
Types
DynamoDBField<T, K>- Attribute definition for propertyKof typeTDynamoDBSchema<T>- Complete attribute definitions array for interfaceT
Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
