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

Function getWhereCondition

packages/core/src/utils/upsert-utils.ts:169–207  ·  view source on GitHub ↗
(
  meta: EntityMetadata<T>,
  onConflictFields: (keyof T)[] | Raw | undefined,
  data: EntityData<T>,
  where: FilterQuery<T>,
)

Source from the content-addressed store, hash-verified

167
168/** @internal */
169export 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 */
210export function resetUntouchedCollections<Entity extends object>(meta: EntityMetadata<Entity>, entity: Entity): void {

Callers 2

upsertMethod · 0.85
upsertManyMethod · 0.85

Calls 6

isRawFunction · 0.85
getPropertyValueFunction · 0.85
filterMethod · 0.80
asArrayMethod · 0.80
reduceMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected