| 167 | |
| 168 | /** @internal */ |
| 169 | export function getWhereCondition<T extends object>( |
| 170 | meta: EntityMetadata<T>, |
| 171 | onConflictFields: (keyof T)[] | Raw | undefined, |
| 172 | data: EntityData<T>, |
| 173 | where: FilterQuery<T>, |
| 174 | ): { where: FilterQuery<T>; propIndex: number | false } { |
| 175 | const unique = (onConflictFields as string[]) ?? meta.props.filter(p => p.unique).map(p => p.name); |
| 176 | const propIndex = |
| 177 | !isRaw(unique) && |
| 178 | unique.findIndex(p => (data as Dictionary)[p] ?? (data as Dictionary)[p.substring(0, p.indexOf('.'))] != null); |
| 179 | |
| 180 | if (onConflictFields || where == null) { |
| 181 | if (propIndex !== false && propIndex >= 0) { |
| 182 | let key = unique[propIndex]; |
| 183 | |
| 184 | if (key.includes('.')) { |
| 185 | const prop = meta.properties[key.substring(0, key.indexOf('.')) as EntityKey<T>]; |
| 186 | |
| 187 | if (prop) { |
| 188 | key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | where = { [key]: getPropertyValue(data as Dictionary, unique[propIndex]) } as FilterQuery<T>; |
| 193 | } else if (meta.uniques.length > 0) { |
| 194 | for (const u of meta.uniques) { |
| 195 | if (Utils.asArray<EntityKey<T>>(u.properties).every(p => data[p] != null)) { |
| 196 | where = Utils.asArray<EntityKey<T>>(u.properties).reduce((o, key) => { |
| 197 | o[key] = data[key]; |
| 198 | return o; |
| 199 | }, {} as any); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return { where, propIndex }; |
| 207 | } |
| 208 | |
| 209 | /** @internal */ |
| 210 | export function resetUntouchedCollections<Entity extends object>(meta: EntityMetadata<Entity>, entity: Entity): void { |