@@ -3,6 +3,7 @@ import type {
33 PostgrestResponse ,
44 PostgrestSingleResponse ,
55} from "@supabase/supabase-js" ;
6+ import { PostgrestError } from "@supabase/supabase-js" ;
67import type { Database , Tables , TablesInsert } from "@repo/database/dbTypes" ;
78
89export const KNOWN_EMBEDDING_TABLES : {
@@ -95,7 +96,9 @@ export const getOrCreateEntity = async <
9596} ) : Promise < PostgrestSingleResponse < Tables < TableName > > > => {
9697 const result : PostgrestSingleResponse < Tables < TableName > > = await supabase
9798 . from ( tableName )
98- . upsert ( insertData , {
99+ // Typescript gets confused with latest supabase
100+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
101+ . upsert ( insertData as any , {
99102 onConflict : uniqueOn === undefined ? undefined : uniqueOn . join ( "," ) ,
100103 ignoreDuplicates : false ,
101104 count : "estimated" ,
@@ -138,14 +141,15 @@ export const getOrCreateEntity = async <
138141 console . log ( `Found ${ tableName } on re-fetch:` , reFetchedEntity ) ;
139142 // Note: Using a PostgrestResult means I cannot have both data and error non-null...
140143 return {
141- error : {
144+ error : new PostgrestError ( {
142145 ...result . error ,
143146 message : `Upsert failed because of conflict with this entity: ${ reFetchedEntity } "` ,
144- } ,
147+ } ) ,
145148 statusText : result . statusText ,
146149 data : null ,
147150 count : null ,
148151 status : 400 ,
152+ success : false ,
149153 } ;
150154 }
151155 }
@@ -183,7 +187,9 @@ export const InsertValidatedBatch = async <
183187} ) : Promise < PostgrestResponse < Tables < TableName > > > => {
184188 const result : PostgrestResponse < Tables < TableName > > = await supabase
185189 . from ( tableName )
186- . upsert ( items , {
190+ // Typescript gets confused with latest supabase
191+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
192+ . upsert ( items as any , {
187193 onConflict : uniqueOn === undefined ? undefined : uniqueOn . join ( "," ) ,
188194 ignoreDuplicates : false ,
189195 count : "estimated" ,
@@ -231,14 +237,14 @@ export const validateAndInsertBatch = async <
231237 const validationErrors : { index : number ; error : string } [ ] = [ ] ;
232238 if ( ! Array . isArray ( items ) || items . length === 0 ) {
233239 return {
234- error : {
240+ error : new PostgrestError ( {
235241 message : `Request body must be a non-empty array of ${ tableName } items.` ,
236242 details : "" ,
237- hint : "" ,
243+ hint : "nonempty " ,
238244 code : "1" ,
239- name : "nonempty" ,
240- } ,
245+ } ) ,
241246 status : 400 ,
247+ success : false ,
242248 data : null ,
243249 count : null ,
244250 statusText : "Empty input" ,
@@ -269,13 +275,13 @@ export const validateAndInsertBatch = async <
269275
270276 if ( validationErrors . length > 0 ) {
271277 return {
272- error : {
278+ error : new PostgrestError ( {
273279 message : `Validation failed for one or more ${ tableName } items.` ,
274280 details : `${ validationErrors } ` ,
275- hint : "" ,
281+ hint : "invalid " ,
276282 code : "2" ,
277- name : "invalid" ,
278- } ,
283+ } ) ,
284+ success : false ,
279285 status : 400 ,
280286 data : null ,
281287 count : null ,
@@ -322,20 +328,21 @@ export const validateAndInsertBatch = async <
322328 // Erring on the side of returning data with an error in status.
323329 return {
324330 error : null ,
331+ success : true ,
325332 status : 500 ,
326333 data : validatedResults ,
327334 count : validatedResults . length ,
328335 statusText : `Validation failed for one or more ${ tableName } items, and succeeded for ${ validatedResults . length } /${ result . data . length } .` ,
329336 } ;
330337 } else {
331338 return {
332- error : {
339+ error : new PostgrestError ( {
333340 message : `Post-validation failed for all ${ tableName } items.` ,
334341 details : `${ validationErrors } ` ,
335- hint : "" ,
342+ hint : "invalid " ,
336343 code : "2" ,
337- name : "invalid" ,
338- } ,
344+ } ) ,
345+ success : false ,
339346 status : 500 ,
340347 data : null ,
341348 count : null ,
@@ -366,17 +373,17 @@ export const processAndInsertBatch = async <
366373 inputProcessor : ItemProcessor < InputType , TablesInsert < TableName > > ;
367374 outputProcessor : ItemProcessor < Tables < TableName > , OutputType > ;
368375} ) : Promise < PostgrestResponse < OutputType > > => {
369- let processedItems : TablesInsert < TableName > [ ] = [ ] ;
376+ const processedItems : TablesInsert < TableName > [ ] = [ ] ;
370377 const validationErrors : { index : number ; error : string } [ ] = [ ] ;
371378 if ( ! Array . isArray ( items ) || items . length === 0 ) {
372379 return {
373- error : {
380+ error : new PostgrestError ( {
374381 message : `Request body must be a non-empty array of ${ tableName } items.` ,
375382 details : "" ,
376- hint : "" ,
383+ hint : "nonempty " ,
377384 code : "1" ,
378- name : "nonempty" ,
379- } ,
385+ } ) ,
386+ success : false ,
380387 status : 400 ,
381388 data : null ,
382389 count : null ,
@@ -407,13 +414,13 @@ export const processAndInsertBatch = async <
407414
408415 if ( validationErrors . length > 0 ) {
409416 return {
410- error : {
417+ error : new PostgrestError ( {
411418 message : `Validation failed for one or more ${ tableName } items.` ,
412419 details : `${ validationErrors } ` ,
413- hint : "" ,
420+ hint : "invalid " ,
414421 code : "2" ,
415- name : "invalid" ,
416- } ,
422+ } ) ,
423+ success : false ,
417424 status : 400 ,
418425 data : null ,
419426 count : null ,
@@ -457,19 +464,20 @@ export const processAndInsertBatch = async <
457464 return {
458465 error : null ,
459466 status : 500 ,
467+ success : true ,
460468 data : processedResults ,
461469 count : processedResults . length ,
462470 statusText : `Validation failed for one or more ${ tableName } items, and succeeded for ${ processedResults . length } /${ result . data . length } .` ,
463471 } ;
464472 } else {
465473 return {
466- error : {
474+ error : new PostgrestError ( {
467475 message : `Post-validation failed for all ${ tableName } items.` ,
468476 details : `${ validationErrors } ` ,
469- hint : "" ,
477+ hint : "invalid " ,
470478 code : "2" ,
471- name : "invalid" ,
472- } ,
479+ } ) ,
480+ success : false ,
473481 status : 500 ,
474482 data : null ,
475483 count : null ,
0 commit comments