@type-map-for/elasticsearch
v0.1.1
Published
Compile-time TypeScript → Elasticsearch field mapping
Readme
@type-map-for/elasticsearch
Generate Elasticsearch mapping schemas from TypeScript interfaces at compile time with full support for nested objects.
Installation
npm install @type-map-for/elasticsearchUsage
import type { ElasticsearchProperties } from '@type-map-for/elasticsearch';
interface User {
id: string;
name: string;
email?: string;
profile: {
age: number;
settings: { theme: string };
};
posts: Array<{
title: string;
tags: string[];
}>;
}
type UserMapping = ElasticsearchProperties<User>;
// Result: {
// id: { type: 'text' }
// name: { type: 'text' }
// email: { type: 'text' }
// profile: {
// type: 'object'
// properties: {
// age: { type: 'integer' | 'float' }
// settings: {
// type: 'object'
// properties: { theme: { type: 'text' } }
// }
// }
// }
// posts: {
// type: 'nested'
// properties: {
// title: { type: 'text' }
// tags: { type: 'text' }
// }
// }
// }
// Use with schema validation
const userMapping: UserMapping = {
id: { type: 'text' },
name: { type: 'text' },
email: { type: 'text' },
profile: {
type: 'object',
properties: {
age: { type: 'integer' },
settings: {
type: 'object',
properties: { theme: { type: 'text' } }
}
}
},
posts: {
type: 'nested',
properties: {
title: { type: 'text' },
tags: { type: 'text' }
}
}
} as const satisfies UserMapping;Type Mapping
| TypeScript | Elasticsearch |
|------------|---------------|
| string | { type: 'text' } |
| number | { type: 'integer' \| 'float' } |
| boolean | { type: 'boolean' } |
| { ... } (object) | { type: 'object', properties: {...} } |
| Array<{ ... }> (object array) | { type: 'nested', properties: {...} } |
| T[] (primitive array) | { type: primitive } (arrays implicit) |
Types
ElasticsearchField<T, K>- Mapping field for propertyKof typeTElasticsearchProperties<T>- Complete properties mapping for interfaceT
Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
