11import fs from 'node:fs' ;
22import type { DBService } from '@/lib/database' ;
3- import { getDemoSqlitePath } from './paths' ;
3+ import { DEMO_SQLITE_CONNECTION_PATH , getDemoSqlitePath } from './paths' ;
44
55const DEMO_CONNECTION_NAME = 'Demo Database' ;
6+ type DemoConnectionService = Pick < DBService , 'connections' > ;
7+ export type EnsureDemoConnectionResult = 'created' | 'updated' | 'exists' | 'skipped' ;
68
79/**
810 * Ensure a "Demo Database" SQLite connection exists for the given organization.
911 * Idempotent: skips if the connection already exists or if the demo.sqlite file is not available.
1012 */
1113export async function ensureDemoConnection (
12- db : DBService ,
14+ db : DemoConnectionService ,
1315 userId : string ,
1416 organizationId : string ,
15- ) : Promise < void > {
17+ ) : Promise < EnsureDemoConnectionResult > {
1618 const demoPath = getDemoSqlitePath ( ) ;
1719 if ( ! demoPath || ! fs . existsSync ( demoPath ) ) {
1820 console . log ( '[demo] demo.sqlite not found, skipping demo connection creation' ) ;
19- return ;
21+ return 'skipped' ;
2022 }
2123
2224 const existing = await db . connections . list ( organizationId ) ;
23- const hasDemoConnection = existing . some ( ( c ) => c . name === DEMO_CONNECTION_NAME ) ;
24- if ( hasDemoConnection ) {
25- return ;
25+ const existingDemoConnection = existing . find ( item => item . connection . name === DEMO_CONNECTION_NAME ) ;
26+ if ( existingDemoConnection ) {
27+ if ( existingDemoConnection . connection . path !== DEMO_SQLITE_CONNECTION_PATH ) {
28+ await db . connections . update ( organizationId , existingDemoConnection . connection . id , {
29+ connection : {
30+ organizationId,
31+ type : 'sqlite' ,
32+ engine : 'sqlite' ,
33+ name : existingDemoConnection . connection . name ,
34+ description : existingDemoConnection . connection . description ?? undefined ,
35+ host : existingDemoConnection . connection . host ,
36+ port : existingDemoConnection . connection . port ,
37+ httpPort : existingDemoConnection . connection . httpPort ?? undefined ,
38+ database : existingDemoConnection . connection . database ?? undefined ,
39+ options : existingDemoConnection . connection . options ,
40+ status : existingDemoConnection . connection . status ,
41+ environment : existingDemoConnection . connection . environment ,
42+ tags : existingDemoConnection . connection . tags ,
43+ path : DEMO_SQLITE_CONNECTION_PATH ,
44+ } ,
45+ identities : [ ] ,
46+ } ) ;
47+ return 'updated' ;
48+ }
49+ return 'exists' ;
2650 }
2751
2852 console . log ( `[demo] creating "${ DEMO_CONNECTION_NAME } " connection for org ${ organizationId } ` ) ;
@@ -37,21 +61,29 @@ export async function ensureDemoConnection(
3761 host : null ,
3862 port : null ,
3963 database : 'main' ,
40- path : demoPath ,
41- status : 'ready' ,
64+ path : DEMO_SQLITE_CONNECTION_PATH ,
4265 } ,
4366 identities : [
4467 {
68+ id : '' ,
69+ connectionId : '' ,
70+ organizationId,
4571 name : 'Default' ,
4672 username : 'sqlite' ,
73+ role : undefined ,
74+ options : '{}' ,
4775 isDefault : true ,
4876 database : 'main' ,
4977 enabled : true ,
50- role : null ,
51- options : '{}' ,
52- } as any ,
78+ status : 'active' ,
79+ createdAt : new Date ( ) ,
80+ updatedAt : new Date ( ) ,
81+ deletedAt : null ,
82+ password : undefined ,
83+ } ,
5384 ] ,
5485 } ) ;
5586
5687 console . log ( `[demo] "${ DEMO_CONNECTION_NAME } " connection created` ) ;
88+ return 'created' ;
5789}
0 commit comments