electerm-data-tool 1.0.0
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/LICENSE +21 -0
- package/README.md +264 -0
- package/package.json +63 -0
- package/src/common/app-props.js +24 -0
- package/src/common/log.js +1 -0
- package/src/common/pass-enc.js +53 -0
- package/src/common/uid.js +5 -0
- package/src/common/version-compare.js +25 -0
- package/src/index.js +234 -0
- package/src/migrate/index.js +85 -0
- package/src/migrate/migrate-1-to-2.js +117 -0
- package/src/migrate/v1.25.0.js +48 -0
- package/src/migrate/v1.27.17.js +66 -0
- package/src/migrate/v1.3.9.js +44 -0
- package/src/migrate/v1.32.36.js +44 -0
- package/src/migrate/v1.34.20.js +43 -0
- package/src/migrate/v1.34.59.js +32 -0
- package/src/migrate/v1.5.13.js +57 -0
- package/src/migrate/v1.7.0.js +36 -0
- package/src/migrate/version-upgrade.js +39 -0
- package/src/nedb.js +52 -0
- package/src/sqlite.js +132 -0
package/src/sqlite.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sqlite api wrapper
|
|
3
|
+
* Updated to use two database files: one for 'data' table, one for others
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { appPath, defaultUserName } = require('./common/app-props')
|
|
7
|
+
const { resolve } = require('path')
|
|
8
|
+
const uid = require('./common/uid')
|
|
9
|
+
const { DatabaseSync } = require('node:sqlite')
|
|
10
|
+
|
|
11
|
+
// Define paths for two database files
|
|
12
|
+
const mainDbPath = resolve(appPath, 'electerm', 'users', defaultUserName, 'electerm.db')
|
|
13
|
+
const dataDbPath = resolve(appPath, 'electerm', 'users', defaultUserName, 'electerm_data.db')
|
|
14
|
+
|
|
15
|
+
// Create two database instances
|
|
16
|
+
const mainDb = new DatabaseSync(mainDbPath)
|
|
17
|
+
const dataDb = new DatabaseSync(dataDbPath)
|
|
18
|
+
|
|
19
|
+
const tables = [
|
|
20
|
+
'bookmarks',
|
|
21
|
+
'bookmarkGroups',
|
|
22
|
+
'addressBookmarks',
|
|
23
|
+
'terminalThemes',
|
|
24
|
+
'lastStates',
|
|
25
|
+
'data',
|
|
26
|
+
'quickCommands',
|
|
27
|
+
'log',
|
|
28
|
+
'dbUpgradeLog',
|
|
29
|
+
'profiles'
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
// Create tables in appropriate databases
|
|
33
|
+
for (const table of tables) {
|
|
34
|
+
if (table === 'data') {
|
|
35
|
+
dataDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
|
|
36
|
+
} else {
|
|
37
|
+
mainDb.exec(`CREATE TABLE IF NOT EXISTS \`${table}\` (_id TEXT PRIMARY KEY, data TEXT)`)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Helper function to get the appropriate database for a table
|
|
42
|
+
function getDatabase (dbName) {
|
|
43
|
+
return dbName === 'data' ? dataDb : mainDb
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function toDoc (row) {
|
|
47
|
+
if (!row) return null
|
|
48
|
+
return {
|
|
49
|
+
...JSON.parse(row.data || '{}'),
|
|
50
|
+
_id: row._id
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function toRow (doc) {
|
|
55
|
+
const _id = doc._id || doc.id || uid()
|
|
56
|
+
const copy = { ...doc }
|
|
57
|
+
delete copy._id
|
|
58
|
+
delete copy.id
|
|
59
|
+
return {
|
|
60
|
+
_id,
|
|
61
|
+
data: JSON.stringify(copy)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function dbAction (dbName, op, ...args) {
|
|
66
|
+
if (op === 'compactDatafile') {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
if (!tables.includes(dbName)) {
|
|
70
|
+
throw new Error(`Table ${dbName} does not exist`)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Get the appropriate database for this table
|
|
74
|
+
const db = getDatabase(dbName)
|
|
75
|
+
|
|
76
|
+
if (op === 'find') {
|
|
77
|
+
const sql = `SELECT * FROM \`${dbName}\``
|
|
78
|
+
const stmt = db.prepare(sql)
|
|
79
|
+
const rows = stmt.all()
|
|
80
|
+
return (rows || []).map(toDoc).filter(Boolean)
|
|
81
|
+
} else if (op === 'findOne') {
|
|
82
|
+
const query = args[0] || {}
|
|
83
|
+
const sql = `SELECT * FROM \`${dbName}\` WHERE _id = ? LIMIT 1`
|
|
84
|
+
const params = [query._id]
|
|
85
|
+
const stmt = db.prepare(sql)
|
|
86
|
+
const row = stmt.get(...params)
|
|
87
|
+
return toDoc(row)
|
|
88
|
+
} else if (op === 'insert') {
|
|
89
|
+
const inserts = Array.isArray(args[0]) ? args[0] : [args[0]]
|
|
90
|
+
const inserted = []
|
|
91
|
+
for (const doc of inserts) {
|
|
92
|
+
const { _id, data } = toRow(doc)
|
|
93
|
+
const stmt = db.prepare(`INSERT INTO \`${dbName}\` (_id, data) VALUES (?, ?)`)
|
|
94
|
+
stmt.run(_id, data)
|
|
95
|
+
inserted.push({ ...doc, _id })
|
|
96
|
+
}
|
|
97
|
+
return Array.isArray(args[0]) ? inserted : inserted[0]
|
|
98
|
+
} else if (op === 'remove') {
|
|
99
|
+
const query = args[0] || {}
|
|
100
|
+
const sql = `DELETE FROM \`${dbName}\` WHERE _id = ?`
|
|
101
|
+
const params = [query._id]
|
|
102
|
+
const stmt = db.prepare(sql)
|
|
103
|
+
const res = stmt.run(...params)
|
|
104
|
+
return res.changes
|
|
105
|
+
} else if (op === 'update') {
|
|
106
|
+
const query = args[0]
|
|
107
|
+
const updateObj = args[1]
|
|
108
|
+
const options = args[2] || {}
|
|
109
|
+
const { upsert = false } = options
|
|
110
|
+
const qid = query._id || query.id
|
|
111
|
+
const newData = updateObj.$set || updateObj
|
|
112
|
+
const { _id, data } = toRow({
|
|
113
|
+
_id: qid,
|
|
114
|
+
...newData
|
|
115
|
+
})
|
|
116
|
+
let stmt
|
|
117
|
+
let res
|
|
118
|
+
if (upsert) {
|
|
119
|
+
stmt = db.prepare(`REPLACE INTO \`${dbName}\` (_id, data) VALUES (?, ?)`)
|
|
120
|
+
res = stmt.run(_id, data)
|
|
121
|
+
} else {
|
|
122
|
+
stmt = db.prepare(`UPDATE \`${dbName}\` SET data = ? WHERE _id = ?`)
|
|
123
|
+
res = stmt.run(data, qid)
|
|
124
|
+
}
|
|
125
|
+
return res.changes
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = {
|
|
130
|
+
dbAction,
|
|
131
|
+
tables
|
|
132
|
+
}
|