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]
}
}
2 Replies
GabrielC
GabrielC14mo ago
I'm trying to do something like this now
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 BaseMysqlTableType = ReturnType<typeof baseMysqlTable>;

const withPagination = async <S extends AnyMySqlSelect, T extends AnyMySqlTable<BaseMysqlTableType>> (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({}).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 BaseMysqlTableType = ReturnType<typeof baseMysqlTable>;

const withPagination = async <S extends AnyMySqlSelect, T extends AnyMySqlTable<BaseMysqlTableType>> (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({}).from(table);
return {
data,
offset,
limit,
// total: count[0]
}
}
but still dosen't work
Andrii Sherman
Andrii Sherman14mo ago
How do you want to execute your function? could you write a small usage example (it can be without implementation) just how you want to use it