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

Function nativeInsert

packages/sql/src/AbstractSqlDriver.ts:927–964  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    data: EntityDictionary<T>,
    options: NativeInsertUpdateOptions<T> = {},
  )

Source from the content-addressed store, hash-verified

925 }
926
927 async nativeInsert<T extends object>(
928 entityName: EntityName<T>,
929 data: EntityDictionary<T>,
930 options: NativeInsertUpdateOptions<T> = {},
931 ): Promise<QueryResult<T>> {
932 options.convertCustomTypes ??= true;
933 const meta = this.metadata.get(entityName);
934 const collections = this.extractManyToMany(meta, data);
935 const qb = this.createQueryBuilder(
936 entityName,
937 options.ctx,
938 'write',
939 options.convertCustomTypes,
940 options.loggerContext,
941 ).withSchema(this.getSchemaName(meta, options));
942 qb.setAbortOptions(pickAbortOptions(options));
943 const res = await this.rethrow(qb.insert(data as unknown as RequiredEntityData<T>).execute('run', false));
944 res.row = res.row || {};
945 let pk: any;
946
947 if (meta.primaryKeys.length > 1) {
948 // owner has composite pk
949 pk = Utils.getPrimaryKeyCond(data as T, meta.primaryKeys);
950 } else {
951 /* v8 ignore next */
952 res.insertId = data[meta.primaryKeys[0]] ?? res.insertId ?? res.row[meta.primaryKeys[0]];
953
954 if (options.convertCustomTypes && meta?.getPrimaryProp().customType) {
955 pk = [meta!.getPrimaryProp().customType!.convertToDatabaseValue(res.insertId, this.platform)];
956 } else {
957 pk = [res.insertId];
958 }
959 }
960
961 await this.processManyToMany(meta, pk, collections, false, options);
962
963 return res;
964 }
965
966 override async nativeClone<T extends object>(
967 entityName: EntityName<T>,

Callers

nothing calls this directly

Calls 12

pickAbortOptionsFunction · 0.85
setAbortOptionsMethod · 0.80
rethrowMethod · 0.80
getPrimaryKeyCondMethod · 0.80
getPrimaryPropMethod · 0.80
getMethod · 0.65
withSchemaMethod · 0.65
getSchemaNameMethod · 0.65
executeMethod · 0.65
insertMethod · 0.65
createQueryBuilderMethod · 0.45

Tested by

no test coverage detected