nestjs-query-mikro-orm 0.0.9 → 0.1.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.
- package/README.md +1 -1
- package/dist/index.cjs +61 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.mjs +39 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ The package automatically detects the import method and serves the appropriate f
|
|
|
30
30
|
pnpm add nestjs-query-mikro-orm
|
|
31
31
|
|
|
32
32
|
# Install peer dependencies if you haven't already
|
|
33
|
-
pnpm add @mikro-orm/core @nestjs/common @nestjs/core @nestjs-query
|
|
33
|
+
pnpm add @mikro-orm/core @nestjs/common @nestjs/core @ptc-org/nestjs-query-core reflect-metadata rxjs
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
## Quick Start
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var nestjs = require('@mikro-orm/nestjs');
|
|
4
|
-
var
|
|
5
|
-
var core
|
|
6
|
-
var assembler_serializer = require('@nestjs-query
|
|
4
|
+
var nestjsQueryCore = require('@ptc-org/nestjs-query-core');
|
|
5
|
+
var core = require('@mikro-orm/core');
|
|
6
|
+
var assembler_serializer = require('@ptc-org/nestjs-query-core/src/assemblers/assembler.serializer');
|
|
7
7
|
var common = require('@nestjs/common');
|
|
8
8
|
var classTransformer = require('class-transformer');
|
|
9
9
|
var merge = require('lodash.merge');
|
|
@@ -383,7 +383,7 @@ var FilterQueryBuilder = class {
|
|
|
383
383
|
}
|
|
384
384
|
getReferencedRelations(filter) {
|
|
385
385
|
const relationNames = this.relationNames;
|
|
386
|
-
const referencedFields =
|
|
386
|
+
const referencedFields = nestjsQueryCore.getFilterFields(filter);
|
|
387
387
|
return referencedFields.filter((f) => relationNames.includes(f));
|
|
388
388
|
}
|
|
389
389
|
getReferencedRelationsRecursive(metadataOrFilter = {}, filter) {
|
|
@@ -970,7 +970,8 @@ var AggregateBuilder = class _AggregateBuilder {
|
|
|
970
970
|
});
|
|
971
971
|
const funcSelects = [];
|
|
972
972
|
aggs.forEach(([func, fields]) => {
|
|
973
|
-
|
|
973
|
+
if (!fields || fields.length === 0) return;
|
|
974
|
+
const aliases = fields.map((f) => {
|
|
974
975
|
const col = alias ? `\`${alias}\`.\`${String(f)}\`` : `\`${String(f)}\``;
|
|
975
976
|
return [
|
|
976
977
|
`${func}(${col})`,
|
|
@@ -1025,7 +1026,8 @@ var AggregateBuilder = class _AggregateBuilder {
|
|
|
1025
1026
|
]
|
|
1026
1027
|
];
|
|
1027
1028
|
return aggs.reduce((cols, [func, fields]) => {
|
|
1028
|
-
|
|
1029
|
+
if (!fields || fields.length === 0) return cols;
|
|
1030
|
+
const aliases = fields.map((f) => this.getAggregateAlias(func, f));
|
|
1029
1031
|
return [
|
|
1030
1032
|
...cols,
|
|
1031
1033
|
...aliases
|
|
@@ -1101,19 +1103,39 @@ var AggregateBuilder = class _AggregateBuilder {
|
|
|
1101
1103
|
*/
|
|
1102
1104
|
build(qb, aggregate, alias) {
|
|
1103
1105
|
const metadata = qb.mainAlias?.metadata;
|
|
1104
|
-
const selects = [
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1106
|
+
const selects = [];
|
|
1107
|
+
selects.push(...this.createGroupBySelect(aggregate.groupBy, alias, metadata));
|
|
1108
|
+
const aggs = [
|
|
1109
|
+
[
|
|
1110
|
+
"COUNT",
|
|
1111
|
+
aggregate.count
|
|
1112
|
+
],
|
|
1113
|
+
[
|
|
1114
|
+
"SUM",
|
|
1115
|
+
aggregate.sum
|
|
1116
|
+
],
|
|
1117
|
+
[
|
|
1118
|
+
"AVG",
|
|
1119
|
+
aggregate.avg
|
|
1120
|
+
],
|
|
1121
|
+
[
|
|
1122
|
+
"MAX",
|
|
1123
|
+
aggregate.max
|
|
1124
|
+
],
|
|
1125
|
+
[
|
|
1126
|
+
"MIN",
|
|
1127
|
+
aggregate.min
|
|
1128
|
+
]
|
|
1111
1129
|
];
|
|
1130
|
+
aggs.forEach(([func, fields]) => {
|
|
1131
|
+
if (!fields || fields.length === 0) return;
|
|
1132
|
+
selects.push(...this.createAggSelect(func, fields, alias, metadata));
|
|
1133
|
+
});
|
|
1112
1134
|
if (!selects.length) {
|
|
1113
1135
|
throw new common.BadRequestException("No aggregate fields found.");
|
|
1114
1136
|
}
|
|
1115
1137
|
selects.forEach(([selectExpr, selectAlias]) => {
|
|
1116
|
-
qb.addSelect(core
|
|
1138
|
+
qb.addSelect(core.raw(`${selectExpr} as "${selectAlias}"`));
|
|
1117
1139
|
});
|
|
1118
1140
|
return qb;
|
|
1119
1141
|
}
|
|
@@ -1152,15 +1174,15 @@ var RelationQueryService = class {
|
|
|
1152
1174
|
if (Array.isArray(dto)) {
|
|
1153
1175
|
return this.batchQueryRelations(RelationClass, relationName, dto, query);
|
|
1154
1176
|
}
|
|
1155
|
-
const assembler =
|
|
1177
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1156
1178
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1157
|
-
return assembler.
|
|
1179
|
+
return assembler.convertToDTOs(await relationQueryBuilder.selectAndExecute(dto, assembler.convertQuery(query)));
|
|
1158
1180
|
}
|
|
1159
1181
|
async aggregateRelations(RelationClass, relationName, dto, filter, aggregate) {
|
|
1160
1182
|
if (Array.isArray(dto)) {
|
|
1161
1183
|
return this.batchAggregateRelations(RelationClass, relationName, dto, filter, aggregate);
|
|
1162
1184
|
}
|
|
1163
|
-
const assembler =
|
|
1185
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1164
1186
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1165
1187
|
const rawResults = await relationQueryBuilder.aggregate(dto, assembler.convertQuery({
|
|
1166
1188
|
filter
|
|
@@ -1175,7 +1197,7 @@ var RelationQueryService = class {
|
|
|
1175
1197
|
if (Array.isArray(dto)) {
|
|
1176
1198
|
return this.batchCountRelations(RelationClass, relationName, dto, filter);
|
|
1177
1199
|
}
|
|
1178
|
-
const assembler =
|
|
1200
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1179
1201
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1180
1202
|
return relationQueryBuilder.count(dto, assembler.convertQuery({
|
|
1181
1203
|
filter
|
|
@@ -1185,7 +1207,7 @@ var RelationQueryService = class {
|
|
|
1185
1207
|
if (Array.isArray(dto)) {
|
|
1186
1208
|
return this.batchFindRelations(RelationClass, relationName, dto, opts);
|
|
1187
1209
|
}
|
|
1188
|
-
const assembler =
|
|
1210
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1189
1211
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1190
1212
|
const relations = await relationQueryBuilder.selectAndExecute(dto, {
|
|
1191
1213
|
filter: opts?.filter,
|
|
@@ -1259,7 +1281,7 @@ var RelationQueryService = class {
|
|
|
1259
1281
|
if (!relation) {
|
|
1260
1282
|
throw new Error(`Unable to find ${relationName} to set on ${this.EntityClass.name}`);
|
|
1261
1283
|
}
|
|
1262
|
-
core
|
|
1284
|
+
core.wrap(entity).assign({
|
|
1263
1285
|
[relationName]: relation
|
|
1264
1286
|
});
|
|
1265
1287
|
await this.repo.getEntityManager().flush();
|
|
@@ -1312,7 +1334,7 @@ var RelationQueryService = class {
|
|
|
1312
1334
|
if (fkFieldName in entity) {
|
|
1313
1335
|
assignData[fkFieldName] = null;
|
|
1314
1336
|
}
|
|
1315
|
-
core
|
|
1337
|
+
core.wrap(entity).assign(assignData);
|
|
1316
1338
|
} else {
|
|
1317
1339
|
const collection = entity[relationName];
|
|
1318
1340
|
if (collection && typeof collection.remove === "function") {
|
|
@@ -1334,13 +1356,13 @@ var RelationQueryService = class {
|
|
|
1334
1356
|
* @param query - A query to filter, page or sort relations.
|
|
1335
1357
|
*/
|
|
1336
1358
|
async batchQueryRelations(RelationClass, relationName, entities, query) {
|
|
1337
|
-
const assembler =
|
|
1359
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1338
1360
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1339
1361
|
const convertedQuery = assembler.convertQuery(query);
|
|
1340
1362
|
const results = /* @__PURE__ */ new Map();
|
|
1341
1363
|
await Promise.all(entities.map(async (entity) => {
|
|
1342
1364
|
const relations = await relationQueryBuilder.selectAndExecute(entity, convertedQuery);
|
|
1343
|
-
const relationDtos = assembler.convertToDTOs(relations);
|
|
1365
|
+
const relationDtos = await assembler.convertToDTOs(relations);
|
|
1344
1366
|
if (relationDtos.length > 0) {
|
|
1345
1367
|
results.set(entity, relationDtos);
|
|
1346
1368
|
}
|
|
@@ -1355,7 +1377,7 @@ var RelationQueryService = class {
|
|
|
1355
1377
|
* @param query - A query to filter, page or sort relations.
|
|
1356
1378
|
*/
|
|
1357
1379
|
async batchAggregateRelations(RelationClass, relationName, entities, filter, aggregate) {
|
|
1358
|
-
const assembler =
|
|
1380
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1359
1381
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1360
1382
|
const convertedQuery = assembler.convertQuery({
|
|
1361
1383
|
filter
|
|
@@ -1376,7 +1398,7 @@ var RelationQueryService = class {
|
|
|
1376
1398
|
* @param filter - The filter to apply to the relation query.
|
|
1377
1399
|
*/
|
|
1378
1400
|
async batchCountRelations(RelationClass, relationName, entities, filter) {
|
|
1379
|
-
const assembler =
|
|
1401
|
+
const assembler = nestjsQueryCore.AssemblerFactory.getAssembler(RelationClass, this.getRelationEntity(relationName));
|
|
1380
1402
|
const relationQueryBuilder = this.getRelationQueryBuilder(relationName);
|
|
1381
1403
|
const convertedQuery = assembler.convertQuery({
|
|
1382
1404
|
filter
|
|
@@ -1471,14 +1493,14 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1471
1493
|
this.useSoftDelete = opts?.useSoftDelete ?? false;
|
|
1472
1494
|
const serializer = assembler_serializer.getAssemblerSerializer(this.EntityClass);
|
|
1473
1495
|
if (!serializer) {
|
|
1474
|
-
|
|
1496
|
+
nestjsQueryCore.AssemblerSerializer((e) => {
|
|
1475
1497
|
const json = classTransformer.instanceToPlain(e, {
|
|
1476
1498
|
enableImplicitConversion: true,
|
|
1477
1499
|
excludeExtraneousValues: true,
|
|
1478
1500
|
exposeDefaultValues: true
|
|
1479
1501
|
});
|
|
1480
1502
|
const jsonWithRemovedEmptyObjects = Object.fromEntries(Object.entries(json).filter(([, value]) => !(value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0)));
|
|
1481
|
-
const wrapped = core
|
|
1503
|
+
const wrapped = core.wrap(e, true);
|
|
1482
1504
|
const ormJson = "toObject" in wrapped ? wrapped.toObject() : {};
|
|
1483
1505
|
const data = {
|
|
1484
1506
|
...ormJson,
|
|
@@ -1486,7 +1508,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1486
1508
|
};
|
|
1487
1509
|
return data;
|
|
1488
1510
|
})(this.EntityClass);
|
|
1489
|
-
|
|
1511
|
+
nestjsQueryCore.AssemblerDeserializer((d) => {
|
|
1490
1512
|
const entity = this.repo.getEntityManager().create(this.EntityClass, classTransformer.instanceToPlain(d));
|
|
1491
1513
|
return entity;
|
|
1492
1514
|
})(this.EntityClass);
|
|
@@ -1498,7 +1520,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1498
1520
|
return metadata.class;
|
|
1499
1521
|
}
|
|
1500
1522
|
/**
|
|
1501
|
-
* Query for multiple entities, using a Query from `@nestjs-query
|
|
1523
|
+
* Query for multiple entities, using a Query from `@ptc-org/nestjs-query-core`.
|
|
1502
1524
|
*
|
|
1503
1525
|
* @example
|
|
1504
1526
|
* ```ts
|
|
@@ -1779,12 +1801,12 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1779
1801
|
const dateWithClearUndefined = Object.fromEntries(Object.entries(update).filter(([, value]) => value !== void 0));
|
|
1780
1802
|
this.ensureIdIsNotPresent(dateWithClearUndefined);
|
|
1781
1803
|
const entity = await this.getById(id, opts);
|
|
1782
|
-
core
|
|
1804
|
+
core.wrap(entity).assign(dateWithClearUndefined);
|
|
1783
1805
|
await this.repo.getEntityManager().flush();
|
|
1784
1806
|
return entity;
|
|
1785
1807
|
}
|
|
1786
1808
|
/**
|
|
1787
|
-
* Update multiple entities with a `@nestjs-query
|
|
1809
|
+
* Update multiple entities with a `@ptc-org/nestjs-query-core` Filter.
|
|
1788
1810
|
*
|
|
1789
1811
|
* @example
|
|
1790
1812
|
* ```ts
|
|
@@ -1802,7 +1824,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1802
1824
|
filter
|
|
1803
1825
|
});
|
|
1804
1826
|
for (const entity of entities) {
|
|
1805
|
-
core
|
|
1827
|
+
core.wrap(entity).assign(update);
|
|
1806
1828
|
}
|
|
1807
1829
|
await this.repo.getEntityManager().flush();
|
|
1808
1830
|
return {
|
|
@@ -1825,7 +1847,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1825
1847
|
const entity = await this.getById(id, opts);
|
|
1826
1848
|
const em = this.repo.getEntityManager();
|
|
1827
1849
|
if (this.useSoftDelete) {
|
|
1828
|
-
core
|
|
1850
|
+
core.wrap(entity).assign({
|
|
1829
1851
|
deletedAt: /* @__PURE__ */ new Date()
|
|
1830
1852
|
});
|
|
1831
1853
|
await em.flush();
|
|
@@ -1835,7 +1857,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1835
1857
|
return entity;
|
|
1836
1858
|
}
|
|
1837
1859
|
/**
|
|
1838
|
-
* Delete multiple records with a `@nestjs-query
|
|
1860
|
+
* Delete multiple records with a `@ptc-org/nestjs-query-core` `Filter`.
|
|
1839
1861
|
*
|
|
1840
1862
|
* @example
|
|
1841
1863
|
*
|
|
@@ -1854,7 +1876,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1854
1876
|
const em = this.repo.getEntityManager();
|
|
1855
1877
|
if (this.useSoftDelete) {
|
|
1856
1878
|
for (const entity of entities) {
|
|
1857
|
-
core
|
|
1879
|
+
core.wrap(entity).assign({
|
|
1858
1880
|
deletedAt: /* @__PURE__ */ new Date()
|
|
1859
1881
|
});
|
|
1860
1882
|
}
|
|
@@ -1904,14 +1926,14 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1904
1926
|
if (!entity) {
|
|
1905
1927
|
throw new common.NotFoundException(`Unable to find ${this.EntityClass.name} with id: ${id}`);
|
|
1906
1928
|
}
|
|
1907
|
-
core
|
|
1929
|
+
core.wrap(entity).assign({
|
|
1908
1930
|
deletedAt: null
|
|
1909
1931
|
});
|
|
1910
1932
|
await em.flush();
|
|
1911
1933
|
return entity;
|
|
1912
1934
|
}
|
|
1913
1935
|
/**
|
|
1914
|
-
* Restores multiple records with a `@nestjs-query
|
|
1936
|
+
* Restores multiple records with a `@ptc-org/nestjs-query-core` `Filter`.
|
|
1915
1937
|
*
|
|
1916
1938
|
* @example
|
|
1917
1939
|
*
|
|
@@ -1932,7 +1954,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1932
1954
|
filters: false
|
|
1933
1955
|
});
|
|
1934
1956
|
for (const entity of entities) {
|
|
1935
|
-
core
|
|
1957
|
+
core.wrap(entity).assign({
|
|
1936
1958
|
deletedAt: null
|
|
1937
1959
|
});
|
|
1938
1960
|
}
|
|
@@ -1984,7 +2006,7 @@ var MikroOrmQueryService = class extends RelationQueryService {
|
|
|
1984
2006
|
function createMikroOrmQueryServiceProvider(EntityClass, contextName) {
|
|
1985
2007
|
return {
|
|
1986
2008
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1987
|
-
provide:
|
|
2009
|
+
provide: nestjsQueryCore.getQueryServiceToken(EntityClass),
|
|
1988
2010
|
useFactory(repo) {
|
|
1989
2011
|
return new MikroOrmQueryService(repo);
|
|
1990
2012
|
},
|