@dbcube/core 5.1.13 → 5.2.1

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/index.d.mts CHANGED
@@ -2,21 +2,121 @@ import { EventEmitter } from 'events';
2
2
 
3
3
  export interface ResponseEngine {
4
4
  status: number;
5
- message: String;
5
+ message: string;
6
6
  data: any;
7
7
  }
8
+ /**
9
+ * Connection parameters for one database.
10
+ */
11
+ export interface DatabaseConnectionConfig {
12
+ /** Host name or IP (not used by SQLite) */
13
+ HOST?: string;
14
+ /** Database user (not used by SQLite) */
15
+ USER?: string;
16
+ /** Database password (not used by SQLite) */
17
+ PASSWORD?: string;
18
+ /** Database name (SQLite: file name without extension) */
19
+ DATABASE: string;
20
+ /** Port (not used by SQLite) */
21
+ PORT?: number | string;
22
+ }
23
+ /**
24
+ * Connection-pool tuning. Every field is optional — engine defaults apply
25
+ * when omitted (maxConnections 5 — 10 for SQLite —, minConnections 2,
26
+ * acquireTimeoutMs 3000, idleTimeoutMs 3600000).
27
+ */
28
+ export interface DatabasePoolConfig {
29
+ /** Max simultaneous connections held by the engine pool */
30
+ maxConnections?: number;
31
+ /** Connections kept warm even when idle */
32
+ minConnections?: number;
33
+ /** How long to wait for a free connection before failing (ms) */
34
+ acquireTimeoutMs?: number;
35
+ /** Close connections idle longer than this (ms) */
36
+ idleTimeoutMs?: number;
37
+ }
38
+ /**
39
+ * Daemon-mode tuning.
40
+ */
41
+ export interface DatabaseDaemonConfig {
42
+ /** Set to false to disable the persistent TCP daemon (one-shot mode).
43
+ * Transactions require the daemon. Equivalent to DBCUBE_DAEMON=0. */
44
+ enabled?: boolean;
45
+ /** Per-request timeout against the daemon (ms, default 30000) */
46
+ requestTimeoutMs?: number;
47
+ }
48
+ export type DatabaseType = "mysql" | "postgres" | "postgresql" | "sqlite" | "mongodb";
49
+ /**
50
+ * One database entry in dbcube.config.js.
51
+ */
52
+ export interface DatabaseDefinition {
53
+ type: DatabaseType;
54
+ config: DatabaseConnectionConfig;
55
+ pool?: DatabasePoolConfig;
56
+ daemon?: DatabaseDaemonConfig;
57
+ }
58
+ /**
59
+ * Shape accepted by config.set().
60
+ */
61
+ export interface ConfigData {
62
+ databases?: Record<string, DatabaseDefinition>;
63
+ [key: string]: unknown;
64
+ }
65
+ /**
66
+ * Clase para manejar la configuración del ORM
67
+ */
68
+ export declare class Config {
69
+ private data;
70
+ private databases;
71
+ /**
72
+ * Establece la configuración
73
+ * @param configData - Datos de configuración
74
+ */
75
+ set(configData: ConfigData): void;
76
+ /**
77
+ * Obtiene un valor de configuración
78
+ * @param key - Clave de configuración
79
+ */
80
+ get<T = unknown>(key: string): T;
81
+ /**
82
+ * Obtiene la configuración de una base de datos específica
83
+ * @param dbName - Nombre de la base de datos
84
+ */
85
+ getDatabase(dbName: string): DatabaseDefinition | null;
86
+ /**
87
+ * Obtiene todas las bases de datos configuradas
88
+ */
89
+ getAllDatabases(): Record<string, DatabaseDefinition>;
90
+ }
8
91
  export declare class Engine {
9
92
  private name;
10
93
  private config;
11
94
  private arguments;
12
95
  private binary;
13
96
  private timeout;
97
+ private daemonFailed;
14
98
  constructor(name: string, timeout?: number);
15
99
  initializeBinary(): Promise<void>;
100
+ /**
101
+ * Returns a connected DaemonClient for this database, or null when
102
+ * daemon mode is disabled/unavailable (then callers use spawn mode).
103
+ */
104
+ private getDaemon;
105
+ /**
106
+ * Executes a DML plan. Uses the persistent daemon (sub-millisecond
107
+ * overhead) when available; falls back to one-shot spawn otherwise.
108
+ */
109
+ executeDml(dml: object, txId?: string): Promise<ResponseEngine>;
110
+ /**
111
+ * Executes raw SQL (or a MongoDB command document) with bound parameters.
112
+ */
113
+ rawQuery(query: string, params?: any[], txId?: string): Promise<ResponseEngine>;
114
+ /** Starts a transaction (daemon mode only). Returns the transaction id. */
115
+ beginTransaction(): Promise<string>;
116
+ commitTransaction(txId: string): Promise<void>;
117
+ rollbackTransaction(txId: string): Promise<void>;
16
118
  setArguments(): any[];
17
- setConfig(name: string): {
18
- [x: string]: any;
19
- } | null;
119
+ setConfig(name: string): DatabaseDefinition | null;
20
120
  getConfig(): any;
21
121
  run(binary: string, args: [
22
122
  ]): Promise<ResponseEngine>;
@@ -35,11 +135,26 @@ export declare class QueryEngine {
35
135
  private isPortAvailable;
36
136
  initializeBinary(): Promise<void>;
37
137
  setArguments(): any[];
38
- setConfig(name: string): {
39
- [x: string]: any;
40
- } | null;
138
+ /** El daemon puede desactivarse por config (daemon.enabled=false) o por env (DBCUBE_DAEMON=0). */
139
+ private daemonEnabled;
140
+ setConfig(name: string): DatabaseDefinition | null;
41
141
  getConfig(): any;
42
142
  run(binary: string, args: string[]): Promise<ResponseEngine>;
143
+ private argsToCommand;
144
+ /**
145
+ * Executes a DML plan over the persistent TCP server.
146
+ * Pass txId to run it inside an active transaction.
147
+ */
148
+ executeDml(dml: object, txId?: string): Promise<ResponseEngine>;
149
+ /**
150
+ * Executes raw SQL (or a MongoDB command document) with bound parameters
151
+ * over the persistent TCP server.
152
+ */
153
+ rawQuery(query: string, params?: any[], txId?: string): Promise<ResponseEngine>;
154
+ /** Starts a server-side transaction and returns its id. */
155
+ beginTransaction(): Promise<string>;
156
+ commitTransaction(txId: string): Promise<void>;
157
+ rollbackTransaction(txId: string): Promise<void>;
43
158
  private executeWithTcpServer;
44
159
  private waitForServerReady;
45
160
  private isServerResponding;
@@ -165,42 +280,63 @@ declare class SQLite {
165
280
  convertToParameterizedQuery(sql: string): ParametrizedQueryResult;
166
281
  }
167
282
  export declare const DbConfig: SQLite;
168
- type DatabaseConfig$1 = Record<string, any>;
169
- /**
170
- * Tipo para la configuración general del ORM
171
- */
172
- export interface ConfigData {
173
- [key: string]: any;
174
- databases?: Record<string, DatabaseConfig$1>;
283
+ export interface DaemonResponse {
284
+ status: number;
285
+ message: string;
286
+ data: any;
175
287
  }
176
288
  /**
177
- * Clase para manejar la configuración del ORM
289
+ * TCP client for the query-engine daemon (server mode).
290
+ *
291
+ * Instead of spawning a new engine process per query (30-80ms overhead),
292
+ * the daemon keeps the engine alive with warm connection pools and serves
293
+ * requests over a local TCP socket in <1ms. One daemon per database name.
178
294
  */
179
- export declare class Config {
180
- private data;
181
- private databases;
182
- /**
183
- * Establece la configuración
184
- * @param configData - Datos de configuración
185
- */
186
- set(configData: ConfigData): void;
295
+ export declare class DaemonClient {
296
+ private static registry;
297
+ private name;
298
+ private binaryPath;
299
+ private engineArgs;
300
+ private socket;
301
+ private buffer;
302
+ private pending;
303
+ private starting;
304
+ private requestTimeout;
305
+ private constructor();
306
+ static get(name: string, binaryPath: string, engineArgs: string[]): DaemonClient;
307
+ static isEnabled(): boolean;
308
+ private portfilePath;
309
+ private lockfilePath;
187
310
  /**
188
- * Obtiene un valor de configuración
189
- * @param key - Clave de configuración
190
- * @returns Valor de configuración
311
+ * Exclusive-create lock so two processes never spawn two daemons for the
312
+ * same database at once. Stale locks (crashed starter) expire after 15s.
191
313
  */
192
- get<T = any>(key: string): T;
314
+ private acquireSpawnLock;
315
+ private releaseSpawnLock;
193
316
  /**
194
- * Obtiene la configuración de una base de datos específica
195
- * @param dbName - Nombre de la base de datos
196
- * @returns Configuración de la base de datos o null
317
+ * Ensures a usable daemon connection. Returns false when the daemon
318
+ * can't be used (old binary, startup failure) so callers fall back
319
+ * to one-shot spawn mode.
197
320
  */
198
- getDatabase(dbName: string): DatabaseConfig$1 | null;
321
+ ensure(): Promise<boolean>;
322
+ private connectOrStart;
323
+ private readPortfile;
324
+ private spawnDaemon;
325
+ private tryConnect;
326
+ private attach;
327
+ private detach;
328
+ private onData;
199
329
  /**
200
- * Obtiene todas las bases de datos configuradas
201
- * @returns Todas las configuraciones de bases de datos
330
+ * Sends a request to the daemon. Responses arrive in FIFO order on the
331
+ * single socket, so pending requests resolve in send order.
202
332
  */
203
- getAllDatabases(): Record<string, DatabaseConfig$1>;
333
+ send(payload: Record<string, any>, timeoutMs?: number): Promise<DaemonResponse>;
334
+ execute(dml: object, txId?: string): Promise<DaemonResponse>;
335
+ raw(query: string, params?: any[], txId?: string): Promise<DaemonResponse>;
336
+ begin(): Promise<string>;
337
+ commit(txId: string): Promise<void>;
338
+ rollback(txId: string): Promise<void>;
339
+ shutdown(): Promise<void>;
204
340
  }
205
341
  export interface InterceptOptions {
206
342
  interceptLog?: boolean;
@@ -335,7 +471,7 @@ export interface ComputedFieldConfig {
335
471
  created_at: string;
336
472
  updated_at: string;
337
473
  }
338
- export type DatabaseType = "mysql" | "sqlite" | "postgres" | "mongodb";
474
+ type DatabaseType$1 = "mysql" | "sqlite" | "postgres" | "mongodb";
339
475
  export declare class ComputedFieldProcessor {
340
476
  static getComputedFields(name: string): Promise<any[]>;
341
477
  /**
@@ -360,7 +496,7 @@ export declare class ComputedFieldProcessor {
360
496
  static computedFields<T extends DataObject>(data: T[], computedConfigs: ComputedFieldConfig[]): T[];
361
497
  }
362
498
  export declare class TableProcessor {
363
- static generateAlterQueries(nowQuery: string, dbType: DatabaseType, tableName: string, database_ref: string): Promise<string[]>;
499
+ static generateAlterQueries(nowQuery: string, dbType: DatabaseType$1, tableName: string, database_ref: string): Promise<string[]>;
364
500
  static saveQuery(table_ref: string, database_ref: string, struct: string): Promise<void>;
365
501
  }
366
502
  export declare class TriggerProcessor {
package/dist/index.d.ts CHANGED
@@ -2,21 +2,121 @@ import { EventEmitter } from 'events';
2
2
 
3
3
  export interface ResponseEngine {
4
4
  status: number;
5
- message: String;
5
+ message: string;
6
6
  data: any;
7
7
  }
8
+ /**
9
+ * Connection parameters for one database.
10
+ */
11
+ export interface DatabaseConnectionConfig {
12
+ /** Host name or IP (not used by SQLite) */
13
+ HOST?: string;
14
+ /** Database user (not used by SQLite) */
15
+ USER?: string;
16
+ /** Database password (not used by SQLite) */
17
+ PASSWORD?: string;
18
+ /** Database name (SQLite: file name without extension) */
19
+ DATABASE: string;
20
+ /** Port (not used by SQLite) */
21
+ PORT?: number | string;
22
+ }
23
+ /**
24
+ * Connection-pool tuning. Every field is optional — engine defaults apply
25
+ * when omitted (maxConnections 5 — 10 for SQLite —, minConnections 2,
26
+ * acquireTimeoutMs 3000, idleTimeoutMs 3600000).
27
+ */
28
+ export interface DatabasePoolConfig {
29
+ /** Max simultaneous connections held by the engine pool */
30
+ maxConnections?: number;
31
+ /** Connections kept warm even when idle */
32
+ minConnections?: number;
33
+ /** How long to wait for a free connection before failing (ms) */
34
+ acquireTimeoutMs?: number;
35
+ /** Close connections idle longer than this (ms) */
36
+ idleTimeoutMs?: number;
37
+ }
38
+ /**
39
+ * Daemon-mode tuning.
40
+ */
41
+ export interface DatabaseDaemonConfig {
42
+ /** Set to false to disable the persistent TCP daemon (one-shot mode).
43
+ * Transactions require the daemon. Equivalent to DBCUBE_DAEMON=0. */
44
+ enabled?: boolean;
45
+ /** Per-request timeout against the daemon (ms, default 30000) */
46
+ requestTimeoutMs?: number;
47
+ }
48
+ export type DatabaseType = "mysql" | "postgres" | "postgresql" | "sqlite" | "mongodb";
49
+ /**
50
+ * One database entry in dbcube.config.js.
51
+ */
52
+ export interface DatabaseDefinition {
53
+ type: DatabaseType;
54
+ config: DatabaseConnectionConfig;
55
+ pool?: DatabasePoolConfig;
56
+ daemon?: DatabaseDaemonConfig;
57
+ }
58
+ /**
59
+ * Shape accepted by config.set().
60
+ */
61
+ export interface ConfigData {
62
+ databases?: Record<string, DatabaseDefinition>;
63
+ [key: string]: unknown;
64
+ }
65
+ /**
66
+ * Clase para manejar la configuración del ORM
67
+ */
68
+ export declare class Config {
69
+ private data;
70
+ private databases;
71
+ /**
72
+ * Establece la configuración
73
+ * @param configData - Datos de configuración
74
+ */
75
+ set(configData: ConfigData): void;
76
+ /**
77
+ * Obtiene un valor de configuración
78
+ * @param key - Clave de configuración
79
+ */
80
+ get<T = unknown>(key: string): T;
81
+ /**
82
+ * Obtiene la configuración de una base de datos específica
83
+ * @param dbName - Nombre de la base de datos
84
+ */
85
+ getDatabase(dbName: string): DatabaseDefinition | null;
86
+ /**
87
+ * Obtiene todas las bases de datos configuradas
88
+ */
89
+ getAllDatabases(): Record<string, DatabaseDefinition>;
90
+ }
8
91
  export declare class Engine {
9
92
  private name;
10
93
  private config;
11
94
  private arguments;
12
95
  private binary;
13
96
  private timeout;
97
+ private daemonFailed;
14
98
  constructor(name: string, timeout?: number);
15
99
  initializeBinary(): Promise<void>;
100
+ /**
101
+ * Returns a connected DaemonClient for this database, or null when
102
+ * daemon mode is disabled/unavailable (then callers use spawn mode).
103
+ */
104
+ private getDaemon;
105
+ /**
106
+ * Executes a DML plan. Uses the persistent daemon (sub-millisecond
107
+ * overhead) when available; falls back to one-shot spawn otherwise.
108
+ */
109
+ executeDml(dml: object, txId?: string): Promise<ResponseEngine>;
110
+ /**
111
+ * Executes raw SQL (or a MongoDB command document) with bound parameters.
112
+ */
113
+ rawQuery(query: string, params?: any[], txId?: string): Promise<ResponseEngine>;
114
+ /** Starts a transaction (daemon mode only). Returns the transaction id. */
115
+ beginTransaction(): Promise<string>;
116
+ commitTransaction(txId: string): Promise<void>;
117
+ rollbackTransaction(txId: string): Promise<void>;
16
118
  setArguments(): any[];
17
- setConfig(name: string): {
18
- [x: string]: any;
19
- } | null;
119
+ setConfig(name: string): DatabaseDefinition | null;
20
120
  getConfig(): any;
21
121
  run(binary: string, args: [
22
122
  ]): Promise<ResponseEngine>;
@@ -35,11 +135,26 @@ export declare class QueryEngine {
35
135
  private isPortAvailable;
36
136
  initializeBinary(): Promise<void>;
37
137
  setArguments(): any[];
38
- setConfig(name: string): {
39
- [x: string]: any;
40
- } | null;
138
+ /** El daemon puede desactivarse por config (daemon.enabled=false) o por env (DBCUBE_DAEMON=0). */
139
+ private daemonEnabled;
140
+ setConfig(name: string): DatabaseDefinition | null;
41
141
  getConfig(): any;
42
142
  run(binary: string, args: string[]): Promise<ResponseEngine>;
143
+ private argsToCommand;
144
+ /**
145
+ * Executes a DML plan over the persistent TCP server.
146
+ * Pass txId to run it inside an active transaction.
147
+ */
148
+ executeDml(dml: object, txId?: string): Promise<ResponseEngine>;
149
+ /**
150
+ * Executes raw SQL (or a MongoDB command document) with bound parameters
151
+ * over the persistent TCP server.
152
+ */
153
+ rawQuery(query: string, params?: any[], txId?: string): Promise<ResponseEngine>;
154
+ /** Starts a server-side transaction and returns its id. */
155
+ beginTransaction(): Promise<string>;
156
+ commitTransaction(txId: string): Promise<void>;
157
+ rollbackTransaction(txId: string): Promise<void>;
43
158
  private executeWithTcpServer;
44
159
  private waitForServerReady;
45
160
  private isServerResponding;
@@ -165,42 +280,63 @@ declare class SQLite {
165
280
  convertToParameterizedQuery(sql: string): ParametrizedQueryResult;
166
281
  }
167
282
  export declare const DbConfig: SQLite;
168
- type DatabaseConfig$1 = Record<string, any>;
169
- /**
170
- * Tipo para la configuración general del ORM
171
- */
172
- export interface ConfigData {
173
- [key: string]: any;
174
- databases?: Record<string, DatabaseConfig$1>;
283
+ export interface DaemonResponse {
284
+ status: number;
285
+ message: string;
286
+ data: any;
175
287
  }
176
288
  /**
177
- * Clase para manejar la configuración del ORM
289
+ * TCP client for the query-engine daemon (server mode).
290
+ *
291
+ * Instead of spawning a new engine process per query (30-80ms overhead),
292
+ * the daemon keeps the engine alive with warm connection pools and serves
293
+ * requests over a local TCP socket in <1ms. One daemon per database name.
178
294
  */
179
- export declare class Config {
180
- private data;
181
- private databases;
182
- /**
183
- * Establece la configuración
184
- * @param configData - Datos de configuración
185
- */
186
- set(configData: ConfigData): void;
295
+ export declare class DaemonClient {
296
+ private static registry;
297
+ private name;
298
+ private binaryPath;
299
+ private engineArgs;
300
+ private socket;
301
+ private buffer;
302
+ private pending;
303
+ private starting;
304
+ private requestTimeout;
305
+ private constructor();
306
+ static get(name: string, binaryPath: string, engineArgs: string[]): DaemonClient;
307
+ static isEnabled(): boolean;
308
+ private portfilePath;
309
+ private lockfilePath;
187
310
  /**
188
- * Obtiene un valor de configuración
189
- * @param key - Clave de configuración
190
- * @returns Valor de configuración
311
+ * Exclusive-create lock so two processes never spawn two daemons for the
312
+ * same database at once. Stale locks (crashed starter) expire after 15s.
191
313
  */
192
- get<T = any>(key: string): T;
314
+ private acquireSpawnLock;
315
+ private releaseSpawnLock;
193
316
  /**
194
- * Obtiene la configuración de una base de datos específica
195
- * @param dbName - Nombre de la base de datos
196
- * @returns Configuración de la base de datos o null
317
+ * Ensures a usable daemon connection. Returns false when the daemon
318
+ * can't be used (old binary, startup failure) so callers fall back
319
+ * to one-shot spawn mode.
197
320
  */
198
- getDatabase(dbName: string): DatabaseConfig$1 | null;
321
+ ensure(): Promise<boolean>;
322
+ private connectOrStart;
323
+ private readPortfile;
324
+ private spawnDaemon;
325
+ private tryConnect;
326
+ private attach;
327
+ private detach;
328
+ private onData;
199
329
  /**
200
- * Obtiene todas las bases de datos configuradas
201
- * @returns Todas las configuraciones de bases de datos
330
+ * Sends a request to the daemon. Responses arrive in FIFO order on the
331
+ * single socket, so pending requests resolve in send order.
202
332
  */
203
- getAllDatabases(): Record<string, DatabaseConfig$1>;
333
+ send(payload: Record<string, any>, timeoutMs?: number): Promise<DaemonResponse>;
334
+ execute(dml: object, txId?: string): Promise<DaemonResponse>;
335
+ raw(query: string, params?: any[], txId?: string): Promise<DaemonResponse>;
336
+ begin(): Promise<string>;
337
+ commit(txId: string): Promise<void>;
338
+ rollback(txId: string): Promise<void>;
339
+ shutdown(): Promise<void>;
204
340
  }
205
341
  export interface InterceptOptions {
206
342
  interceptLog?: boolean;
@@ -335,7 +471,7 @@ export interface ComputedFieldConfig {
335
471
  created_at: string;
336
472
  updated_at: string;
337
473
  }
338
- export type DatabaseType = "mysql" | "sqlite" | "postgres" | "mongodb";
474
+ type DatabaseType$1 = "mysql" | "sqlite" | "postgres" | "mongodb";
339
475
  export declare class ComputedFieldProcessor {
340
476
  static getComputedFields(name: string): Promise<any[]>;
341
477
  /**
@@ -360,7 +496,7 @@ export declare class ComputedFieldProcessor {
360
496
  static computedFields<T extends DataObject>(data: T[], computedConfigs: ComputedFieldConfig[]): T[];
361
497
  }
362
498
  export declare class TableProcessor {
363
- static generateAlterQueries(nowQuery: string, dbType: DatabaseType, tableName: string, database_ref: string): Promise<string[]>;
499
+ static generateAlterQueries(nowQuery: string, dbType: DatabaseType$1, tableName: string, database_ref: string): Promise<string[]>;
364
500
  static saveQuery(table_ref: string, database_ref: string, struct: string): Promise<void>;
365
501
  }
366
502
  export declare class TriggerProcessor {