@dbcube/core 5.2.3 → 5.2.5
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/dist/bin.cjs +2 -3
- package/dist/bin.cjs.map +1 -1
- package/dist/bin.js +2 -3
- package/dist/bin.js.map +1 -1
- package/dist/index.cjs +150 -357
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +36 -33
- package/dist/index.d.ts +36 -33
- package/dist/index.js +166 -373
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -37,6 +37,7 @@ __export(index_exports, {
|
|
|
37
37
|
DaemonClient: () => DaemonClient,
|
|
38
38
|
DbConfig: () => DbConfig,
|
|
39
39
|
Engine: () => Engine,
|
|
40
|
+
EnvLoader: () => EnvLoader,
|
|
40
41
|
FileLogger: () => FileLogger,
|
|
41
42
|
QueryEngine: () => QueryEngine,
|
|
42
43
|
TableProcessor: () => TableProcessor,
|
|
@@ -46,6 +47,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
46
47
|
|
|
47
48
|
// src/lib/Engine.ts
|
|
48
49
|
var import_path4 = __toESM(require("path"));
|
|
50
|
+
var fs5 = __toESM(require("fs"));
|
|
49
51
|
|
|
50
52
|
// src/lib/Arquitecture.ts
|
|
51
53
|
var os = __toESM(require("os"));
|
|
@@ -186,8 +188,7 @@ var Downloader = class {
|
|
|
186
188
|
static currentSpinner = null;
|
|
187
189
|
static VERSION_URLS = {
|
|
188
190
|
query: "https://raw.githubusercontent.com/Dbcube/binaries/main/query-engines.json",
|
|
189
|
-
schema: "https://raw.githubusercontent.com/Dbcube/binaries/main/schema-engines.json"
|
|
190
|
-
sqlite: "https://raw.githubusercontent.com/Dbcube/binaries/main/sqlite-engines.json"
|
|
191
|
+
schema: "https://raw.githubusercontent.com/Dbcube/binaries/main/schema-engines.json"
|
|
191
192
|
};
|
|
192
193
|
/**
|
|
193
194
|
* Fetch latest version from GitHub
|
|
@@ -357,7 +358,7 @@ var Downloader = class {
|
|
|
357
358
|
spinner: "dots12"
|
|
358
359
|
}).start();
|
|
359
360
|
const binariesToProcess = [];
|
|
360
|
-
for (const prefix of ["query", "schema"
|
|
361
|
+
for (const prefix of ["query", "schema"]) {
|
|
361
362
|
try {
|
|
362
363
|
const localVersion = this.getLocalVersion(binDir, prefix);
|
|
363
364
|
const remoteVersion = await this.fetchLatestVersion(prefix);
|
|
@@ -747,6 +748,67 @@ var Config = class {
|
|
|
747
748
|
}
|
|
748
749
|
};
|
|
749
750
|
|
|
751
|
+
// src/lib/EnvLoader.ts
|
|
752
|
+
var fs3 = __toESM(require("fs"));
|
|
753
|
+
var path3 = __toESM(require("path"));
|
|
754
|
+
var EnvLoader = class _EnvLoader {
|
|
755
|
+
/**
|
|
756
|
+
* Carga el archivo .env indicado (o ./.env del cwd) en process.env.
|
|
757
|
+
* Silencioso si el archivo no existe. Devuelve true si cargó algo.
|
|
758
|
+
*/
|
|
759
|
+
static load(envPath) {
|
|
760
|
+
const file = envPath || path3.resolve(process.cwd(), ".env");
|
|
761
|
+
if (!fs3.existsSync(file)) return false;
|
|
762
|
+
const proc = process;
|
|
763
|
+
if (typeof proc.loadEnvFile === "function") {
|
|
764
|
+
try {
|
|
765
|
+
proc.loadEnvFile(file);
|
|
766
|
+
return true;
|
|
767
|
+
} catch {
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
try {
|
|
771
|
+
const parsed = _EnvLoader.parse(fs3.readFileSync(file, "utf8"));
|
|
772
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
773
|
+
if (process.env[key] === void 0) {
|
|
774
|
+
process.env[key] = value;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return true;
|
|
778
|
+
} catch {
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* Parser mínimo de formato .env. Soporta:
|
|
784
|
+
* KEY=value · KEY="value con espacios" · KEY='literal' · export KEY=value
|
|
785
|
+
* comentarios con # · líneas en blanco · \n \t en comillas dobles.
|
|
786
|
+
*/
|
|
787
|
+
static parse(content) {
|
|
788
|
+
const out = {};
|
|
789
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
790
|
+
const line = rawLine.trim();
|
|
791
|
+
if (!line || line.startsWith("#")) continue;
|
|
792
|
+
const withoutExport = line.startsWith("export ") ? line.slice(7).trim() : line;
|
|
793
|
+
const eq = withoutExport.indexOf("=");
|
|
794
|
+
if (eq === -1) continue;
|
|
795
|
+
const key = withoutExport.slice(0, eq).trim();
|
|
796
|
+
if (!key) continue;
|
|
797
|
+
let value = withoutExport.slice(eq + 1).trim();
|
|
798
|
+
if (value.length >= 2 && value[0] === '"' && value[value.length - 1] === '"') {
|
|
799
|
+
value = value.slice(1, -1).replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\"/g, '"');
|
|
800
|
+
} else if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
801
|
+
value = value.slice(1, -1);
|
|
802
|
+
} else {
|
|
803
|
+
const hash = value.indexOf(" #");
|
|
804
|
+
if (hash !== -1) value = value.slice(0, hash).trim();
|
|
805
|
+
}
|
|
806
|
+
out[key] = value;
|
|
807
|
+
}
|
|
808
|
+
return out;
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
|
|
750
812
|
// src/lib/Engine.ts
|
|
751
813
|
var import_child_process2 = require("child_process");
|
|
752
814
|
var import_module = require("module");
|
|
@@ -1084,6 +1146,7 @@ var Engine = class {
|
|
|
1084
1146
|
}
|
|
1085
1147
|
setArguments() {
|
|
1086
1148
|
let args = [];
|
|
1149
|
+
const motor = this.config.type === "postgres" ? "postgresql" : this.config.type;
|
|
1087
1150
|
if (this.config.type == "sqlite") {
|
|
1088
1151
|
args = [
|
|
1089
1152
|
"--id",
|
|
@@ -1093,7 +1156,7 @@ var Engine = class {
|
|
|
1093
1156
|
"--database",
|
|
1094
1157
|
this.config.config.DATABASE + ".db",
|
|
1095
1158
|
"--motor",
|
|
1096
|
-
|
|
1159
|
+
motor
|
|
1097
1160
|
];
|
|
1098
1161
|
} else {
|
|
1099
1162
|
args = [
|
|
@@ -1108,7 +1171,7 @@ var Engine = class {
|
|
|
1108
1171
|
"--port",
|
|
1109
1172
|
String(this.config.config.PORT),
|
|
1110
1173
|
"--motor",
|
|
1111
|
-
|
|
1174
|
+
motor
|
|
1112
1175
|
];
|
|
1113
1176
|
if (this.config.config.USER != null && this.config.config.USER !== "") {
|
|
1114
1177
|
args.push("--user", this.config.config.USER);
|
|
@@ -1121,8 +1184,10 @@ var Engine = class {
|
|
|
1121
1184
|
}
|
|
1122
1185
|
setConfig(name) {
|
|
1123
1186
|
const configInstance = new Config();
|
|
1187
|
+
EnvLoader.load();
|
|
1124
1188
|
try {
|
|
1125
|
-
const
|
|
1189
|
+
const cjsPath = import_path4.default.resolve(process.cwd(), "dbcube.config.cjs");
|
|
1190
|
+
const configFilePath = fs5.existsSync(cjsPath) ? cjsPath : import_path4.default.resolve(process.cwd(), "dbcube.config.js");
|
|
1126
1191
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
1127
1192
|
const require2 = (0, import_module.createRequire)(requireUrl);
|
|
1128
1193
|
if (require2.cache && require2.resolve) {
|
|
@@ -1236,10 +1301,11 @@ var Engine = class {
|
|
|
1236
1301
|
|
|
1237
1302
|
// src/lib/QueryEngine.ts
|
|
1238
1303
|
var import_path5 = __toESM(require("path"));
|
|
1304
|
+
var fs7 = __toESM(require("fs"));
|
|
1239
1305
|
|
|
1240
1306
|
// src/lib/EmbeddedEngine.ts
|
|
1241
|
-
var
|
|
1242
|
-
var
|
|
1307
|
+
var fs6 = __toESM(require("fs"));
|
|
1308
|
+
var path6 = __toESM(require("path"));
|
|
1243
1309
|
if (!process.env.UV_THREADPOOL_SIZE) {
|
|
1244
1310
|
process.env.UV_THREADPOOL_SIZE = "16";
|
|
1245
1311
|
}
|
|
@@ -1255,11 +1321,11 @@ function platTag() {
|
|
|
1255
1321
|
}
|
|
1256
1322
|
function findFile(name) {
|
|
1257
1323
|
const candidates = [
|
|
1258
|
-
|
|
1259
|
-
|
|
1324
|
+
path6.resolve(process.cwd(), ".dbcube", "bin", name),
|
|
1325
|
+
path6.resolve(process.cwd(), "node_modules", ".dbcube", "bin", name)
|
|
1260
1326
|
];
|
|
1261
1327
|
for (const c of candidates) {
|
|
1262
|
-
if (
|
|
1328
|
+
if (fs6.existsSync(c)) return c;
|
|
1263
1329
|
}
|
|
1264
1330
|
return null;
|
|
1265
1331
|
}
|
|
@@ -1504,9 +1570,9 @@ var QueryEngine = class {
|
|
|
1504
1570
|
timeout;
|
|
1505
1571
|
connectionId;
|
|
1506
1572
|
tcpPort;
|
|
1507
|
-
constructor(name, timeout = 3e4) {
|
|
1573
|
+
constructor(name, timeout = 3e4, explicitConfig) {
|
|
1508
1574
|
this.name = name;
|
|
1509
|
-
this.config = this.setConfig(name);
|
|
1575
|
+
this.config = explicitConfig ?? this.setConfig(name);
|
|
1510
1576
|
this.arguments = this.setArguments();
|
|
1511
1577
|
this.timeout = this.config?.daemon?.requestTimeoutMs ?? timeout;
|
|
1512
1578
|
this.connectionId = `${name}_query_engine_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
|
|
@@ -1541,6 +1607,7 @@ var QueryEngine = class {
|
|
|
1541
1607
|
}
|
|
1542
1608
|
setArguments() {
|
|
1543
1609
|
let args = [];
|
|
1610
|
+
const motor = this.config.type === "postgres" ? "postgresql" : this.config.type;
|
|
1544
1611
|
if (this.config.type == "sqlite") {
|
|
1545
1612
|
args = [
|
|
1546
1613
|
"--id",
|
|
@@ -1550,7 +1617,7 @@ var QueryEngine = class {
|
|
|
1550
1617
|
"--database",
|
|
1551
1618
|
this.config.config.DATABASE + ".db",
|
|
1552
1619
|
"--motor",
|
|
1553
|
-
|
|
1620
|
+
motor
|
|
1554
1621
|
];
|
|
1555
1622
|
} else {
|
|
1556
1623
|
args = [
|
|
@@ -1565,7 +1632,7 @@ var QueryEngine = class {
|
|
|
1565
1632
|
"--port",
|
|
1566
1633
|
String(this.config.config.PORT),
|
|
1567
1634
|
"--motor",
|
|
1568
|
-
|
|
1635
|
+
motor
|
|
1569
1636
|
];
|
|
1570
1637
|
if (this.config.config.USER != null && this.config.config.USER !== "") {
|
|
1571
1638
|
args.push("--user", this.config.config.USER);
|
|
@@ -1589,8 +1656,10 @@ var QueryEngine = class {
|
|
|
1589
1656
|
}
|
|
1590
1657
|
setConfig(name) {
|
|
1591
1658
|
const configInstance = new Config();
|
|
1659
|
+
EnvLoader.load();
|
|
1592
1660
|
try {
|
|
1593
|
-
const
|
|
1661
|
+
const cjsPath = import_path5.default.resolve(process.cwd(), "dbcube.config.cjs");
|
|
1662
|
+
const configFilePath = fs7.existsSync(cjsPath) ? cjsPath : import_path5.default.resolve(process.cwd(), "dbcube.config.js");
|
|
1594
1663
|
const requireUrl = typeof __filename !== "undefined" ? __filename : process.cwd();
|
|
1595
1664
|
const require2 = (0, import_module2.createRequire)(requireUrl);
|
|
1596
1665
|
if (require2.cache && require2.resolve) {
|
|
@@ -2053,347 +2122,70 @@ var QueryEngine = class {
|
|
|
2053
2122
|
}
|
|
2054
2123
|
};
|
|
2055
2124
|
|
|
2056
|
-
// src/lib/SqliteExecutor.ts
|
|
2057
|
-
var import_child_process4 = require("child_process");
|
|
2058
|
-
var path7 = __toESM(require("path"));
|
|
2059
|
-
var fs5 = __toESM(require("fs"));
|
|
2060
|
-
var import_util = require("util");
|
|
2061
|
-
var import_module3 = require("module");
|
|
2062
|
-
var import_url3 = require("url");
|
|
2063
|
-
var import_path6 = require("path");
|
|
2064
|
-
var import_meta3 = {};
|
|
2065
|
-
var execAsync = (0, import_util.promisify)(import_child_process4.exec);
|
|
2066
|
-
var SqliteExecutor = class {
|
|
2067
|
-
binaryPath;
|
|
2068
|
-
dbPath;
|
|
2069
|
-
constructor(dbPath) {
|
|
2070
|
-
this.dbPath = dbPath;
|
|
2071
|
-
this.binaryPath = this.getBinaryPath();
|
|
2072
|
-
}
|
|
2073
|
-
findVersionedBinary(binDir, platform2) {
|
|
2074
|
-
try {
|
|
2075
|
-
const files = fs5.readdirSync(binDir);
|
|
2076
|
-
const extension = platform2 === "win32" ? ".exe" : "";
|
|
2077
|
-
const platformName = platform2 === "win32" ? "windows" : platform2 === "darwin" ? "macos" : "linux";
|
|
2078
|
-
const pattern = new RegExp(`^sqlite-engine-v\\d+\\.\\d+\\.\\d+-${platformName}-x64${extension.replace(".", "\\.")}$`);
|
|
2079
|
-
const matchingFile = files.find((f) => pattern.test(f));
|
|
2080
|
-
if (matchingFile) {
|
|
2081
|
-
return path7.join(binDir, matchingFile);
|
|
2082
|
-
}
|
|
2083
|
-
} catch (error) {
|
|
2084
|
-
}
|
|
2085
|
-
return null;
|
|
2086
|
-
}
|
|
2087
|
-
getBinaryPath() {
|
|
2088
|
-
const __filename2 = typeof import_meta3 !== "undefined" && import_meta3.url ? (0, import_url3.fileURLToPath)(import_meta3.url) : "";
|
|
2089
|
-
const __dirname = __filename2 ? (0, import_path6.dirname)(__filename2) : process.cwd();
|
|
2090
|
-
const possibleDirs = [
|
|
2091
|
-
path7.resolve(process.cwd(), ".dbcube", "bin"),
|
|
2092
|
-
path7.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
|
|
2093
|
-
path7.resolve(__dirname, "..", "bin")
|
|
2094
|
-
];
|
|
2095
|
-
const platform2 = process.platform;
|
|
2096
|
-
const extension = platform2 === "win32" ? ".exe" : "";
|
|
2097
|
-
const platformName = platform2 === "win32" ? "windows" : platform2 === "darwin" ? "macos" : "linux";
|
|
2098
|
-
for (const dir of possibleDirs) {
|
|
2099
|
-
const versionedPath = this.findVersionedBinary(dir, platform2);
|
|
2100
|
-
if (versionedPath && fs5.existsSync(versionedPath)) {
|
|
2101
|
-
return versionedPath;
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
const binaryName = `sqlite-engine-${platformName}-x64${extension}`;
|
|
2105
|
-
for (const dir of possibleDirs) {
|
|
2106
|
-
const fullPath = path7.join(dir, binaryName);
|
|
2107
|
-
if (fs5.existsSync(fullPath)) {
|
|
2108
|
-
return fullPath;
|
|
2109
|
-
}
|
|
2110
|
-
}
|
|
2111
|
-
const fallbackName = `sqlite-engine${extension}`;
|
|
2112
|
-
for (const dir of possibleDirs) {
|
|
2113
|
-
const fullPath = path7.join(dir, fallbackName);
|
|
2114
|
-
if (fs5.existsSync(fullPath)) {
|
|
2115
|
-
return fullPath;
|
|
2116
|
-
}
|
|
2117
|
-
}
|
|
2118
|
-
return path7.join(possibleDirs[0], binaryName);
|
|
2119
|
-
}
|
|
2120
|
-
async executeBinary(args) {
|
|
2121
|
-
const escapedArgs = args.map((arg) => {
|
|
2122
|
-
if (arg.includes(" ") || arg.includes('"') || arg.includes("(") || arg.includes(")")) {
|
|
2123
|
-
return `"${arg.replace(/"/g, '\\"')}"`;
|
|
2124
|
-
}
|
|
2125
|
-
return arg;
|
|
2126
|
-
});
|
|
2127
|
-
const command = `"${this.binaryPath}" ${escapedArgs.join(" ")}`;
|
|
2128
|
-
try {
|
|
2129
|
-
const { stdout, stderr } = await execAsync(command, {
|
|
2130
|
-
maxBuffer: 10 * 1024 * 1024,
|
|
2131
|
-
// 10MB buffer
|
|
2132
|
-
timeout: 3e4
|
|
2133
|
-
// 30s timeout
|
|
2134
|
-
});
|
|
2135
|
-
if (stderr && stderr.trim()) {
|
|
2136
|
-
console.warn("SQLite Engine Warning:", stderr);
|
|
2137
|
-
}
|
|
2138
|
-
const result = JSON.parse(stdout.trim());
|
|
2139
|
-
return result;
|
|
2140
|
-
} catch (error) {
|
|
2141
|
-
if (error.stdout) {
|
|
2142
|
-
try {
|
|
2143
|
-
const result = JSON.parse(error.stdout.trim());
|
|
2144
|
-
return result;
|
|
2145
|
-
} catch (parseError) {
|
|
2146
|
-
}
|
|
2147
|
-
}
|
|
2148
|
-
throw new Error(`SQLite execution failed: ${error.message}`);
|
|
2149
|
-
}
|
|
2150
|
-
}
|
|
2151
|
-
async connect() {
|
|
2152
|
-
try {
|
|
2153
|
-
const result = await this.executeBinary([
|
|
2154
|
-
"--action",
|
|
2155
|
-
"connect",
|
|
2156
|
-
"--database",
|
|
2157
|
-
this.dbPath
|
|
2158
|
-
]);
|
|
2159
|
-
return result.status === "success";
|
|
2160
|
-
} catch (error) {
|
|
2161
|
-
return false;
|
|
2162
|
-
}
|
|
2163
|
-
}
|
|
2164
|
-
async exists() {
|
|
2165
|
-
try {
|
|
2166
|
-
const result = await this.executeBinary([
|
|
2167
|
-
"--action",
|
|
2168
|
-
"exists",
|
|
2169
|
-
"--database",
|
|
2170
|
-
this.dbPath
|
|
2171
|
-
]);
|
|
2172
|
-
return result.status === "success" && result.data === true;
|
|
2173
|
-
} catch (error) {
|
|
2174
|
-
return false;
|
|
2175
|
-
}
|
|
2176
|
-
}
|
|
2177
|
-
async query(sql, params) {
|
|
2178
|
-
const args = [
|
|
2179
|
-
"--action",
|
|
2180
|
-
"query",
|
|
2181
|
-
"--database",
|
|
2182
|
-
this.dbPath,
|
|
2183
|
-
"--query",
|
|
2184
|
-
sql
|
|
2185
|
-
];
|
|
2186
|
-
if (params && params.length > 0) {
|
|
2187
|
-
args.push("--params", JSON.stringify(params));
|
|
2188
|
-
}
|
|
2189
|
-
return this.executeBinary(args);
|
|
2190
|
-
}
|
|
2191
|
-
// Método para múltiples queries (como lo hace better-sqlite3)
|
|
2192
|
-
async queryMultiple(sql) {
|
|
2193
|
-
return this.query(sql);
|
|
2194
|
-
}
|
|
2195
|
-
// Método prepare que simula prepared statements
|
|
2196
|
-
prepare(sql) {
|
|
2197
|
-
return {
|
|
2198
|
-
all: async (...params) => {
|
|
2199
|
-
const result = await this.query(sql, params);
|
|
2200
|
-
if (result.status === "error") {
|
|
2201
|
-
throw new Error(result.message);
|
|
2202
|
-
}
|
|
2203
|
-
return Array.isArray(result.data) ? result.data : [];
|
|
2204
|
-
},
|
|
2205
|
-
run: async (...params) => {
|
|
2206
|
-
const result = await this.query(sql, params);
|
|
2207
|
-
if (result.status === "error") {
|
|
2208
|
-
throw new Error(result.message);
|
|
2209
|
-
}
|
|
2210
|
-
if (typeof result.data === "object" && result.data.hasOwnProperty("changes") && result.data.hasOwnProperty("lastID")) {
|
|
2211
|
-
return result.data;
|
|
2212
|
-
}
|
|
2213
|
-
return { changes: 0, lastID: 0 };
|
|
2214
|
-
}
|
|
2215
|
-
};
|
|
2216
|
-
}
|
|
2217
|
-
// Para compatibilidad con better-sqlite3 API sincrona usando deasync si es necesario
|
|
2218
|
-
prepareSync(sql) {
|
|
2219
|
-
const __filename2 = typeof import_meta3 !== "undefined" && import_meta3.url ? (0, import_url3.fileURLToPath)(import_meta3.url) : "";
|
|
2220
|
-
const requireUrl = __filename2 || import_meta3.url;
|
|
2221
|
-
const require2 = (0, import_module3.createRequire)(requireUrl);
|
|
2222
|
-
const deasync = require2("deasync");
|
|
2223
|
-
return {
|
|
2224
|
-
all: (...params) => {
|
|
2225
|
-
let result;
|
|
2226
|
-
let done = false;
|
|
2227
|
-
let error;
|
|
2228
|
-
this.query(sql, params).then((res) => {
|
|
2229
|
-
if (res.status === "error") {
|
|
2230
|
-
error = new Error(res.message);
|
|
2231
|
-
} else {
|
|
2232
|
-
result = Array.isArray(res.data) ? res.data : [];
|
|
2233
|
-
}
|
|
2234
|
-
done = true;
|
|
2235
|
-
}).catch((err) => {
|
|
2236
|
-
error = err;
|
|
2237
|
-
done = true;
|
|
2238
|
-
});
|
|
2239
|
-
deasync.loopWhile(() => !done);
|
|
2240
|
-
if (error) throw error;
|
|
2241
|
-
return result;
|
|
2242
|
-
},
|
|
2243
|
-
run: (...params) => {
|
|
2244
|
-
let result;
|
|
2245
|
-
let done = false;
|
|
2246
|
-
let error;
|
|
2247
|
-
this.query(sql, params).then((res) => {
|
|
2248
|
-
if (res.status === "error") {
|
|
2249
|
-
error = new Error(res.message);
|
|
2250
|
-
} else if (typeof res.data === "object" && res.data.hasOwnProperty("changes") && res.data.hasOwnProperty("lastID")) {
|
|
2251
|
-
result = res.data;
|
|
2252
|
-
} else {
|
|
2253
|
-
result = { changes: 0, lastID: 0 };
|
|
2254
|
-
}
|
|
2255
|
-
done = true;
|
|
2256
|
-
}).catch((err) => {
|
|
2257
|
-
error = err;
|
|
2258
|
-
done = true;
|
|
2259
|
-
});
|
|
2260
|
-
deasync.loopWhile(() => !done);
|
|
2261
|
-
if (error) throw error;
|
|
2262
|
-
return result;
|
|
2263
|
-
}
|
|
2264
|
-
};
|
|
2265
|
-
}
|
|
2266
|
-
};
|
|
2267
|
-
|
|
2268
2125
|
// src/lib/DbConfig.ts
|
|
2269
2126
|
var path8 = __toESM(require("path"));
|
|
2270
2127
|
var import_fs2 = __toESM(require("fs"));
|
|
2271
2128
|
var rootPath = path8.resolve(process.cwd(), ".dbcube");
|
|
2272
2129
|
var SQLite = class {
|
|
2273
|
-
executor = null;
|
|
2274
2130
|
database;
|
|
2131
|
+
engine = null;
|
|
2275
2132
|
constructor(config) {
|
|
2276
|
-
this.database = config.DATABASE;
|
|
2133
|
+
this.database = config.DATABASE || "config";
|
|
2134
|
+
}
|
|
2135
|
+
/** Ruta del archivo SQLite interno (.dbcube/<database>.db). */
|
|
2136
|
+
dbFilePath() {
|
|
2137
|
+
return path8.join(rootPath, this.database + ".db");
|
|
2138
|
+
}
|
|
2139
|
+
/** QueryEngine sqlite apuntando al archivo interno, con config explícito
|
|
2140
|
+
* (no vive en dbcube.config.js). La ruta relativa `.dbcube/<db>` deja que
|
|
2141
|
+
* QueryEngine le añada `.db` → `.dbcube/<db>.db`, relativo al CWD. */
|
|
2142
|
+
getEngine() {
|
|
2143
|
+
if (!this.engine) {
|
|
2144
|
+
const relativeDb = path8.join(".dbcube", this.database).replace(/\\/g, "/");
|
|
2145
|
+
this.engine = new QueryEngine(`dbcube-internal-${this.database}`, 3e4, {
|
|
2146
|
+
type: "sqlite",
|
|
2147
|
+
config: { DATABASE: relativeDb }
|
|
2148
|
+
});
|
|
2149
|
+
}
|
|
2150
|
+
return this.engine;
|
|
2277
2151
|
}
|
|
2278
2152
|
async ifExist() {
|
|
2279
|
-
if (
|
|
2280
|
-
|
|
2281
|
-
const configPath = path8.join(rootPath, dbPath + ".db");
|
|
2282
|
-
if (!import_fs2.default.existsSync(rootPath)) {
|
|
2283
|
-
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
2284
|
-
}
|
|
2285
|
-
if (import_fs2.default.existsSync(configPath)) {
|
|
2286
|
-
return true;
|
|
2287
|
-
}
|
|
2288
|
-
if (!this.executor) {
|
|
2289
|
-
this.executor = new SqliteExecutor(configPath);
|
|
2290
|
-
}
|
|
2291
|
-
return await this.executor.exists();
|
|
2153
|
+
if (!import_fs2.default.existsSync(rootPath)) {
|
|
2154
|
+
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
2292
2155
|
}
|
|
2293
|
-
return
|
|
2156
|
+
return import_fs2.default.existsSync(this.dbFilePath());
|
|
2294
2157
|
}
|
|
2295
2158
|
async connect() {
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
const configPath = path8.join(rootPath, dbPath + ".db");
|
|
2301
|
-
if (!import_fs2.default.existsSync(rootPath)) {
|
|
2302
|
-
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
2303
|
-
}
|
|
2304
|
-
this.executor = new SqliteExecutor(configPath);
|
|
2305
|
-
const connected = await this.executor.connect();
|
|
2306
|
-
if (!connected) {
|
|
2307
|
-
throw new Error("Failed to connect to SQLite database");
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
resolve6(this.executor);
|
|
2311
|
-
} catch (error) {
|
|
2312
|
-
reject(error);
|
|
2313
|
-
}
|
|
2314
|
-
});
|
|
2159
|
+
if (!import_fs2.default.existsSync(rootPath)) {
|
|
2160
|
+
import_fs2.default.mkdirSync(rootPath, { recursive: true });
|
|
2161
|
+
}
|
|
2162
|
+
return this.getEngine();
|
|
2315
2163
|
}
|
|
2316
2164
|
async disconnect() {
|
|
2317
|
-
return new Promise((resolve6) => {
|
|
2318
|
-
if (this.executor) {
|
|
2319
|
-
this.executor = null;
|
|
2320
|
-
}
|
|
2321
|
-
resolve6();
|
|
2322
|
-
});
|
|
2323
2165
|
}
|
|
2324
2166
|
async query(sqlQuery) {
|
|
2325
|
-
return
|
|
2326
|
-
try {
|
|
2327
|
-
if (typeof sqlQuery !== "string") {
|
|
2328
|
-
throw new Error("The SQL query must be a string.");
|
|
2329
|
-
}
|
|
2330
|
-
if (!this.executor) {
|
|
2331
|
-
await this.connect();
|
|
2332
|
-
}
|
|
2333
|
-
if (!this.executor) {
|
|
2334
|
-
throw new Error("Database connection is not available.");
|
|
2335
|
-
}
|
|
2336
|
-
const result = await this.executor.queryMultiple(sqlQuery);
|
|
2337
|
-
if (result.status === "error") {
|
|
2338
|
-
resolve6({
|
|
2339
|
-
status: "error",
|
|
2340
|
-
message: result.message,
|
|
2341
|
-
data: null
|
|
2342
|
-
});
|
|
2343
|
-
} else {
|
|
2344
|
-
resolve6({
|
|
2345
|
-
status: "success",
|
|
2346
|
-
message: "Query executed successfully",
|
|
2347
|
-
data: result.data
|
|
2348
|
-
});
|
|
2349
|
-
}
|
|
2350
|
-
} catch (error) {
|
|
2351
|
-
resolve6({
|
|
2352
|
-
status: "error",
|
|
2353
|
-
message: error.message || "An error occurred while executing the query.",
|
|
2354
|
-
data: null
|
|
2355
|
-
});
|
|
2356
|
-
}
|
|
2357
|
-
});
|
|
2167
|
+
return this.queryWithParameters(sqlQuery, []);
|
|
2358
2168
|
}
|
|
2359
2169
|
async queryWithParameters(sqlQuery, params = []) {
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
throw new Error("The SQL query must be a string.");
|
|
2364
|
-
}
|
|
2365
|
-
if (!Array.isArray(params)) {
|
|
2366
|
-
throw new Error("Parameters must be an array.");
|
|
2367
|
-
}
|
|
2368
|
-
if (!this.executor) {
|
|
2369
|
-
await this.connect();
|
|
2370
|
-
}
|
|
2371
|
-
if (!this.executor) {
|
|
2372
|
-
throw new Error("Database connection is not available.");
|
|
2373
|
-
}
|
|
2374
|
-
const result = await this.executor.query(sqlQuery, params);
|
|
2375
|
-
if (result.status === "error") {
|
|
2376
|
-
resolve6({
|
|
2377
|
-
status: "error",
|
|
2378
|
-
message: result.message,
|
|
2379
|
-
data: null
|
|
2380
|
-
});
|
|
2381
|
-
} else {
|
|
2382
|
-
resolve6({
|
|
2383
|
-
status: "success",
|
|
2384
|
-
message: "Query executed successfully",
|
|
2385
|
-
data: result.data
|
|
2386
|
-
});
|
|
2387
|
-
}
|
|
2388
|
-
} catch (error) {
|
|
2389
|
-
console.log(error);
|
|
2390
|
-
resolve6({
|
|
2391
|
-
status: "error",
|
|
2392
|
-
message: error.message || "An error occurred while executing the query.",
|
|
2393
|
-
data: null
|
|
2394
|
-
});
|
|
2170
|
+
try {
|
|
2171
|
+
if (typeof sqlQuery !== "string") {
|
|
2172
|
+
throw new Error("The SQL query must be a string.");
|
|
2395
2173
|
}
|
|
2396
|
-
|
|
2174
|
+
if (!Array.isArray(params)) {
|
|
2175
|
+
throw new Error("Parameters must be an array.");
|
|
2176
|
+
}
|
|
2177
|
+
const response = await this.getEngine().rawQuery(sqlQuery, params);
|
|
2178
|
+
if (response.status !== 200) {
|
|
2179
|
+
return { status: "error", message: response.message || "Query failed", data: null };
|
|
2180
|
+
}
|
|
2181
|
+
return { status: "success", message: "Query executed successfully", data: response.data };
|
|
2182
|
+
} catch (error) {
|
|
2183
|
+
return {
|
|
2184
|
+
status: "error",
|
|
2185
|
+
message: error.message || "An error occurred while executing the query.",
|
|
2186
|
+
data: null
|
|
2187
|
+
};
|
|
2188
|
+
}
|
|
2397
2189
|
}
|
|
2398
2190
|
convertToParameterizedQuery(sql) {
|
|
2399
2191
|
const normalizedSql = sql.replace(/\s+/g, " ").trim();
|
|
@@ -2481,7 +2273,7 @@ var DbConfig = new SQLite({ DATABASE: "config" });
|
|
|
2481
2273
|
var DbConfig_default = DbConfig;
|
|
2482
2274
|
|
|
2483
2275
|
// src/lib/FileLogger.ts
|
|
2484
|
-
var
|
|
2276
|
+
var fs9 = __toESM(require("fs"));
|
|
2485
2277
|
var path9 = __toESM(require("path"));
|
|
2486
2278
|
var import_events = require("events");
|
|
2487
2279
|
var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
@@ -2498,8 +2290,8 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2498
2290
|
static async write(filePath, message, level = "INFO", append = true) {
|
|
2499
2291
|
try {
|
|
2500
2292
|
const dir = path9.dirname(filePath);
|
|
2501
|
-
if (!
|
|
2502
|
-
|
|
2293
|
+
if (!fs9.existsSync(dir)) {
|
|
2294
|
+
fs9.mkdirSync(dir, { recursive: true });
|
|
2503
2295
|
}
|
|
2504
2296
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
2505
2297
|
const formattedMessage = `[${timestamp}] [${level}] ${message}
|
|
@@ -2509,9 +2301,9 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2509
2301
|
return true;
|
|
2510
2302
|
}
|
|
2511
2303
|
if (append) {
|
|
2512
|
-
await
|
|
2304
|
+
await fs9.promises.appendFile(filePath, formattedMessage, "utf8");
|
|
2513
2305
|
} else {
|
|
2514
|
-
await
|
|
2306
|
+
await fs9.promises.writeFile(filePath, formattedMessage, "utf8");
|
|
2515
2307
|
}
|
|
2516
2308
|
return true;
|
|
2517
2309
|
} catch (error) {
|
|
@@ -2537,11 +2329,11 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2537
2329
|
if (buffer && buffer.length > 0) {
|
|
2538
2330
|
try {
|
|
2539
2331
|
const dir = path9.dirname(filePath);
|
|
2540
|
-
if (!
|
|
2541
|
-
|
|
2332
|
+
if (!fs9.existsSync(dir)) {
|
|
2333
|
+
fs9.mkdirSync(dir, { recursive: true });
|
|
2542
2334
|
}
|
|
2543
2335
|
const content = buffer.join("");
|
|
2544
|
-
await
|
|
2336
|
+
await fs9.promises.appendFile(filePath, content, "utf8");
|
|
2545
2337
|
_FileLogger.buffers.delete(filePath);
|
|
2546
2338
|
return true;
|
|
2547
2339
|
} catch (error) {
|
|
@@ -2677,10 +2469,10 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2677
2469
|
// Si debe retornar como array de líneas
|
|
2678
2470
|
} = options;
|
|
2679
2471
|
try {
|
|
2680
|
-
if (!
|
|
2472
|
+
if (!fs9.existsSync(filePath)) {
|
|
2681
2473
|
return asArray ? [] : "";
|
|
2682
2474
|
}
|
|
2683
|
-
let content = await
|
|
2475
|
+
let content = await fs9.promises.readFile(filePath, "utf8");
|
|
2684
2476
|
if (asArray) {
|
|
2685
2477
|
let linesArray = content.split("\n").filter((line) => line.trim() !== "");
|
|
2686
2478
|
if (lines !== null) {
|
|
@@ -2723,15 +2515,15 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2723
2515
|
} = options;
|
|
2724
2516
|
let lastSize = 0;
|
|
2725
2517
|
let lastPosition = 0;
|
|
2726
|
-
if (
|
|
2727
|
-
const stats =
|
|
2518
|
+
if (fs9.existsSync(filePath)) {
|
|
2519
|
+
const stats = fs9.statSync(filePath);
|
|
2728
2520
|
lastSize = stats.size;
|
|
2729
2521
|
lastPosition = fromEnd ? stats.size : 0;
|
|
2730
2522
|
}
|
|
2731
2523
|
const listener = async (curr, prev) => {
|
|
2732
2524
|
try {
|
|
2733
2525
|
if (curr.size > lastSize) {
|
|
2734
|
-
const stream =
|
|
2526
|
+
const stream = fs9.createReadStream(filePath, {
|
|
2735
2527
|
start: lastPosition,
|
|
2736
2528
|
end: curr.size - 1,
|
|
2737
2529
|
encoding: "utf8"
|
|
@@ -2759,7 +2551,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2759
2551
|
console.error("Error en watcher:", error);
|
|
2760
2552
|
}
|
|
2761
2553
|
};
|
|
2762
|
-
|
|
2554
|
+
fs9.watchFile(filePath, { persistent, interval }, listener);
|
|
2763
2555
|
const watcherId = `${filePath}_${Date.now()}`;
|
|
2764
2556
|
_FileLogger.watchers.set(watcherId, listener);
|
|
2765
2557
|
return {
|
|
@@ -2767,7 +2559,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2767
2559
|
stop: () => {
|
|
2768
2560
|
const storedListener = _FileLogger.watchers.get(watcherId);
|
|
2769
2561
|
if (storedListener) {
|
|
2770
|
-
|
|
2562
|
+
fs9.unwatchFile(filePath, storedListener);
|
|
2771
2563
|
_FileLogger.watchers.delete(watcherId);
|
|
2772
2564
|
}
|
|
2773
2565
|
},
|
|
@@ -2780,7 +2572,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2780
2572
|
static stopAllWatchers() {
|
|
2781
2573
|
for (const [watcherId] of _FileLogger.watchers) {
|
|
2782
2574
|
const filePath = watcherId.split("_")[0];
|
|
2783
|
-
|
|
2575
|
+
fs9.unwatchFile(filePath);
|
|
2784
2576
|
}
|
|
2785
2577
|
_FileLogger.watchers.clear();
|
|
2786
2578
|
}
|
|
@@ -2810,7 +2602,7 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2810
2602
|
if (lines.length > maxLines) {
|
|
2811
2603
|
const keepLines = lines.slice(-maxLines);
|
|
2812
2604
|
const content = keepLines.join("\n") + "\n";
|
|
2813
|
-
await
|
|
2605
|
+
await fs9.promises.writeFile(filePath, content, "utf8");
|
|
2814
2606
|
return lines.length - maxLines;
|
|
2815
2607
|
}
|
|
2816
2608
|
return 0;
|
|
@@ -2825,8 +2617,8 @@ var FileLogger = class _FileLogger extends import_events.EventEmitter {
|
|
|
2825
2617
|
*/
|
|
2826
2618
|
static async deleteLogFile(filePath) {
|
|
2827
2619
|
try {
|
|
2828
|
-
if (
|
|
2829
|
-
await
|
|
2620
|
+
if (fs9.existsSync(filePath)) {
|
|
2621
|
+
await fs9.promises.unlink(filePath);
|
|
2830
2622
|
return true;
|
|
2831
2623
|
}
|
|
2832
2624
|
return false;
|
|
@@ -3242,6 +3034,7 @@ function convertToType(value, type2) {
|
|
|
3242
3034
|
DaemonClient,
|
|
3243
3035
|
DbConfig,
|
|
3244
3036
|
Engine,
|
|
3037
|
+
EnvLoader,
|
|
3245
3038
|
FileLogger,
|
|
3246
3039
|
QueryEngine,
|
|
3247
3040
|
TableProcessor,
|