© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
4 replies
Gabriel

Error types with custom schema

Hi, I'm trying to do a general function to paginate my querys, but I'm having some problems with the types.
I'm trying to use the types of the library, but I'm not sure if I'm doing it right.
All my tables have the same columns, so I created a function to create the base table, and I'm trying to use it to create the pagination function,
and definitly i'm doing it wrong but I'm not shure how to do it, could you point me in the right direction? to know if it's possible to do it.

const baseMysqlTable = <TColumnsMap extends Record<string, AnyMySqlColumnBuilder>>(name: string, columns: TColumnsMap) => {
    return mysqlTable(name, {
        ...columns,
        id: int('id').primaryKey().autoincrement().notNull(),
        updatedAt: timestamp('updated_at').onUpdateNow(),
        createdAt: timestamp('created_at').defaultNow(),
        deletedAt: timestamp('deleted_at')
    });
};

type BaseMysqlTable = InferModel<typeof baseMysqlTable>;

const withPagination = async <S extends AnyMySqlSelect, T extends BaseMysqlTable>(qb: S, table: T, offset = 0, limit = 10) => {
    const data = await qb.offset(offset).limit(limit).orderBy(desc(table.id));
    // const count = await db.select(sql<number>`count(${table.id})`).from(table);
    return {
        data,
        offset,
        limit,
        // total: count[0]
    }
}
const baseMysqlTable = <TColumnsMap extends Record<string, AnyMySqlColumnBuilder>>(name: string, columns: TColumnsMap) => {
    return mysqlTable(name, {
        ...columns,
        id: int('id').primaryKey().autoincrement().notNull(),
        updatedAt: timestamp('updated_at').onUpdateNow(),
        createdAt: timestamp('created_at').defaultNow(),
        deletedAt: timestamp('deleted_at')
    });
};

type BaseMysqlTable = InferModel<typeof baseMysqlTable>;

const withPagination = async <S extends AnyMySqlSelect, T extends BaseMysqlTable>(qb: S, table: T, offset = 0, limit = 10) => {
    const data = await qb.offset(offset).limit(limit).orderBy(desc(table.id));
    // const count = await db.select(sql<number>`count(${table.id})`).from(table);
    return {
        data,
        offset,
        limit,
        // total: count[0]
    }
}
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

schema types
Drizzle TeamDTDrizzle Team / help
3y ago
drizzle-zod with custom types
Drizzle TeamDTDrizzle Team / help
3y ago
Use 'query' with custom schema
Drizzle TeamDTDrizzle Team / help
12mo ago
Inferring Types from Schema?
Drizzle TeamDTDrizzle Team / help
13mo ago