uql-orm 0.93.0 → 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 (71) 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 +37 -13
  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/ddl/index.d.ts +1 -1
  16. package/dist/migrate/ddl/index.js +3 -3
  17. package/dist/migrate/ddl/mssqlTableDdl.d.ts +3 -1
  18. package/dist/migrate/ddl/mssqlTableDdl.js +4 -0
  19. package/dist/migrate/ddl/mysqlTableDdl.d.ts +1 -1
  20. package/dist/migrate/ddl/mysqlTableDdl.js +2 -2
  21. package/dist/migrate/ddl/pgTableDdl.d.ts +5 -1
  22. package/dist/migrate/ddl/pgTableDdl.js +12 -6
  23. package/dist/migrate/ddl/tableDdl.d.ts +9 -6
  24. package/dist/migrate/ddl/tableDdl.js +15 -6
  25. package/dist/migrate/ddl/tableRebuild.d.ts +2 -5
  26. package/dist/migrate/drift/driftDetector.js +7 -2
  27. package/dist/migrate/schemaChange.d.ts +2 -5
  28. package/dist/migrate/schemaGenerator.js +1 -1
  29. package/dist/mongo/mongoDialect.d.ts +1 -2
  30. package/dist/mongo/mongoDialect.js +18 -15
  31. package/dist/mongo/mongodbQuerier.js +3 -10
  32. package/dist/mssql/mssqlDialect.d.ts +3 -2
  33. package/dist/mssql/mssqlDialect.js +3 -2
  34. package/dist/mssql/mssqlQuerier.d.ts +2 -2
  35. package/dist/mssql/mssqlQuerier.js +1 -3
  36. package/dist/mysql/mysql2Querier.d.ts +1 -1
  37. package/dist/mysql/mysql2Querier.js +1 -9
  38. package/dist/pglite/index.d.ts +1 -0
  39. package/dist/pglite/index.js +1 -0
  40. package/dist/pglite/pgliteDialect.d.ts +6 -0
  41. package/dist/pglite/pgliteDialect.js +6 -0
  42. package/dist/pglite/pgliteQuerierPool.d.ts +2 -2
  43. package/dist/pglite/pgliteQuerierPool.js +2 -2
  44. package/dist/postgres/pgQuerier.d.ts +2 -4
  45. package/dist/postgres/pgQuerier.js +1 -9
  46. package/dist/querier/abstractQuerier.d.ts +21 -3
  47. package/dist/querier/abstractQuerier.js +86 -17
  48. package/dist/querier/abstractQuerierPool.d.ts +1 -1
  49. package/dist/querier/abstractQuerierPool.js +23 -23
  50. package/dist/querier/abstractSqlQuerier.d.ts +15 -10
  51. package/dist/querier/abstractSqlQuerier.js +54 -57
  52. package/dist/querier/cursorStream.d.ts +1 -1
  53. package/dist/schema/sqlExpression.d.ts +4 -1
  54. package/dist/schema/sqlExpression.js +20 -2
  55. package/dist/schema/types.d.ts +2 -5
  56. package/dist/sqlite/abstractSqliteQuerier.d.ts +1 -1
  57. package/dist/sqlite/sqliteDialect.d.ts +1 -0
  58. package/dist/sqlite/sqliteDialect.js +1 -0
  59. package/dist/type/entity.d.ts +9 -9
  60. package/dist/type/migration.d.ts +7 -8
  61. package/dist/type/querier.d.ts +1 -1
  62. package/dist/type/universalQuerier.d.ts +1 -1
  63. package/dist/util/dialect.util.d.ts +1 -1
  64. package/dist/util/object.util.d.ts +2 -0
  65. package/dist/util/object.util.js +8 -0
  66. package/dist/util/query.util.d.ts +3 -1
  67. package/dist/util/query.util.js +7 -0
  68. package/dist/util/sql.util.d.ts +2 -4
  69. package/dist/util/sql.util.js +10 -19
  70. package/package.json +10 -10
  71. package/skills/uql-orm/SKILL.md +22 -51
@@ -3,15 +3,21 @@ import { TableDdl } from './tableDdl.js';
3
3
  /** Table DDL for the Postgres family, which alters each part of a column in a separate clause. */
4
4
  export class PgTableDdl extends TableDdl {
5
5
  /** A clause for each part that differs from `from`, the column as it was, or for every part without it. */
6
- alterColumn(table, column, _definition, from) {
6
+ alterClauses(column, _definition, from) {
7
7
  const name = this.dialect.escapeId(column.name);
8
- const alter = `ALTER TABLE ${this.dialect.escapeId(table)} ALTER COLUMN ${name}`;
8
+ const alter = `ALTER COLUMN ${name}`;
9
9
  return [
10
10
  // Casts explicitly: Postgres converts on its own only between compatible types, and text to integer is not.
11
- (!from || from.type !== column.type) && `${alter} TYPE ${column.type} USING ${name}::${column.type};`,
12
- (!from || from.nullable !== column.nullable) && `${alter} ${column.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'};`,
11
+ (!from || from.type !== column.type) && `${alter} TYPE ${column.type} USING ${name}::${column.type}`,
12
+ (!from || from.nullable !== column.nullable) && `${alter} ${column.nullable ? 'DROP NOT NULL' : 'SET NOT NULL'}`,
13
13
  (!from || !sameDefault(column.defaultValue, from.defaultValue, this.dialect)) &&
14
- (column.defaultValue === undefined ? `${alter} DROP DEFAULT;` : `${alter} SET${this.defaultClause(column)};`),
15
- ].filter((statement) => statement !== false);
14
+ (column.defaultValue === undefined ? `${alter} DROP DEFAULT` : `${alter} SET${this.defaultClause(column)}`),
15
+ ].filter((clause) => clause !== false);
16
+ }
17
+ }
18
+ /** CockroachDB refuses a rewriting retype beside any other clause, and changes a schema online anyway. */
19
+ export class CockroachTableDdl extends PgTableDdl {
20
+ alterTable(table, clauses) {
21
+ return clauses.flatMap((clause) => super.alterTable(table, [clause]));
16
22
  }
17
23
  }
@@ -1,5 +1,5 @@
1
1
  import type { AbstractSqlDialect } from '../../dialect/abstractSqlDialect.js';
2
- import type { ColumnSchema } from '../../type/index.js';
2
+ import type { Alteration, ColumnSchema } from '../../type/index.js';
3
3
  /**
4
4
  * A column's type with the size it was read back with, unless its spelling already carries one:
5
5
  * introspection reports `VARCHAR` and `255` apart, where a type from an entity is already whole.
@@ -20,11 +20,14 @@ export declare class TableDdl {
20
20
  /** The statements that add `column`, whose definition `render` writes. */
21
21
  addColumnStatements(table: string, column: ColumnSchema, render: (column: ColumnSchema) => string): string[];
22
22
  dropColumn(table: string, column: string): string[];
23
- /**
24
- * The statements that change a column from `from`, what it was, to `column`, what it now declares.
25
- * `definition` is the complete new column definition.
26
- */
27
- alterColumn(table: string, _column: ColumnSchema, definition: string, _from?: ColumnSchema): string[];
23
+ /** The statements that change a column from `from`, what it was, to `column`, whose definition is `definition`. */
24
+ alterColumn(table: string, column: ColumnSchema, definition: string, from?: ColumnSchema): string[];
25
+ /** The statements making `alterations` to one table, whose new definitions `render` writes. */
26
+ alterColumns(table: string, alterations: readonly Alteration<ColumnSchema>[], render: (column: ColumnSchema) => string): string[];
27
+ /** The `ALTER TABLE` clauses that change a column, as {@link alterColumn} takes it. */
28
+ protected alterClauses(_column: ColumnSchema, definition: string, _from?: ColumnSchema): string[];
29
+ /** One `ALTER TABLE` making every clause, and none for no clause. */
30
+ protected alterTable(table: string, clauses: readonly string[]): string[];
28
31
  renameColumn(table: string, oldName: string, newName: string): string;
29
32
  renameTable(oldName: string, newName: string): string;
30
33
  /** `schema` is the table's schema, which also holds its indexes. */
@@ -39,12 +39,21 @@ export class TableDdl {
39
39
  dropColumn(table, column) {
40
40
  return [`ALTER TABLE ${this.dialect.escapeId(table)} DROP COLUMN ${this.dialect.escapeId(column)};`];
41
41
  }
42
- /**
43
- * The statements that change a column from `from`, what it was, to `column`, what it now declares.
44
- * `definition` is the complete new column definition.
45
- */
46
- alterColumn(table, _column, definition, _from) {
47
- return [`ALTER TABLE ${this.dialect.escapeId(table)} ALTER COLUMN ${definition};`];
42
+ /** The statements that change a column from `from`, what it was, to `column`, whose definition is `definition`. */
43
+ alterColumn(table, column, definition, from) {
44
+ return this.alterTable(table, this.alterClauses(column, definition, from));
45
+ }
46
+ /** The statements making `alterations` to one table, whose new definitions `render` writes. */
47
+ alterColumns(table, alterations, render) {
48
+ return this.alterTable(table, alterations.flatMap(({ from, to }) => this.alterClauses(to, render(to), from)));
49
+ }
50
+ /** The `ALTER TABLE` clauses that change a column, as {@link alterColumn} takes it. */
51
+ alterClauses(_column, definition, _from) {
52
+ return [`ALTER COLUMN ${definition}`];
53
+ }
54
+ /** One `ALTER TABLE` making every clause, and none for no clause. */
55
+ alterTable(table, clauses) {
56
+ return clauses.length ? [`ALTER TABLE ${this.dialect.escapeId(table)} ${clauses.join(', ')};`] : [];
48
57
  }
49
58
  renameColumn(table, oldName, newName) {
50
59
  const [target, from, to] = [table, oldName, newName].map((name) => this.dialect.escapeId(name));
@@ -1,5 +1,5 @@
1
1
  import type { AbstractSqlDialect } from '../../dialect/index.js';
2
- import type { RebuiltTable, Rename } from '../../type/index.js';
2
+ import type { Alteration, RebuiltTable, Rename } from '../../type/index.js';
3
3
  /** What the copy reads besides the columns both sides share: renamed ones, and a default filling a column's nulls. */
4
4
  export type RebuildCopy = {
5
5
  readonly renames: readonly Rename[];
@@ -11,7 +11,4 @@ export type RebuildCopy = {
11
11
  * key pointing at the table would delete or null its rows with the drop, so the guard fails the whole
12
12
  * rebuild first unless foreign keys are off, as the migrator turns them off on a SQLite connection.
13
13
  */
14
- export declare function rebuildTable(dialect: AbstractSqlDialect, table: string, { from, to }: {
15
- readonly from: RebuiltTable;
16
- readonly to: RebuiltTable;
17
- }, copy: RebuildCopy): string[];
14
+ export declare function rebuildTable(dialect: AbstractSqlDialect, table: string, { from, to }: Alteration<RebuiltTable>, copy: RebuildCopy): string[];
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { canonicalToSql, engineType } from '../../schema/canonicalType.js';
8
8
  import { defaultsEqualAsWritten, diffSchemas, referentialActions } from '../../schema/schemaASTDiffer.js';
9
+ import { SqlExpression, writtenDefault } from '../../schema/sqlExpression.js';
9
10
  function resolveOptions(options) {
10
11
  return {
11
12
  checkTypes: options.checkTypes ?? true,
@@ -156,8 +157,8 @@ function addAlterColumnDrifts(colDiff, drifts, opts) {
156
157
  severity: 'info',
157
158
  table: colDiff.table,
158
159
  column: colDiff.column,
159
- expected: String(colDiff.to.defaultValue ?? 'NULL'),
160
- actual: String(colDiff.from.defaultValue ?? 'NULL'),
160
+ expected: shownDefault(colDiff.to.defaultValue),
161
+ actual: shownDefault(colDiff.from.defaultValue),
161
162
  details: `Default mismatch for "${colDiff.column}"`,
162
163
  suggestion: 'Align the default in the entity or the database',
163
164
  });
@@ -245,6 +246,10 @@ function detectRelationshipDrifts(diff) {
245
246
  }
246
247
  return drifts;
247
248
  }
249
+ /** A default as a report shows it: SQL as its SQL, a JSON document as JSON rather than `[object Object]`. */
250
+ function shownDefault(value) {
251
+ return value == null ? 'NULL' : SqlExpression.isExpression(value) ? String(value) : writtenDefault(value);
252
+ }
248
253
  /** Both actions spelled out, so a side that left one unstated reads the same as one that stated the default. */
249
254
  function formatActions(rel) {
250
255
  const { onDelete, onUpdate } = referentialActions(rel);
@@ -1,4 +1,4 @@
1
- import type { Change, ColumnSchema, SchemaDiff } from '../type/index.js';
1
+ import type { Alteration, Change, ColumnSchema, SchemaDiff } from '../type/index.js';
2
2
  /** Each change's end on `side`, where it has one: what a drop half removes (`from`), or an add half creates (`to`). */
3
3
  export declare function sides<T>(changes: readonly Change<T>[] | undefined, side: 'from' | 'to'): T[];
4
4
  /** What the changes add: each `to` with no `from`. */
@@ -6,10 +6,7 @@ export declare function added<T>(changes?: readonly Change<T>[]): T[];
6
6
  /** What the changes drop: each `from` with no `to`. */
7
7
  export declare function dropped<T>(changes?: readonly Change<T>[]): T[];
8
8
  /** The changes that alter an object in place, which only a column can. */
9
- export declare function alterations<T>(changes?: readonly Change<T>[]): {
10
- readonly from: T;
11
- readonly to: T;
12
- }[];
9
+ export declare function alterations<T>(changes?: readonly Change<T>[]): Alteration<T>[];
13
10
  /** Each column `changes` make required on the rows already there: added so, or no longer nullable. */
14
11
  export declare function newlyRequired(changes?: readonly Change<ColumnSchema>[]): {
15
12
  readonly from?: ColumnSchema;
@@ -200,7 +200,7 @@ export class SqlSchemaGenerator {
200
200
  const name = this.escapeId(column);
201
201
  return `UPDATE ${target} SET ${name} = ${value} WHERE ${name} IS NULL;`;
202
202
  }),
203
- ...alterations(columns).flatMap(({ from, to }) => this.tableDdl.alterColumn(tableName, to, this.generateColumnDefinitionFromSchema(to), from)),
203
+ ...this.tableDdl.alterColumns(tableName, alterations(columns), (column) => this.generateColumnDefinitionFromSchema(column)),
204
204
  ...dropped(columns).flatMap((column) => this.tableDdl.dropColumn(tableName, column.name)),
205
205
  ...this.addIndexStatements(tableName, sides(diff.indexes, 'to')),
206
206
  ...(primaryKey?.to ? [this.generateAddPrimaryKeySql(tableName, primaryKey.to.columns, primaryKey.to.name)] : []),
@@ -79,8 +79,6 @@ export declare class MongoDialect extends AbstractDialect {
79
79
  * Each end is one field matched against one `_id`, so both sides must be sole-keyed.
80
80
  */
81
81
  private junctionOf;
82
- /** `raw()` renders SQL, so it has no MongoDB equivalent - say so instead of emitting `{}`. */
83
- private assertNoRaw;
84
82
  /**
85
83
  * A dotted key addresses an embedded path, whose root must still be a declared field - otherwise it
86
84
  * is a typo (or an injected key) that would silently match nothing, the same guard the SQL dialects
@@ -211,6 +209,7 @@ export declare class MongoDialect extends AbstractDialect {
211
209
  private referenceKeys;
212
210
  normalizeIds<E extends Document>(meta: EntityMeta<E>, docs: Document[]): E[];
213
211
  /** `doc` is the wire shape - `_id`, stored names, `ObjectId`s - and what comes back is the code's. */
212
+ normalizeId<E extends Document>(meta: EntityMeta<E>, doc: Document): E;
214
213
  normalizeId<E extends Document>(meta: EntityMeta<E>, doc: Document | undefined): E | undefined;
215
214
  /** An aggregate's rows with each 64-bit integer decoded as a document's is: a `bigint` only where the column reads a `BigInt` field. */
216
215
  normalizeAggregateRows<E extends Document, const G extends QueryGroupMap<E>, const A extends QueryAggMap<E>, R extends Document>(entity: Type<E>, q: QueryAggregate<E, G, A>, rows: R[]): R[];
@@ -34,6 +34,15 @@ export const mongoDialectFeatures = {
34
34
  };
35
35
  /** What `toWireId` converts: the hex spelling of an `ObjectId`, and nothing looser. */
36
36
  const HEX_24 = /^[0-9a-f]{24}$/i;
37
+ /** `raw()` renders SQL, which MongoDB has no equivalent of: refused rather than emitted as `{}`. */
38
+ function rawRefusal(place) {
39
+ return new UqlUsageError(`raw() in ${place} is not supported on MongoDB`);
40
+ }
41
+ function assertNoRaw(value, place) {
42
+ if (value instanceof QueryRaw) {
43
+ throw rawRefusal(place);
44
+ }
45
+ }
37
46
  /** How a declared column type reads in a message: `Number`, `uuid` - not its whole source. */
38
47
  function declaredTypeName(type) {
39
48
  return typeof type === 'function' ? type.name : String(type);
@@ -170,14 +179,14 @@ export class MongoDialect extends AbstractDialect {
170
179
  filter['$text'] = { $search: $value, ...($config && { $language: textLanguage($config) }) };
171
180
  }
172
181
  else if (meta.relations[key]) {
173
- this.assertNoRaw(val);
182
+ assertNoRaw(val, '$where');
174
183
  if (!lookups) {
175
184
  throw new UqlUsageError(`filtering by relation '${key}' is not supported here on MongoDB`);
176
185
  }
177
186
  this.appendRelationLookup(filter, meta, key, val, lookups);
178
187
  }
179
188
  else {
180
- this.assertNoRaw(val);
189
+ assertNoRaw(val, '$where');
181
190
  this.assertKnownPathRoot(meta, key);
182
191
  if (aggregateOf(meta.fields[key])) {
183
192
  if (!lookups) {
@@ -219,7 +228,7 @@ export class MongoDialect extends AbstractDialect {
219
228
  const { join, negate } = GROUP_OPS[key];
220
229
  const parts = groupClauses(key, val)
221
230
  .map((filterIt) => {
222
- this.assertNoRaw(filterIt);
231
+ assertNoRaw(filterIt, '$where');
223
232
  return this.renderFilter(entity, filterIt, lookups);
224
233
  })
225
234
  .filter((part) => Object.keys(part).length > 0);
@@ -310,12 +319,6 @@ export class MongoDialect extends AbstractDialect {
310
319
  target: this.columnOf(throughMeta, targetColumn),
311
320
  };
312
321
  }
313
- /** `raw()` renders SQL, so it has no MongoDB equivalent - say so instead of emitting `{}`. */
314
- assertNoRaw(value) {
315
- if (value instanceof QueryRaw) {
316
- throw new UqlUsageError('raw() in $where is not supported on MongoDB');
317
- }
318
- }
319
322
  /**
320
323
  * A dotted key addresses an embedded path, whose root must still be a declared field - otherwise it
321
324
  * is a typo (or an injected key) that would silently match nothing, the same guard the SQL dialects
@@ -424,7 +427,7 @@ export class MongoDialect extends AbstractDialect {
424
427
  return {};
425
428
  }
426
429
  if (isSelectList(select)) {
427
- throw new UqlUsageError('raw $select is not supported on MongoDB');
430
+ throw rawRefusal('$select');
428
431
  }
429
432
  const fields = normalizeScalarFieldSelection(meta, select, exclude);
430
433
  // Projected by column, not by field key; `normalizeId` maps them back on the way out.
@@ -909,7 +912,6 @@ export class MongoDialect extends AbstractDialect {
909
912
  normalizeIds(meta, docs) {
910
913
  return docs.map((doc) => this.normalizeId(meta, doc));
911
914
  }
912
- /** `doc` is the wire shape - `_id`, stored names, `ObjectId`s - and what comes back is the code's. */
913
915
  normalizeId(meta, doc) {
914
916
  if (!doc) {
915
917
  return doc;
@@ -1004,6 +1006,7 @@ export class MongoDialect extends AbstractDialect {
1004
1006
  for (const [key, value] of Object.entries(persistable)) {
1005
1007
  if (isFieldUpdateOp(value)) {
1006
1008
  const [op, operand] = fieldUpdateOf(key, value);
1009
+ assertNoRaw(operand, 'a write');
1007
1010
  arithmetic[key] = { [MONGO_ARITHMETIC[op]]: [{ $ifNull: [`$${key}`, 0] }, { $literal: operand }] };
1008
1011
  continue;
1009
1012
  }
@@ -1100,7 +1103,9 @@ export class MongoDialect extends AbstractDialect {
1100
1103
  if (key === idKey)
1101
1104
  continue;
1102
1105
  const field = meta.fields[key];
1103
- doc[this.resolveColumnName(key, field)] = field.references ? this.toWireId(it[key]) : it[key];
1106
+ const value = it[key];
1107
+ assertNoRaw(value, 'a write');
1108
+ doc[this.resolveColumnName(key, field)] = field.references ? this.toWireId(value) : value;
1104
1109
  }
1105
1110
  return doc;
1106
1111
  });
@@ -1220,9 +1225,7 @@ export class MongoDialect extends AbstractDialect {
1220
1225
  if (isGroupOp(key)) {
1221
1226
  const { join, negate } = GROUP_OPS[key];
1222
1227
  const clauses = groupClauses(key, where[key]).map((clause) => {
1223
- if (clause instanceof QueryRaw) {
1224
- throw new UqlUsageError('raw SQL is not supported in an aggregate $where on MongoDB');
1225
- }
1228
+ assertNoRaw(clause, 'an aggregate $where');
1226
1229
  return this.whereExpression(meta, clause, named);
1227
1230
  });
1228
1231
  return negate ? { $not: [{ [join]: clauses }] } : { [join]: clauses };
@@ -1,7 +1,7 @@
1
1
  import { AGGREGATE_VALUE_ALIAS } from '../dialect/aliases.js';
2
2
  import { hasRequiredJoin } from '../dialect/queryJoins.js';
3
3
  import { fieldOf, getMeta, namesKey, soleIdOf } from '../entity/index.js';
4
- import { AbstractQuerier, enrichError } from '../querier/index.js';
4
+ import { AbstractQuerier } from '../querier/index.js';
5
5
  import { clone, getKeys, getSoftDeleteValue, hasKeys, hasTriggers, populatesRelations, textSortOf, vectorCandidates, withoutSoftDeleteFilter, whereEach, } from '../util/index.js';
6
6
  import { UqlUsageError } from '../util/uqlError.js';
7
7
  /**
@@ -53,15 +53,8 @@ export class MongodbQuerier extends AbstractQuerier {
53
53
  return;
54
54
  }
55
55
  const meta = getMeta(entity);
56
- const cursor = this.readCursor(entity, q, opts);
57
- try {
58
- for await (const doc of cursor) {
59
- const [normalized] = this.dialect.normalizeIds(meta, [doc]);
60
- yield normalized;
61
- }
62
- }
63
- catch (err) {
64
- throw enrichError(err, this.logger, 'internalFindManyStream');
56
+ for await (const doc of this.timedStream('internalFindManyStream', undefined, this.readCursor(entity, q, opts))) {
57
+ yield this.dialect.normalizeId(meta, doc);
65
58
  }
66
59
  }
67
60
  /**
@@ -24,8 +24,9 @@ export declare class MsSqlDialect extends MergeSqlDialect {
24
24
  readonly sqlValues: SqlValues;
25
25
  /** Only where the statement touched a row the trigger selects, since it fires once per statement, even for none. */
26
26
  protected refusal(message: string, rows?: TriggerRows): string;
27
- /** [The hard server limit](https://github.com/yiisoft/yii2/issues/10371), not a driver preference. */
28
- readonly maxBindValues = 2100;
27
+ /** The server's 2100 per request, less the two `sp_executesql` takes for the statement and its parameter list. */
28
+ readonly maxBindValues = 2098;
29
+ readonly maxInsertRows = 1000;
29
30
  /** `OUTPUT` has no trailing form: it sits between the column list and `VALUES`. */
30
31
  readonly returningPosition = "after-target";
31
32
  /** `OUTPUT` reads the written row off the `INSERTED` pseudo-table. */
@@ -99,8 +99,9 @@ export class MsSqlDialect extends MergeSqlDialect {
99
99
  const touched = rows ? `IF EXISTS (SELECT 1${fromRows(rows)}) ` : '';
100
100
  return `${touched}THROW 50000, ${message}, 1;`;
101
101
  }
102
- /** [The hard server limit](https://github.com/yiisoft/yii2/issues/10371), not a driver preference. */
103
- maxBindValues = 2100;
102
+ /** The server's 2100 per request, less the two `sp_executesql` takes for the statement and its parameter list. */
103
+ maxBindValues = 2098;
104
+ maxInsertRows = 1000;
104
105
  /** `OUTPUT` has no trailing form: it sits between the column list and `VALUES`. */
105
106
  returningPosition = 'after-target';
106
107
  /** `OUTPUT` reads the written row off the `INSERTED` pseudo-table. */
@@ -11,7 +11,7 @@ type MsSqlResult = {
11
11
  rowsAffected: number[];
12
12
  };
13
13
  /** The part of the `Readable` a request streams into that a querier drives. */
14
- type MsSqlRowStream = AsyncIterable<unknown> & {
14
+ type MsSqlRowStream = AsyncIterable<RawRow> & {
15
15
  destroy(error: Error): unknown;
16
16
  on(event: 'error', listener: (error: Error) => void): unknown;
17
17
  };
@@ -49,7 +49,7 @@ export declare class MsSqlQuerier extends AbstractPoolQuerier<MsSqlConnection> {
49
49
  * rows arrive only as fast as they are read. A failure the driver reports through the promise alone
50
50
  * would leave the loop waiting for rows, so it ends the stream instead.
51
51
  */
52
- internalStream<T>(query: string, values?: unknown[]): AsyncGenerator<Awaited<T>, void, unknown>;
52
+ internalStream(query: string, values?: unknown[]): AsyncGenerator<RawRow, void, any>;
53
53
  /**
54
54
  * `mssql` owns its pool and hands out a `Request` per call, so a `BEGIN TRANSACTION` sent as text
55
55
  * would open one on a connection the next call may not be given. Its `Transaction` object is the
@@ -46,9 +46,7 @@ export class MsSqlQuerier extends AbstractPoolQuerier {
46
46
  rows.destroy(err instanceof Error ? err : new Error(String(err)));
47
47
  });
48
48
  try {
49
- for await (const row of rows) {
50
- yield row;
51
- }
49
+ yield* rows;
52
50
  }
53
51
  finally {
54
52
  // The cancel reports itself as an error on the stream, and the loop that would hear it is gone.
@@ -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>;