MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / nativeUpdateMany

Function nativeUpdateMany

packages/sql/src/AbstractSqlDriver.ts:1441–1650  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: FilterQuery<T>[],
    data: EntityDictionary<T>[],
    options: NativeInsertUpdateManyOptions<T> & UpsertManyOptions<T> = {},
    transform?: (sql: string, params: any[]) => string,
  )

Source from the content-addressed store, hash-verified

1439 }
1440
1441 override async nativeUpdateMany<T extends object>(
1442 entityName: EntityName<T>,
1443 where: FilterQuery<T>[],
1444 data: EntityDictionary<T>[],
1445 options: NativeInsertUpdateManyOptions<T> & UpsertManyOptions<T> = {},
1446 transform?: (sql: string, params: any[]) => string,
1447 ): Promise<QueryResult<T>> {
1448 options.processCollections ??= true;
1449 options.convertCustomTypes ??= true;
1450 const meta = this.metadata.get<T>(entityName);
1451
1452 if (options.upsert) {
1453 const uniqueFields =
1454 options.onConflictFields ??
1455 ((Utils.isPlainObject(where[0])
1456 ? Object.keys(where[0]).flatMap(key => Utils.splitPrimaryKeys(key))
1457 : meta.primaryKeys) as (keyof T)[]);
1458 const qb = this.createQueryBuilder<T>(
1459 entityName,
1460 options.ctx,
1461 'write',
1462 options.convertCustomTypes,
1463 options.loggerContext,
1464 ).withSchema(this.getSchemaName(meta, options));
1465 qb.setAbortOptions(pickAbortOptions(options));
1466 const returning = getOnConflictReturningFields(meta, data[0], uniqueFields, options);
1467 qb.insert(data as T[])
1468 .onConflict(uniqueFields as any)
1469 .returning(returning as any);
1470
1471 if (!options.onConflictAction || options.onConflictAction === 'merge') {
1472 const fields = getOnConflictFields(meta, data[0], uniqueFields, options);
1473 qb.merge(fields as any);
1474 }
1475
1476 if (options.onConflictAction === 'ignore') {
1477 qb.ignore();
1478 }
1479
1480 if (options.onConflictWhere) {
1481 qb.where(options.onConflictWhere as any);
1482 }
1483
1484 return this.rethrow(qb.execute('run', false));
1485 }
1486
1487 const collections = options.processCollections ? data.map(d => this.extractManyToMany(meta, d)) : [];
1488 const keys = new Set<EntityKey<T>>();
1489 const fields = new Set<string>();
1490 const returning = new Set<EntityKey<T>>();
1491
1492 for (const row of data) {
1493 for (const k of Utils.keys(row)) {
1494 keys.add(k as EntityKey<T>);
1495
1496 if (isRaw(row[k])) {
1497 returning.add(k);
1498 }

Callers

nothing calls this directly

Calls 15

getOnConflictFieldsFunction · 0.90
isRawFunction · 0.90
pickAbortOptionsFunction · 0.85
addParamsFunction · 0.85
withAbortContextFunction · 0.85
isPlainObjectMethod · 0.80
splitPrimaryKeysMethod · 0.80
setAbortOptionsMethod · 0.80
ignoreMethod · 0.80
rethrowMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected