@nest-omni/core 4.1.2 → 4.1.3-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -174,10 +174,41 @@ const insertWithPk = function (entity) {
|
|
|
174
174
|
valuePlaceholders.push(`$${columnValues.length}`);
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
|
-
const
|
|
177
|
+
const connection = this.manager.connection;
|
|
178
|
+
const databaseType = connection.options.type;
|
|
178
179
|
try {
|
|
179
|
-
|
|
180
|
-
|
|
180
|
+
let result;
|
|
181
|
+
if (['postgres', 'sqlite'].includes(databaseType)) {
|
|
182
|
+
const sql = `INSERT INTO ${tableName} (${columnNames.join(', ')}) VALUES (${valuePlaceholders.join(', ')}) RETURNING *`;
|
|
183
|
+
const queryResult = yield this.query(sql, columnValues);
|
|
184
|
+
result = queryResult;
|
|
185
|
+
}
|
|
186
|
+
else if (['mysql', 'mariadb'].includes(databaseType)) {
|
|
187
|
+
const insertSql = `INSERT INTO ${tableName} (${columnNames.join(', ')}) VALUES (${valuePlaceholders.join(', ')})`;
|
|
188
|
+
yield this.query(insertSql, columnValues);
|
|
189
|
+
const pkFields = this.getPrimaryKeyFields();
|
|
190
|
+
const whereConditions = pkFields.map(pkField => {
|
|
191
|
+
const column = columns.find(col => col.propertyName === pkField);
|
|
192
|
+
const dbName = (column === null || column === void 0 ? void 0 : column.databaseName) || pkField;
|
|
193
|
+
return `${dbName} = ?`;
|
|
194
|
+
});
|
|
195
|
+
const pkValues = pkFields.map(pkField => entity[pkField]);
|
|
196
|
+
const selectSql = `SELECT * FROM ${tableName} WHERE ${whereConditions.join(' AND ')}`;
|
|
197
|
+
const queryResult = yield this.query(selectSql, pkValues);
|
|
198
|
+
result = queryResult;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const sql = `INSERT INTO ${tableName} (${columnNames.join(', ')}) VALUES (${valuePlaceholders.join(', ')})`;
|
|
202
|
+
yield this.query(sql, columnValues);
|
|
203
|
+
return entity;
|
|
204
|
+
}
|
|
205
|
+
if (result && result.length > 0) {
|
|
206
|
+
return result[0];
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
console.warn(`Native SQL insert completed but no result returned for ${tableName}, falling back to regular save`);
|
|
210
|
+
return yield this.save(entity);
|
|
211
|
+
}
|
|
181
212
|
}
|
|
182
213
|
catch (error) {
|
|
183
214
|
console.warn(`Native SQL insert failed for ${tableName}, falling back to regular save:`, error);
|
package/package.json
CHANGED