@nest-omni/core 4.1.2-9 → 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.
|
@@ -43,7 +43,7 @@ class AbstractEntity extends AbstractBaseEntity {
|
|
|
43
43
|
}
|
|
44
44
|
exports.AbstractEntity = AbstractEntity;
|
|
45
45
|
__decorate([
|
|
46
|
-
(0, typeorm_1.
|
|
46
|
+
(0, typeorm_1.PrimaryGeneratedColumn)('increment'),
|
|
47
47
|
(0, decorators_1.NumberFieldOptional)({ swagger: false }),
|
|
48
48
|
__metadata("design:type", Number)
|
|
49
49
|
], AbstractEntity.prototype, "id", void 0);
|
|
@@ -16,6 +16,10 @@ declare global {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
declare module 'typeorm' {
|
|
19
|
+
interface Repository<Entity extends ObjectLiteral> {
|
|
20
|
+
saveWithPk(this: Repository<Entity>, entity: Entity | Entity[]): Promise<Entity | Entity[]>;
|
|
21
|
+
getPrimaryKeyFields(this: Repository<Entity>): string[];
|
|
22
|
+
}
|
|
19
23
|
interface SelectQueryBuilder<Entity> {
|
|
20
24
|
searchByString(q: string, columnNames: string[], options?: {
|
|
21
25
|
formStart: boolean;
|
|
@@ -25,6 +25,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
25
25
|
require("source-map-support/register");
|
|
26
26
|
const lodash_1 = require("lodash");
|
|
27
27
|
const typeorm_1 = require("typeorm");
|
|
28
|
+
const typeorm_2 = require("typeorm");
|
|
28
29
|
const page_dto_1 = require("./dto/page.dto");
|
|
29
30
|
const page_meta_dto_1 = require("./dto/page-meta.dto");
|
|
30
31
|
const providers_1 = require("../providers");
|
|
@@ -43,11 +44,11 @@ Array.prototype.getByLanguage = function (languageCode) {
|
|
|
43
44
|
Array.prototype.toPageDto = function (pageMetaDto, options) {
|
|
44
45
|
return new page_dto_1.PageDto(this.toDtos(options), pageMetaDto);
|
|
45
46
|
};
|
|
46
|
-
|
|
47
|
+
typeorm_2.SelectQueryBuilder.prototype.searchByString = function (q, columnNames, options) {
|
|
47
48
|
if (!q) {
|
|
48
49
|
return this;
|
|
49
50
|
}
|
|
50
|
-
this.andWhere(new
|
|
51
|
+
this.andWhere(new typeorm_2.Brackets((qb) => {
|
|
51
52
|
for (const item of columnNames) {
|
|
52
53
|
qb.orWhere(`${item} ILIKE :q`);
|
|
53
54
|
}
|
|
@@ -60,7 +61,7 @@ typeorm_1.SelectQueryBuilder.prototype.searchByString = function (q, columnNames
|
|
|
60
61
|
}
|
|
61
62
|
return this;
|
|
62
63
|
};
|
|
63
|
-
|
|
64
|
+
typeorm_2.SelectQueryBuilder.prototype.paginate = function (pageOptionsDto, options) {
|
|
64
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
66
|
if (!(options === null || options === void 0 ? void 0 : options.takeAll)) {
|
|
66
67
|
this.skip(pageOptionsDto.skip).take(pageOptionsDto.pageSize);
|
|
@@ -77,7 +78,7 @@ typeorm_1.SelectQueryBuilder.prototype.paginate = function (pageOptionsDto, opti
|
|
|
77
78
|
return [entities, pageMetaDto];
|
|
78
79
|
});
|
|
79
80
|
};
|
|
80
|
-
|
|
81
|
+
typeorm_2.SelectQueryBuilder.prototype.paginateAndMap = function (pageOptionsDto, options) {
|
|
81
82
|
return __awaiter(this, void 0, void 0, function* () {
|
|
82
83
|
const [entities, pageMetaDto] = yield this.paginate(pageOptionsDto, {
|
|
83
84
|
skipCount: options === null || options === void 0 ? void 0 : options.skipCount,
|
|
@@ -90,7 +91,7 @@ typeorm_1.SelectQueryBuilder.prototype.paginateAndMap = function (pageOptionsDto
|
|
|
90
91
|
return transformedEntities.toPageDto(pageMetaDto, options === null || options === void 0 ? void 0 : options.dtoOptions);
|
|
91
92
|
});
|
|
92
93
|
};
|
|
93
|
-
|
|
94
|
+
typeorm_2.SelectQueryBuilder.prototype.withTenant = function (tenantId, tenantFieldName = 'tenantId') {
|
|
94
95
|
var _a;
|
|
95
96
|
if (!tenantId) {
|
|
96
97
|
tenantId = providers_1.ContextProvider.getTenantId();
|
|
@@ -106,7 +107,7 @@ typeorm_1.SelectQueryBuilder.prototype.withTenant = function (tenantId, tenantFi
|
|
|
106
107
|
}
|
|
107
108
|
return this;
|
|
108
109
|
};
|
|
109
|
-
|
|
110
|
+
typeorm_2.SelectQueryBuilder.prototype.iterate = function (options) {
|
|
110
111
|
return __asyncGenerator(this, arguments, function* () {
|
|
111
112
|
const batchSize = (options === null || options === void 0 ? void 0 : options.batchSize) || 100;
|
|
112
113
|
let offset = 0;
|
|
@@ -124,7 +125,7 @@ typeorm_1.SelectQueryBuilder.prototype.iterate = function (options) {
|
|
|
124
125
|
}
|
|
125
126
|
});
|
|
126
127
|
};
|
|
127
|
-
|
|
128
|
+
typeorm_2.SelectQueryBuilder.prototype.eachBatch = function (callback, options) {
|
|
128
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
129
130
|
const batchSize = (options === null || options === void 0 ? void 0 : options.batchSize) || 100;
|
|
130
131
|
const mode = (options === null || options === void 0 ? void 0 : options.mode) || 'batch';
|
|
@@ -150,3 +151,126 @@ typeorm_1.SelectQueryBuilder.prototype.eachBatch = function (callback, options)
|
|
|
150
151
|
}
|
|
151
152
|
});
|
|
152
153
|
};
|
|
154
|
+
const insertWithPk = function (entity) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
if (!this.metadata) {
|
|
157
|
+
throw new Error('Repository metadata is not available. Make sure the repository is properly initialized.');
|
|
158
|
+
}
|
|
159
|
+
const tableName = this.metadata.tableName;
|
|
160
|
+
const columns = this.metadata.columns;
|
|
161
|
+
const columnNames = [];
|
|
162
|
+
const columnValues = [];
|
|
163
|
+
const valuePlaceholders = [];
|
|
164
|
+
for (const column of columns) {
|
|
165
|
+
const propertyName = column.propertyName;
|
|
166
|
+
const value = entity[propertyName];
|
|
167
|
+
if (value === undefined) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const columnName = column.databaseName;
|
|
171
|
+
if (columnName) {
|
|
172
|
+
columnNames.push(columnName);
|
|
173
|
+
columnValues.push(value);
|
|
174
|
+
valuePlaceholders.push(`$${columnValues.length}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const connection = this.manager.connection;
|
|
178
|
+
const databaseType = connection.options.type;
|
|
179
|
+
try {
|
|
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
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
console.warn(`Native SQL insert failed for ${tableName}, falling back to regular save:`, error);
|
|
215
|
+
return yield this.save(entity);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
typeorm_1.Repository.prototype.getPrimaryKeyFields = function () {
|
|
220
|
+
if (!this.metadata) {
|
|
221
|
+
throw new Error('Repository metadata is not available. Make sure the repository is properly initialized.');
|
|
222
|
+
}
|
|
223
|
+
const primaryKeys = this.metadata.primaryColumns;
|
|
224
|
+
if (!primaryKeys || primaryKeys.length === 0) {
|
|
225
|
+
throw new Error(`Entity ${this.metadata.targetName} does not have any primary key columns defined.`);
|
|
226
|
+
}
|
|
227
|
+
return primaryKeys.map(column => column.propertyName);
|
|
228
|
+
};
|
|
229
|
+
typeorm_1.Repository.prototype.saveWithPk = function (entity) {
|
|
230
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
231
|
+
const isArray = Array.isArray(entity);
|
|
232
|
+
const entities = isArray ? entity : [entity];
|
|
233
|
+
if (entities.length === 0) {
|
|
234
|
+
return isArray ? [] : null;
|
|
235
|
+
}
|
|
236
|
+
const pkFields = this.getPrimaryKeyFields();
|
|
237
|
+
const results = yield Promise.all(entities.map((singleEntity) => __awaiter(this, void 0, void 0, function* () {
|
|
238
|
+
const hasPkValues = pkFields.every(pkField => {
|
|
239
|
+
const value = singleEntity[pkField];
|
|
240
|
+
return value !== undefined && value !== null && value !== '';
|
|
241
|
+
});
|
|
242
|
+
if (hasPkValues) {
|
|
243
|
+
try {
|
|
244
|
+
const existingEntity = yield this.findOne({
|
|
245
|
+
where: pkFields.reduce((acc, pkField) => {
|
|
246
|
+
acc[pkField] = singleEntity[pkField];
|
|
247
|
+
return acc;
|
|
248
|
+
}, {})
|
|
249
|
+
});
|
|
250
|
+
if (existingEntity) {
|
|
251
|
+
yield this.update(pkFields.reduce((acc, pkField) => {
|
|
252
|
+
acc[pkField] = singleEntity[pkField];
|
|
253
|
+
return acc;
|
|
254
|
+
}, {}), singleEntity);
|
|
255
|
+
return yield this.findOne({
|
|
256
|
+
where: pkFields.reduce((acc, pkField) => {
|
|
257
|
+
acc[pkField] = singleEntity[pkField];
|
|
258
|
+
return acc;
|
|
259
|
+
}, {})
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
return yield insertWithPk.call(this, singleEntity);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
return yield insertWithPk.call(this, singleEntity);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return yield this.save(singleEntity);
|
|
272
|
+
}
|
|
273
|
+
})));
|
|
274
|
+
return isArray ? results : results[0];
|
|
275
|
+
});
|
|
276
|
+
};
|
package/package.json
CHANGED