(
entityName: EntityName<T>,
data: EntityDictionary<T>[],
options: NativeInsertUpdateManyOptions<T> = {},
transform?: (sql: string) => string,
)
| 1153 | } |
| 1154 | |
| 1155 | async nativeInsertMany<T extends object>( |
| 1156 | entityName: EntityName<T>, |
| 1157 | data: EntityDictionary<T>[], |
| 1158 | options: NativeInsertUpdateManyOptions<T> = {}, |
| 1159 | transform?: (sql: string) => string, |
| 1160 | ): Promise<QueryResult<T>> { |
| 1161 | options.processCollections ??= true; |
| 1162 | options.convertCustomTypes ??= true; |
| 1163 | const entityMeta = this.metadata.get(entityName); |
| 1164 | const meta = entityMeta.inheritanceType === 'tpt' ? entityMeta : entityMeta.root; |
| 1165 | const collections = options.processCollections ? data.map(d => this.extractManyToMany(meta, d)) : []; |
| 1166 | const pks = this.getPrimaryKeyFields(meta); |
| 1167 | const set = new Set<EntityKey<T>>(); |
| 1168 | data.forEach(row => Utils.keys(row).forEach(k => set.add(k))); |
| 1169 | const props = [...set].map(name => meta.properties[name] ?? { name, fieldNames: [name] }) as EntityProperty<T>[]; |
| 1170 | // For STI with conflicting fieldNames, include all alternative columns |
| 1171 | let fields = Utils.flatten(props.map(prop => prop.stiFieldNames ?? prop.fieldNames)); |
| 1172 | const duplicates = Utils.findDuplicates(fields); |
| 1173 | const params: unknown[] = []; |
| 1174 | |
| 1175 | if (duplicates.length) { |
| 1176 | fields = Utils.unique(fields); |
| 1177 | } |
| 1178 | |
| 1179 | const tableName = this.getTableName(meta, options); |
| 1180 | let sql = `insert into ${tableName} `; |
| 1181 | sql += |
| 1182 | fields.length > 0 |
| 1183 | ? '(' + fields.map(k => this.platform.quoteIdentifier(k)).join(', ') + ')' |
| 1184 | : `(${this.platform.quoteIdentifier(pks[0])})`; |
| 1185 | |
| 1186 | if (this.platform.usesOutputStatement()) { |
| 1187 | const returningProps = this.getTableProps(meta) |
| 1188 | .filter(prop => (prop.persist !== false && prop.defaultRaw) || prop.autoincrement || prop.generated) |
| 1189 | .filter(prop => !(prop.name in data[0]) || isRaw(data[0][prop.name])); |
| 1190 | const returningFields = Utils.flatten(returningProps.map(prop => prop.fieldNames)); |
| 1191 | sql += |
| 1192 | returningFields.length > 0 |
| 1193 | ? ` output ${returningFields.map(field => 'inserted.' + this.platform.quoteIdentifier(field)).join(', ')}` |
| 1194 | : ''; |
| 1195 | } |
| 1196 | |
| 1197 | if (fields.length > 0 || this.platform.usesDefaultKeyword()) { |
| 1198 | sql += ' values '; |
| 1199 | } else { |
| 1200 | sql += ' ' + data.map(() => `select null as ${this.platform.quoteIdentifier(pks[0])}`).join(' union all '); |
| 1201 | } |
| 1202 | |
| 1203 | const addParams = (prop: EntityProperty<T>, row: Dictionary) => { |
| 1204 | const rowValue = row[prop.name]; |
| 1205 | |
| 1206 | if (prop.nullable && rowValue === null) { |
| 1207 | params.push(null); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | let value = rowValue ?? prop.default; |
| 1212 |
nothing calls this directly
no test coverage detected