masterrecord 1.2.6 → 1.2.8
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/CHANGES.md +21 -0
- package/SQLLiteEngine.js +30 -0
- package/context.js +77 -4
- package/docs/TROUBLESHOOTING.md +16 -1
- package/mySQLEngine.js +20 -0
- package/package.json +1 -1
- package/postgresEngine.js +21 -0
- package/readme.md +16 -13
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.2.8 — batched-insert auto-increment PK handling
|
|
4
|
+
|
|
5
|
+
**Bug:** the batched-insert path could fail with `Type mismatch for <Entity>.id: Expected integer, got function` for an unset auto-increment primary key, then fall back to individual inserts (which handled it). The INSERT builder (`_buildSQLInsertObjectParameterized`, shared by single + batch inserts) only skipped the auto-PK when its value was `undefined`/`null`. When the unset PK surfaced as its **schema-definition function** (`id(db){…}`, e.g. when the row is a class instance rather than a `.new()` data instance), the builder fell through to the type validator and threw.
|
|
6
|
+
|
|
7
|
+
**Fix (all three engines):** an auto-increment primary key is database-assigned and is now **never emitted in the INSERT unless the caller set an explicit value** — "unset" is recognized whether it surfaces as `undefined`, `null`, or a function. A function value is treated as never-valid for any column (a leaked schema method/getter) and skipped. Single- and batched-insert paths share the builder, so both behave identically; an explicitly-set PK value is still honored.
|
|
8
|
+
|
|
9
|
+
New test: `test/bulk-insert-autopk.test.js` — builder excludes a function-valued auto-PK (and an `undefined` one), honors an explicit value, and an end-to-end `bulkCreate` assigns auto IDs across a batch with no fallback. Verified on SQLite; the MySQL/Postgres builders received the identical change.
|
|
10
|
+
|
|
11
|
+
## v1.2.7 — public engine-agnostic raw-SQL escape hatch (`ctx.query` / `ctx.execute`)
|
|
12
|
+
|
|
13
|
+
Adds a public, portable raw-SQL API so apps stop reaching into `ctx.db` (the raw, engine-specific driver). `ctx.db.prepare()` is better-sqlite3's synchronous API; mysql2/pg have no `.prepare()`, so code written against `ctx.db` "works on SQLite, breaks on MySQL." The only engine-agnostic path was the private `context._execute` (the migration/DDL path, which logs as a migration and doesn't return rows on SQLite).
|
|
14
|
+
|
|
15
|
+
- **`ctx.query(sql, params)`** (alias **`ctx.execute(sql, params)`**) — runs raw SQL on SQLite/MySQL/Postgres and returns an **array of row objects** for row-returning statements; executes writes (returns the driver's write result). Placeholders are engine-native (`?` for SQLite/MySQL, `$1,$2,…` for Postgres). Each engine gained a matching `query()` method (SQLite uses `stmt.reader` to pick `all()` vs `run()`; pg normalizes `result.rows`).
|
|
16
|
+
- **DX guard (loud-failure parity):** on MySQL/Postgres, `ctx.db.prepare()` (and `.pragma()`) now throw a descriptive error pointing to `ctx.query()` instead of the generic "is not a function". `ctx.db` is never used internally (the engines hold their own pool), so this only affects user-facing access; every real driver method is forwarded untouched. SQLite's `ctx.db` is the real better-sqlite3 handle and is unaffected.
|
|
17
|
+
|
|
18
|
+
This closes the entire "works on SQLite, breaks on MySQL" class for legitimate raw-SQL cases (e.g. cross-context FK updates the query builder can't express). Prefer the ORM where possible; `ctx.query()` is the escape hatch.
|
|
19
|
+
|
|
20
|
+
New test: `test/raw-query-escape-hatch.test.js`. Docs: README "Raw SQL Queries" + API reference updated; TROUBLESHOOTING gains a "`ctx.db.prepare is not a function`" entry. Suite green; 0 lint errors.
|
|
21
|
+
|
|
22
|
+
> Verified executing on SQLite (rows/writes/params). The MySQL/Postgres `query()` and the `ctx.db` guard share the same code shape but aren't run against live servers here.
|
|
23
|
+
|
|
3
24
|
## v1.2.6 — idempotent addColumn (skip-if-exists)
|
|
4
25
|
|
|
5
26
|
`schema.addColumn` now **skips if the column already exists**, symmetric with `dropColumn`'s skip-if-gone and `createTable`'s `IF NOT EXISTS`. None of SQLite/MySQL support `ADD COLUMN IF NOT EXISTS`, so it probes the live schema via `getTableInfo` (which throws on a real introspection error — so a genuine failure still aborts loudly; only a clean "already present" result short-circuits, with a `[masterrecord:migration]` skip log).
|
package/SQLLiteEngine.js
CHANGED
|
@@ -1212,6 +1212,22 @@ class SQLLiteEngine {
|
|
|
1212
1212
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
1213
1213
|
}
|
|
1214
1214
|
|
|
1215
|
+
// Auto-increment primary keys are assigned by the database, so
|
|
1216
|
+
// they must never be emitted in the INSERT unless the caller
|
|
1217
|
+
// set an explicit value. An unset auto PK surfaces either as
|
|
1218
|
+
// undefined/null (an unread `.new()` getter) or as the schema-
|
|
1219
|
+
// definition function `id(db){…}` (when the row is a class
|
|
1220
|
+
// instance). A function is also never a valid value for any
|
|
1221
|
+
// column. Skip in those cases so the batched-insert path
|
|
1222
|
+
// behaves like the single-insert path instead of failing type
|
|
1223
|
+
// validation with "Expected integer, got function".
|
|
1224
|
+
const _columnDef = modelEntity[column];
|
|
1225
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
1226
|
+
if (typeof fieldColumn === 'function' ||
|
|
1227
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1215
1231
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
1216
1232
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
1217
1233
|
// objects into scalars (e.g. JSON.stringify), so running them
|
|
@@ -1342,6 +1358,20 @@ class SQLLiteEngine {
|
|
|
1342
1358
|
return this.db.prepare(query).run(...params);
|
|
1343
1359
|
}
|
|
1344
1360
|
|
|
1361
|
+
// Engine-agnostic raw query backing the public ctx.query()/ctx.execute().
|
|
1362
|
+
// Returns an array of rows for row-returning statements (SELECT / PRAGMA /
|
|
1363
|
+
// RETURNING); for writes returns better-sqlite3's run info
|
|
1364
|
+
// ({ changes, lastInsertRowid }). `stmt.reader` tells the two apart.
|
|
1365
|
+
query(query, params = []){
|
|
1366
|
+
if (process.env.LOG_SQL === 'true' || process.env.NODE_ENV !== 'production') {
|
|
1367
|
+
console.debug("[SQL]", query);
|
|
1368
|
+
if (params && params.length) console.debug("[Params]", params);
|
|
1369
|
+
}
|
|
1370
|
+
const stmt = this.db.prepare(query);
|
|
1371
|
+
const args = Array.isArray(params) ? params : (params === undefined ? [] : [params]);
|
|
1372
|
+
return stmt.reader ? stmt.all(...args) : stmt.run(...args);
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1345
1375
|
setDB(db, type){
|
|
1346
1376
|
this.db = db;
|
|
1347
1377
|
this.dbType = type; // this will let us know which type of sqlengine to use.
|
package/context.js
CHANGED
|
@@ -887,8 +887,8 @@ class context {
|
|
|
887
887
|
// MySQL is async - caller must await env()
|
|
888
888
|
// Store promise so migration schema can await it
|
|
889
889
|
this._initPromise = (async () => {
|
|
890
|
-
this.db = await this.__mysqlInit(options, 'mysql2');
|
|
891
|
-
// Note: engine is already set in __mysqlInit
|
|
890
|
+
this.db = this.#guardRawDriverHandle(await this.__mysqlInit(options, 'mysql2'), 'MySQL');
|
|
891
|
+
// Note: engine is already set in __mysqlInit (uses the real pool)
|
|
892
892
|
return this;
|
|
893
893
|
})();
|
|
894
894
|
// Prevent unhandled rejection crash — _ensureReady() will re-throw on query
|
|
@@ -910,8 +910,8 @@ class context {
|
|
|
910
910
|
// PostgreSQL is async - caller must await env()
|
|
911
911
|
// Store promise so migration schema can await it
|
|
912
912
|
this._initPromise = (async () => {
|
|
913
|
-
this.db = await this.__postgresInit(options, 'pg');
|
|
914
|
-
// Note: engine is already set in __postgresInit
|
|
913
|
+
this.db = this.#guardRawDriverHandle(await this.__postgresInit(options, 'pg'), 'PostgreSQL');
|
|
914
|
+
// Note: engine is already set in __postgresInit (uses the real pool)
|
|
915
915
|
return this;
|
|
916
916
|
})();
|
|
917
917
|
// Prevent unhandled rejection crash — _ensureReady() will re-throw on query
|
|
@@ -2004,6 +2004,79 @@ class context {
|
|
|
2004
2004
|
return this._SQLEngine._execute(query);
|
|
2005
2005
|
}
|
|
2006
2006
|
|
|
2007
|
+
/**
|
|
2008
|
+
* Engine-agnostic raw-SQL escape hatch.
|
|
2009
|
+
*
|
|
2010
|
+
* Prefer the ORM (`ctx.Model.where(...).toList()`, `.add()`,
|
|
2011
|
+
* `saveChanges()`) — it's portable across SQLite/MySQL/Postgres. Use this
|
|
2012
|
+
* only for SQL the query builder can't express. Unlike reaching into
|
|
2013
|
+
* `ctx.db` (which is the raw, engine-specific driver — e.g. better-sqlite3's
|
|
2014
|
+
* synchronous `prepare()`, which doesn't exist on mysql2/pg), `query()`
|
|
2015
|
+
* works the same on every engine.
|
|
2016
|
+
*
|
|
2017
|
+
* Returns an array of row objects for row-returning statements
|
|
2018
|
+
* (SELECT / RETURNING) on all three engines. For write statements
|
|
2019
|
+
* (INSERT/UPDATE/DELETE/DDL) it executes and returns the driver's write
|
|
2020
|
+
* result (shape varies by engine — prefer the ORM when you need a portable
|
|
2021
|
+
* result). Use `?` placeholders for SQLite/MySQL and `$1,$2,…` for Postgres.
|
|
2022
|
+
*
|
|
2023
|
+
* @param {string} sql - SQL with parameter placeholders
|
|
2024
|
+
* @param {Array} [params] - Bind values
|
|
2025
|
+
* @returns {Promise<Array<object>|*>}
|
|
2026
|
+
*
|
|
2027
|
+
* @example
|
|
2028
|
+
* const rows = await ctx.query('SELECT * FROM "User" WHERE age > $1', [25]); // pg
|
|
2029
|
+
* await ctx.execute('UPDATE Step SET run_id = ? WHERE id = ?', ['run_x', 1]); // sqlite/mysql
|
|
2030
|
+
*/
|
|
2031
|
+
async query(sql, params = []) {
|
|
2032
|
+
if (this._initPromise) { await this._initPromise; } // MySQL/Postgres async init
|
|
2033
|
+
if (!this._SQLEngine) {
|
|
2034
|
+
throw new DatabaseConnectionError(
|
|
2035
|
+
'Cannot run query: database engine not initialized. Ensure you have awaited env() before querying.',
|
|
2036
|
+
this.isMySQL ? 'MySQL' : this.isPostgres ? 'PostgreSQL' : 'SQLite'
|
|
2037
|
+
);
|
|
2038
|
+
}
|
|
2039
|
+
return this._SQLEngine.query(sql, params || []);
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
/**
|
|
2043
|
+
* Alias of {@link query} — read naturally for write statements
|
|
2044
|
+
* (`await ctx.execute('UPDATE …', [...])`). Both run raw SQL on any engine.
|
|
2045
|
+
*/
|
|
2046
|
+
async execute(sql, params = []) {
|
|
2047
|
+
return this.query(sql, params);
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
/**
|
|
2051
|
+
* Wrap a non-SQLite raw driver handle (mysql2 / pg pool) so that calling
|
|
2052
|
+
* a SQLite-only method on `ctx.db` fails with guidance instead of the
|
|
2053
|
+
* generic "X is not a function". Apps commonly reach for
|
|
2054
|
+
* `ctx.db.prepare()` (better-sqlite3's API); on MySQL/Postgres `ctx.db`
|
|
2055
|
+
* is the raw driver/pool and has no such method.
|
|
2056
|
+
*
|
|
2057
|
+
* The Proxy forwards every real pool method (bound to the real pool, so
|
|
2058
|
+
* driver internals keep working) and only intercepts the SQLite-specific
|
|
2059
|
+
* names. `ctx.db` is never used internally — the engines hold their own
|
|
2060
|
+
* pool reference — so this only affects user-facing access.
|
|
2061
|
+
* @private
|
|
2062
|
+
*/
|
|
2063
|
+
#guardRawDriverHandle(pool, engineLabel) {
|
|
2064
|
+
if (!pool || typeof pool !== 'object') return pool;
|
|
2065
|
+
const sqliteOnly = new Set(['prepare', 'pragma']);
|
|
2066
|
+
return new Proxy(pool, {
|
|
2067
|
+
get(target, prop) {
|
|
2068
|
+
if (typeof prop === 'string' && sqliteOnly.has(prop)) {
|
|
2069
|
+
throw new Error(
|
|
2070
|
+
`masterrecord: ctx.db on ${engineLabel} is the raw ${engineLabel} driver, which has no .${prop}() — that's SQLite-only. ` +
|
|
2071
|
+
`Use the engine-agnostic ctx.query(sql, params) / ctx.execute(sql, params), or the ORM (ctx.Model.where()…).`
|
|
2072
|
+
);
|
|
2073
|
+
}
|
|
2074
|
+
const value = target[prop];
|
|
2075
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
2076
|
+
}
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
2079
|
+
|
|
2007
2080
|
/**
|
|
2008
2081
|
* Get query cache statistics
|
|
2009
2082
|
*
|
package/docs/TROUBLESHOOTING.md
CHANGED
|
@@ -305,4 +305,19 @@ step.created_at = String(Date.now()); // ← stored as a plain JS prop, INSERT
|
|
|
305
305
|
|
|
306
306
|
**Fix:** restart the Node process. There is no library-level fix; this is how Node module loading works.
|
|
307
307
|
|
|
308
|
-
Until restart, the workaround is to set the field via raw
|
|
308
|
+
Until restart, the workaround is to set the field via the engine-agnostic raw escape hatch — `await ctx.execute('INSERT INTO Step (...) VALUES (?, ?)', [...])` (works on every engine) — or, for fields that exist in the DB but not the class definition, lazy-load the entity (which iterates the DB row's actual columns and creates setters for them on the instance, even if the class doesn't declare them).
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
## `TypeError: ctx.db.prepare is not a function` (MySQL / Postgres)
|
|
313
|
+
|
|
314
|
+
`ctx.db` is the **raw, engine-specific driver**. On SQLite it's a better-sqlite3 database (which has `.prepare()/.get()/.run()/.all()`); on MySQL it's a mysql2 pool and on Postgres a pg pool — neither has `.prepare()`. Code written against `ctx.db.prepare(...)` therefore works on SQLite and throws on MySQL/Postgres.
|
|
315
|
+
|
|
316
|
+
**Fix:** use the engine-agnostic raw-SQL escape hatch (since v1.2.7):
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
const rows = await ctx.query('SELECT * FROM "User" WHERE id = $1', [id]); // returns rows, all engines
|
|
320
|
+
await ctx.execute('UPDATE "User" SET name = ? WHERE id = ?', ['Alice', id]);
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Better still, prefer the ORM (`ctx.User.where(u => u.id == $$, id).single()`), which is portable by construction. On a non-SQLite engine, `ctx.db.prepare()` now throws a descriptive error pointing you to `ctx.query()`.
|
package/mySQLEngine.js
CHANGED
|
@@ -233,6 +233,13 @@ class MySQLEngine {
|
|
|
233
233
|
return await this._runWithParams(query, params);
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
+
// Engine-agnostic raw query backing the public ctx.query()/ctx.execute().
|
|
237
|
+
// mysql2 returns an array of rows for SELECT and a ResultSetHeader for
|
|
238
|
+
// writes.
|
|
239
|
+
async query(query, params = []) {
|
|
240
|
+
return await this._runWithParams(query, params);
|
|
241
|
+
}
|
|
242
|
+
|
|
236
243
|
/**
|
|
237
244
|
* Introspection: Check if table exists
|
|
238
245
|
*/
|
|
@@ -856,6 +863,19 @@ class MySQLEngine {
|
|
|
856
863
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
857
864
|
}
|
|
858
865
|
|
|
866
|
+
// Auto-increment PKs are DB-assigned — never emit an unset one.
|
|
867
|
+
// "Unset" surfaces as undefined/null (a .new() getter) or as the
|
|
868
|
+
// schema-definition function `id(db){…}` (a class instance); a
|
|
869
|
+
// function is never a valid column value. This keeps the batched
|
|
870
|
+
// multi-row INSERT consistent with the single-insert path
|
|
871
|
+
// (otherwise it threw "Expected integer, got function").
|
|
872
|
+
const _columnDef = modelEntity[column];
|
|
873
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
874
|
+
if (typeof fieldColumn === 'function' ||
|
|
875
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
|
|
859
879
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
860
880
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
861
881
|
// objects into scalars (e.g. JSON.stringify) so running them
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|
package/postgresEngine.js
CHANGED
|
@@ -275,6 +275,14 @@ class postgresEngine {
|
|
|
275
275
|
return await this._runWithParams(query, params);
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
+
// Engine-agnostic raw query backing the public ctx.query()/ctx.execute().
|
|
279
|
+
// Normalizes pg's result object to an array of rows for row-returning
|
|
280
|
+
// statements; for writes returns the driver result.
|
|
281
|
+
async query(query, params = []) {
|
|
282
|
+
const result = await this._runWithParams(query, params);
|
|
283
|
+
return (result && Array.isArray(result.rows)) ? result.rows : result;
|
|
284
|
+
}
|
|
285
|
+
|
|
278
286
|
/**
|
|
279
287
|
* Introspection: Check if a table exists in the current Postgres schema.
|
|
280
288
|
* Mirrors the SQLite/MySQL engines so migration code and the CLI's
|
|
@@ -820,6 +828,19 @@ class postgresEngine {
|
|
|
820
828
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
821
829
|
}
|
|
822
830
|
|
|
831
|
+
// Auto-increment PKs are DB-assigned — never emit an unset one.
|
|
832
|
+
// "Unset" surfaces as undefined/null (a .new() getter) or as the
|
|
833
|
+
// schema-definition function `id(db){…}` (a class instance); a
|
|
834
|
+
// function is never a valid column value. Keeps the batched
|
|
835
|
+
// multi-row INSERT consistent with the single-insert path
|
|
836
|
+
// (otherwise it threw "Expected integer, got function").
|
|
837
|
+
const _columnDef = modelEntity[column];
|
|
838
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
839
|
+
if (typeof fieldColumn === 'function' ||
|
|
840
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
|
|
823
844
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
824
845
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
825
846
|
// objects into scalars (e.g. JSON.stringify) so running them
|
package/readme.md
CHANGED
|
@@ -1368,26 +1368,25 @@ masterrecord update-database-all
|
|
|
1368
1368
|
|
|
1369
1369
|
### Raw SQL Queries
|
|
1370
1370
|
|
|
1371
|
-
|
|
1371
|
+
For SQL the query builder can't express, use the **engine-agnostic** escape hatch `db.query(sql, params)` (alias `db.execute(sql, params)`). It works the same on SQLite, MySQL, and PostgreSQL and returns an **array of row objects** for row-returning statements:
|
|
1372
1372
|
|
|
1373
1373
|
```javascript
|
|
1374
|
-
//
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
// PostgreSQL parameterized query
|
|
1379
|
-
const users = await db._SQLEngine.exec(
|
|
1380
|
-
'SELECT * FROM "User" WHERE age > $1 AND status = $2',
|
|
1374
|
+
// SELECT → array of rows (all engines)
|
|
1375
|
+
const users = await db.query(
|
|
1376
|
+
'SELECT * FROM "User" WHERE age > $1 AND status = $2', // Postgres: $1,$2
|
|
1381
1377
|
[25, 'active']
|
|
1382
1378
|
);
|
|
1379
|
+
// SQLite / MySQL use ? placeholders:
|
|
1380
|
+
const users = await db.query('SELECT * FROM User WHERE age > ? AND status = ?', [25, 'active']);
|
|
1383
1381
|
|
|
1384
|
-
//
|
|
1385
|
-
|
|
1386
|
-
'SELECT * FROM User WHERE age > ? AND status = ?',
|
|
1387
|
-
[25, 'active']
|
|
1388
|
-
);
|
|
1382
|
+
// Writes — reads naturally as execute() (works on every engine)
|
|
1383
|
+
await db.execute('UPDATE Step SET run_id = ? WHERE id = ?', ['run_x', 1]);
|
|
1389
1384
|
```
|
|
1390
1385
|
|
|
1386
|
+
> **Do not reach into `db.db`.** That is the *raw, engine-specific driver* — `db.db.prepare()` is better-sqlite3's synchronous API and does **not** exist on mysql2/pg, so code written against it works on SQLite and breaks on MySQL/Postgres. (On a non-SQLite engine, `db.db.prepare()` now throws a descriptive error pointing you here.) Prefer the ORM (`db.User.where(...).toList()`); use `db.query()` only when you must.
|
|
1387
|
+
|
|
1388
|
+
Placeholders are engine-native (`?` for SQLite/MySQL, `$1,$2,…` for Postgres) — `query()` passes them straight through, so a raw statement is as portable as the SQL you write. For fully portable code, prefer the query builder.
|
|
1389
|
+
|
|
1391
1390
|
## API Reference
|
|
1392
1391
|
|
|
1393
1392
|
### Context Methods
|
|
@@ -1410,6 +1409,10 @@ context.attach(entity, { field: value }) // Attach with specific changes
|
|
|
1410
1409
|
context.attachAll([entity1, entity2]) // Attach multiple entities
|
|
1411
1410
|
await context.update('Entity', id, changes) // Update by primary key
|
|
1412
1411
|
|
|
1412
|
+
// Engine-agnostic raw SQL (escape hatch — prefer the query builder)
|
|
1413
|
+
await context.query(sql, params) // raw SQL → array of rows (all engines)
|
|
1414
|
+
await context.execute(sql, params) // alias; reads naturally for writes
|
|
1415
|
+
|
|
1413
1416
|
// Cache management
|
|
1414
1417
|
context.getCacheStats() // Get cache statistics
|
|
1415
1418
|
context.clearQueryCache() // Clear all cached queries
|