Reputation: 97
So I'm new to drizzle, big fan of typeorm here and I know how questionable is that but anyway.
I created my schema and my very first query, read something about inferSelect types and typeof to safely check the results of my queries but I can hit the right key to find how to use them.
import {pgTable, varchar} from 'drizzle-orm/pg-core';
import {defaults} from "../../db/schema/defaults";
export const eyeSchema = pgTable('eye', {
id: defaults.id,
value: varchar('value', { length: 256 }).notNull(),
});
export type Eye = typeof eyeSchema.$inferSelect; // return type when queried
export type NewEye = typeof eyeSchema.$inferInsert; // insert type
const a: typeof eyeSchema = await db.query.eye.findMany();
const b = await db.select().from(eyeSchema);
This is failing when compiling the ts file and I really don't know why..
Type '{ [x: string]: unknown; }[]' is not assignable to type 'PgTableWithColumns<{ name: "eye"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "eye"; dataType: "number"; columnType: "PgSmallInt"; data: number; driverParam: string | number; notNull: true; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>; value: PgColumn<...>; }; diale...'.
Type '{ [x: string]: unknown; }[]' is missing the following properties from type 'PgTable<{ name: "eye"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "eye"; dataType: "number"; columnType: "PgSmallInt"; data: number; driverParam: string | number; notNull: true; hasDefault: false; enumValues: undefined; baseColumn: never; }, {}, {}>; value: PgColumn<...>; }; dialect: "pg"; }>': _, $inferSelect, $inferInsert, [IsDrizzleTable], getSQL
Upvotes: 3
Views: 3381
Reputation: 11
You do not need to assign the eyeScheme type to the query. This is done automatically by Drizzle itself. For example, the inferSelect types are there to control the incoming types of a function.
async function createEye({ value }: NewEye) {
await db.insert(eyeSchema).values({ value });
}
Upvotes: 1