What's the correct type to use for an update function?
I'm trying to do a simple function where I update a record in a table:
When doing this:
I get the error:
Here's my schema
Because I'm using
Should I use something like
import { NewVideo, VideoTable } from '~/db/schema/video';
const Table = VideoTable;
export function update(id: string, data: NewVideo) {
const query = db.update(Table).set(data).where(eq(Table.id, id));
return query;
}import { NewVideo, VideoTable } from '~/db/schema/video';
const Table = VideoTable;
export function update(id: string, data: NewVideo) {
const query = db.update(Table).set(data).where(eq(Table.id, id));
return query;
}When doing this:
await Video.update(newVideo.id, {
transcription_id: transcribeJobData.transcribeJobName,
}); await Video.update(newVideo.id, {
transcription_id: transcribeJobData.transcribeJobName,
});I get the error:
Property 'url' is missing in type '{ transcription_id: string | undefined; }' but required in type '{ url: string; id?: string | undefined; transcription_id?: string | null | undefined; transcription?: { start_time: number; end_time: number; type: string; content: string; }[] | null | undefined; createdAt?: Date | ... 1 more ... | undefined; updatedAt?: Date | ... 1 more ... | undefined; }'Property 'url' is missing in type '{ transcription_id: string | undefined; }' but required in type '{ url: string; id?: string | undefined; transcription_id?: string | null | undefined; transcription?: { start_time: number; end_time: number; type: string; content: string; }[] | null | undefined; createdAt?: Date | ... 1 more ... | undefined; updatedAt?: Date | ... 1 more ... | undefined; }'Here's my schema
import {
jsonb,
pgTable,
text,
timestamp,
uuid,
varchar,
} from 'drizzle-orm/pg-core';
export const tableName = 'videos';
export const VideoTable = pgTable(tableName, {
id: uuid('id').primaryKey().defaultRandom(),
url: text('url').notNull(),
transcription_id: varchar('transcription_id'),
transcription: jsonb('transcription').$type<
Array<{
start_time: number;
end_time: number;
type: string;
content: string;
}>
>(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
});
const Table = VideoTable;
export type Video = typeof Table.$inferSelect;
export type NewVideo = typeof Table.$inferInsert;import {
jsonb,
pgTable,
text,
timestamp,
uuid,
varchar,
} from 'drizzle-orm/pg-core';
export const tableName = 'videos';
export const VideoTable = pgTable(tableName, {
id: uuid('id').primaryKey().defaultRandom(),
url: text('url').notNull(),
transcription_id: varchar('transcription_id'),
transcription: jsonb('transcription').$type<
Array<{
start_time: number;
end_time: number;
type: string;
content: string;
}>
>(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
});
const Table = VideoTable;
export type Video = typeof Table.$inferSelect;
export type NewVideo = typeof Table.$inferInsert;Because I'm using
NewVideoNewVideo, it TS expects a urlurl param, but because the record has already been created, this should already exist.Should I use something like
Partial<NewVideo>Partial<NewVideo>?