@egi/smart-db 2.6.5 → 3.0.4
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/README.md +355 -0
- package/assets/mysql/smart-db-core-update.001.sql +10 -0
- package/assets/oracle/smart-db-core-update.001.sql +13 -0
- package/assets/sqlite3/smart-db-core-update.001.sql +10 -0
- package/drivers/smart-db-better-sqlite3.d.ts +103 -16
- package/drivers/smart-db-better-sqlite3.js +1 -1
- package/drivers/smart-db-mysql.d.ts +53 -10
- package/drivers/smart-db-mysql.js +1 -1
- package/drivers/smart-db-oracle.d.ts +93 -20
- package/drivers/smart-db-oracle.js +1 -1
- package/helpers/extract-db-api.js +1 -1
- package/models/abstract-model.d.ts +39 -4
- package/models/abstract-model.js +1 -1
- package/models/mysql-tables-model.d.ts +109 -0
- package/models/mysql-tables-model.js +1 -0
- package/models/oracle-all-cons-columns-model.d.ts +55 -0
- package/models/oracle-all-cons-columns-model.js +1 -0
- package/models/oracle-all-constraints-model.d.ts +177 -0
- package/models/oracle-all-constraints-model.js +1 -0
- package/models/oracle-all-dependencies-model.d.ts +77 -0
- package/models/oracle-all-dependencies-model.js +1 -0
- package/models/oracle-all-objects-model.d.ts +201 -0
- package/models/oracle-all-objects-model.js +1 -0
- package/models/oracle-all-tab-columns-model.d.ts +305 -0
- package/models/oracle-all-tab-columns-model.js +1 -0
- package/models/oracle-all-triggers-model.d.ts +187 -0
- package/models/oracle-all-triggers-model.js +1 -0
- package/{models.generated/smart-db-user-tab-columns-view-model.d.ts → models/smart-db-all-tab-columns-view-model.d.ts} +14 -6
- package/models/smart-db-all-tab-columns-view-model.js +1 -0
- package/models/smart-db-core-table-model.d.ts +24 -22
- package/models/smart-db-core-table-model.js +1 -1
- package/models/smart-db-dictionary.d.ts +2 -10
- package/models/smart-db-dictionary.js +1 -1
- package/models/smart-db-log-model.d.ts +25 -8
- package/models/smart-db-log-model.js +1 -1
- package/models/smart-db-test-table-model.d.ts +79 -0
- package/models/smart-db-test-table-model.js +1 -0
- package/models/smart-db-user-tab-columns-view-model.d.ts +2 -0
- package/models/smart-db-user-tab-columns-view-model.js +1 -1
- package/models/smart-db-version-model.d.ts +25 -8
- package/models/smart-db-version-model.js +1 -1
- package/models/smart-db-version-view-model.d.ts +27 -8
- package/models/smart-db-version-view-model.js +1 -1
- package/models/sqlite-master-model.d.ts +14 -12
- package/models/sqlite-master-model.js +1 -1
- package/models/sqlite-sequence-model.d.ts +9 -7
- package/models/sqlite-sequence-model.js +1 -1
- package/package.json +40 -56
- package/smart-db-api.d.ts +2 -1
- package/smart-db-api.js +1 -1
- package/smart-db-browser.d.ts +5 -0
- package/smart-db-browser.js +1 -0
- package/smart-db-globals.d.ts +37 -5
- package/smart-db-globals.js +1 -1
- package/smart-db-interfaces.d.ts +330 -40
- package/smart-db-interfaces.js +1 -1
- package/smart-db-sql-build-data.d.ts +10 -1
- package/smart-db-sql-build-data.js +1 -1
- package/smart-db-upgrade-manager.d.ts +6 -4
- package/smart-db-upgrade-manager.js +1 -1
- package/smart-db.d.ts +451 -55
- package/smart-db.js +1 -1
- package/smart-error.d.ts +9 -28
- package/smart-error.js +1 -1
- package/smart-tools.d.ts +178 -0
- package/smart-tools.js +1 -0
- package/drivers/smart-db-mysql2.d.ts +0 -20
- package/drivers/smart-db-mysql2.js +0 -1
- package/drivers/smart-db-sqlite3.d.ts +0 -23
- package/drivers/smart-db-sqlite3.js +0 -1
- package/models/oracle-cat-model.d.ts +0 -23
- package/models/oracle-cat-model.js +0 -1
- package/models/oracle-user-tab-columns-model.d.ts +0 -39
- package/models/oracle-user-tab-columns-model.js +0 -1
- package/models/oracle-user-tab-columns-view-model.d.ts +0 -43
- package/models/oracle-user-tab-columns-view-model.js +0 -1
- package/models.generated/abstract-model.d.ts +0 -23
- package/models.generated/abstract-model.js +0 -1
- package/models.generated/smart-db-core-table-model.d.ts +0 -41
- package/models.generated/smart-db-core-table-model.js +0 -1
- package/models.generated/smart-db-dictionary.d.ts +0 -14
- package/models.generated/smart-db-dictionary.js +0 -1
- package/models.generated/smart-db-log-model.d.ts +0 -82
- package/models.generated/smart-db-log-model.js +0 -1
- package/models.generated/smart-db-user-tab-columns-view-model.js +0 -1
- package/models.generated/smart-db-version-model.d.ts +0 -82
- package/models.generated/smart-db-version-model.js +0 -1
- package/models.generated/smart-db-version-view-model.d.ts +0 -90
- package/models.generated/smart-db-version-view-model.js +0 -1
- package/smart-db-log.d.ts +0 -40
- package/smart-db-log.js +0 -1
package/smart-db.d.ts
CHANGED
|
@@ -1,87 +1,483 @@
|
|
|
1
1
|
import { BehaviorSubject, Observable } from "rxjs";
|
|
2
2
|
import { AbstractModel } from "./models/abstract-model";
|
|
3
|
+
import { ISmartLogDbWriter, SmartLog } from "@egi/smart-log";
|
|
3
4
|
import { SmartDbVersionViewModel } from "./models/smart-db-version-view-model";
|
|
4
|
-
import { GenericModelData, IndexedGenericModelData, SmartDbConnector,
|
|
5
|
-
import { SmartDbLog } from "./smart-db-log";
|
|
5
|
+
import { AttributeInfo, GenericModelData, IndexedGenericModelData, MakeDbValueOptions, ModelClass, SmartDbConnector, SmartDbInterface, SmartDbOptions, SmartDbReadyState, SmartDbRunResult, SmartDbSqlOptions, SmartDbTableInfo, SqlFieldDescriptor, SqlLimit, SqlOrderBy, SqlUpdateValues, SqlValueType, SqlValueTypes, SqlWhere } from "./smart-db-interfaces";
|
|
6
6
|
import { SmartDbSqlBuildData } from "./smart-db-sql-build-data";
|
|
7
7
|
import { SmartError } from "./smart-error";
|
|
8
|
-
|
|
8
|
+
import { SmartDbDateTimeMode } from "./smart-tools";
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base class for all SmartDB database drivers.
|
|
11
|
+
*
|
|
12
|
+
* Provides a unified, type-safe API for SQL operations (SELECT, INSERT, UPDATE, DELETE),
|
|
13
|
+
* transaction management, schema upgrades, and logging across SQLite, MySQL, and Oracle.
|
|
14
|
+
*
|
|
15
|
+
* Concrete drivers extend this class and implement {@link getDatabaseType}, {@link getTableInfo},
|
|
16
|
+
* and {@link getDb}. Use the ready-state API ({@link databaseReady}, {@link onReady}) to wait
|
|
17
|
+
* for the database to finish initializing before issuing queries.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T - The concrete driver subclass (enables fluent `onReady` callbacks typed to the subclass).
|
|
20
|
+
*/
|
|
21
|
+
export declare abstract class SmartDb<T extends SmartDb<T>> implements SmartDbInterface {
|
|
22
|
+
/**
|
|
23
|
+
* Returns a short identifier for the underlying database engine,
|
|
24
|
+
* e.g. `"sqlite"`, `"mysql"`, or `"oracle"`.
|
|
25
|
+
* Used internally to locate the correct asset directory for SQL upgrade scripts.
|
|
26
|
+
*/
|
|
9
27
|
abstract getDatabaseType(): string;
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves column metadata for a database table.
|
|
30
|
+
* @param table - The table name to inspect.
|
|
31
|
+
* @returns A {@link SmartDbTableInfo} describing the table's fields, name, and optional PK sequence.
|
|
32
|
+
*/
|
|
10
33
|
abstract getTableInfo(table: string): Promise<SmartDbTableInfo>;
|
|
11
|
-
|
|
12
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Returns the raw underlying database connection/handle.
|
|
36
|
+
* The exact type depends on the driver (e.g. `better-sqlite3` `Database`, `mysql2` `Pool`).
|
|
37
|
+
* Prefer the higher-level SmartDB API over using this directly.
|
|
38
|
+
*/
|
|
39
|
+
abstract getDb(): any;
|
|
40
|
+
protected _onReady: BehaviorSubject<SmartDbReadyState>;
|
|
41
|
+
protected db: any;
|
|
13
42
|
protected dbConnector: SmartDbConnector;
|
|
14
43
|
protected lastBuildData: SmartDbSqlBuildData;
|
|
15
|
-
protected
|
|
16
|
-
|
|
17
|
-
protected
|
|
44
|
+
protected log: SmartLog;
|
|
45
|
+
private dbWriter;
|
|
46
|
+
protected options: SmartDbOptions<T>;
|
|
18
47
|
private _lastError;
|
|
19
|
-
private dictionaries;
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
48
|
+
private readonly dictionaries;
|
|
49
|
+
private moduleVersions;
|
|
50
|
+
private waitReadySubscriptions;
|
|
51
|
+
protected constructor(dbConnector: SmartDbConnector, options?: SmartDbOptions<T>);
|
|
52
|
+
/** Returns the {@link SmartDbOptions} object that was passed to the constructor. */
|
|
53
|
+
getOptions(): SmartDbOptions<T>;
|
|
54
|
+
/** Whether database-level logging (persisting log entries to a DB table) is currently active. */
|
|
55
|
+
get dbLogging(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* `true` when the database has reached at least the {@link SmartDbReadyState.CONNECTED} state,
|
|
58
|
+
* meaning the underlying connection is established (though schema upgrades may still be running).
|
|
59
|
+
*/
|
|
60
|
+
get isConnected(): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* `true` when the database has reached {@link SmartDbReadyState.READY},
|
|
63
|
+
* meaning all connections and schema upgrades have completed successfully.
|
|
64
|
+
* Use {@link databaseReady} to await this state asynchronously.
|
|
65
|
+
*/
|
|
66
|
+
get isReady(): boolean;
|
|
67
|
+
/** The last error that occurred, wrapped in a {@link SmartError}. */
|
|
68
|
+
get lastError(): SmartError;
|
|
69
|
+
set lastError(value: any);
|
|
70
|
+
/**
|
|
71
|
+
* RxJS observable that emits every time the ready state changes.
|
|
72
|
+
* Useful for reacting to connect/disconnect/error transitions.
|
|
73
|
+
* For a one-time await, prefer {@link databaseReady}.
|
|
74
|
+
*/
|
|
75
|
+
get onReady(): Observable<SmartDbReadyState>;
|
|
76
|
+
/**
|
|
77
|
+
* The current {@link SmartDbReadyState} of the database connection.
|
|
78
|
+
* Progresses: `INIT` → `PREPARING` → `CONNECTED` → `READY` (or `ERROR` / `CLOSED`).
|
|
79
|
+
*/
|
|
80
|
+
get readyState(): SmartDbReadyState;
|
|
81
|
+
protected set readyState(state: SmartDbReadyState);
|
|
82
|
+
/**
|
|
83
|
+
* `true` if this driver supports synchronous query methods
|
|
84
|
+
* (e.g. {@link getSync}, {@link insertSync}, {@link deleteSync}, {@link updateSync}).
|
|
85
|
+
* Only the SQLite driver returns `true`; MySQL and Oracle are async-only.
|
|
86
|
+
*/
|
|
87
|
+
get supportSyncCalls(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Returns a promise that resolves once the database reaches `targetState` (default: `READY`).
|
|
90
|
+
* Rejects with the last {@link SmartError} if the state transitions to `ERROR` first.
|
|
91
|
+
*
|
|
92
|
+
* @param targetState - The state to wait for. Defaults to {@link SmartDbReadyState.READY}.
|
|
93
|
+
* @returns A promise that resolves to the reached {@link SmartDbReadyState}.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* await db.databaseReady();
|
|
98
|
+
* const rows = await db.getAll(MyModel);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
databaseReady(targetState?: SmartDbReadyState): Promise<SmartDbReadyState>;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the version information for all database modules that were initialized
|
|
104
|
+
* during {@link initDb}. Useful for diagnostics and schema migration auditing.
|
|
105
|
+
*/
|
|
106
|
+
getModuleVersions(): SmartDbVersionViewModel[];
|
|
107
|
+
/**
|
|
108
|
+
* Builds a SELECT SQL statement from the given model and options without executing it.
|
|
109
|
+
* The resulting {@link SmartDbSqlBuildData} contains the SQL string and bound parameter values.
|
|
110
|
+
* Also available via {@link getLastBuildData} after any query.
|
|
111
|
+
*
|
|
112
|
+
* @param modelClass - A model class (extends {@link AbstractModel}) or a raw table-name string.
|
|
113
|
+
* @param options - Query options: fields, WHERE, ORDER BY, GROUP BY, LIMIT, UNION/INTERSECT/MINUS, etc.
|
|
114
|
+
*/
|
|
115
|
+
buildSelectStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): SmartDbSqlBuildData;
|
|
116
|
+
/** Closes the database connection synchronously. Only supported by drivers that implement synchronous calls. */
|
|
24
117
|
closeSync(): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Commits the current transaction asynchronously.
|
|
120
|
+
* Delegates to {@link commitSync} — override in the driver if async commit is needed.
|
|
121
|
+
*/
|
|
25
122
|
commit(): Promise<void>;
|
|
123
|
+
/** Commits the current transaction synchronously. Throws if not implemented by the driver. */
|
|
26
124
|
commitSync(): void;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Deletes rows from the table associated with `modelClass` that match `where`.
|
|
127
|
+
* Omitting `where` deletes all rows.
|
|
128
|
+
*
|
|
129
|
+
* @param modelClass - A model class or raw table-name string.
|
|
130
|
+
* @param where - Optional WHERE clause. See {@link SqlWhere} for filter syntax.
|
|
131
|
+
* @returns The number of rows deleted.
|
|
132
|
+
* @throws {@link SmartError} if the statement fails.
|
|
133
|
+
*/
|
|
134
|
+
delete<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere): Promise<number>;
|
|
135
|
+
/**
|
|
136
|
+
* Synchronous variant of {@link delete}. Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
137
|
+
*
|
|
138
|
+
* @param modelClass - A model class or raw table-name string.
|
|
139
|
+
* @param where - Optional WHERE clause.
|
|
140
|
+
* @returns The number of rows deleted, or `false` if an error occurred.
|
|
141
|
+
* @throws A string error if the driver does not support sync calls.
|
|
142
|
+
*/
|
|
143
|
+
deleteSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere): number | false;
|
|
144
|
+
/**
|
|
145
|
+
* Executes a raw SQL statement asynchronously (no result rows returned).
|
|
146
|
+
* Delegates to {@link execSync} — override in the driver for native async execution.
|
|
147
|
+
*
|
|
148
|
+
* @param script - The SQL statement to execute.
|
|
149
|
+
* @param params - Optional positional parameters bound to `?` placeholders.
|
|
150
|
+
*/
|
|
151
|
+
exec(script: string, params?: SqlValueType[]): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Executes a multi-statement SQL script asynchronously, splitting on statement boundaries.
|
|
154
|
+
* Wraps execution in a transaction: rolls back and throws on error (unless the failing
|
|
155
|
+
* statement is a `DROP` and `ignoreDropError` is `true`).
|
|
156
|
+
*
|
|
157
|
+
* @param script - One or more SQL statements separated by statement terminators.
|
|
158
|
+
* @param ignoreDropError - When `true`, errors from `DROP` statements are silently skipped.
|
|
159
|
+
* @throws {@link SmartError} on the first non-DROP failure, after rolling back.
|
|
160
|
+
*/
|
|
30
161
|
execScript(script: string, ignoreDropError?: boolean): Promise<void>;
|
|
162
|
+
/**
|
|
163
|
+
* Synchronous variant of {@link execScript}. Only available when {@link supportSyncCalls} is `true`.
|
|
164
|
+
* Errors from `DROP` statements are silently ignored; all other errors are re-thrown.
|
|
165
|
+
*
|
|
166
|
+
* @param script - One or more SQL statements separated by statement terminators.
|
|
167
|
+
* @throws A string error if the driver does not support sync calls.
|
|
168
|
+
*/
|
|
31
169
|
execScriptSync(script: string): void;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Executes a raw SQL statement synchronously. Throws if not implemented by the driver.
|
|
172
|
+
*
|
|
173
|
+
* @param script - The SQL statement to execute.
|
|
174
|
+
* @param params - Optional positional parameters bound to `?` placeholders.
|
|
175
|
+
* @returns `true` on success.
|
|
176
|
+
*/
|
|
177
|
+
execSync(script: string, params?: SqlValueType[]): boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Executes a raw SQL query asynchronously and returns all result rows.
|
|
180
|
+
* Delegates to {@link querySync} — override in the driver for native async execution.
|
|
181
|
+
*
|
|
182
|
+
* @param script - A SQL SELECT (or other query) statement.
|
|
183
|
+
* @param params - Optional positional parameters bound to `?` placeholders.
|
|
184
|
+
* @returns An array of plain objects keyed by column name.
|
|
185
|
+
*/
|
|
186
|
+
query(script: string, params?: SqlValueType[]): Promise<GenericModelData[]>;
|
|
187
|
+
/**
|
|
188
|
+
* Executes a raw SQL query synchronously and returns all result rows.
|
|
189
|
+
* Throws if not implemented by the driver.
|
|
190
|
+
*
|
|
191
|
+
* @param script - A SQL SELECT (or other query) statement.
|
|
192
|
+
* @param params - Optional positional parameters bound to `?` placeholders.
|
|
193
|
+
* @returns An array of plain objects keyed by column name.
|
|
194
|
+
*/
|
|
195
|
+
querySync(script: string, params?: SqlValueType[]): GenericModelData[];
|
|
196
|
+
/**
|
|
197
|
+
* Checks whether a table, view, or index exists in the database asynchronously.
|
|
198
|
+
* Delegates to {@link existsSync}.
|
|
199
|
+
*
|
|
200
|
+
* @param modelClass - A model class or raw table/view name string.
|
|
201
|
+
* @param type - Optional object type to check: `"table"`, `"view"`, or `"index"`.
|
|
202
|
+
* @param indexTableName - For `"index"` checks, the parent table name.
|
|
203
|
+
* @returns `true` if the object exists.
|
|
204
|
+
*/
|
|
205
|
+
exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
|
|
206
|
+
/**
|
|
207
|
+
* Synchronous variant of {@link exists}. Throws if not implemented by the driver.
|
|
208
|
+
*
|
|
209
|
+
* @param modelClass - A model class or raw table/view name string.
|
|
210
|
+
* @param type - Optional object type to check: `"table"`, `"view"`, or `"index"`.
|
|
211
|
+
* @param indexTableName - For `"index"` checks, the parent table name.
|
|
212
|
+
* @returns `true` if the object exists.
|
|
213
|
+
*/
|
|
214
|
+
existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, type?: "view" | "table" | "index", indexTableName?: string): boolean;
|
|
215
|
+
/**
|
|
216
|
+
* The primary async query method. Executes a SELECT statement built from `options` and returns
|
|
217
|
+
* the results in one of several shapes depending on the options provided:
|
|
218
|
+
*
|
|
219
|
+
* - Default: an array of model instances (`T[]`).
|
|
220
|
+
* - `options.firstOnly = true`: a single model instance (`T`), or `false` if not found.
|
|
221
|
+
* - `options.count = true`: a numeric count value.
|
|
222
|
+
* - `options.style = FieldNamingStyle.Database`: plain objects with raw column names.
|
|
223
|
+
*
|
|
224
|
+
* For simpler call sites consider the convenience methods {@link getAll}, {@link getFirst},
|
|
225
|
+
* {@link getAllWithOptions}, and {@link getFirstWithOptionsSync}.
|
|
226
|
+
*
|
|
227
|
+
* @param modelClass - A model class or raw table-name string.
|
|
228
|
+
* @param options - Full query options ({@link SmartDbSqlOptions}).
|
|
229
|
+
*/
|
|
230
|
+
get<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): Promise<(T | D)[] | T | D | SqlValueType | IndexedGenericModelData<T, D> | string | string[] | false>;
|
|
231
|
+
/**
|
|
232
|
+
* Async convenience wrapper for {@link get} with `firstOnly: true`.
|
|
233
|
+
* Returns the first matching row as a typed model instance.
|
|
234
|
+
*
|
|
235
|
+
* @param modelClass - A model class or raw table-name string.
|
|
236
|
+
* @param options - Query options; `firstOnly` is forced to `true`.
|
|
237
|
+
* @returns The first matching model instance.
|
|
238
|
+
*/
|
|
239
|
+
async<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): Promise<T>;
|
|
240
|
+
/**
|
|
241
|
+
* Synchronous convenience wrapper for {@link getSync} with `firstOnly: true`.
|
|
242
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
243
|
+
*
|
|
244
|
+
* @param modelClass - A model class or raw table-name string.
|
|
245
|
+
* @param options - Query options; `firstOnly` is forced to `true`.
|
|
246
|
+
* @returns The first matching model instance, or `false` if none found.
|
|
247
|
+
*/
|
|
248
|
+
getFirstWithOptionsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): T | false;
|
|
249
|
+
/**
|
|
250
|
+
* Async convenience wrapper for {@link get} that always returns a typed array.
|
|
251
|
+
*
|
|
252
|
+
* @param modelClass - A model class or raw table-name string.
|
|
253
|
+
* @param options - Full query options ({@link SmartDbSqlOptions}).
|
|
254
|
+
* @returns An array of model instances.
|
|
255
|
+
*/
|
|
256
|
+
getAllWithOptions<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): Promise<T[]>;
|
|
257
|
+
/**
|
|
258
|
+
* Synchronous convenience wrapper for {@link getSync} that always returns a typed array.
|
|
259
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
260
|
+
*
|
|
261
|
+
* @param modelClass - A model class or raw table-name string.
|
|
262
|
+
* @param options - Full query options ({@link SmartDbSqlOptions}).
|
|
263
|
+
* @returns An array of model instances, or `false` if an error occurred.
|
|
264
|
+
*/
|
|
265
|
+
getAllWithOptionsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): T[] | false;
|
|
266
|
+
/**
|
|
267
|
+
* Retrieves all rows matching the given criteria as an array of typed model instances.
|
|
268
|
+
* A simpler alternative to {@link getAllWithOptions} for common query patterns.
|
|
269
|
+
*
|
|
270
|
+
* @param modelClass - A model class or raw table-name string.
|
|
271
|
+
* @param where - Optional WHERE clause ({@link SqlWhere}).
|
|
272
|
+
* @param fields - Optional field list to SELECT. Pass `null` or omit for all columns.
|
|
273
|
+
* @param orderBy - Optional ORDER BY clause (field name(s), optionally with `ASC`/`DESC`).
|
|
274
|
+
* @param limit - Optional LIMIT/OFFSET ({@link SqlLimit}).
|
|
275
|
+
* @returns An array of model instances.
|
|
276
|
+
*/
|
|
277
|
+
getAll<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): Promise<T[]>;
|
|
278
|
+
/**
|
|
279
|
+
* Synchronous variant of {@link getAll}.
|
|
280
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
281
|
+
*
|
|
282
|
+
* @param modelClass - A model class or raw table-name string.
|
|
283
|
+
* @param where - Optional WHERE clause.
|
|
284
|
+
* @param fields - Optional field list. Pass `null` or omit for all columns.
|
|
285
|
+
* @param orderBy - Optional ORDER BY clause.
|
|
286
|
+
* @param limit - Optional LIMIT/OFFSET.
|
|
287
|
+
* @returns An array of model instances, or `false` if an error occurred.
|
|
288
|
+
*/
|
|
289
|
+
getAllSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): T[] | false;
|
|
290
|
+
/** Returns the database connector (connection string or driver-specific connection object). */
|
|
39
291
|
getDbConnector(): string | SmartDbConnector;
|
|
292
|
+
/**
|
|
293
|
+
* Returns the quoting character used to delimit identifiers (column/table names) in SQL.
|
|
294
|
+
* Defaults to `"` (ANSI SQL); drivers may override this (e.g. MySQL uses `` ` ``).
|
|
295
|
+
*/
|
|
40
296
|
getDbQuote(): string;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
297
|
+
/**
|
|
298
|
+
* Retrieves the first row matching the given criteria as a typed model instance.
|
|
299
|
+
*
|
|
300
|
+
* @param modelClass - A model class or raw table-name string.
|
|
301
|
+
* @param where - Optional WHERE clause ({@link SqlWhere}).
|
|
302
|
+
* @param fields - Optional field list to SELECT.
|
|
303
|
+
* @param orderBy - Optional ORDER BY clause.
|
|
304
|
+
* @returns The first matching model instance.
|
|
305
|
+
*/
|
|
306
|
+
getFirst<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere, fields?: SqlFieldDescriptor | string | (SqlFieldDescriptor | string)[] | null, orderBy?: SqlOrderBy): Promise<T>;
|
|
307
|
+
/**
|
|
308
|
+
* Synchronous variant of {@link getFirst}.
|
|
309
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
310
|
+
*
|
|
311
|
+
* @param modelClass - A model class or raw table-name string.
|
|
312
|
+
* @param where - Optional WHERE clause.
|
|
313
|
+
* @param fields - Optional field list to SELECT.
|
|
314
|
+
* @param orderBy - Optional ORDER BY clause.
|
|
315
|
+
* @returns The first matching model instance, or `false` if none found or an error occurred.
|
|
316
|
+
*/
|
|
317
|
+
getFirstSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere, fields?: SqlFieldDescriptor | string | (SqlFieldDescriptor | string)[] | null, orderBy?: SqlOrderBy): T | false;
|
|
318
|
+
/**
|
|
319
|
+
* Returns the {@link SmartDbSqlBuildData} produced by the most recent query.
|
|
320
|
+
* Useful for debugging: inspect `buildData.sql` and `buildData.values` to see the
|
|
321
|
+
* exact SQL and parameters that were sent to the database.
|
|
322
|
+
*/
|
|
323
|
+
getLastBuildData<T extends AbstractModel<T, D>, D extends GenericModelData>(): SmartDbSqlBuildData;
|
|
324
|
+
/** Returns the last {@link SmartError} that occurred. Equivalent to reading the {@link lastError} getter. */
|
|
44
325
|
getLastError(): SmartError;
|
|
45
|
-
|
|
46
|
-
|
|
326
|
+
/** Returns the {@link SmartLog} logger instance used by this database connection. */
|
|
327
|
+
getLogger(): SmartLog;
|
|
328
|
+
/** Returns the current {@link ISmartLogDbWriter} used for database logging, or `null` if not active. */
|
|
329
|
+
getDbWriter(): ISmartLogDbWriter | null;
|
|
330
|
+
/**
|
|
331
|
+
* Checks whether the `smart_db_log` table exists and, if {@link SmartDbOptions.dbLogging} is enabled,
|
|
332
|
+
* installs a db writer on the logger. Called automatically during {@link initDb}.
|
|
333
|
+
*/
|
|
334
|
+
enableDbLogging(): Promise<void>;
|
|
335
|
+
/**
|
|
336
|
+
* The primary synchronous query method. Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
337
|
+
* Mirrors {@link get} but executes synchronously. See {@link get} for full option/return-type documentation.
|
|
338
|
+
*
|
|
339
|
+
* @param modelClass - A model class or raw table-name string.
|
|
340
|
+
* @param options - Full query options ({@link SmartDbSqlOptions}).
|
|
341
|
+
* @throws A string error if the driver does not support sync calls.
|
|
342
|
+
*/
|
|
343
|
+
getSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): (T | D)[] | T | D | SqlValueType | IndexedGenericModelData<T, D> | string | string[] | false;
|
|
344
|
+
/**
|
|
345
|
+
* Returns `true` if the driver supports concurrent (nested/savepoint) transactions.
|
|
346
|
+
* The base implementation always returns `false`; override in drivers that support it.
|
|
347
|
+
*/
|
|
47
348
|
hasConcurrentTransactions(): boolean;
|
|
349
|
+
/**
|
|
350
|
+
* Returns `true` if a transaction is currently active on this connection.
|
|
351
|
+
* Throws if not implemented by the driver.
|
|
352
|
+
*/
|
|
48
353
|
hasTransaction(): boolean;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Initializes the database: runs the upgrade manager to apply any pending SQL migration
|
|
356
|
+
* scripts for the `smart-db-core` module and any user-defined modules specified in
|
|
357
|
+
* {@link SmartDbOptions.module}. Called automatically from the constructor unless
|
|
358
|
+
* `delayInit: true` is set.
|
|
359
|
+
*
|
|
360
|
+
* @returns An array of {@link SmartDbVersionViewModel} entries — one per initialized module.
|
|
361
|
+
*/
|
|
362
|
+
initDb(): Promise<SmartDbVersionViewModel[]>;
|
|
363
|
+
/**
|
|
364
|
+
* Inserts a new row into the table associated with `modelClass`.
|
|
365
|
+
*
|
|
366
|
+
* @param modelClass - A model class or raw table-name string.
|
|
367
|
+
* @param values - A plain object of `{columnName: value}` pairs, or a model instance.
|
|
368
|
+
* When passing a model instance its DB-style plain object is extracted automatically.
|
|
369
|
+
* @returns The auto-generated primary key (last insert ID) of the new row.
|
|
370
|
+
* @throws {@link SmartError} if the statement fails.
|
|
371
|
+
*/
|
|
372
|
+
insert<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: SqlUpdateValues | T): Promise<number>;
|
|
373
|
+
/**
|
|
374
|
+
* Synchronous variant of {@link insert}.
|
|
375
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
376
|
+
*
|
|
377
|
+
* @param modelClass - A model class or raw table-name string.
|
|
378
|
+
* @param values - A plain object or model instance with the values to insert.
|
|
379
|
+
* @returns The last insert ID, or `false` if an error occurred.
|
|
380
|
+
* @throws A string error if the driver does not support sync calls.
|
|
381
|
+
*/
|
|
382
|
+
insertSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: SqlUpdateValues | T): number | false;
|
|
383
|
+
/**
|
|
384
|
+
* Rolls back the current transaction asynchronously.
|
|
385
|
+
* Delegates to {@link rollbackSync} — override in the driver if async rollback is needed.
|
|
386
|
+
*/
|
|
52
387
|
rollback(): Promise<void>;
|
|
388
|
+
/** Rolls back the current transaction synchronously. Throws if not implemented by the driver. */
|
|
53
389
|
rollbackSync(): void;
|
|
390
|
+
/**
|
|
391
|
+
* Converts a string, number (Unix ms), or `Date` to a JavaScript `Date`.
|
|
392
|
+
* Returns `null` for invalid or unparseable input.
|
|
393
|
+
*
|
|
394
|
+
* @param d - The value to convert.
|
|
395
|
+
*/
|
|
54
396
|
toDate(d: Date | number | string): Date | null;
|
|
397
|
+
/**
|
|
398
|
+
* Converts a JavaScript `Date` or Unix timestamp to a local-time database datetime string
|
|
399
|
+
* (`YYYY-MM-DD HH:MM:SS`). Use {@link AbstractModel.toDbDate} directly when zone control
|
|
400
|
+
* or millisecond precision is needed.
|
|
401
|
+
*
|
|
402
|
+
* @param d - The date to convert.
|
|
403
|
+
*/
|
|
55
404
|
toDbDate(d: Date | number): string;
|
|
405
|
+
/**
|
|
406
|
+
* Converts a JavaScript `Date` or Unix timestamp to a UTC-based ISO-like timestamp string
|
|
407
|
+
* (`YYYY-MM-DD HH:MM:SS.mmm`). Suitable for storing in `string`-typed columns when full
|
|
408
|
+
* precision is needed. For timezone-aware storage prefer {@link AbstractModel.toDbDate}.
|
|
409
|
+
*
|
|
410
|
+
* @param d - The date/time to convert.
|
|
411
|
+
*/
|
|
56
412
|
toDbTimestamp(d: Date | number): string;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Updates rows in the table associated with `modelClass`.
|
|
415
|
+
*
|
|
416
|
+
* @param modelClass - A model class or raw table-name string.
|
|
417
|
+
* @param values - A plain object or model instance with the column values to set.
|
|
418
|
+
* @param where - Optional WHERE clause to restrict which rows are updated.
|
|
419
|
+
* Omitting `where` updates all rows in the table.
|
|
420
|
+
* @returns The number of rows affected.
|
|
421
|
+
* @throws {@link SmartError} if the statement fails.
|
|
422
|
+
*/
|
|
423
|
+
update<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: SqlUpdateValues | T, where?: SqlWhere): Promise<number>;
|
|
424
|
+
/**
|
|
425
|
+
* Synchronous variant of {@link update}.
|
|
426
|
+
* Only available when {@link supportSyncCalls} is `true` (SQLite driver).
|
|
427
|
+
*
|
|
428
|
+
* @param modelClass - A model class or raw table-name string.
|
|
429
|
+
* @param values - A plain object or model instance with the column values to set.
|
|
430
|
+
* @param where - Optional WHERE clause.
|
|
431
|
+
* @returns The number of rows affected, or `false` if an error occurred.
|
|
432
|
+
* @throws A string error if the driver does not support sync calls.
|
|
433
|
+
*/
|
|
434
|
+
updateSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: SqlUpdateValues | T, where?: SqlWhere): number | false;
|
|
435
|
+
/**
|
|
436
|
+
* The active date/time conversion mode for this connection.
|
|
437
|
+
* Defaults to `"rule"` when not set in {@link SmartDbOptions}.
|
|
438
|
+
* See {@link SmartDbDateTimeMode} for the effect of each mode.
|
|
439
|
+
*/
|
|
440
|
+
get dateTimeMode(): SmartDbDateTimeMode;
|
|
441
|
+
/**
|
|
442
|
+
* Converts a value to its database representation using the column's type metadata.
|
|
443
|
+
* Delegates to {@link AbstractModel.makeDbValue}; drivers may override to inject
|
|
444
|
+
* the connection's {@link dateTimeMode}.
|
|
445
|
+
*
|
|
446
|
+
* @param value - The JavaScript value to convert.
|
|
447
|
+
* @param options - Optional type hints: `dbFullType` and/or `dateTimeMode`.
|
|
448
|
+
*/
|
|
449
|
+
makeDbValue(value: SqlValueType, options?: MakeDbValueOptions): SqlValueType;
|
|
450
|
+
protected buildDeleteStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where?: SqlWhere): SmartDbSqlBuildData;
|
|
451
|
+
protected buildFieldList<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, fields: (SqlValueType | SqlFieldDescriptor)[]): string[];
|
|
452
|
+
protected buildInsertStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: SqlUpdateValues | T): SmartDbSqlBuildData;
|
|
453
|
+
protected buildSelectSectionStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, options: SmartDbSqlOptions<T, D>): SmartDbSqlBuildData;
|
|
454
|
+
protected buildUpdateStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, values: D | T, where?: SqlWhere): SmartDbSqlBuildData;
|
|
455
|
+
protected buildWhere<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, where: SqlWhere, op?: string): SmartDbSqlBuildData;
|
|
456
|
+
protected makeArgumentDbValue(value: SqlValueType, rawType?: string): SqlValueType;
|
|
457
|
+
protected getJsType(rawType?: string): SqlValueTypes;
|
|
458
|
+
protected prepareField<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, field: SqlValueType | SqlFieldDescriptor, values?: SqlValueType[]): string;
|
|
459
|
+
protected prepareFieldValue<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, fieldOperation: SqlFieldDescriptor): string;
|
|
460
|
+
protected saveExecute<T>(fn: () => T): T | false;
|
|
61
461
|
protected statementGet<T extends object>(buildData: SmartDbSqlBuildData): Promise<T>;
|
|
62
|
-
protected statementGetSync(buildData: SmartDbSqlBuildData): any;
|
|
63
462
|
protected statementGetAll<T>(buildData: SmartDbSqlBuildData): Promise<T[]>;
|
|
64
463
|
protected statementGetAllSync(buildData: SmartDbSqlBuildData): any[];
|
|
65
|
-
protected
|
|
66
|
-
protected
|
|
67
|
-
protected
|
|
68
|
-
protected
|
|
69
|
-
protected
|
|
70
|
-
protected
|
|
71
|
-
protected
|
|
72
|
-
protected
|
|
73
|
-
protected
|
|
74
|
-
protected
|
|
75
|
-
protected makeArgumentDbValue(value: SqlValueType): SqlValueType;
|
|
76
|
-
protected saveExecute<T>(fn: () => T): T | false;
|
|
464
|
+
protected statementGetSync(buildData: SmartDbSqlBuildData): any;
|
|
465
|
+
protected statementRun(buildData: SmartDbSqlBuildData): Promise<SmartDbRunResult>;
|
|
466
|
+
protected statementRunSync(buildData: SmartDbSqlBuildData): SmartDbRunResult;
|
|
467
|
+
protected getFieldInfo<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, field: string): AttributeInfo;
|
|
468
|
+
protected translateFieldName<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, field: string): string;
|
|
469
|
+
protected getFieldType<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, field: string): string;
|
|
470
|
+
protected getFieldFullType<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>, field: string): string;
|
|
471
|
+
protected getTableClass<T extends AbstractModel<T, D>, D extends GenericModelData>(model: string | ModelClass<T>): ModelClass<T>;
|
|
472
|
+
protected getPkSequenceName<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>): string;
|
|
473
|
+
protected getTableName<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | ModelClass<T>): string;
|
|
77
474
|
private scriptParser;
|
|
78
475
|
private prepareResultRow;
|
|
79
476
|
private prepareResultRows;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
get supportSyncCalls(): boolean;
|
|
477
|
+
/**
|
|
478
|
+
* Closes the database connection and cancels any pending ready-state subscriptions.
|
|
479
|
+
* Sets {@link readyState} to {@link SmartDbReadyState.CLOSED} then delegates to {@link closeSync}.
|
|
480
|
+
* @returns `true` if the connection was closed successfully.
|
|
481
|
+
*/
|
|
482
|
+
close(): Promise<boolean>;
|
|
87
483
|
}
|