metal-orm 1.1.10 → 1.1.11

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/dist/index.cjs CHANGED
@@ -12353,6 +12353,25 @@ var databaseFunction = {
12353
12353
  fn: "DATABASE",
12354
12354
  args: []
12355
12355
  };
12356
+ var readMysqlField = (row, field) => {
12357
+ const record = row;
12358
+ if (Object.prototype.hasOwnProperty.call(record, field)) {
12359
+ return record[field];
12360
+ }
12361
+ const lower2 = field.toLowerCase();
12362
+ if (lower2 !== field && Object.prototype.hasOwnProperty.call(record, lower2)) {
12363
+ return record[lower2];
12364
+ }
12365
+ const upper2 = field.toUpperCase();
12366
+ if (upper2 !== field && Object.prototype.hasOwnProperty.call(record, upper2)) {
12367
+ return record[upper2];
12368
+ }
12369
+ return void 0;
12370
+ };
12371
+ var readMysqlStringField = (row, field) => {
12372
+ const value = readMysqlField(row, field);
12373
+ return typeof value === "string" ? value : void 0;
12374
+ };
12356
12375
  var mysqlIntrospector = {
12357
12376
  async introspect(ctx, options) {
12358
12377
  const schema = options.schema;
@@ -12494,39 +12513,68 @@ var mysqlIntrospector = {
12494
12513
  const indexRows = await runSelectNode(indexQuery, ctx);
12495
12514
  const tableComments = /* @__PURE__ */ new Map();
12496
12515
  tableRows.forEach((r) => {
12497
- const key = `${r.table_schema}.${r.table_name}`;
12498
- if (r.table_comment) {
12499
- tableComments.set(key, r.table_comment);
12516
+ const tableSchema = readMysqlStringField(r, "table_schema");
12517
+ const tableName = readMysqlStringField(r, "table_name");
12518
+ const tableComment = readMysqlStringField(r, "table_comment");
12519
+ if (!tableSchema || !tableName) return;
12520
+ const key = `${tableSchema}.${tableName}`;
12521
+ if (tableComment) {
12522
+ tableComments.set(key, tableComment);
12500
12523
  }
12501
12524
  });
12502
12525
  const pkMap = /* @__PURE__ */ new Map();
12503
12526
  pkRows.forEach((r) => {
12504
- const key = `${r.table_schema}.${r.table_name}`;
12527
+ const tableSchema = readMysqlStringField(r, "table_schema");
12528
+ const tableName = readMysqlStringField(r, "table_name");
12529
+ const columnName = readMysqlStringField(r, "column_name");
12530
+ if (!tableSchema || !tableName || !columnName) return;
12531
+ const key = `${tableSchema}.${tableName}`;
12505
12532
  const list = pkMap.get(key) || [];
12506
- list.push(r.column_name);
12533
+ list.push(columnName);
12507
12534
  pkMap.set(key, list);
12508
12535
  });
12509
12536
  const fkMap = /* @__PURE__ */ new Map();
12510
12537
  fkRows.forEach((r) => {
12511
- const key = `${r.table_schema}.${r.table_name}.${r.column_name}`;
12538
+ const tableSchema = readMysqlStringField(r, "table_schema");
12539
+ const tableName = readMysqlStringField(r, "table_name");
12540
+ const columnName = readMysqlStringField(r, "column_name");
12541
+ const constraintName = readMysqlStringField(r, "constraint_name");
12542
+ const referencedTableSchema = readMysqlStringField(r, "referenced_table_schema");
12543
+ const referencedTableName = readMysqlStringField(r, "referenced_table_name");
12544
+ const referencedColumnName = readMysqlStringField(r, "referenced_column_name");
12545
+ const deleteRule = readMysqlStringField(r, "delete_rule");
12546
+ const updateRule = readMysqlStringField(r, "update_rule");
12547
+ if (!tableSchema || !tableName || !columnName || !constraintName || !referencedTableSchema || !referencedTableName || !referencedColumnName) {
12548
+ return;
12549
+ }
12550
+ const key = `${tableSchema}.${tableName}.${columnName}`;
12512
12551
  const list = fkMap.get(key) || [];
12513
12552
  list.push({
12514
- table: `${r.referenced_table_schema}.${r.referenced_table_name}`,
12515
- column: r.referenced_column_name,
12516
- onDelete: r.delete_rule,
12517
- onUpdate: r.update_rule,
12518
- name: r.constraint_name
12553
+ table: `${referencedTableSchema}.${referencedTableName}`,
12554
+ column: referencedColumnName,
12555
+ onDelete: deleteRule,
12556
+ onUpdate: updateRule,
12557
+ name: constraintName
12519
12558
  });
12520
12559
  fkMap.set(key, list);
12521
12560
  });
12522
12561
  const tablesByKey = /* @__PURE__ */ new Map();
12523
12562
  columnRows.forEach((r) => {
12524
- const key = `${r.table_schema}.${r.table_name}`;
12525
- if (!shouldIncludeTable(r.table_name, options)) return;
12563
+ const tableSchema = readMysqlStringField(r, "table_schema");
12564
+ const tableName = readMysqlStringField(r, "table_name");
12565
+ const columnName = readMysqlStringField(r, "column_name");
12566
+ const columnType = readMysqlStringField(r, "column_type") || readMysqlStringField(r, "data_type");
12567
+ const isNullable = readMysqlStringField(r, "is_nullable");
12568
+ const columnDefault = readMysqlField(r, "column_default");
12569
+ const extra = readMysqlStringField(r, "extra");
12570
+ const columnComment = readMysqlStringField(r, "column_comment");
12571
+ if (!tableSchema || !tableName || !columnName || !columnType || !isNullable) return;
12572
+ const key = `${tableSchema}.${tableName}`;
12573
+ if (!shouldIncludeTable(tableName, options)) return;
12526
12574
  if (!tablesByKey.has(key)) {
12527
12575
  tablesByKey.set(key, {
12528
- name: r.table_name,
12529
- schema: r.table_schema,
12576
+ name: tableName,
12577
+ schema: tableSchema,
12530
12578
  columns: [],
12531
12579
  primaryKey: pkMap.get(key) || [],
12532
12580
  indexes: [],
@@ -12534,17 +12582,16 @@ var mysqlIntrospector = {
12534
12582
  });
12535
12583
  }
12536
12584
  const table = tablesByKey.get(key);
12537
- const columnType = r.column_type || r.data_type;
12538
- const comment = r.column_comment?.trim() ? r.column_comment : void 0;
12585
+ const comment = columnComment?.trim() ? columnComment : void 0;
12539
12586
  const column = {
12540
- name: r.column_name,
12587
+ name: columnName,
12541
12588
  type: columnType,
12542
- notNull: r.is_nullable === "NO",
12543
- default: r.column_default ?? void 0,
12544
- autoIncrement: typeof r.extra === "string" && r.extra.includes("auto_increment"),
12589
+ notNull: isNullable === "NO",
12590
+ default: columnDefault ?? void 0,
12591
+ autoIncrement: typeof extra === "string" && extra.includes("auto_increment"),
12545
12592
  comment
12546
12593
  };
12547
- const fk = fkMap.get(`${key}.${r.column_name}`)?.[0];
12594
+ const fk = fkMap.get(`${key}.${columnName}`)?.[0];
12548
12595
  if (fk) {
12549
12596
  column.references = {
12550
12597
  table: fk.table,
@@ -12557,14 +12604,20 @@ var mysqlIntrospector = {
12557
12604
  table.columns.push(column);
12558
12605
  });
12559
12606
  indexRows.forEach((r) => {
12560
- const key = `${r.table_schema}.${r.table_name}`;
12607
+ const tableSchema = readMysqlStringField(r, "table_schema");
12608
+ const tableName = readMysqlStringField(r, "table_name");
12609
+ const indexName = readMysqlStringField(r, "index_name");
12610
+ const nonUnique = readMysqlField(r, "non_unique");
12611
+ const colsValue = readMysqlField(r, "cols");
12612
+ if (!tableSchema || !tableName || !indexName) return;
12613
+ const key = `${tableSchema}.${tableName}`;
12561
12614
  const table = tablesByKey.get(key);
12562
12615
  if (!table) return;
12563
- const cols = (typeof r.cols === "string" ? r.cols.split(",") : []).map((c) => ({ column: c.trim() }));
12616
+ const cols = (typeof colsValue === "string" ? colsValue.split(",") : []).map((c) => ({ column: c.trim() }));
12564
12617
  const idx = {
12565
- name: r.index_name,
12618
+ name: indexName,
12566
12619
  columns: cols,
12567
- unique: r.non_unique === 0
12620
+ unique: Number(nonUnique) === 0
12568
12621
  };
12569
12622
  table.indexes = table.indexes || [];
12570
12623
  table.indexes.push(idx);
@@ -12626,25 +12679,35 @@ var mysqlIntrospector = {
12626
12679
  const viewColumnRows = await runSelectNode(viewColumnsQuery, ctx);
12627
12680
  const viewsByKey = /* @__PURE__ */ new Map();
12628
12681
  for (const r of viewRows) {
12629
- if (!shouldIncludeView(r.table_name, options)) continue;
12630
- const key = `${r.table_schema}.${r.table_name}`;
12682
+ const tableSchema = readMysqlStringField(r, "table_schema");
12683
+ const tableName = readMysqlStringField(r, "table_name");
12684
+ const viewDefinition = readMysqlStringField(r, "view_definition");
12685
+ if (!tableSchema || !tableName) continue;
12686
+ if (!shouldIncludeView(tableName, options)) continue;
12687
+ const key = `${tableSchema}.${tableName}`;
12631
12688
  viewsByKey.set(key, {
12632
- name: r.table_name,
12633
- schema: r.table_schema,
12689
+ name: tableName,
12690
+ schema: tableSchema,
12634
12691
  columns: [],
12635
- definition: r.view_definition || void 0
12692
+ definition: viewDefinition || void 0
12636
12693
  });
12637
12694
  }
12638
12695
  for (const r of viewColumnRows) {
12639
- const key = `${r.table_schema}.${r.table_name}`;
12696
+ const tableSchema = readMysqlStringField(r, "table_schema");
12697
+ const tableName = readMysqlStringField(r, "table_name");
12698
+ const columnName = readMysqlStringField(r, "column_name");
12699
+ const columnType = readMysqlStringField(r, "column_type") || readMysqlStringField(r, "data_type");
12700
+ const isNullable = readMysqlStringField(r, "is_nullable");
12701
+ const columnComment = readMysqlStringField(r, "column_comment");
12702
+ if (!tableSchema || !tableName || !columnName || !columnType || !isNullable) continue;
12703
+ const key = `${tableSchema}.${tableName}`;
12640
12704
  const view = viewsByKey.get(key);
12641
12705
  if (!view) continue;
12642
- const columnType = r.column_type || r.data_type;
12643
12706
  const column = {
12644
- name: r.column_name,
12707
+ name: columnName,
12645
12708
  type: columnType,
12646
- notNull: r.is_nullable === "NO",
12647
- comment: r.column_comment?.trim() || void 0
12709
+ notNull: isNullable === "NO",
12710
+ comment: columnComment?.trim() || void 0
12648
12711
  };
12649
12712
  view.columns.push(column);
12650
12713
  }
@@ -21710,21 +21773,21 @@ var BulkInsertExecutor = class extends BulkBaseExecutor {
21710
21773
  constructor(session, table, rows, options = {}) {
21711
21774
  super(session, table, rows, options);
21712
21775
  }
21713
- async executeChunk(chunk, chunkIndex) {
21776
+ async executeChunk(chunk, _chunkIndex) {
21714
21777
  const returningColumns = resolveReturningColumns(this.ctx, this.table, this.options.returning);
21715
21778
  let builder = new InsertQueryBuilder(this.table).values(chunk);
21716
21779
  if (this.options.onConflict) {
21717
21780
  const conflictColumns = this.options.onConflict.target?.columns ?? [];
21718
- builder = builder.onConflict(conflictColumns);
21781
+ const conflictBuilder = builder.onConflict(conflictColumns);
21719
21782
  if (this.options.onConflict.action.type === "DoNothing") {
21720
- builder = builder.doNothing();
21783
+ builder = conflictBuilder.doNothing();
21721
21784
  } else if (this.options.onConflict.action.type === "DoUpdate" && this.options.onConflict.action.set) {
21722
21785
  const setMap = {};
21723
21786
  for (const assignment of this.options.onConflict.action.set) {
21724
21787
  const colName = typeof assignment.column === "object" ? assignment.column.name : assignment.column;
21725
21788
  setMap[colName] = assignment.value;
21726
21789
  }
21727
- builder = builder.doUpdate(setMap);
21790
+ builder = conflictBuilder.doUpdate(setMap);
21728
21791
  }
21729
21792
  }
21730
21793
  const finalBuilder = builder;
@@ -21759,7 +21822,7 @@ var BulkUpdateExecutor = class extends BulkBaseExecutor {
21759
21822
  super(session, table, rows, options);
21760
21823
  this.byColumns = resolveByColumns(table, options.by);
21761
21824
  }
21762
- async executeChunk(chunk, chunkIndex) {
21825
+ async executeChunk(chunk, _chunkIndex) {
21763
21826
  const allReturning = [];
21764
21827
  const returningColumns = resolveReturningColumns(this.ctx, this.table, this.options.returning);
21765
21828
  const extraWhere = this.options.where;
@@ -21879,7 +21942,7 @@ var BulkDeleteExecutor = class extends BulkBaseExecutor {
21879
21942
  super(session, table, ids, options);
21880
21943
  this.byColumnName = options.by ?? findPrimaryKey(table);
21881
21944
  }
21882
- async executeChunk(chunk, chunkIndex) {
21945
+ async executeChunk(chunk, _chunkIndex) {
21883
21946
  const byColumn = this.table.columns[this.byColumnName];
21884
21947
  if (!byColumn) {
21885
21948
  throw new Error(
@@ -21935,7 +21998,7 @@ var BulkUpsertExecutor = class extends BulkBaseExecutor {
21935
21998
  const conflictSet = new Set(conflictTargetNames);
21936
21999
  this.updateColumns = options.updateColumns ?? Object.keys(rows[0] ?? {}).filter((col2) => !conflictSet.has(col2) && col2 in table.columns);
21937
22000
  }
21938
- async executeChunk(chunk, chunkIndex) {
22001
+ async executeChunk(chunk, _chunkIndex) {
21939
22002
  const returningColumns = resolveReturningColumns(this.ctx, this.table, this.options.returning);
21940
22003
  const set = {};
21941
22004
  for (const col2 of this.updateColumns) {