11import { Database } from "@sqlitecloud/drivers" ;
22
33// CREATE OBJECTS TABLE
4- export const initializeObjectsTable = ( db : Database ) => {
4+ export const initializeObjectsTable = (
5+ db : Database
6+ ) : Promise < { error : Error | null ; message : string } > => {
57 const createTableStatement = `
68 CREATE TABLE IF NOT EXISTS objects (
79 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@@ -16,13 +18,30 @@ export const initializeObjectsTable = (db: Database) => {
1618 CREATE INDEX IF NOT EXISTS idx_objects_bucket_key ON objects (bucket, key);
1719 CREATE INDEX IF NOT EXISTS idx_objects_key ON objects (key);
1820 ` ;
19- try {
20- db . run ( createTableStatement ) ;
21- console . log ( "Successfully created table or table already exists" ) ;
22- db . run ( createIndexStatement ) ;
23- console . log ( "Successfully created index or index already" ) ;
24- return { error : null , message : "Successfully created table and index" } ;
25- } catch ( error ) {
26- return { error, message : "Error creating table and index" } ;
27- }
21+
22+ return new Promise ( ( resolve , reject ) => {
23+ db . run ( createTableStatement , [ ] , ( tableError ) => {
24+ if ( tableError ) {
25+ console . log ( "Error creating table" , tableError ) ;
26+ return reject ( { error : tableError , message : "Error creating table" } ) ;
27+ } else {
28+ console . log ( "Successfully created table or table already exists" ) ;
29+ db . run ( createIndexStatement , [ ] , ( indexError ) => {
30+ if ( indexError ) {
31+ console . log ( "Error creating index" , indexError ) ;
32+ return reject ( {
33+ error : indexError ,
34+ message : "Error creating index" ,
35+ } ) ;
36+ } else {
37+ console . log ( "Successfully created index or index already" ) ;
38+ return resolve ( {
39+ error : null ,
40+ message : "Successfully created table and index" ,
41+ } ) ;
42+ }
43+ } ) ;
44+ }
45+ } ) ;
46+ } ) ;
2847} ;
0 commit comments