Type alias InferTableSchema<T>

InferTableSchema<T>: T extends CreateTableDefinition
    ? InferTableSchemaFromDefinition<T>
    : 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

Automagically extracts a table's schema from some Table-like type, most useful when performing a Db.createTable (or Table.alter) operation.

You can think of it as similar to Zod or arktype's infer<Schema> types.

Accepts various different (contextually) isomorphic types to account for differences in instantiation & usage:

  • CreateTableDefinition
  • (...) => Promise<Table<infer Schema>>
  • (...) => Table<infer Schema>
  • Promise<Table<infer Schema>>
  • Table<infer Schema>

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

const mkUserTable = () => db.createTable('users', {
definition: {
columns: {
name: 'text',
dob: {
type: 'timestamp',
},
friends: {
type: 'set',
valueType: 'text',
},
},
primaryKey: {
partitionBy: ['name', 'height'],
partitionSort: { dob: 1 },
},
},
});

// Type inference is as simple as that
type User = InferTableSchema<typeof mkUserTable>;
type UserPK = InferTablePrimaryKey<typeof mkUserTable>;

// Utility types for demonstration purposes
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
type Expect<T extends true> = T;

// User evaluates to this object representing its TS representation
// for the table's schema
type _Proof = Equal<User, {
name: string,
dob: DataAPITimestamp,
friends: Set<string>,
}>;

// UserPK evaluates to this object representing its TS representation
// for `insert*` operations' return types
type _ProofPK = Equal<UserPK, {
name: string,
height: TypeErr<'Field `height` not found as property in table definition'>,
dob: DataAPITimestamp,
}>;

// And now `User` can be used wherever.
const main = async () => {
const table: Table<User> = await mkUserTable();
const found: User | null = await table.findOne({});
};