@@ -8,17 +8,63 @@ const db = new sqlite3.Database(dbPath);
88
99const saltRounds = 10 ;
1010
11- // create users table if it doesn't exist with a default admin user
12- db . serialize ( ( ) => {
13- db . each ( "SELECT * FROM users" , ( err , rows ) => {
14- if ( ! err || rows ) return ;
15- db . run ( "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT, email TEXT, role TEXt, created_at DATETIME DEFAULT CURRENT_TIMESTAMP)" , async ( ) => {
16- await createUser ( { username : "admin" , password : "admin" , email : "test@mail.com" , role : "admin" } ) ;
17- console . log ( "Created default user: admin/admin" ) ;
11+ // create tables if they don't exist or add missing fields if needed
12+ function createTables ( ) {
13+ const userFields = [
14+ "id INTEGER PRIMARY KEY AUTOINCREMENT" ,
15+ "username TEXT" ,
16+ "password TEXT" ,
17+ "email TEXT" ,
18+ "role TEXt" ,
19+ "created_at DATETIME DEFAULT CURRENT_TIMESTAMP" ,
20+ ] ;
21+ const transcriptionFields = [
22+ "id INTEGER PRIMARY KEY AUTOINCREMENT" ,
23+ "filename TEXT" ,
24+ "path TEXT" ,
25+ "size INTEGER" ,
26+ "mimetype TEXT" ,
27+ "duration INTEGER" ,
28+ "language TEXT" ,
29+ "status TEXT" ,
30+ "user_id INTEGER" ,
31+ "result TEXT" ,
32+ "created_at DATETIME DEFAULT CURRENT_TIMESTAMP" ,
33+ "updated_at DATETIME DEFAULT CURRENT_TIMESTAMP" ,
34+ ]
35+ db . serialize ( ( ) => {
36+ db . get ( `SELECT count(*) AS count FROM sqlite_master WHERE type='table' AND name='users'` , ( err , rows ) => {
37+ if ( rows . count === 0 ) {
38+ db . run ( `CREATE TABLE users (${ userFields . join ( ", " ) } )` ) ;
39+ createUser ( { username : "admin" , password : "admin" , email : "test@mail.com" , role : "admin" } ) ;
40+ }
41+ else {
42+ // check that all fields are present
43+ db . all ( `PRAGMA table_info(users)` , ( err , rows ) => {
44+ const missingFields = userFields . filter ( field => ! rows . find ( row => row . name === field . split ( " " ) [ 0 ] ) ) ;
45+ if ( missingFields . length > 0 ) {
46+ db . run ( `ALTER TABLE users ADD COLUMN ${ missingFields . join ( ", " ) } ` ) ;
47+ }
48+ } ) ;
49+ }
50+ } ) ;
51+ db . get ( `SELECT count(*) AS count FROM sqlite_master WHERE type='table' AND name='transcriptions'` , ( err , rows ) => {
52+ if ( rows . count === 0 ) {
53+ db . run ( `CREATE TABLE transcriptions (${ transcriptionFields . join ( ", " ) } )` ) ;
54+ }
55+ else {
56+ // check that all fields are present
57+ db . all ( `PRAGMA table_info(transcriptions)` , ( err , rows ) => {
58+ const missingFields = transcriptionFields . filter ( field => ! rows . find ( row => row . name === field . split ( " " ) [ 0 ] ) ) ;
59+ if ( missingFields . length > 0 ) {
60+ db . run ( `ALTER TABLE transcriptions ADD COLUMN ${ missingFields . join ( ", " ) } ` ) ;
61+ }
62+ } ) ;
63+ }
1864 } ) ;
19- db . run ( "CREATE TABLE IF NOT EXISTS transcriptions (id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT, path TEXT, size INTEGER, mimetype TEXT, duration INTEGER, status TEXT, user_id INTEGER, result TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)" ) ;
2065 } ) ;
21- } ) ;
66+ }
67+ createTables ( ) ;
2268
2369export async function createUser ( { username, password, email, role } ) {
2470 password = await bcrypt . hash ( password , saltRounds ) ;
@@ -118,9 +164,9 @@ export async function deleteUser(id) {
118164 } ) ;
119165}
120166
121- export async function createTranscription ( { filename, path, size, mimetype, duration, status, user_id, result } ) {
167+ export async function createTranscription ( { filename, path, size, mimetype, duration, language , status, user_id, result } ) {
122168 return new Promise ( ( resolve , reject ) => {
123- db . run ( "INSERT INTO transcriptions (filename, path, size, mimetype, duration, status, user_id, result) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" , [ filename , path , size , mimetype , duration , status , user_id , result ] , function ( err ) {
169+ db . run ( "INSERT INTO transcriptions (filename, path, size, mimetype, duration, language, status, user_id, result) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? )" , [ filename , path , size , mimetype , duration , language , status , user_id , result ] , function ( err ) {
124170 if ( err ) {
125171 reject ( err ) ;
126172 } else {
@@ -166,14 +212,15 @@ export async function getTranscriptions({ filters, filterParams, limit, offset,
166212 } ) ;
167213}
168214
169- export async function updateTranscription ( { id, filename, path, size, mimetype, duration, status, user_id, result } = { } ) {
215+ export async function updateTranscription ( { id, filename, path, size, mimetype, duration, language , status, user_id, result } = { } ) {
170216 return new Promise ( async ( resolve , reject ) => {
171217 const params = [ ] ;
172218 if ( filename ) params . push ( [ "filename" , filename ] ) ;
173219 if ( path ) params . push ( [ "path" , path ] ) ;
174220 if ( size ) params . push ( [ "size" , size ] ) ;
175221 if ( mimetype ) params . push ( [ "mimetype" , mimetype ] ) ;
176222 if ( duration ) params . push ( [ "duration" , duration ] ) ;
223+ if ( language ) params . push ( [ "language" , language ] ) ;
177224 if ( status ) params . push ( [ "status" , status ] ) ;
178225 if ( user_id ) params . push ( [ "user_id" , user_id ] ) ;
179226 if ( result ) params . push ( [ "result" , result ] ) ;
0 commit comments