-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
MySqlDriver.ts
29 lines (23 loc) · 1.23 KB
/
MySqlDriver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { Configuration, EntityDictionary, NativeInsertUpdateManyOptions, QueryResult } from '@mikro-orm/core';
import { AbstractSqlDriver } from '@mikro-orm/knex';
import { MySqlConnection } from './MySqlConnection';
import { MySqlPlatform } from './MySqlPlatform';
export class MySqlDriver extends AbstractSqlDriver<MySqlConnection, MySqlPlatform> {
constructor(config: Configuration) {
super(config, new MySqlPlatform(), MySqlConnection, ['knex', 'mysql2']);
}
async init(): Promise<void> {
await super.init();
// preload the value early
await this.platform.getAutoIncrementIncrement(this.connection);
}
async nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options: NativeInsertUpdateManyOptions<T> = {}): Promise<QueryResult<T>> {
options.processCollections ??= true;
const res = await super.nativeInsertMany(entityName, data, options);
const pks = this.getPrimaryKeyFields(entityName);
const autoIncrementIncrement = await this.platform.getAutoIncrementIncrement(this.connection);
data.forEach((item, idx) => res.rows![idx] = { [pks[0]]: item[pks[0]] ?? res.insertId as number + (idx * autoIncrementIncrement) });
res.row = res.rows![0];
return res;
}
}