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

Method nativeClone

packages/mongodb/src/MongoDriver.ts:334–368  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: FilterQuery<T>,
    overrides?: EntityData<T>,
    options: NativeInsertUpdateOptions<T> = {},
  )

Source from the content-addressed store, hash-verified

332 }
333
334 override async nativeClone<T extends object>(
335 entityName: EntityName<T>,
336 where: FilterQuery<T>,
337 overrides?: EntityData<T>,
338 options: NativeInsertUpdateOptions<T> = {},
339 ): Promise<QueryResult<T>> {
340 const meta = this.metadata.find(entityName)!;
341 const pk = meta.getPrimaryProps()[0].fieldNames[0] ?? '_id';
342 const normalizedWhere = Utils.isPrimaryKey(where) ? ({ [pk]: where } as FilterQuery<T>) : where;
343 const renameWhere = this.renameFields(entityName, normalizedWhere as unknown as EntityDictionary<T>);
344 const source = await this.rethrow(
345 this.getConnection('read').find<T>(entityName, renameWhere as FilterQuery<T>, { ctx: options.ctx, limit: 1 }),
346 );
347
348 if (!source.length) {
349 throw new Error('Cannot clone: no entity found matching the given condition');
350 }
351
352 const doc = source[0] as Dictionary;
353 delete doc[pk];
354
355 if (overrides) {
356 const mapped = this.renameFields(entityName, overrides as unknown as EntityDictionary<T>);
357 Object.assign(doc, mapped);
358 }
359
360 if (meta.versionProperty) {
361 const vProp = meta.properties[meta.versionProperty];
362 doc[vProp.fieldNames[0]] = vProp.runtimeType === 'Date' ? new Date() : 1;
363 }
364
365 return this.rethrow(
366 this.getConnection('write').insertOne<T>(entityName, doc as EntityDictionary<T>, options.ctx),
367 ) as unknown as Promise<QueryResult<T>>;
368 }
369
370 async nativeInsertMany<T extends object>(
371 entityName: EntityName<T>,

Callers

nothing calls this directly

Calls 8

renameFieldsMethod · 0.95
getPrimaryPropsMethod · 0.80
isPrimaryKeyMethod · 0.80
rethrowMethod · 0.80
insertOneMethod · 0.80
findMethod · 0.65
getConnectionMethod · 0.65
assignMethod · 0.65

Tested by

no test coverage detected