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:
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 NewVideo, it TS expects a url param, but because the record has already been created, this should already exist. Should I use something like Partial<NewVideo>?
2 Replies
Noahh
Noahh15mo ago
Yes, it's because NewVideo is what is required/optional when inserting something. You could do Partial<Video> or Partial<NewVideo>, just note that that will allow anyone using that function to update any of the fields. If you care about which fields, you could use the TypeScript Pick<Video, ...> and choose which fields to allow (or Omit<Video, ...> to do the opposite)
PGT
PGTOP15mo ago
thanks @Noahh that's good confirmation, I was thinking there might be a Drizzle way, but I'll stick with this for now
Want results from more Discord servers?
Add your server