(
leafMeta: EntityMetadata<T>,
where: FilterQuery<T>,
overrides?: EntityData<T>,
options: NativeInsertUpdateOptions<T> = {},
)
| 1017 | } |
| 1018 | |
| 1019 | private async nativeCloneTPT<T extends object>( |
| 1020 | leafMeta: EntityMetadata<T>, |
| 1021 | where: FilterQuery<T>, |
| 1022 | overrides?: EntityData<T>, |
| 1023 | options: NativeInsertUpdateOptions<T> = {}, |
| 1024 | ): Promise<QueryResult<T>> { |
| 1025 | const hierarchy: EntityMetadata[] = []; |
| 1026 | let current: EntityMetadata | undefined = leafMeta; |
| 1027 | |
| 1028 | while (current) { |
| 1029 | hierarchy.unshift(current); |
| 1030 | current = current.tptParent; |
| 1031 | } |
| 1032 | |
| 1033 | const rootMeta = hierarchy[0]; |
| 1034 | let newPk: any; |
| 1035 | let rootResult: QueryResult<T> | undefined; |
| 1036 | |
| 1037 | for (const tableMeta of hierarchy) { |
| 1038 | const props = this.getCloneableProps(tableMeta as EntityMetadata<T>, true); |
| 1039 | const mappedOverrides = this.mapCloneOverrides(overrides, tableMeta as EntityMetadata<T>, options); |
| 1040 | const { selectFields, insertColumns } = this.buildCloneFields( |
| 1041 | props, |
| 1042 | mappedOverrides, |
| 1043 | tableMeta as EntityMetadata<T>, |
| 1044 | ); |
| 1045 | |
| 1046 | // For child tables, prepend the new PK value |
| 1047 | if (tableMeta !== rootMeta && newPk != null) { |
| 1048 | for (const pkName of tableMeta.primaryKeys) { |
| 1049 | const prop = tableMeta.properties[pkName as EntityKey<T>] as EntityProperty; |
| 1050 | |
| 1051 | for (const fieldName of prop.fieldNames) { |
| 1052 | insertColumns.unshift(fieldName); |
| 1053 | selectFields.unshift(raw('? as ??', [newPk, fieldName])); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | const sourceWhere = |
| 1059 | tableMeta === rootMeta ? where : ((Utils.extractPK(where as any, tableMeta) as FilterQuery<T>) ?? where); |
| 1060 | |
| 1061 | const abort = pickAbortOptions(options); |
| 1062 | const selectQb = this.createQueryBuilder<T>( |
| 1063 | tableMeta.class as any, |
| 1064 | options.ctx, |
| 1065 | 'read', |
| 1066 | options.convertCustomTypes, |
| 1067 | options.loggerContext, |
| 1068 | ).withSchema(this.getSchemaName(tableMeta as EntityMetadata<T>, options)); |
| 1069 | selectQb.select(selectFields as any).where(sourceWhere as any); |
| 1070 | selectQb.setAbortOptions(abort); |
| 1071 | |
| 1072 | const insertQb = this.createQueryBuilder<T>( |
| 1073 | tableMeta.class as any, |
| 1074 | options.ctx, |
| 1075 | 'write', |
| 1076 | options.convertCustomTypes, |
nothing calls this directly
no test coverage detected