uql-orm 0.93.1 → 0.94.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/browser/uql-browser.min.js +1 -1
  2. package/dist/browser/uql-browser.min.js.map +3 -3
  3. package/dist/dialect/abstractDialect.d.ts +2 -0
  4. package/dist/dialect/abstractDialect.js +4 -0
  5. package/dist/dialect/abstractSqlDialect.d.ts +11 -6
  6. package/dist/dialect/abstractSqlDialect.js +28 -11
  7. package/dist/dialect/mysqlLikeSqlDialect.d.ts +1 -0
  8. package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
  9. package/dist/dialect/pgLikeSqlDialect.d.ts +2 -0
  10. package/dist/dialect/pgLikeSqlDialect.js +4 -0
  11. package/dist/dialect/queryContext.d.ts +2 -0
  12. package/dist/dialect/queryContext.js +6 -0
  13. package/dist/maria/mariadbQuerier.d.ts +2 -1
  14. package/dist/maria/mariadbQuerier.js +2 -8
  15. package/dist/migrate/drift/driftDetector.js +7 -2
  16. package/dist/mongo/mongoDialect.d.ts +1 -0
  17. package/dist/mongo/mongoDialect.js +0 -1
  18. package/dist/mongo/mongodbQuerier.js +3 -10
  19. package/dist/mssql/mssqlDialect.d.ts +3 -2
  20. package/dist/mssql/mssqlDialect.js +3 -2
  21. package/dist/mssql/mssqlQuerier.d.ts +2 -2
  22. package/dist/mssql/mssqlQuerier.js +1 -3
  23. package/dist/mysql/mysql2Querier.d.ts +1 -1
  24. package/dist/mysql/mysql2Querier.js +1 -9
  25. package/dist/pglite/index.d.ts +1 -0
  26. package/dist/pglite/index.js +1 -0
  27. package/dist/pglite/pgliteDialect.d.ts +6 -0
  28. package/dist/pglite/pgliteDialect.js +6 -0
  29. package/dist/pglite/pgliteQuerierPool.d.ts +2 -2
  30. package/dist/pglite/pgliteQuerierPool.js +2 -2
  31. package/dist/postgres/pgQuerier.d.ts +2 -4
  32. package/dist/postgres/pgQuerier.js +1 -9
  33. package/dist/querier/abstractQuerier.d.ts +21 -3
  34. package/dist/querier/abstractQuerier.js +86 -17
  35. package/dist/querier/abstractQuerierPool.d.ts +1 -1
  36. package/dist/querier/abstractQuerierPool.js +1 -1
  37. package/dist/querier/abstractSqlQuerier.d.ts +15 -10
  38. package/dist/querier/abstractSqlQuerier.js +54 -57
  39. package/dist/querier/cursorStream.d.ts +1 -1
  40. package/dist/schema/sqlExpression.d.ts +4 -1
  41. package/dist/schema/sqlExpression.js +20 -2
  42. package/dist/sqlite/abstractSqliteQuerier.d.ts +1 -1
  43. package/dist/sqlite/sqliteDialect.d.ts +1 -0
  44. package/dist/sqlite/sqliteDialect.js +1 -0
  45. package/dist/type/entity.d.ts +4 -4
  46. package/dist/type/querier.d.ts +1 -1
  47. package/dist/type/universalQuerier.d.ts +1 -1
  48. package/dist/util/object.util.d.ts +2 -0
  49. package/dist/util/object.util.js +8 -0
  50. package/dist/util/query.util.d.ts +3 -1
  51. package/dist/util/query.util.js +7 -0
  52. package/dist/util/sql.util.d.ts +2 -4
  53. package/dist/util/sql.util.js +10 -19
  54. package/package.json +1 -1
  55. package/skills/uql-orm/SKILL.md +3 -1
@@ -3,6 +3,6 @@ import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
3
3
  export declare class MySql2Querier extends AbstractPoolQuerier<PoolConnection> {
4
4
  internalAll<T>(query: string, values?: unknown[]): Promise<T[]>;
5
5
  internalRun(query: string, values?: unknown[]): Promise<import("../index.js").QueryUpdateResult>;
6
- internalStream<T>(query: string, values?: unknown[]): AsyncGenerator<Awaited<T>, void, unknown>;
6
+ internalStream(query: string, values?: unknown[]): AsyncGenerator<any, void, any>;
7
7
  protected releaseConn(conn: PoolConnection, discard: boolean): Promise<void>;
8
8
  }
@@ -14,15 +14,7 @@ export class MySql2Querier extends AbstractPoolQuerier {
14
14
  }
15
15
  async *internalStream(query, values) {
16
16
  const rawConn = this.getConn().connection;
17
- const stream = rawConn.query(query, values).stream();
18
- try {
19
- for await (const row of stream) {
20
- yield row;
21
- }
22
- }
23
- finally {
24
- stream.destroy();
25
- }
17
+ yield* rawConn.query(query, values).stream();
26
18
  }
27
19
  async releaseConn(conn, discard) {
28
20
  // mysql2 resets nothing on release, so a connection the pool takes back after a failed rollback
@@ -1,2 +1,3 @@
1
+ export * from './pgliteDialect.js';
1
2
  export * from './pgliteQuerier.js';
2
3
  export * from './pgliteQuerierPool.js';
@@ -1,2 +1,3 @@
1
+ export * from './pgliteDialect.js';
1
2
  export * from './pgliteQuerier.js';
2
3
  export * from './pgliteQuerierPool.js';
@@ -0,0 +1,6 @@
1
+ import { PostgresDialect } from '../postgres/postgresDialect.js';
2
+ /** Postgres as PGlite runs it, which binds fewer values per statement than a server. */
3
+ export declare class PgliteDialect extends PostgresDialect {
4
+ /** Past 32767 PGlite answers no rows, and wrong ones from then on, where a server takes 65535. */
5
+ readonly maxBindValues: number;
6
+ }
@@ -0,0 +1,6 @@
1
+ import { PostgresDialect } from '../postgres/postgresDialect.js';
2
+ /** Postgres as PGlite runs it, which binds fewer values per statement than a server. */
3
+ export class PgliteDialect extends PostgresDialect {
4
+ /** Past 32767 PGlite answers no rows, and wrong ones from then on, where a server takes 65535. */
5
+ maxBindValues = 32767;
6
+ }
@@ -1,7 +1,7 @@
1
1
  import type { PGliteOptions } from '@electric-sql/pglite';
2
- import { PostgresDialect } from '../postgres/postgresDialect.js';
3
2
  import { AbstractSharedHandleQuerierPool } from '../querier/abstractSharedHandleQuerierPool.js';
4
3
  import type { ExtraOptions } from '../type/index.js';
4
+ import { PgliteDialect } from './pgliteDialect.js';
5
5
  import { type PgliteDatabase, PgliteQuerier } from './pgliteQuerier.js';
6
6
  /** PGlite's own options but `dataDir`, the pool's first argument; `extensions: { vector }` enables pgvector. Type-only. */
7
7
  export type PglitePoolOptions = Omit<PGliteOptions, 'dataDir'>;
@@ -10,7 +10,7 @@ export type PglitePoolOptions = Omit<PGliteOptions, 'dataDir'>;
10
10
  * the open transaction, so a unit of work needing its own needs its own pool. Transactions are plain
11
11
  * statements, so pass `relaxedDurability` on a persistent `dataDir` to skip a flush per statement.
12
12
  */
13
- export declare class PgliteQuerierPool extends AbstractSharedHandleQuerierPool<PgliteDatabase, PgliteQuerier, PostgresDialect> {
13
+ export declare class PgliteQuerierPool extends AbstractSharedHandleQuerierPool<PgliteDatabase, PgliteQuerier, PgliteDialect> {
14
14
  readonly dataDir: string;
15
15
  readonly opts?: PglitePoolOptions | undefined;
16
16
  constructor(dataDir?: string, opts?: PglitePoolOptions | undefined, extra?: ExtraOptions);
@@ -1,8 +1,8 @@
1
1
  import { dialectOptionsFrom } from '../dialect/abstractDialect.js';
2
- import { PostgresDialect } from '../postgres/postgresDialect.js';
3
2
  import { AbstractSharedHandleQuerierPool } from '../querier/abstractSharedHandleQuerierPool.js';
4
3
  import { decodeDate } from '../util/date.js';
5
4
  import { decodeWideNumber } from '../util/wideNumber.js';
5
+ import { PgliteDialect } from './pgliteDialect.js';
6
6
  import { PgliteQuerier } from './pgliteQuerier.js';
7
7
  /**
8
8
  * A pool for PGlite, Postgres in WASM in this process, on one connection. A second `BEGIN` silently joins
@@ -13,7 +13,7 @@ export class PgliteQuerierPool extends AbstractSharedHandleQuerierPool {
13
13
  dataDir;
14
14
  opts;
15
15
  constructor(dataDir = 'memory://', opts, extra) {
16
- super(new PostgresDialect(dialectOptionsFrom(extra)), extra);
16
+ super(new PgliteDialect(dialectOptionsFrom(extra)), extra);
17
17
  this.dataDir = dataDir;
18
18
  this.opts = opts;
19
19
  }
@@ -5,9 +5,7 @@ export interface PgAnyClient {
5
5
  rows: RawRow[];
6
6
  rowCount: number | null;
7
7
  }>;
8
- query(stream: object): AsyncIterable<RawRow> & {
9
- destroy(): void;
10
- };
8
+ query(stream: object): AsyncIterable<RawRow>;
11
9
  /** Any truthy argument makes `pg-pool` evict the client instead of returning it to the idle list. */
12
10
  release(discard?: boolean): void | Promise<void>;
13
11
  }
@@ -18,6 +16,6 @@ export interface PgAnyClient {
18
16
  export declare class PgQuerier<C extends PgAnyClient = PgAnyClient> extends AbstractPoolQuerier<C> {
19
17
  internalAll<T>(query: string, values?: unknown[]): Promise<T[]>;
20
18
  internalRun(query: string, values?: unknown[]): Promise<import("../type/query.js").QueryUpdateResult>;
21
- internalStream<T>(query: string, values?: unknown[]): AsyncGenerator<Awaited<T>, void, unknown>;
19
+ internalStream(query: string, values?: unknown[]): AsyncGenerator<RawRow, void, any>;
22
20
  protected releaseConn(conn: C, discard: boolean): Promise<void>;
23
21
  }
@@ -14,15 +14,7 @@ export class PgQuerier extends AbstractPoolQuerier {
14
14
  }
15
15
  async *internalStream(query, values) {
16
16
  const { default: QueryStream } = await import('pg-query-stream');
17
- const stream = this.getConn().query(new QueryStream(query, values));
18
- try {
19
- for await (const row of stream) {
20
- yield row;
21
- }
22
- }
23
- finally {
24
- stream.destroy();
25
- }
17
+ yield* this.getConn().query(new QueryStream(query, values));
26
18
  }
27
19
  async releaseConn(conn, discard) {
28
20
  await conn.release(discard);
@@ -3,6 +3,7 @@ import type { CursorPage, EntityData, EntityId, EntityWrite, ExtraOptions, Field
3
3
  import { LoggerWrapper } from '../util/index.js';
4
4
  /** Base class for all database queriers. */
5
5
  export declare abstract class AbstractQuerier implements Querier {
6
+ #private;
6
7
  readonly extra?: ExtraOptions | undefined;
7
8
  /**
8
9
  * Internal promise used to queue database operations.
@@ -43,11 +44,22 @@ export declare abstract class AbstractQuerier implements Querier {
43
44
  * or pipeline. [The design](../../../../architecture/relations-in-one-statement.md).
44
45
  */
45
46
  protected abstract internalFindMany<E extends object>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): Promise<E[]>;
46
- /** Stream records with the relations and counts `findMany` reads, the entity passed first or as `$entity`. No hooks fire. */
47
+ /**
48
+ * Stream records with the relations and counts `findMany` reads, the entity passed first or as `$entity`.
49
+ * `afterLoad` runs on each row as it arrives, and the querier runs nothing else until the loop ends.
50
+ */
47
51
  findManyStream<E extends object, const S extends FieldKey<E> = never, const V = true, const X extends FieldKey<E> = never, const P extends RelationKey<E> = never, const C extends RelationKey<E> = never>(q: QueryProjected<E, S, V, X, P, C> & {
48
52
  $entity: Type<E>;
49
53
  }, opts?: QueryOptions): AsyncIterable<QueryFindResult<E, S, V, X, P, C>>;
50
54
  findManyStream<E extends object, const S extends FieldKey<E> = never, const V = true, const X extends FieldKey<E> = never, const P extends RelationKey<E> = never, const C extends RelationKey<E> = never>(entity: Type<E>, q: QueryProjected<E, S, V, X, P, C>, opts?: QueryOptions): AsyncIterable<QueryFindResult<E, S, V, X, P, C>>;
55
+ /** `afterLoad` on each row before the loop sees it, as `findMany` runs it on each row it read. */
56
+ private loadedEach;
57
+ /**
58
+ * `rows`, holding the querier from the first row asked for until the loop ends. Most drivers queue a
59
+ * statement behind an open read, which a loop awaiting it never finishes, so `serialize` refuses one
60
+ * meanwhile, on every engine alike, and `release` closes the stream first.
61
+ */
62
+ private holdWhileReading;
51
63
  protected abstract internalFindManyStream<E extends object>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): AsyncIterable<E>;
52
64
  /** Find many records and count every match, the entity passed first or as the query's `$entity`. */
53
65
  findManyAndCount<E extends object, const S extends FieldKey<E> = never, const V = true, const X extends FieldKey<E> = never, const P extends RelationKey<E> = never, const C extends RelationKey<E> = never>(q: QueryProjected<E, S, V, X, P, C> & {
@@ -197,6 +209,11 @@ export declare abstract class AbstractQuerier implements Querier {
197
209
  protected serialize<T>(task: () => Promise<T>): Promise<T>;
198
210
  /** Runs `task`, logs `query` with its duration, and tags a failure with it: a method, since a decorator would lose the generics. */
199
211
  protected timed<T>(query: string, values: readonly unknown[] | undefined, task: () => Promise<T>): Promise<T>;
212
+ /**
213
+ * `rows` as {@link timed} runs a statement: logged once they end, timed to the first row since the
214
+ * time after it is the loop's, and a failure tagged with `query`.
215
+ */
216
+ protected timedStream<T>(query: string, values: readonly unknown[] | undefined, rows: AsyncIterable<T>): AsyncGenerator<T>;
200
217
  beginTransaction(opts?: TransactionOptions): Promise<void>;
201
218
  /** Strict: this is the check that catches a forgotten `beginTransaction`. */
202
219
  commitTransaction(): Promise<void>;
@@ -210,8 +227,9 @@ export declare abstract class AbstractQuerier implements Querier {
210
227
  /** Commits the open transaction on the engine, or rolls it back when `commit` is false. */
211
228
  protected abstract endTransaction(commit: boolean): Promise<void>;
212
229
  /**
213
- * Rolls back an unfinished transaction, then hands the connection back, discarding it if the rollback
214
- * failed. Never throws first, since `await using` has no other way to release.
230
+ * Closes a stream left open and rolls back an unfinished transaction, then hands the connection back,
231
+ * discarding it if either failed: the pool would otherwise take one still reading. Never throws first,
232
+ * since `await using` has no other way to release.
215
233
  */
216
234
  release(): Promise<void>;
217
235
  [Symbol.asyncDispose](): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  import { assertSoleId, getMeta, idOf, namesKey, relationOf } from '../entity/index.js';
2
2
  import { namesRows } from '../dialect/operators.js';
3
3
  import { parseQueryLock } from '../type/index.js';
4
- import { cascadesOnDelete, childrenOf, clone, entityName, fillOnFields, filterFieldKeys, filterPersistableRelationKeys, forEachRequestedRelation, getKeys, hasKeys, getRelationRequestSummary, guardWrite, idOnlyQuery, keySet, isPagedQuery, isScalarId, LoggerWrapper, parentJoins, queryLoggerFor, parseRelationAtKey, parseRelationQueryValue, rowKey, runHooks, securityConditions, someKey, targetKeyColumns, whereAnyOf, whereEach, whereIds, whereWith, withoutSoftDeleteFilter, } from '../util/index.js';
4
+ import { cascadesOnDelete, childrenOf, chunk, clone, entityName, fillOnFields, filterFieldKeys, filterPersistableRelationKeys, forEachRequestedRelation, getKeys, hasKeys, getRelationRequestSummary, guardWrite, idOnlyQuery, keySet, isPagedQuery, isScalarId, LoggerWrapper, parentJoins, queryLoggerFor, parseRelationAtKey, parseRelationQueryValue, rowKey, runHooks, securityConditions, someKey, targetKeyColumns, whereEach, whereIds, whereKeysIn, whereWith, withoutSoftDeleteFilter, } from '../util/index.js';
5
5
  import { UqlOptimisticLockError, UqlUsageError } from '../util/uqlError.js';
6
6
  import { keysetRead } from './keyset.js';
7
7
  import { enrichError } from './queryError.js';
@@ -126,6 +126,7 @@ function isEntityFirst(args) {
126
126
  const [first] = args;
127
127
  return typeof first === 'function' && first.prototype !== undefined;
128
128
  }
129
+ const STREAM_HOLDS_QUERIER = 'a stream is reading on this querier: run the statement after its loop, or on another querier';
129
130
  /** Base class for all database queriers. */
130
131
  export class AbstractQuerier {
131
132
  extra;
@@ -135,6 +136,8 @@ export class AbstractQuerier {
135
136
  * and ensuring that the database connection is used safely across concurrent calls.
136
137
  */
137
138
  taskQueue = Promise.resolve();
139
+ /** The stream reading on the connection, from its first row asked for until its loop ends. */
140
+ #stream;
138
141
  /**
139
142
  * A querier is one unit of work, so releasing ends it. Checked where each backend reaches for its
140
143
  * connection, on every driver and not just the pooled ones.
@@ -205,7 +208,37 @@ export class AbstractQuerier {
205
208
  findManyStream(...args) {
206
209
  const [entity, q, opts] = entityArgs(args);
207
210
  this.validateReadQuery(entity, q);
208
- return this.internalFindManyStream(entity, q, opts);
211
+ const rows = this.internalFindManyStream(entity, q, opts);
212
+ return this.holdWhileReading(this.listensForLoad(entity, q.$populate) ? this.loadedEach(entity, rows, q.$populate) : rows);
213
+ }
214
+ /** `afterLoad` on each row before the loop sees it, as `findMany` runs it on each row it read. */
215
+ async *loadedEach(entity, rows, populate) {
216
+ for await (const row of rows) {
217
+ await this.emitLoaded(entity, [row], populate);
218
+ yield row;
219
+ }
220
+ }
221
+ /**
222
+ * `rows`, holding the querier from the first row asked for until the loop ends. Most drivers queue a
223
+ * statement behind an open read, which a loop awaiting it never finishes, so `serialize` refuses one
224
+ * meanwhile, on every engine alike, and `release` closes the stream first.
225
+ */
226
+ holdWhileReading(rows) {
227
+ const read = async function* (querier) {
228
+ if (querier.#stream) {
229
+ throw new UqlUsageError(STREAM_HOLDS_QUERIER);
230
+ }
231
+ querier.#stream = stream;
232
+ try {
233
+ await querier.taskQueue;
234
+ yield* rows;
235
+ }
236
+ finally {
237
+ querier.#stream = undefined;
238
+ }
239
+ };
240
+ const stream = read(this);
241
+ return stream;
209
242
  }
210
243
  async findManyAndCount(...args) {
211
244
  const [entity, q, opts] = entityArgs(args);
@@ -312,7 +345,10 @@ export class AbstractQuerier {
312
345
  if (!ids.length) {
313
346
  return 0;
314
347
  }
315
- const changes = await this.updateColumns(entity, { $where: whereIds(meta, ids) }, row, opts, ids.length);
348
+ let changes = 0;
349
+ for (const batch of chunk(ids, this.dialect.keyListCapacity(meta.ids.length))) {
350
+ changes += await this.updateColumns(entity, { $where: whereIds(meta, batch) }, row, opts, batch.length);
351
+ }
316
352
  for (const relKey of relKeys) {
317
353
  await this.saveRelation(entity, relKey, ids.map((id) => ({ id, value: row[relKey] })), true);
318
354
  }
@@ -453,11 +489,14 @@ export class AbstractQuerier {
453
489
  return 0;
454
490
  }
455
491
  await this.emitHook(entity, 'beforeDelete', doomed);
456
- // Children first: they hold the foreign key, which a schema without `ON DELETE CASCADE` enforces.
457
- if (cascades) {
458
- await this.deleteRelations(entity, ids, opts);
492
+ let changes = 0;
493
+ for (const batch of chunk(ids, this.dialect.keyListCapacity(meta.ids.length))) {
494
+ // Children first: they hold the foreign key, which a schema without `ON DELETE CASCADE` enforces.
495
+ if (cascades) {
496
+ await this.deleteRelations(entity, batch, opts);
497
+ }
498
+ changes += await this.internalDeleteMany(entity, { $where: whereIds(meta, batch) }, opts);
459
499
  }
460
- const changes = await this.internalDeleteMany(entity, { $where: whereIds(meta, ids) }, opts);
461
500
  await this.emitHook(entity, 'afterDelete', doomed);
462
501
  return changes;
463
502
  }
@@ -554,8 +593,9 @@ export class AbstractQuerier {
554
593
  const holder = relOpts.through ? relOpts.through() : relEntity;
555
594
  const parentColumn = soleParentColumn(relOpts);
556
595
  if (isUpdate) {
557
- const ids = writes.map(({ id }) => id);
558
- await this.deleteMany(holder, { $where: { [parentColumn]: ids } });
596
+ for (const batch of chunk(writes, this.dialect.keyListCapacity(1))) {
597
+ await this.deleteMany(holder, { $where: { [parentColumn]: batch.map(({ id }) => id) } });
598
+ }
559
599
  }
560
600
  // Each parent gets its own copies, so a row listed for two parents is written twice.
561
601
  const children = writes.flatMap(({ id, value }) => [value ?? []].flat().map((row) => ({ id, row })));
@@ -650,13 +690,14 @@ export class AbstractQuerier {
650
690
  const meta = getMeta(entity);
651
691
  const [idKey] = meta.ids;
652
692
  const keys = getKeys(conflictPaths);
653
- const q = {
654
- $select: keySet([idKey, ...keys]),
655
- $where: whereAnyOf(rows.map((row) => whereEach(keys, (key) => row[key]))),
656
- };
657
- const found = await this.internalFindMany(entity, q, { filters: withoutSoftDeleteFilter(undefined) });
693
+ const distinct = [...new Map(rows.map((row) => [rowKey(row, keys), row])).values()];
694
+ const found = [];
695
+ for (const batch of chunk(distinct, this.dialect.keyListCapacity(keys.length))) {
696
+ const q = { $select: keySet([idKey, ...keys]), $where: whereKeysIn(keys, batch) };
697
+ found.push(await this.internalFindMany(entity, q, { filters: withoutSoftDeleteFilter(undefined) }));
698
+ }
658
699
  const byConflict = new Map();
659
- for (const row of found) {
700
+ for (const row of found.flat()) {
660
701
  const key = rowKey(row, keys);
661
702
  byConflict.set(key, byConflict.has(key) ? undefined : row[idKey]);
662
703
  }
@@ -694,6 +735,9 @@ export class AbstractQuerier {
694
735
  }
695
736
  /** Runs `task` after everything already queued, one at a time. Not re-entrant: never nest `serialize` calls. */
696
737
  serialize(task) {
738
+ if (this.#stream) {
739
+ return Promise.reject(new UqlUsageError(STREAM_HOLDS_QUERIER));
740
+ }
697
741
  const res = this.taskQueue.then(task);
698
742
  this.taskQueue = res.catch(() => { });
699
743
  return res;
@@ -711,6 +755,26 @@ export class AbstractQuerier {
711
755
  this.logger.logQuery(query, values, Math.round(performance.now() - startTime));
712
756
  }
713
757
  }
758
+ /**
759
+ * `rows` as {@link timed} runs a statement: logged once they end, timed to the first row since the
760
+ * time after it is the loop's, and a failure tagged with `query`.
761
+ */
762
+ async *timedStream(query, values, rows) {
763
+ const startTime = performance.now();
764
+ let answeredAt;
765
+ try {
766
+ for await (const row of rows) {
767
+ answeredAt ??= performance.now();
768
+ yield row;
769
+ }
770
+ }
771
+ catch (err) {
772
+ throw enrichError(err, this.logger, query, values);
773
+ }
774
+ finally {
775
+ this.logger.logQuery(query, values, Math.round((answeredAt ?? performance.now()) - startTime));
776
+ }
777
+ }
714
778
  beginTransaction(opts) {
715
779
  return this.serialize(async () => {
716
780
  if (this.hasOpenTransaction) {
@@ -740,11 +804,16 @@ export class AbstractQuerier {
740
804
  });
741
805
  }
742
806
  /**
743
- * Rolls back an unfinished transaction, then hands the connection back, discarding it if the rollback
744
- * failed. Never throws first, since `await using` has no other way to release.
807
+ * Closes a stream left open and rolls back an unfinished transaction, then hands the connection back,
808
+ * discarding it if either failed: the pool would otherwise take one still reading. Never throws first,
809
+ * since `await using` has no other way to release.
745
810
  */
746
811
  async release() {
747
812
  let discard = false;
813
+ await this.#stream?.return(undefined).catch((err) => {
814
+ this.logger.logError('closing an open stream failed; discarding the connection', err);
815
+ discard = true;
816
+ });
748
817
  if (this.hasOpenTransaction) {
749
818
  this.logger.logWarn('rolling back a transaction left open at release');
750
819
  // The rollback doubles as a health check. One that succeeds proves the connection round-trips and
@@ -29,7 +29,7 @@ export declare abstract class AbstractQuerierPool<Q extends Querier, D extends A
29
29
  readonly findMany: UniversalQuerier['findMany'];
30
30
  /**
31
31
  * The connection outlives the call: it is held until the iterator is drained or closed by a `break` or
32
- * `throw`. Abandoning the iterator leaks it until GC, so consume it in a `for await`.
32
+ * `throw`. Abandoning the iterator leaks it, so consume it in a `for await`.
33
33
  */
34
34
  readonly findManyStream: UniversalQuerier['findManyStream'];
35
35
  private streamWithQuerier;
@@ -91,7 +91,7 @@ export class AbstractQuerierPool {
91
91
  findMany = (...args) => this.withQuerier((querier) => querier.findMany(...args));
92
92
  /**
93
93
  * The connection outlives the call: it is held until the iterator is drained or closed by a `break` or
94
- * `throw`. Abandoning the iterator leaks it until GC, so consume it in a `for await`.
94
+ * `throw`. Abandoning the iterator leaks it, so consume it in a `for await`.
95
95
  */
96
96
  findManyStream = (...args) => this.streamWithQuerier((querier) => querier.findManyStream(...args));
97
97
  async *streamWithQuerier(read) {
@@ -1,5 +1,5 @@
1
1
  import type { AbstractSqlDialect } from '../dialect/index.js';
2
- import type { EntityData, ExtraOptions, Query, QueryAggMap, QueryAggregate, QueryAggregateResult, QueryConflictPaths, QueryPage, QueryGroupMap, QueryOptions, QuerySearch, QueryUpdateResult, SqlQuerier, TransactionOptions, Type, UpdatePayload, UpsertOptions } from '../type/index.js';
2
+ import type { EntityData, ExtraOptions, Query, QueryAggMap, QueryAggregate, QueryAggregateResult, QueryConflictPaths, QueryPage, QueryGroupMap, QueryOptions, QuerySearch, QueryUpdateResult, RawRow, SqlQuerier, TransactionOptions, Type, UpdatePayload, UpsertOptions } from '../type/index.js';
3
3
  import type { BuildUpdateResultPayload } from '../util/sql.util.js';
4
4
  import { AbstractQuerier } from './abstractQuerier.js';
5
5
  export declare abstract class AbstractSqlQuerier extends AbstractQuerier implements SqlQuerier {
@@ -34,6 +34,8 @@ export declare abstract class AbstractSqlQuerier extends AbstractQuerier impleme
34
34
  protected lazyConnect(): Promise<void>;
35
35
  all<T>(query: string, values?: readonly unknown[]): Promise<T[]>;
36
36
  run(query: string, values?: readonly unknown[]): Promise<QueryUpdateResult>;
37
+ /** Refused before the driver fails it, or PGlite answers it and every read after it wrong. */
38
+ private assertBindBudget;
37
39
  /** The rows of a statement the dialect builds. */
38
40
  private query;
39
41
  /** Runs a statement the dialect builds. */
@@ -53,22 +55,25 @@ export declare abstract class AbstractSqlQuerier extends AbstractQuerier impleme
53
55
  */
54
56
  protected internalFindManyAndCount<E extends object>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): Promise<[E[], number]>;
55
57
  private selectRows;
56
- private hydrateRows;
58
+ /**
59
+ * The one read not going through `all`, so it checks its budget and connects on its own; its values go as
60
+ * the context holds them, each normalized as it was bound. The tuning is guarded as `selectRows` says.
61
+ */
57
62
  protected internalFindManyStream<E extends object>(entity: Type<E>, q: Query<E>, opts?: QueryOptions): AsyncGenerator<Awaited<E>, void, unknown>;
58
63
  /**
59
64
  * A read's rows one at a time: paged through a server-side cursor where the engine has one, read whole
60
- * where it has none. A driver that streams on its own overrides this.
65
+ * where it has none. A driver that streams on its own overrides this; a Node `Readable` it hands back
66
+ * closes itself when the loop exits early.
61
67
  */
62
- protected internalStream<T>(query: string, values?: unknown[]): AsyncIterable<T>;
68
+ protected internalStream(query: string, values?: unknown[]): AsyncIterable<RawRow>;
63
69
  /**
64
- * Turn what a driver returned back into the types the entity declares, for every row and everything
65
- * populated under them. Which columns, and as what, is `hydratableFields`, resolved once for all the
66
- * rows; the per-cell decode is `decodeColumn`. Both live with the dialect, because a `sparsevec` is
67
- * only sparse on Postgres.
70
+ * How each row of one statement becomes the entity's, for a read, a stream and a to-many alike: its dotted
71
+ * columns nested as the first row names them, and its values decoded as `hydratableFields` says, resolved
72
+ * once. Both live with the dialect, because a `sparsevec` is only sparse on Postgres.
68
73
  */
69
- private hydrateAll;
74
+ private rowReader;
70
75
  /**
71
- * One row of {@link hydrateAll}. A related row arrives as its parent's statement read it: a to-one
76
+ * One row of {@link rowReader}. A related row arrives as its parent's statement read it: a to-one
72
77
  * joined and unflattened, there only when its key is, since an unmatched join still fills a computed
73
78
  * column or a to-many's empty array; a to-many as a JSON array, which a driver may hand over as text.
74
79
  * Each is an object of its own, so the walk reaches none twice.