@type-map-for/avro
v0.1.1
Published
Compile-time TypeScript → Avro field mapping
Readme
@type-map-for/avro
Generate Avro schemas from TypeScript interfaces at compile time with union types for optional fields.
Installation
npm install @type-map-for/avroUsage
import type { AvroSchema } from '@type-map-for/avro';
interface User {
id: string;
name: string;
email?: string;
tags: string[];
metadata?: { source: string };
}
type UserSchema = AvroSchema<User>;
// Result: {
// type: 'record'
// fields: Array<{
// { name: 'id'; type: 'string' }
// { name: 'name'; type: 'string' }
// { name: 'email'; type: ['null', 'string'] }
// { name: 'tags'; type: { type: 'array'; items: 'string' } }
// { name: 'metadata'; type: ['null', 'bytes'] }
// }>
// }
// Use with schema validation
const userSchema: UserSchema = {
type: 'record',
fields: [
{ name: 'id', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'email', type: ['null', 'string'] },
{ name: 'tags', type: { type: 'array', items: 'string' } },
{ name: 'metadata', type: ['null', 'bytes'] }
]
} as const satisfies UserSchema;Type Mapping
| TypeScript | Avro |
|------------|------|
| string | 'string' |
| number | 'int' | 'double' |
| boolean | 'boolean' |
| object | 'bytes' |
| T[] | { type: 'array', items: T } |
| T \| undefined | ['null', T] (union type) |
| T[] \| undefined | ['null', { type: 'array', items: T }] |
Types
AvroType<T>- Avro type definition for TypeScript typeTAvroField<T, K>- Schema field for propertyKof typeTAvroSchema<T>- Complete Avro record schema for interfaceT
Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
