(
entityName: EntityName<T>,
where: FilterQuery<T>,
data: EntityDictionary<T>,
options: NativeInsertUpdateOptions<T> & UpsertOptions<T> = {},
)
| 1364 | } |
| 1365 | |
| 1366 | async nativeUpdate<T extends object>( |
| 1367 | entityName: EntityName<T>, |
| 1368 | where: FilterQuery<T>, |
| 1369 | data: EntityDictionary<T>, |
| 1370 | options: NativeInsertUpdateOptions<T> & UpsertOptions<T> = {}, |
| 1371 | ): Promise<QueryResult<T>> { |
| 1372 | options.convertCustomTypes ??= true; |
| 1373 | const meta = this.metadata.get(entityName); |
| 1374 | const pks = this.getPrimaryKeyFields(meta); |
| 1375 | const collections = this.extractManyToMany(meta, data); |
| 1376 | let res = { affectedRows: 0, insertId: 0, row: {} } as QueryResult<T>; |
| 1377 | |
| 1378 | if (Utils.isPrimaryKey(where) && pks.length === 1) { |
| 1379 | /* v8 ignore next */ |
| 1380 | where = { [meta.primaryKeys[0] ?? pks[0]]: where } as FilterQuery<T>; |
| 1381 | } |
| 1382 | |
| 1383 | if (!options.upsert && options.unionWhere?.length) { |
| 1384 | where = (await this.applyUnionWhere(meta, where as ObjectQuery<T>, options, true)) as FilterQuery<T>; |
| 1385 | } |
| 1386 | |
| 1387 | if (Utils.hasObjectKeys(data)) { |
| 1388 | const qb = this.createQueryBuilder<T>( |
| 1389 | entityName, |
| 1390 | options.ctx, |
| 1391 | 'write', |
| 1392 | options.convertCustomTypes, |
| 1393 | options.loggerContext, |
| 1394 | ).withSchema(this.getSchemaName(meta, options)); |
| 1395 | qb.setAbortOptions(pickAbortOptions(options)); |
| 1396 | |
| 1397 | if (options.upsert) { |
| 1398 | /* v8 ignore next */ |
| 1399 | const uniqueFields = |
| 1400 | options.onConflictFields ?? |
| 1401 | ((Utils.isPlainObject(where) ? (Utils.keys(where) as EntityKey<T>[]) : meta.primaryKeys) as (keyof T)[]); |
| 1402 | const returning = getOnConflictReturningFields(meta, data, uniqueFields, options); |
| 1403 | qb.insert(data as T) |
| 1404 | .onConflict(uniqueFields as any) |
| 1405 | .returning(returning as any); |
| 1406 | |
| 1407 | if (!options.onConflictAction || options.onConflictAction === 'merge') { |
| 1408 | const fields = getOnConflictFields(meta, data, uniqueFields, options); |
| 1409 | qb.merge(fields as any); |
| 1410 | } |
| 1411 | |
| 1412 | if (options.onConflictAction === 'ignore') { |
| 1413 | qb.ignore(); |
| 1414 | } |
| 1415 | |
| 1416 | if (options.onConflictWhere) { |
| 1417 | qb.where(options.onConflictWhere as any); |
| 1418 | } |
| 1419 | } else { |
| 1420 | qb.update(data).where(where as any); |
| 1421 | |
| 1422 | // reload generated columns and version fields |
| 1423 | const returning: string[] = []; |
nothing calls this directly
no test coverage detected