D1 client API is fully-typed via the @cloudflare/workers-types package, and also supports generic types as part of its TypeScript API. A generic type allows you to provide an optional type parameter so that a function understands the type of the data it is handling.When using the query statement methods stmt.all(), stmt.raw() and stmt.first(), you can provide a type representing each database row. D1’s API will return the result object with the correct type.For example, providing an OrderRow type as a type parameter to stmt.all() will return a typed Array<OrderRow> object instead of the default Record<string, unknown> type:
D1 client API is fully-typed via the @cloudflare/workers-types package, and also supports generic types as part of its TypeScript API. A generic type allows you to provide an optional type parameter so that a function understands the type of the data it is handling.When using the query statement methods stmt.all(), stmt.raw() and stmt.first(), you can provide a type representing each database row. D1’s API will return the result object with the correct type.For example, providing an OrderRow type as a type parameter to stmt.all() will return a typed Array<OrderRow> object instead of the default Record<string, unknown> type:
but trying this out it's giving false type-safety.
(note the camel case property names, and there is no
breed
breed
field)
and the following query:
const result = await context.env.DB.prepare( `SELECT id, type, name, date_of_birth, date_of_death, chip_number, breed FROM pet ORDER BY name ASC LIMIT 100` ).all<PetRow>(); const pets = result.results;
const result = await context.env.DB.prepare( `SELECT id, type, name, date_of_birth, date_of_death, chip_number, breed FROM pet ORDER BY name ASC LIMIT 100` ).all<PetRow>(); const pets = result.results;
(note the underscore case as it is stored like that in the database)