11import type { PreferencesStore } from '../types'
22import { homedir , platform } from 'node:os'
3+ import path from 'node:path'
4+ import { app } from 'electron'
35import Store from 'electron-store'
6+ import fs from 'fs-extra'
47import { EDITOR_DEFAULTS } from '../constants'
58
69const isWin = platform ( ) === 'win32'
710
811const storagePath = isWin ? `${ homedir ( ) } \\massCode` : `${ homedir ( ) } /massCode`
912const backupPath = isWin ? `${ storagePath } \\backups` : `${ storagePath } /backups`
1013
14+ // Detect the correct default engine BEFORE the store constructor merges
15+ // defaults into the preferences file. Without this, existing SQLite users
16+ // who never had a `storage.engine` key would get 'markdown' as default,
17+ // making all their snippets invisible.
18+ function detectDefaultEngine ( ) : 'sqlite' | 'markdown' {
19+ try {
20+ const prefsPath = path . join (
21+ app . getPath ( 'userData' ) ,
22+ 'v2' ,
23+ 'preferences.json' ,
24+ )
25+
26+ if ( fs . existsSync ( prefsPath ) ) {
27+ const raw = JSON . parse ( fs . readFileSync ( prefsPath , 'utf8' ) )
28+
29+ // User already has an explicit engine setting — respect it
30+ if ( raw . storage ?. engine ) {
31+ return raw . storage . engine
32+ }
33+
34+ // No engine setting — check if SQLite DB exists (existing user)
35+ const userStoragePath = raw . storagePath || storagePath
36+ const dbPath = path . join ( userStoragePath , 'massCode.db' )
37+
38+ if ( fs . existsSync ( dbPath ) ) {
39+ return 'sqlite'
40+ }
41+ }
42+ }
43+ catch {
44+ // If anything goes wrong reading the file, fall through to default
45+ }
46+
47+ return 'markdown'
48+ }
49+
1150const preferencesStore = new Store < PreferencesStore > ( {
1251 name : 'preferences' ,
1352 cwd : 'v2' ,
@@ -19,7 +58,7 @@ const preferencesStore = new Store<PreferencesStore>({
1958 theme : 'auto' ,
2059 editor : EDITOR_DEFAULTS ,
2160 storage : {
22- engine : 'markdown' ,
61+ engine : detectDefaultEngine ( ) ,
2362 vaultPath : null ,
2463 } ,
2564 markdown : {
0 commit comments