Type alias TableSchemaTypeOverrides

TableSchemaTypeOverrides: Partial<Record<LitUnion<keyof CqlNonGenericType2TSTypeDict | keyof CqlGenericType2TSTypeDict<any, any>>, unknown>>
Overview

Provides a way to override the type of specific datatypes in the InferTableSchema-like utility types.

Use-case: Custom ser/des

This is especially useful when working with custom ser/des, necessitating you to use a different type for a specific datatype.

Example

const BigIntAsBigNumberCodec = TableCodecs.forType('bigint', {
deserialize: (value, ctx) => ctx.done(BigNumber(value)),
});

const ProductSchema = Table.schema({
columns: {
id: 'bigint',
description: 'text',
price: 'bigint',
},
primaryKey: 'id',
});

// type Product = {
// id: BigNumber, (primary key is always non-null)
// description?: string | null,
// price?: BigNumber | null,
// }
type Product = InferTableSchema<typeof ProductSchema, { bigint: BigNumber | null }>;

const table = await db.createTable('products', {
definition: ProductSchema,
serdes: {
codecs: [BigIntAsBigNumberCodec],
},
});
Use-case: Removing | null

If you really want a column to be non-null, you can use the TypeOverrides type to override the type of a specific datatype to be non-null.

Example

const ProductSchema = Table.schema({
columns: {
id: 'bigint',
description: 'text',
price: 'bigint',
},
primaryKey: 'id',
});

// type Product = {
// id: BigNumber, (primary key is always non-null)
// description?: string,
// price?: BigNumber,
// }
type Product = InferTableSchema<typeof ProductSchema, {
bigint: BigNumber,
text: string,
}>;
Use-case: Adding datatypes

You can also add typing support for a datatype which isn't yet supported by the client.

Example

const ProductSchema = Table.schema({
columns: {
id: 'bigint',
description: 'text',
price: 'super_duper_bigint',
},
primaryKey: 'id',
});

// type Product = {
// id: BigNumber,
// description?: string,
// price?: BigNumber,
// }
type Product = InferTableSchema<typeof ProductSchema, {
super_duper_bigint: BigNumber,
}>;
Overriding collection types

🚨Important: Because TypeScript does not natively support higher-kinded types, it is not yet possible to polymorphically override the type of collection types (e.g. list, set, map).

However, you can still technically override the type of a collection datatype monomorphically.

Example

const ExampleSchema = Table.schema({
columns: {
id: 'uuid',
map: { type: 'map', keyType: 'text', valueType: 'int' },
},
primaryKey: 'id',
});

// type Example = {
// id: UUID,
// map?: Record<string, number>,
// }
type Example = InferTableSchema<typeof ExampleSchema, {
map: Map<string, number>,
}>;