electerm-data-tool 2.1.9 → 2.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electerm-data-tool",
3
- "version": "2.1.9",
3
+ "version": "2.1.10",
4
4
  "description": "electerm data export/migrate tool",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -44,9 +44,15 @@ Object.defineProperty(exports, 'appPath', {
44
44
  get: getElectermDataPath
45
45
  })
46
46
 
47
+ // Function to get current app path dynamically (for modules that need it as a function)
48
+ function getCurrentAppPath () {
49
+ return getElectermDataPath()
50
+ }
51
+
47
52
  exports.defaultUserName = 'default_user'
48
53
  exports.setAppType = setAppType
49
54
  exports.getAppType = getAppType
55
+ exports.getCurrentAppPath = getCurrentAppPath
50
56
  exports.packInfo = {
51
57
  version
52
58
  }
@@ -4,20 +4,22 @@
4
4
 
5
5
  const { resolve } = require('path')
6
6
  const { existsSync, renameSync } = require('fs')
7
- const { appPath, defaultUserName, getAppType } = require('../common/app-props')
7
+ const { defaultUserName, getAppType, getCurrentAppPath } = require('../common/app-props')
8
8
  const log = require('../common/log')
9
9
 
10
10
  const reso = (name) => {
11
11
  const appType = getAppType()
12
+ const currentAppPath = getCurrentAppPath()
12
13
  let basePath
13
14
 
14
15
  if (appType === 'web') {
15
- basePath = resolve(appPath, 'nedb-database', 'users', defaultUserName)
16
+ basePath = resolve(currentAppPath, 'nedb-database', 'users', defaultUserName)
16
17
  } else {
17
- basePath = resolve(appPath, 'electerm', 'users', defaultUserName)
18
+ basePath = resolve(currentAppPath, 'electerm', 'users', defaultUserName)
18
19
  }
19
20
 
20
- return resolve(basePath, `electerm.${name}.nedb`)
21
+ const fullPath = resolve(basePath, `electerm.${name}.nedb`)
22
+ return fullPath
21
23
  }
22
24
 
23
25
  const tables = [
package/src/nedb.js CHANGED
@@ -2,19 +2,20 @@
2
2
  * nedb api wrapper
3
3
  */
4
4
 
5
- const { appPath, defaultUserName, getAppType } = require('./common/app-props')
5
+ const { defaultUserName, getAppType, getCurrentAppPath } = require('./common/app-props')
6
6
  const { resolve } = require('path')
7
7
  const Datastore = require('@yetzt/nedb')
8
8
  const db = {}
9
9
 
10
10
  const reso = (name) => {
11
11
  const appType = getAppType()
12
+ const currentAppPath = getCurrentAppPath()
12
13
  let basePath
13
14
 
14
15
  if (appType === 'web') {
15
- basePath = resolve(appPath, 'nedb-database', 'users', defaultUserName)
16
+ basePath = resolve(currentAppPath, 'nedb-database', 'users', defaultUserName)
16
17
  } else {
17
- basePath = resolve(appPath, 'electerm', 'users', defaultUserName)
18
+ basePath = resolve(currentAppPath, 'electerm', 'users', defaultUserName)
18
19
  }
19
20
 
20
21
  return resolve(basePath, `electerm.${name}.nedb`)
package/src/sqlite.js CHANGED
@@ -3,21 +3,28 @@
3
3
  * Updated to use two database files: one for 'data' table, one for others
4
4
  */
5
5
 
6
- const { appPath, defaultUserName, getAppType } = require('./common/app-props')
6
+ const { defaultUserName, getAppType, getCurrentAppPath } = require('./common/app-props')
7
7
  const { resolve } = require('path')
8
+ const { mkdirSync, existsSync } = require('fs')
8
9
  const uid = require('./common/uid')
9
10
  const { DatabaseSync } = require('node:sqlite')
10
11
 
11
12
  // Define paths for two database files based on appType
12
13
  function getDbPaths () {
13
14
  const appType = getAppType()
15
+ const currentAppPath = getCurrentAppPath()
14
16
  let basePath
15
17
 
16
18
  if (appType === 'web') {
17
19
  // For web type, SQLite files go directly in sqlite folder
18
- basePath = resolve(appPath, 'sqlite')
20
+ basePath = resolve(currentAppPath, 'sqlite')
19
21
  } else {
20
- basePath = resolve(appPath, 'electerm', 'users', defaultUserName)
22
+ basePath = resolve(currentAppPath, 'electerm', 'users', defaultUserName)
23
+ }
24
+
25
+ // Ensure the directory exists - check first, then create if needed
26
+ if (!existsSync(basePath)) {
27
+ mkdirSync(basePath, { recursive: true })
21
28
  }
22
29
 
23
30
  return {
@@ -26,11 +33,26 @@ function getDbPaths () {
26
33
  }
27
34
  }
28
35
 
29
- const { mainDbPath, dataDbPath } = getDbPaths()
36
+ // Lazy initialization of database instances
37
+ let mainDb = null
38
+ let dataDb = null
39
+
40
+ function initializeDatabases () {
41
+ if (mainDb === null || dataDb === null) {
42
+ const { mainDbPath, dataDbPath } = getDbPaths()
43
+ mainDb = new DatabaseSync(mainDbPath)
44
+ dataDb = new DatabaseSync(dataDbPath)
30
45
 
31
- // Create two database instances
32
- const mainDb = new DatabaseSync(mainDbPath)
33
- const dataDb = new DatabaseSync(dataDbPath)
46
+ // Create tables in appropriate databases
47
+ for (const table of tables) {
48
+ if (table === 'data') {
49
+ dataDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
50
+ } else {
51
+ mainDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
52
+ }
53
+ }
54
+ }
55
+ }
34
56
 
35
57
  const tables = [
36
58
  'bookmarks',
@@ -45,17 +67,9 @@ const tables = [
45
67
  'profiles'
46
68
  ]
47
69
 
48
- // Create tables in appropriate databases
49
- for (const table of tables) {
50
- if (table === 'data') {
51
- dataDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
52
- } else {
53
- mainDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
54
- }
55
- }
56
-
57
70
  // Helper function to get the appropriate database for a table
58
71
  function getDatabase (dbName) {
72
+ initializeDatabases()
59
73
  return dbName === 'data' ? dataDb : mainDb
60
74
  }
61
75