2026-07-06 14:23:37 +07:00
import { sql } from 'drizzle-orm' ;
import { db } from './index' ;
export interface Bucket {
id : string ;
name : string ;
createdAt : Date ;
updatedAt : Date ;
}
2026-07-07 00:49:45 +07:00
type QueryRow = Record < string , unknown >;
type QueryResult = QueryRow [];
2026-07-06 14:23:37 +07:00
export const createBucket = async ( name : string ) : Promise < Bucket > => {
const result = ( await db . execute (
sql `INSERT INTO buckets (name) VALUES ( ${ name } ) RETURNING id, name, created_at, updated_at` ,
)) as unknown as QueryResult ;
2026-07-07 00:49:45 +07:00
const row = result [ 0 ] ! ;
2026-07-06 14:23:37 +07:00
return {
id : row.id as string ,
name : row.name as string ,
createdAt : new Date ( row . created_at as string ),
updatedAt : new Date ( row . updated_at as string ),
};
};
export const findBucketByName = async ( name : string ) : Promise < Bucket | null > => {
const result = ( await db . execute (
sql `SELECT id, name, created_at, updated_at FROM buckets WHERE name = ${ name } ` ,
)) as unknown as QueryResult ;
2026-07-07 00:49:45 +07:00
if ( result . length === 0 ) return null ;
const row = result [ 0 ] ! ;
2026-07-06 14:23:37 +07:00
return {
id : row.id as string ,
name : row.name as string ,
createdAt : new Date ( row . created_at as string ),
updatedAt : new Date ( row . updated_at as string ),
};
};
export const listBuckets = async () : Promise < Bucket [] > => {
const result = ( await db . execute (
sql `SELECT id, name, created_at, updated_at FROM buckets ORDER BY name` ,
)) as unknown as QueryResult ;
2026-07-07 00:49:45 +07:00
return result . map (( row ) => ({
2026-07-06 14:23:37 +07:00
id : row.id as string ,
name : row.name as string ,
createdAt : new Date ( row . created_at as string ),
updatedAt : new Date ( row . updated_at as string ),
}));
};
export const deleteBucket = async ( name : string ) : Promise < boolean > => {
2026-07-07 00:49:45 +07:00
// Cascade-delete rows that hold FK references to the bucket
await db . execute (
sql `DELETE FROM multipart_parts WHERE upload_id IN (SELECT upload_id FROM multipart_uploads WHERE bucket_id IN (SELECT id FROM buckets WHERE name = ${ name } ))` ,
). catch (() => {});
await db . execute (
sql `DELETE FROM multipart_uploads WHERE bucket_id IN (SELECT id FROM buckets WHERE name = ${ name } )` ,
). catch (() => {});
await db . execute (
sql `DELETE FROM files WHERE bucket_id IN (SELECT id FROM buckets WHERE name = ${ name } )` ,
). catch (() => {});
2026-07-06 14:23:37 +07:00
const result = ( await db . execute (
sql `DELETE FROM buckets WHERE name = ${ name } ` ,
)) as unknown as QueryResult ;
2026-07-07 00:49:45 +07:00
return result . length > 0 ;
2026-07-06 14:23:37 +07:00
};
export const bucketExists = async ( name : string ) : Promise < boolean > => {
const result = ( await db . execute (
sql `SELECT 1 FROM buckets WHERE name = ${ name } ` ,
)) as unknown as QueryResult ;
2026-07-07 00:49:45 +07:00
return result . length > 0 ;
2026-07-06 14:23:37 +07:00
};