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],
},
});
| 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.
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,
}>;
You can also add typing support for a datatype which isn't yet supported by the client.
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,
}>;
🚨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.
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>,
}>;
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.