Skip to main content

@terascope/data-types / data-type / DataType

Class: DataType

Defined in: data-types/src/data-type.ts:41

A DataType is used to define the structure of data with version support and can be converted to the following formats:

  • Elasticsearch Mappings
  • GraphQL Schemas
  • xLucene

It is constructed from a DataTypeConfig (a version plus a map of field names to their BaseType configs); nested Object and Tuple fields declared with dot-notation are grouped into GroupType / TupleType instances during construction.

Example​

const dataType = new DataType({
version: 1,
fields: {
name: { type: 'Keyword' },
location: { type: 'GeoPoint' },
},
}, 'Person');

dataType.toESMapping(); // Elasticsearch/OpenSearch mapping
dataType.toGraphQL(); // GraphQL schema string
dataType.toXlucene(); // xLucene type config

Constructors​

Constructor​

new DataType(config, typeName?, description?): DataType

Defined in: data-types/src/data-type.ts:123

Parameters​

ParameterTypeDescription
configPartial<DataTypeConfig>the data type config; version defaults to the latest and fields to {}, and the config is validated (throwing on an unknown version or invalid field config).
typeName?stringoptional name for the type (required by some conversions, e.g. GraphQL).
description?stringoptional description carried into the GraphQL output.

Returns​

DataType

Properties​

PropertyModifierTypeDescriptionDefined in
description?readonlystring-data-types/src/data-type.ts:43
fieldsreadonlyReadonlyDataTypeFields-data-types/src/data-type.ts:44
groupedFieldsreadonlyGroupedFieldsAn object of base fields with their child fieldsdata-types/src/data-type.ts:47
namereadonlystring-data-types/src/data-type.ts:42
versionreadonlynumber-data-types/src/data-type.ts:45

Methods​

toESMapping()​

toESMapping(__namedParameters?): ESMapping

Defined in: data-types/src/data-type.ts:142

Convert the DataType to an elasticsearch mapping.

Parameters​

ParameterType
__namedParametersPartial<ESMappingOptions>

Returns​

ESMapping


toGraphQL()​

toGraphQL(args?, removeScalars?): string

Defined in: data-types/src/data-type.ts:199

Convert the DataType to a formatted GraphQL schema string. Combines the generated custom types, the base type, an optional input type, and any args.customTypes, then validates and prints the result. Pass removeScalars to strip scalar declarations from the output.

Note: fields that reference an undeclared scalar (e.g. Any/Tuple → JSON) require that scalar to be supplied for the schema to validate.

Parameters​

ParameterTypeDefault value
args?GraphQLOptionsundefined
removeScalars?booleanfalse

Returns​

string


toGraphQLTypes()​

toGraphQLTypes(args?): GraphQLTypesResult

Defined in: data-types/src/data-type.ts:215

Build the GraphQL type parts without assembling/validating a full schema: the baseType definition, an optional inputType, and the customTypes (scalars and generated nested types) the fields reference. Use this when composing several data types into one schema (see DataType.mergeGraphQLDataTypes); use toGraphQL for a single, ready-to-use schema string. Throws if no type name is available.

Parameters​

ParameterType
argsGraphQLOptions

Returns​

GraphQLTypesResult


toXlucene()​

toXlucene(): xLuceneTypeConfig

Defined in: data-types/src/data-type.ts:294

Convert the DataType to an xLucene type config — a flat map of field name (including nested dot-notation paths) to its xLucene field type, merged across every field.

Returns​

xLuceneTypeConfig


mergeGraphQLDataTypes()​

static mergeGraphQLDataTypes(types, options?): string

Defined in: data-types/src/data-type.ts:58

Merge multiple data types into a single GraphQL schema, de-duplicating shared custom types and scalars. Throws on a missing or duplicate type name (or duplicate input name when createInputTypes is set). Options control input-type generation, snake_case naming, scalar removal, and per-type reference/virtual fields.

Parameters​

ParameterType
typesDataType[]
optionsMergeGraphQLOptions

Returns​

string