|
|
@ -2066,18 +2066,13 @@ class BaseModelSqlv2 { |
|
|
|
{ |
|
|
|
{ |
|
|
|
chunkSize: _chunkSize = 100, |
|
|
|
chunkSize: _chunkSize = 100, |
|
|
|
cookie, |
|
|
|
cookie, |
|
|
|
foreign_key_checks = true, |
|
|
|
|
|
|
|
raw = false, |
|
|
|
|
|
|
|
}: { |
|
|
|
}: { |
|
|
|
chunkSize?: number; |
|
|
|
chunkSize?: number; |
|
|
|
cookie?: any; |
|
|
|
cookie?: any; |
|
|
|
foreign_key_checks?: boolean; |
|
|
|
|
|
|
|
raw?: boolean; |
|
|
|
|
|
|
|
} = {} |
|
|
|
} = {} |
|
|
|
) { |
|
|
|
) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
// TODO: ag column handling for raw bulk insert
|
|
|
|
const insertDatas = await Promise.all( |
|
|
|
const insertDatas = raw ? datas : await Promise.all( |
|
|
|
|
|
|
|
datas.map(async (d) => { |
|
|
|
datas.map(async (d) => { |
|
|
|
await populatePk(this.model, d); |
|
|
|
await populatePk(this.model, d); |
|
|
|
return this.model.mapAliasToColumn(d); |
|
|
|
return this.model.mapAliasToColumn(d); |
|
|
@ -2086,10 +2081,8 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
// await this.beforeInsertb(insertDatas, null);
|
|
|
|
// await this.beforeInsertb(insertDatas, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (!raw) { |
|
|
|
for (const data of datas) { |
|
|
|
for (const data of datas) { |
|
|
|
await this.validate(data); |
|
|
|
await this.validate(data); |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// fallbacks to `10` if database client is sqlite
|
|
|
|
// fallbacks to `10` if database client is sqlite
|
|
|
@ -2097,38 +2090,18 @@ class BaseModelSqlv2 { |
|
|
|
// refer : https://www.sqlite.org/limits.html
|
|
|
|
// refer : https://www.sqlite.org/limits.html
|
|
|
|
const chunkSize = this.isSqlite ? 10 : _chunkSize; |
|
|
|
const chunkSize = this.isSqlite ? 10 : _chunkSize; |
|
|
|
|
|
|
|
|
|
|
|
const trx = await this.dbDriver.transaction(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!foreign_key_checks) { |
|
|
|
|
|
|
|
if (this.isPg) { |
|
|
|
|
|
|
|
await trx.raw('set session_replication_role to replica;'); |
|
|
|
|
|
|
|
} else if (this.isMySQL) { |
|
|
|
|
|
|
|
await trx.raw('SET foreign_key_checks = 0;'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const response = |
|
|
|
const response = |
|
|
|
this.isPg || this.isMssql |
|
|
|
this.isPg || this.isMssql |
|
|
|
? await trx |
|
|
|
? await this.dbDriver |
|
|
|
.batchInsert(this.tnPath, insertDatas, chunkSize) |
|
|
|
.batchInsert(this.tnPath, insertDatas, chunkSize) |
|
|
|
.returning(this.model.primaryKey?.column_name) |
|
|
|
.returning(this.model.primaryKey?.column_name) |
|
|
|
: await trx.batchInsert( |
|
|
|
: await this.dbDriver.batchInsert( |
|
|
|
this.tnPath, |
|
|
|
this.tnPath, |
|
|
|
insertDatas, |
|
|
|
insertDatas, |
|
|
|
chunkSize |
|
|
|
chunkSize |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (!foreign_key_checks) { |
|
|
|
await this.afterBulkInsert(insertDatas, this.dbDriver, cookie); |
|
|
|
if (this.isPg) { |
|
|
|
|
|
|
|
await trx.raw('set session_replication_role to origin;'); |
|
|
|
|
|
|
|
} else if (this.isMySQL) { |
|
|
|
|
|
|
|
await trx.raw('SET foreign_key_checks = 1;'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await trx.commit(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!raw) await this.afterBulkInsert(insertDatas, this.dbDriver, cookie); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
return response; |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
@ -2746,7 +2719,7 @@ class BaseModelSqlv2 { |
|
|
|
await this.afterInsert(response, this.dbDriver, cookie); |
|
|
|
await this.afterInsert(response, this.dbDriver, cookie); |
|
|
|
await this.afterAddChild(rowId, childId, cookie); |
|
|
|
await this.afterAddChild(rowId, childId, cookie); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async afterAddChild(rowId, childId, req): Promise<void> { |
|
|
|
public async afterAddChild(rowId, childId, req): Promise<void> { |
|
|
|
await Audit.insert({ |
|
|
|
await Audit.insert({ |
|
|
|
fk_model_id: this.model.id, |
|
|
|
fk_model_id: this.model.id, |
|
|
|