Ensure Drizzle findMany conforms to Typebox schema

Hi All, I am running into an issue when using drizzle with an elysia app. I have the following typebox schema which uses drizzle-typebox to generate types for typebox
import { t } from "elysia";
...
import { createSelectSchema } from "drizzle-typebox";

export const companiesSchema = createSelectSchema(companies);

const getCompaniesSchema = {
query: t.Object({
page: t.Optional(t.Numeric()),
limit: t.Optional(t.Numeric()),
}),
response: t.Object({
200: t.Object({
data: t.Array(companiesSchema),
pagination: t.Object({
page: t.Number(),
limit: t.Number(),
total_count: t.Number(),
}),
}),
}),
};

export { getCompaniesSchema };
import { t } from "elysia";
...
import { createSelectSchema } from "drizzle-typebox";

export const companiesSchema = createSelectSchema(companies);

const getCompaniesSchema = {
query: t.Object({
page: t.Optional(t.Numeric()),
limit: t.Optional(t.Numeric()),
}),
response: t.Object({
200: t.Object({
data: t.Array(companiesSchema),
pagination: t.Object({
page: t.Number(),
limit: t.Number(),
total_count: t.Number(),
}),
}),
}),
};

export { getCompaniesSchema };
in the file where i define the route handler for the Elysia app, the getCompanies function is highlighted saying that the response object does not match the schema provided in the interface.
import { Handler } from "elysia";


import {
companiesSchema,
getCompaniesSchema,
} from "../routes/schema/companies-schema";

// Services
import {
getPaginatedCompanies,
getAllCompanies,
} from "../services/companies-service";

interface ICompaniesHandler {
getCompanies: Handler<typeof getCompaniesSchema>;
addCompany: Handler;
}

const companiesHandler: ICompaniesHandler = {
getCompanies: async ({ query, set }) => {
// Extract Page and Limit
const { page = 1, limit = 5 } = query;

// Init Offset
const offset = (page - 1) * limit;

// Get Companies and Count
const companies = await getPaginatedCompanies.execute({
limit,
offset,
});
const total_count = (await getAllCompanies.execute()).length;

set.status = 200;

const response = {
data: companies,
pagination: {
page,
limit,
total_count,
},
};

return response;
},
addCompany: async ({}) => {},
};

export default companiesHandler;
import { Handler } from "elysia";


import {
companiesSchema,
getCompaniesSchema,
} from "../routes/schema/companies-schema";

// Services
import {
getPaginatedCompanies,
getAllCompanies,
} from "../services/companies-service";

interface ICompaniesHandler {
getCompanies: Handler<typeof getCompaniesSchema>;
addCompany: Handler;
}

const companiesHandler: ICompaniesHandler = {
getCompanies: async ({ query, set }) => {
// Extract Page and Limit
const { page = 1, limit = 5 } = query;

// Init Offset
const offset = (page - 1) * limit;

// Get Companies and Count
const companies = await getPaginatedCompanies.execute({
limit,
offset,
});
const total_count = (await getAllCompanies.execute()).length;

set.status = 200;

const response = {
data: companies,
pagination: {
page,
limit,
total_count,
},
};

return response;
},
addCompany: async ({}) => {},
};

export default companiesHandler;
any idea on how to fix this? thanks in advance!
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server