{"version":3,"file":"inserted-ids.mjs","sources":["../../src/entity-manager/inserted-ids.ts"],"sourcesContent":["import type { ID } from '../types';\n\n/**\n * MySQL/MariaDB inserts do not use RETURNING. Knex reports only the first\n * auto-increment id of a multi-row INSERT (`[insertId]`), while InnoDB assigns\n * the rest `auto_increment_increment` apart. Postgres and SQLite already\n * return one id per row.\n */\nconst idsFromInsertResult = (\n  chunkResult: unknown,\n  rowCount: number,\n  useReturning: boolean,\n  increment = 1\n): ID[] => {\n  const list = Array.isArray(chunkResult) ? chunkResult : [chunkResult];\n  const ids = list.map((entry) => {\n    if (entry && typeof entry === 'object' && 'id' in entry) {\n      return (entry as { id: ID }).id;\n    }\n\n    return entry as ID;\n  });\n\n  if (useReturning || ids.length !== 1 || rowCount <= 1) {\n    return ids;\n  }\n\n  const first = Number(ids[0]);\n  if (!Number.isSafeInteger(first) || first <= 0) {\n    return ids;\n  }\n\n  const step = Number.isSafeInteger(increment) && increment > 0 ? increment : 1;\n\n  return Array.from({ length: rowCount }, (_, index) => first + index * step);\n};\n\n/**\n * MySQL only reports the first id of a multi-row insert. The rest are\n * `auto_increment_increment` apart, which is 1 unless the server is configured\n * otherwise (Group Replication, Galera).\n */\nconst readAutoIncrementStep = async (connection: {\n  raw: (sql: string) => Promise<unknown>;\n}): Promise<number> => {\n  try {\n    const result = await connection.raw('SELECT @@auto_increment_increment AS step');\n    const rows = Array.isArray(result) ? result[0] : (result as { rows?: unknown }).rows;\n    const step = Number(\n      Array.isArray(rows) ? (rows[0] as { step?: unknown } | undefined)?.step : undefined\n    );\n\n    if (Number.isSafeInteger(step) && step > 0) {\n      return step;\n    }\n  } catch {\n    // Drivers that cannot read the setting keep the consecutive-id default.\n  }\n\n  return 1;\n};\n\n/**\n * One MySQL statement can be expanded from a single insert id only when every\n * generated value stays on that sequence. An explicit id greater than the\n * counter jumps the next generated value, and the driver does not report the\n * jump. Keep each statement either all explicit or all generated.\n */\nconst splitByIdSource = (rows: Array<Record<string, unknown>>) => {\n  const runs: Array<Array<Record<string, unknown>>> = [];\n\n  for (const row of rows) {\n    const explicit = row.id != null;\n    const current = runs.at(-1);\n    const currentExplicit = current?.[0]?.id != null;\n\n    if (!current || currentExplicit !== explicit) {\n      runs.push([row]);\n      continue;\n    }\n\n    current.push(row);\n  }\n\n  return runs;\n};\n\n/**\n * Rows that already carry a primary key must keep those ids. MySQL reports\n * one insert id per statement: the first generated value. Callers pass rows\n * from `splitByIdSource`, so a statement does not mix the two.\n */\nconst idsForInsertedRows = (\n  rows: Array<Record<string, unknown>>,\n  chunkResult: unknown,\n  useReturning: boolean,\n  increment = 1\n): ID[] => {\n  if (useReturning || rows.length === 0) {\n    return idsFromInsertResult(chunkResult, rows.length, useReturning, increment);\n  }\n\n  const explicit = rows.map((row) => row.id != null);\n  const explicitCount = explicit.filter(Boolean).length;\n\n  if (explicitCount === rows.length) {\n    return rows.map((row) => row.id as ID);\n  }\n\n  if (explicitCount !== 0) {\n    throw new Error(\n      'MySQL createMany inserts explicit ids and generated ids as separate statements'\n    );\n  }\n\n  return idsFromInsertResult(chunkResult, rows.length, false, increment);\n};\n\nexport { idsForInsertedRows, idsFromInsertResult, readAutoIncrementStep, splitByIdSource };\n"],"names":["idsFromInsertResult","chunkResult","rowCount","useReturning","increment","list","Array","isArray","ids","map","entry","id","length","first","Number","isSafeInteger","step","from","_","index","readAutoIncrementStep","connection","result","raw","rows","undefined","splitByIdSource","runs","row","explicit","current","at","currentExplicit","push","idsForInsertedRows","explicitCount","filter","Boolean","Error"],"mappings":"AAEA;;;;;AAKC,UACKA,mBAAAA,GAAsB,CAC1BC,aACAC,QAAAA,EACAC,YAAAA,EACAC,YAAY,CAAC,GAAA;AAEb,IAAA,MAAMC,IAAAA,GAAOC,KAAAA,CAAMC,OAAO,CAACN,eAAeA,WAAAA,GAAc;AAACA,QAAAA;AAAY,KAAA;AACrE,IAAA,MAAMO,GAAAA,GAAMH,IAAAA,CAAKI,GAAG,CAAC,CAACC,KAAAA,GAAAA;AACpB,Q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