@mikro-orm/sql 7.1.5-dev.2 → 7.1.5-dev.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.5-dev.2",
3
+ "version": "7.1.5-dev.21",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.1.4"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.5-dev.2"
56
+ "@mikro-orm/core": "7.1.5-dev.21"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -389,7 +389,7 @@ export class NativeQueryBuilder {
389
389
  }
390
390
  processInsertData() {
391
391
  const dataAsArray = Utils.asArray(this.options.data);
392
- const keys = Object.keys(dataAsArray[0]);
392
+ const keys = dataAsArray.length > 1 ? [...new Set(dataAsArray.flatMap(row => Object.keys(row)))] : Object.keys(dataAsArray[0]);
393
393
  const values = keys.map(() => '?');
394
394
  const parts = [];
395
395
  this.parts.push(`(${keys.map(key => this.quote(key)).join(', ')})`);
@@ -39,7 +39,11 @@ export class ObjectCriteriaNode extends CriteriaNode {
39
39
  throw new Error('Mixing collection operators with other filters is not allowed.');
40
40
  }
41
41
  const payload = this.payload[key].unwrap();
42
- const qb2 = qb.clone(true, ['schema']);
42
+ // entities with a fixed schema must resolve the `from` table's own schema in the subquery,
43
+ // otherwise a nested operator inherits the root entity's schema (GH #7894); for wildcard or
44
+ // schema-less entities the schema is resolved dynamically and needs to be carried over
45
+ const fixedSchema = parentMeta.schema && parentMeta.schema !== '*';
46
+ const qb2 = qb.clone(true, fixedSchema ? undefined : ['schema']);
43
47
  const joinAlias = qb2.getNextAlias(this.prop.targetMeta.class);
44
48
  const sub = qb2
45
49
  .from(parentMeta.class)
@@ -600,6 +600,8 @@ export class QueryBuilderHelper {
600
600
  }
601
601
  return `(${tmp.join(', ')})`;
602
602
  }
603
+ // array custom types (e.g. `$contains`/`$overlap` on `string[]` columns) are converted to a single
604
+ // array literal, so we emit a single bound parameter — not one per element wrapped in a tuple
603
605
  if (prop?.customType instanceof ArrayType) {
604
606
  const item = prop.customType.convertToDatabaseValue(value, this.#platform, {
605
607
  fromQuery: true,
@@ -607,10 +609,9 @@ export class QueryBuilderHelper {
607
609
  mode: 'query',
608
610
  });
609
611
  params.push(item);
612
+ return '?';
610
613
  }
611
- else {
612
- value.forEach(p => params.push(p));
613
- }
614
+ value.forEach(p => params.push(p));
614
615
  return `(${value.map(() => '?').join(', ')})`;
615
616
  }
616
617
  if (value === null) {
@@ -1,5 +1,6 @@
1
1
  import { ReferenceKind, isRaw, } from '@mikro-orm/core';
2
2
  import { DatabaseTable } from './DatabaseTable.js';
3
+ import { normalizeViewDefinition } from './SchemaHelper.js';
3
4
  import { getTablePartitioning } from './partitioning.js';
4
5
  /**
5
6
  * @internal
@@ -364,7 +365,7 @@ export class DatabaseSchema {
364
365
  }
365
366
  static getViewDefinition(meta, em, platform) {
366
367
  if (typeof meta.expression === 'string') {
367
- return meta.expression;
368
+ return normalizeViewDefinition(meta.expression);
368
369
  }
369
370
  // Expression is a function, need to evaluate it
370
371
  /* v8 ignore next */
@@ -378,7 +379,7 @@ export class DatabaseSchema {
378
379
  }
379
380
  /* v8 ignore next */
380
381
  if (typeof result === 'string') {
381
- return result;
382
+ return normalizeViewDefinition(result);
382
383
  }
383
384
  /* v8 ignore next */
384
385
  if (isRaw(result)) {
@@ -6,6 +6,17 @@ import type { DatabaseSchema } from './DatabaseSchema.js';
6
6
  import type { DatabaseTable } from './DatabaseTable.js';
7
7
  /** Flattens `;\n` boundaries so the schema-generator's statement splitter doesn't break the routine DDL apart. Other whitespace is preserved. */
8
8
  export declare function stripStatementNewlines(body: string): string;
9
+ /**
10
+ * Strips SQL line comments and blank lines from a view definition. Comments are dropped by the
11
+ * database on `create view` anyway, and a `--` comment would otherwise comment out the rest of the
12
+ * definition once newlines are collapsed; blank lines act as statement separators in the
13
+ * schema-generator's statement splitter, which would break the view DDL apart.
14
+ *
15
+ * Comment stripping is not string-literal aware, so a `--` inside a string literal (or `a--b` style
16
+ * arithmetic) is treated as a comment — an accepted tradeoff to avoid a full SQL tokenizer.
17
+ * @see https://github.com/mikro-orm/mikro-orm/issues/7875
18
+ */
19
+ export declare function normalizeViewDefinition(definition: string): string;
9
20
  /** Base class for database-specific schema helpers. Provides SQL generation for DDL operations. */
10
21
  export declare abstract class SchemaHelper {
11
22
  protected readonly platform: AbstractSqlPlatform;
@@ -3,6 +3,24 @@ import { isRaw, Utils, } from '@mikro-orm/core';
3
3
  export function stripStatementNewlines(body) {
4
4
  return body.replace(/;[\t ]*\r?\n/g, '; ');
5
5
  }
6
+ /**
7
+ * Strips SQL line comments and blank lines from a view definition. Comments are dropped by the
8
+ * database on `create view` anyway, and a `--` comment would otherwise comment out the rest of the
9
+ * definition once newlines are collapsed; blank lines act as statement separators in the
10
+ * schema-generator's statement splitter, which would break the view DDL apart.
11
+ *
12
+ * Comment stripping is not string-literal aware, so a `--` inside a string literal (or `a--b` style
13
+ * arithmetic) is treated as a comment — an accepted tradeoff to avoid a full SQL tokenizer.
14
+ * @see https://github.com/mikro-orm/mikro-orm/issues/7875
15
+ */
16
+ export function normalizeViewDefinition(definition) {
17
+ return definition
18
+ .replace(/--[^\n]*/g, '')
19
+ .split('\n')
20
+ .filter(line => line.trim() !== '')
21
+ .join('\n')
22
+ .trim();
23
+ }
6
24
  /** Base class for database-specific schema helpers. Provides SQL generation for DDL operations. */
7
25
  export class SchemaHelper {
8
26
  platform;
@@ -130,7 +130,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
130
130
  await this.execute(this.helper.disableForeignKeysSQL());
131
131
  }
132
132
  const schema = options?.schema ?? this.config.get('schema', this.platform.getDefaultSchemaName());
133
- for (const meta of this.getOrderedMetadata(schema).reverse()) {
133
+ for (const meta of this.getOrderedMetadataForClear(schema).reverse()) {
134
134
  try {
135
135
  await this.driver
136
136
  .createQueryBuilder(meta.class, this.em?.getTransactionContext(), 'write', false)