nestjs-query-mikro-orm 0.1.2 → 0.1.3
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.
- package/dist/index.cjs +35 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +36 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { MikroOrmModule, getRepositoryToken } from '@mikro-orm/nestjs';
|
|
2
|
-
import { getFilterFields, AssemblerFactory, getQueryServiceToken } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { getFilterFields, AssemblerFactory, AssemblerSerializer, AssemblerDeserializer, getQueryServiceToken } from '@ptc-org/nestjs-query-core';
|
|
3
3
|
import { raw, wrap } from '@mikro-orm/core';
|
|
4
|
+
import { getAssemblerSerializer } from '@ptc-org/nestjs-query-core/src/assemblers/assembler.serializer';
|
|
4
5
|
import { BadRequestException, NotFoundException, MethodNotAllowedException } from '@nestjs/common';
|
|
6
|
+
import { instanceToPlain } from 'class-transformer';
|
|
5
7
|
import merge from 'lodash.merge';
|
|
6
8
|
|
|
7
9
|
var __defProp = Object.defineProperty;
|
|
@@ -1563,6 +1565,34 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1563
1565
|
super(), this.repo = repo;
|
|
1564
1566
|
this.filterQueryBuilder = opts?.filterQueryBuilder ?? new FilterQueryBuilder(this.repo);
|
|
1565
1567
|
this.useSoftDelete = opts?.useSoftDelete ?? false;
|
|
1568
|
+
const serializer = getAssemblerSerializer(this.EntityClass);
|
|
1569
|
+
if (!serializer) {
|
|
1570
|
+
AssemblerSerializer((e) => {
|
|
1571
|
+
const json = instanceToPlain(e, {
|
|
1572
|
+
enableImplicitConversion: true,
|
|
1573
|
+
excludeExtraneousValues: true,
|
|
1574
|
+
exposeDefaultValues: true
|
|
1575
|
+
});
|
|
1576
|
+
const jsonWithRemovedEmptyObjects = Object.fromEntries(Object.entries(json).filter(([, value]) => !(value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0)));
|
|
1577
|
+
const wrapped = wrap(e, true);
|
|
1578
|
+
const ormJson = "toObject" in wrapped ? wrapped.toObject() : {};
|
|
1579
|
+
const data = {
|
|
1580
|
+
...ormJson,
|
|
1581
|
+
...jsonWithRemovedEmptyObjects
|
|
1582
|
+
};
|
|
1583
|
+
return data;
|
|
1584
|
+
})(this.EntityClass);
|
|
1585
|
+
AssemblerDeserializer((d) => {
|
|
1586
|
+
const wrapped = wrap(d, true);
|
|
1587
|
+
if (wrapped.getPrimaryKey()) {
|
|
1588
|
+
const entity = this.repo.getEntityManager().merge(this.EntityClass, d);
|
|
1589
|
+
return entity;
|
|
1590
|
+
} else {
|
|
1591
|
+
const entity = this.repo.getEntityManager().create(this.EntityClass, d);
|
|
1592
|
+
return entity;
|
|
1593
|
+
}
|
|
1594
|
+
})(this.EntityClass);
|
|
1595
|
+
}
|
|
1566
1596
|
}
|
|
1567
1597
|
get EntityClass() {
|
|
1568
1598
|
const em = this.repo.getEntityManager();
|
|
@@ -1848,7 +1878,8 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1848
1878
|
* @param opts - Additional options.
|
|
1849
1879
|
*/
|
|
1850
1880
|
async updateOne(id, update, opts) {
|
|
1851
|
-
const
|
|
1881
|
+
const data = "toPOJO" in wrap(update) ? wrap(update).toPOJO() : update;
|
|
1882
|
+
const dateWithClearUndefined = Object.fromEntries(Object.entries(data).filter(([, value]) => value !== void 0));
|
|
1852
1883
|
this.ensureIdIsNotPresent(dateWithClearUndefined);
|
|
1853
1884
|
const entity = await this.getById(id, opts);
|
|
1854
1885
|
wrap(entity).assign(dateWithClearUndefined);
|
|
@@ -1869,12 +1900,14 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1869
1900
|
* @param filter - A Filter used to find the records to update
|
|
1870
1901
|
*/
|
|
1871
1902
|
async updateMany(update, filter) {
|
|
1903
|
+
const data = "toPOJO" in wrap(update) ? wrap(update).toPOJO() : update;
|
|
1904
|
+
const dateWithClearUndefined = Object.fromEntries(Object.entries(data).filter(([, value]) => value !== void 0));
|
|
1872
1905
|
this.ensureIdIsNotPresent(update);
|
|
1873
1906
|
const entities = await this.query({
|
|
1874
1907
|
filter
|
|
1875
1908
|
});
|
|
1876
1909
|
for (const entity of entities) {
|
|
1877
|
-
wrap(entity).assign(
|
|
1910
|
+
wrap(entity).assign(dateWithClearUndefined);
|
|
1878
1911
|
}
|
|
1879
1912
|
await this.repo.getEntityManager().flush();
|
|
1880
1913
|
return {
|