Type alias InferTableSchema<T, Overrides>

InferTableSchema<T, Overrides>: T extends CreateTableDefinition
    ? InferTableSchemaFromDefinition<T, Overrides>
    : Record<never, never> extends Overrides
        ? T extends ((..._) => Promise<Table<infer Schema, any, any>>)
            ? Schema
            : T extends ((..._) => Table<infer Schema, any, any>)
                ? Schema
                : T extends Promise<Table<infer Schema, any, any>>
                    ? Schema
                    : T extends Table<infer Schema, any, any>
                        ? Schema
                        : never
        : "ERROR: Can not provide TypeOverrides if not inferring the type from a CreateTableDefinition"
Overview

Automagically extracts a table's schema from a CreateTableDefinition or some Table-like type.

  • You can think of it as similar to Zod or arktype's infer<Schema> types.
  • This is most useful when creating a new table via Db.createTable.

It accepts various different (contextually) isomorphic types to account for differences in instantiation & usage.

💡Tip: Please see InferrableTableSchema for the different ways to use this utility type, and see TableSchemaTypeOverrides for how to override the type of specific datatypes.

✏️Note: A DB's type information is inferred by db.createTable by default. To override this behavior, please provide the table's type explicitly to help with transpilation times (e.g. db.createTable<SomeRow>(...)).

Type Parameters

Example

// Table.schema just validates the type of the definition
const UserSchema = Table.schema({
columns: {
id: 'text',
dob: 'date',
friends: { type: 'map', keyType: 'text', valueType: 'uuid' },
},
primaryKey: {
partitionBy: ['id'],
partitionSort: { dob: -1 }
},
});

// equivalent to:
// type User = {
// id: string,
// dob: DataAPIDate,
// friends?: Map<string, UUID>, (optional since it's not in the primary key)
// }
type User = InferTableSchema<typeof UserSchema>;

// equivalent to:
// type UserPK = Pick<User, 'id' | 'dob'>;
type UserPK = InferTablePrimaryKey<typeof mkTable>;

// table :: Table<User, UserPK>
const table = await db.createTable<User, UserPK>('users', {
definition: UserSchema,
ifNotExists: true,
});

See

  • InferrableTableSchema
  • TableSchemaTypeOverrides
  • Table.schema
  • InferTablePrimaryKey
  • InferTableReadSchema