nicot 1.2.13 → 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.
- package/README.md +230 -0
- package/dist/index.cjs +343 -79
- package/dist/index.cjs.map +4 -4
- package/dist/index.mjs +325 -63
- package/dist/index.mjs.map +4 -4
- package/dist/src/bases/base-restful-controller.d.ts +2 -1
- package/dist/src/bases/id-base.d.ts +6 -0
- package/dist/src/bases/time-base.d.ts +6 -0
- package/dist/src/crud-base.d.ts +12 -5
- package/dist/src/decorators/access.d.ts +2 -3
- package/dist/src/decorators/index.d.ts +1 -0
- package/dist/src/decorators/property.d.ts +1 -0
- package/dist/src/decorators/upsert.d.ts +5 -0
- package/dist/src/restful.d.ts +31 -19
- package/dist/src/utility/metadata.d.ts +10 -6
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -612,6 +612,7 @@ __export(index_exports, {
|
|
|
612
612
|
NotCreatable: () => NotCreatable,
|
|
613
613
|
NotInResult: () => NotInResult,
|
|
614
614
|
NotQueryable: () => NotQueryable,
|
|
615
|
+
NotUpsertable: () => NotUpsertable,
|
|
615
616
|
NotWritable: () => NotWritable,
|
|
616
617
|
OmitPipe: () => OmitPipe,
|
|
617
618
|
OptionalDataPipe: () => OptionalDataPipe,
|
|
@@ -652,6 +653,8 @@ __export(index_exports, {
|
|
|
652
653
|
TimeBase: () => TimeBase,
|
|
653
654
|
TransactionalTypeOrmInterceptor: () => TransactionalTypeOrmInterceptor,
|
|
654
655
|
TransactionalTypeOrmModule: () => TransactionalTypeOrmModule,
|
|
656
|
+
UpsertColumn: () => UpsertColumn,
|
|
657
|
+
UpsertableEntity: () => UpsertableEntity,
|
|
655
658
|
UuidColumn: () => UuidColumn,
|
|
656
659
|
applyQueryMatchBoolean: () => applyQueryMatchBoolean,
|
|
657
660
|
applyQueryMatchBooleanMySQL: () => applyQueryMatchBooleanMySQL,
|
|
@@ -719,6 +722,7 @@ var import_class_validator2 = require("class-validator");
|
|
|
719
722
|
|
|
720
723
|
// src/utility/metadata.ts
|
|
721
724
|
var import_typed_reflector = require("typed-reflector");
|
|
725
|
+
var import_lodash = __toESM(require("lodash"));
|
|
722
726
|
var Metadata = new import_typed_reflector.MetadataSetter();
|
|
723
727
|
var reflector = new import_typed_reflector.Reflector();
|
|
724
728
|
function getSpecificFields(obj, type, filter = () => true) {
|
|
@@ -731,11 +735,11 @@ function getSpecificFields(obj, type, filter = () => true) {
|
|
|
731
735
|
});
|
|
732
736
|
}
|
|
733
737
|
function getNotInResultFields(obj, keepEntityVersioningDates = false) {
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
"
|
|
737
|
-
|
|
738
|
-
|
|
738
|
+
const res = getSpecificFields(obj, "notInResult");
|
|
739
|
+
if (keepEntityVersioningDates) {
|
|
740
|
+
return import_lodash.default.difference(res, getSpecificFields(obj, "entityVersioningDate"));
|
|
741
|
+
}
|
|
742
|
+
return res;
|
|
739
743
|
}
|
|
740
744
|
|
|
741
745
|
// src/decorators/access.ts
|
|
@@ -751,8 +755,12 @@ var NotCreatable = () => (0, import_nesties2.MergePropertyDecorators)([
|
|
|
751
755
|
var NotChangeable = () => (0, import_nesties2.MergePropertyDecorators)([
|
|
752
756
|
Metadata.set("notChangeable", true, "notChangeableFields")
|
|
753
757
|
]);
|
|
758
|
+
var NotUpsertable = () => (0, import_nesties2.MergePropertyDecorators)([
|
|
759
|
+
(0, import_class_validator2.IsOptional)(),
|
|
760
|
+
Metadata.set("notUpsertable", true, "notUpsertableFields")
|
|
761
|
+
]);
|
|
754
762
|
var NotQueryable = () => Metadata.set("notQueryable", true, "notQueryableFields");
|
|
755
|
-
var NotInResult = (
|
|
763
|
+
var NotInResult = () => Metadata.set("notInResult", true, "notInResultFields");
|
|
756
764
|
|
|
757
765
|
// src/decorators/property.ts
|
|
758
766
|
var import_swagger2 = require("@nestjs/swagger");
|
|
@@ -1434,6 +1442,24 @@ var BindingValue = (bindingKey = DefaultBindingKey) => (obj, key, des) => {
|
|
|
1434
1442
|
)(obj, key);
|
|
1435
1443
|
};
|
|
1436
1444
|
|
|
1445
|
+
// src/decorators/upsert.ts
|
|
1446
|
+
var import_typeorm3 = require("typeorm");
|
|
1447
|
+
var UpsertColumn = () => Metadata.set("upsertColumn", true, "upsertColumnFields");
|
|
1448
|
+
var UpsertableEntity = () => (cls) => {
|
|
1449
|
+
const upsertColumns = getSpecificFields(cls, "upsertColumn");
|
|
1450
|
+
const bindingColumns = getSpecificFields(cls, "bindingColumn");
|
|
1451
|
+
if (!upsertColumns.length && !bindingColumns.length) {
|
|
1452
|
+
throw new Error(
|
|
1453
|
+
`UpsertableEntity ${cls.name} must have at least one UpsertColumn or BindingColumn defined.`
|
|
1454
|
+
);
|
|
1455
|
+
}
|
|
1456
|
+
Metadata.set("upsertableEntity", true)(cls);
|
|
1457
|
+
if (!bindingColumns.length && upsertColumns.length === 1 && upsertColumns[0] === "id") {
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
(0, import_typeorm3.Unique)([.../* @__PURE__ */ new Set([...bindingColumns, ...upsertColumns])])(cls);
|
|
1461
|
+
};
|
|
1462
|
+
|
|
1437
1463
|
// src/dto/cursor-pagination.ts
|
|
1438
1464
|
var CursorPaginationDto = class {
|
|
1439
1465
|
};
|
|
@@ -1507,10 +1533,10 @@ function CursorPaginationReturnMessageDto(type) {
|
|
|
1507
1533
|
}
|
|
1508
1534
|
|
|
1509
1535
|
// src/crud-base.ts
|
|
1510
|
-
var
|
|
1536
|
+
var import_typeorm8 = require("typeorm");
|
|
1511
1537
|
|
|
1512
1538
|
// src/bases/time-base.ts
|
|
1513
|
-
var
|
|
1539
|
+
var import_typeorm4 = require("typeorm");
|
|
1514
1540
|
|
|
1515
1541
|
// src/bases/page-settings.ts
|
|
1516
1542
|
var import_class_validator5 = require("class-validator");
|
|
@@ -1565,6 +1591,7 @@ __decorateClass([
|
|
|
1565
1591
|
], PageSettingsDto.prototype, "recordsPerPage", 2);
|
|
1566
1592
|
|
|
1567
1593
|
// src/bases/time-base.ts
|
|
1594
|
+
var EntityVersioningDate = () => Metadata.set("entityVersioningDate", true, "entityVersioningDateFields");
|
|
1568
1595
|
var TimeBase = class extends PageSettingsDto {
|
|
1569
1596
|
isValidInCreate() {
|
|
1570
1597
|
return;
|
|
@@ -1587,28 +1614,40 @@ var TimeBase = class extends PageSettingsDto {
|
|
|
1587
1614
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
1588
1615
|
async beforeUpdate() {
|
|
1589
1616
|
}
|
|
1617
|
+
isValidInUpsert() {
|
|
1618
|
+
return;
|
|
1619
|
+
}
|
|
1620
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
1621
|
+
async beforeUpsert() {
|
|
1622
|
+
}
|
|
1623
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
1624
|
+
async afterUpsert() {
|
|
1625
|
+
}
|
|
1590
1626
|
};
|
|
1591
1627
|
__decorateClass([
|
|
1592
|
-
(0,
|
|
1628
|
+
(0, import_typeorm4.CreateDateColumn)({ select: false }),
|
|
1593
1629
|
NotColumn(),
|
|
1594
|
-
NotInResult(
|
|
1630
|
+
NotInResult(),
|
|
1631
|
+
EntityVersioningDate(),
|
|
1595
1632
|
Reflect.metadata("design:type", Date)
|
|
1596
1633
|
], TimeBase.prototype, "createTime", 2);
|
|
1597
1634
|
__decorateClass([
|
|
1598
|
-
(0,
|
|
1635
|
+
(0, import_typeorm4.UpdateDateColumn)({ select: false }),
|
|
1599
1636
|
NotColumn(),
|
|
1600
|
-
NotInResult(
|
|
1637
|
+
NotInResult(),
|
|
1638
|
+
EntityVersioningDate(),
|
|
1601
1639
|
Reflect.metadata("design:type", Date)
|
|
1602
1640
|
], TimeBase.prototype, "updateTime", 2);
|
|
1603
1641
|
__decorateClass([
|
|
1604
|
-
(0,
|
|
1642
|
+
(0, import_typeorm4.DeleteDateColumn)({ select: false }),
|
|
1605
1643
|
NotColumn(),
|
|
1606
|
-
NotInResult(
|
|
1644
|
+
NotInResult(),
|
|
1645
|
+
EntityVersioningDate(),
|
|
1607
1646
|
Reflect.metadata("design:type", Date)
|
|
1608
1647
|
], TimeBase.prototype, "deleteTime", 2);
|
|
1609
1648
|
|
|
1610
1649
|
// src/bases/id-base.ts
|
|
1611
|
-
var
|
|
1650
|
+
var import_typeorm5 = require("typeorm");
|
|
1612
1651
|
var import_class_validator6 = require("class-validator");
|
|
1613
1652
|
var import_nesties7 = require("nesties");
|
|
1614
1653
|
function IdBase(idOptions = {}) {
|
|
@@ -1628,7 +1667,7 @@ function IdBase(idOptions = {}) {
|
|
|
1628
1667
|
columnExtras: { nullable: false, primary: true }
|
|
1629
1668
|
}),
|
|
1630
1669
|
Reflect.metadata("design:type", Number),
|
|
1631
|
-
(0,
|
|
1670
|
+
(0, import_typeorm5.Generated)("increment"),
|
|
1632
1671
|
QueryEqual(),
|
|
1633
1672
|
Metadata.set(
|
|
1634
1673
|
"notRequiredButHasDefault",
|
|
@@ -1664,7 +1703,8 @@ function StringIdBase(idOptions) {
|
|
|
1664
1703
|
...idOptions.uuid ? [UuidColumn({ ...columnOptions, generated: true }), NotWritable()] : [
|
|
1665
1704
|
StringColumn(idOptions.length || 255, columnOptions),
|
|
1666
1705
|
(0, import_class_validator6.IsNotEmpty)(),
|
|
1667
|
-
NotChangeable()
|
|
1706
|
+
NotChangeable(),
|
|
1707
|
+
UpsertColumn()
|
|
1668
1708
|
]
|
|
1669
1709
|
];
|
|
1670
1710
|
const dec = (0, import_nesties7.MergePropertyDecorators)(decs);
|
|
@@ -1675,14 +1715,14 @@ function StringIdBase(idOptions) {
|
|
|
1675
1715
|
// src/crud-base.ts
|
|
1676
1716
|
var import_common2 = require("@nestjs/common");
|
|
1677
1717
|
var import_StringUtils = require("typeorm/util/StringUtils");
|
|
1678
|
-
var
|
|
1718
|
+
var import_lodash4 = __toESM(require("lodash"));
|
|
1679
1719
|
var import_nesties8 = require("nesties");
|
|
1680
1720
|
|
|
1681
1721
|
// src/utility/get-typeorm-relations.ts
|
|
1682
|
-
var
|
|
1683
|
-
var
|
|
1722
|
+
var import_typeorm6 = require("typeorm");
|
|
1723
|
+
var import_lodash2 = __toESM(require("lodash"));
|
|
1684
1724
|
function getTypeormRelations(cl) {
|
|
1685
|
-
const relations = (0,
|
|
1725
|
+
const relations = (0, import_typeorm6.getMetadataArgsStorage)().relations.filter(
|
|
1686
1726
|
(r) => r.target === cl
|
|
1687
1727
|
);
|
|
1688
1728
|
const typeormRelations = relations.map((relation) => {
|
|
@@ -1721,7 +1761,7 @@ function getTypeormRelations(cl) {
|
|
|
1721
1761
|
};
|
|
1722
1762
|
}
|
|
1723
1763
|
);
|
|
1724
|
-
return
|
|
1764
|
+
return import_lodash2.default.uniqBy(
|
|
1725
1765
|
[...typeormRelations, ...computedRelations],
|
|
1726
1766
|
// Merge typeorm relations and computed relations
|
|
1727
1767
|
(r) => r.propertyName
|
|
@@ -1729,8 +1769,8 @@ function getTypeormRelations(cl) {
|
|
|
1729
1769
|
}
|
|
1730
1770
|
|
|
1731
1771
|
// src/utility/cursor-pagination-utils.ts
|
|
1732
|
-
var
|
|
1733
|
-
var
|
|
1772
|
+
var import_typeorm7 = require("typeorm");
|
|
1773
|
+
var import_lodash3 = __toESM(require("lodash"));
|
|
1734
1774
|
var import_superjson = __toESM(require("superjson"));
|
|
1735
1775
|
|
|
1736
1776
|
// src/utility/filter-relations.ts
|
|
@@ -1898,7 +1938,7 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
|
|
|
1898
1938
|
);
|
|
1899
1939
|
if (keys.length) {
|
|
1900
1940
|
const staircasedKeys = keys.map(
|
|
1901
|
-
(key, i) =>
|
|
1941
|
+
(key, i) => import_lodash3.default.range(i + 1).map((j) => keys[j])
|
|
1902
1942
|
);
|
|
1903
1943
|
const cursorKey = (key) => `_cursor_${key.replace(/\./g, "__").replace(/"/g, "")}`;
|
|
1904
1944
|
const expressionMatrix = staircasedKeys.map(
|
|
@@ -1948,8 +1988,8 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
|
|
|
1948
1988
|
).filter((s) => !s.includes("__never__"));
|
|
1949
1989
|
if (expressionMatrix.length) {
|
|
1950
1990
|
qb.andWhere(
|
|
1951
|
-
new
|
|
1952
|
-
const levelToBrackets = (level) => new
|
|
1991
|
+
new import_typeorm7.Brackets((sqb) => {
|
|
1992
|
+
const levelToBrackets = (level) => new import_typeorm7.Brackets((qb2) => {
|
|
1953
1993
|
level.forEach((expr, i) => {
|
|
1954
1994
|
if (i === 0) {
|
|
1955
1995
|
qb2.where(expr);
|
|
@@ -2202,22 +2242,30 @@ var CrudBase = class {
|
|
|
2202
2242
|
this._restoreBindings(snap);
|
|
2203
2243
|
return res;
|
|
2204
2244
|
}
|
|
2245
|
+
async _mayBeTransaction(cb, manager = this.repo.manager) {
|
|
2246
|
+
const hasActiveTx = !!manager.queryRunner?.isTransactionActive;
|
|
2247
|
+
const getRepo = (m) => m.getRepository(this.entityClass);
|
|
2248
|
+
if (hasActiveTx) {
|
|
2249
|
+
return cb(manager, getRepo(manager));
|
|
2250
|
+
} else {
|
|
2251
|
+
return manager.transaction(async (tdb) => await cb(tdb, getRepo(tdb)));
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2205
2254
|
async _batchCreate(ents, beforeCreate, skipErrors = false) {
|
|
2206
2255
|
const entsWithId = ents.filter((ent) => ent.id != null);
|
|
2207
|
-
return this.
|
|
2256
|
+
return this._mayBeTransaction(async (mdb, repo) => {
|
|
2208
2257
|
let skipped = [];
|
|
2209
|
-
const repo = mdb.getRepository(this.entityClass);
|
|
2210
2258
|
let entsToSave = ents;
|
|
2211
2259
|
if (entsWithId.length) {
|
|
2212
2260
|
const entIds = entsWithId.map((ent) => ent.id);
|
|
2213
|
-
const entIdChunks =
|
|
2261
|
+
const entIdChunks = import_lodash4.default.chunk(entIds, 65535);
|
|
2214
2262
|
const existingEnts = (await Promise.all(
|
|
2215
2263
|
entIdChunks.map(
|
|
2216
2264
|
(chunk) => repo.find({
|
|
2217
2265
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2218
2266
|
// @ts-ignore
|
|
2219
2267
|
where: {
|
|
2220
|
-
id: (0,
|
|
2268
|
+
id: (0, import_typeorm8.In)(chunk)
|
|
2221
2269
|
},
|
|
2222
2270
|
select: this.crudOptions.createOrUpdate ? void 0 : ["id", "deleteTime"],
|
|
2223
2271
|
withDeleted: true
|
|
@@ -2281,7 +2329,7 @@ var CrudBase = class {
|
|
|
2281
2329
|
await beforeCreate(repo);
|
|
2282
2330
|
}
|
|
2283
2331
|
try {
|
|
2284
|
-
const entChunksToSave =
|
|
2332
|
+
const entChunksToSave = import_lodash4.default.chunk(
|
|
2285
2333
|
entsToSave,
|
|
2286
2334
|
Math.floor(
|
|
2287
2335
|
65535 / Math.max(1, Object.keys(entsToSave[0] || {}).length)
|
|
@@ -2314,14 +2362,13 @@ var CrudBase = class {
|
|
|
2314
2362
|
let ent = new this.entityClass();
|
|
2315
2363
|
Object.assign(
|
|
2316
2364
|
ent,
|
|
2317
|
-
(0,
|
|
2365
|
+
(0, import_lodash4.omit)(_ent, ...this._typeormRelations.map((r) => r.propertyName))
|
|
2318
2366
|
);
|
|
2319
2367
|
const invalidReason = ent.isValidInCreate();
|
|
2320
2368
|
if (invalidReason) {
|
|
2321
2369
|
throw new import_nesties8.BlankReturnMessageDto(400, invalidReason).toException();
|
|
2322
2370
|
}
|
|
2323
|
-
const savedEnt = await this.
|
|
2324
|
-
const repo = mdb.getRepository(this.entityClass);
|
|
2371
|
+
const savedEnt = await this._mayBeTransaction(async (mdb, repo) => {
|
|
2325
2372
|
if (ent.id != null) {
|
|
2326
2373
|
const existingEnt = await repo.findOne({
|
|
2327
2374
|
where: { id: ent.id },
|
|
@@ -2420,12 +2467,24 @@ var CrudBase = class {
|
|
|
2420
2467
|
});
|
|
2421
2468
|
}
|
|
2422
2469
|
}
|
|
2470
|
+
_applyQueryKeepEntityVersioningDates(qb) {
|
|
2471
|
+
if (this.crudOptions.keepEntityVersioningDates) {
|
|
2472
|
+
const versioningDateFields = getSpecificFields(
|
|
2473
|
+
this.entityClass,
|
|
2474
|
+
"entityVersioningDate"
|
|
2475
|
+
);
|
|
2476
|
+
for (const field of versioningDateFields) {
|
|
2477
|
+
qb.addSelect(`${this.entityAliasName}.${field}`);
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2423
2481
|
async findOne(id, extraQuery = () => {
|
|
2424
2482
|
}) {
|
|
2425
2483
|
const bindingEnt = await this.getBindingPartialEntity();
|
|
2426
2484
|
const query = this.queryBuilder().where(`${this.entityAliasName}.id = :id`, { id }).take(1);
|
|
2427
2485
|
this._applyQueryRelations(query);
|
|
2428
2486
|
this._applyQueryFromBinding(bindingEnt, query);
|
|
2487
|
+
this._applyQueryKeepEntityVersioningDates(query);
|
|
2429
2488
|
this.extraGetQuery(query);
|
|
2430
2489
|
extraQuery(query);
|
|
2431
2490
|
query.take(1);
|
|
@@ -2467,6 +2526,7 @@ var CrudBase = class {
|
|
|
2467
2526
|
this._applyQueryRelations(query);
|
|
2468
2527
|
this._applyQueryFilters(query, newEnt);
|
|
2469
2528
|
this._applyQueryFromBinding(bindingEnt, query);
|
|
2529
|
+
this._applyQueryKeepEntityVersioningDates(query);
|
|
2470
2530
|
const pageSettings = newEnt instanceof PageSettingsDto ? newEnt : Object.assign(new PageSettingsDto(), newEnt);
|
|
2471
2531
|
this.extraGetQuery(query);
|
|
2472
2532
|
extraQuery(query);
|
|
@@ -2559,6 +2619,116 @@ var CrudBase = class {
|
|
|
2559
2619
|
}
|
|
2560
2620
|
return new import_nesties8.BlankReturnMessageDto(200, "success");
|
|
2561
2621
|
}
|
|
2622
|
+
async upsert(_ent) {
|
|
2623
|
+
const bindingEnt = await this.getBindingPartialEntity();
|
|
2624
|
+
if (!_ent) {
|
|
2625
|
+
throw new import_nesties8.BlankReturnMessageDto(400, "Invalid entity").toException();
|
|
2626
|
+
}
|
|
2627
|
+
const ent = new this.entityClass();
|
|
2628
|
+
Object.assign(
|
|
2629
|
+
ent,
|
|
2630
|
+
(0, import_lodash4.omit)(_ent, ...this._typeormRelations.map((r) => r.propertyName))
|
|
2631
|
+
);
|
|
2632
|
+
const invalidReason = ent.isValidInUpsert();
|
|
2633
|
+
if (invalidReason) {
|
|
2634
|
+
throw new import_nesties8.BlankReturnMessageDto(400, invalidReason).toException();
|
|
2635
|
+
}
|
|
2636
|
+
const upsertColumns = getSpecificFields(this.entityClass, "upsertColumn");
|
|
2637
|
+
const conditions = {
|
|
2638
|
+
...import_lodash4.default.pick(ent, upsertColumns),
|
|
2639
|
+
...bindingEnt
|
|
2640
|
+
};
|
|
2641
|
+
const conditionKeys = [
|
|
2642
|
+
.../* @__PURE__ */ new Set([
|
|
2643
|
+
...getSpecificFields(this.entityClass, "bindingColumn"),
|
|
2644
|
+
...upsertColumns
|
|
2645
|
+
])
|
|
2646
|
+
];
|
|
2647
|
+
Object.assign(ent, conditions);
|
|
2648
|
+
let deleteColumnProperty = "";
|
|
2649
|
+
if (!this.crudOptions.hardDelete) {
|
|
2650
|
+
const deleteColumn = this.repo.manager.connection.getMetadata(
|
|
2651
|
+
this.entityClass
|
|
2652
|
+
).deleteDateColumn;
|
|
2653
|
+
if (deleteColumn) {
|
|
2654
|
+
ent[deleteColumn.propertyName] = null;
|
|
2655
|
+
deleteColumnProperty = deleteColumn.propertyName;
|
|
2656
|
+
}
|
|
2657
|
+
}
|
|
2658
|
+
await ent.beforeUpsert?.();
|
|
2659
|
+
try {
|
|
2660
|
+
const savedEnt = await this._mayBeTransaction(async (mdb, repo) => {
|
|
2661
|
+
const res = await repo.upsert(ent, {
|
|
2662
|
+
conflictPaths: conditionKeys
|
|
2663
|
+
});
|
|
2664
|
+
const insertedId = res.identifiers[0]?.id;
|
|
2665
|
+
const fetchSaved = () => {
|
|
2666
|
+
const qb = repo.createQueryBuilder(this.entityAliasName);
|
|
2667
|
+
if (insertedId != null) {
|
|
2668
|
+
qb.where(`${this.entityAliasName}.id = :id`, { id: insertedId });
|
|
2669
|
+
} else {
|
|
2670
|
+
conditionKeys.forEach((key, i) => {
|
|
2671
|
+
const paramKey = `_cond_${key}`;
|
|
2672
|
+
qb[i === 0 ? "where" : "andWhere"](
|
|
2673
|
+
`${this.entityAliasName}.${key} = :${paramKey}`,
|
|
2674
|
+
{
|
|
2675
|
+
[paramKey]: conditions[key]
|
|
2676
|
+
}
|
|
2677
|
+
);
|
|
2678
|
+
});
|
|
2679
|
+
}
|
|
2680
|
+
qb.take(1);
|
|
2681
|
+
if (deleteColumnProperty) {
|
|
2682
|
+
if (!this.crudOptions.keepEntityVersioningDates) {
|
|
2683
|
+
qb.addSelect(`${this.entityAliasName}.${deleteColumnProperty}`);
|
|
2684
|
+
}
|
|
2685
|
+
qb.withDeleted();
|
|
2686
|
+
}
|
|
2687
|
+
this._applyQueryKeepEntityVersioningDates(qb);
|
|
2688
|
+
if (this.crudOptions.upsertIncludeRelations) {
|
|
2689
|
+
this._applyQueryRelations(qb);
|
|
2690
|
+
}
|
|
2691
|
+
return qb.getOne();
|
|
2692
|
+
};
|
|
2693
|
+
let saved = await fetchSaved();
|
|
2694
|
+
if (!saved) {
|
|
2695
|
+
this.log.error(
|
|
2696
|
+
`Failed to upsert entity ${JSON.stringify(
|
|
2697
|
+
ent
|
|
2698
|
+
)}: cannot find saved entity after upsert.`
|
|
2699
|
+
);
|
|
2700
|
+
throw new import_nesties8.BlankReturnMessageDto(500, "Internal error").toException();
|
|
2701
|
+
}
|
|
2702
|
+
if (deleteColumnProperty && saved[deleteColumnProperty]) {
|
|
2703
|
+
await repo.restore(insertedId ? { id: insertedId } : conditions);
|
|
2704
|
+
saved = await fetchSaved();
|
|
2705
|
+
if (!saved || saved[deleteColumnProperty]) {
|
|
2706
|
+
this.log.error(
|
|
2707
|
+
`Failed to upsert entity ${JSON.stringify(
|
|
2708
|
+
ent
|
|
2709
|
+
)}: cannot restore soft-deleted entity after upsert.`
|
|
2710
|
+
);
|
|
2711
|
+
throw new import_nesties8.BlankReturnMessageDto(
|
|
2712
|
+
500,
|
|
2713
|
+
"Internal error"
|
|
2714
|
+
).toException();
|
|
2715
|
+
}
|
|
2716
|
+
}
|
|
2717
|
+
return saved;
|
|
2718
|
+
});
|
|
2719
|
+
await savedEnt.afterUpsert?.();
|
|
2720
|
+
this.cleanEntityNotInResultFields(savedEnt);
|
|
2721
|
+
return new this.entityReturnMessageDto(200, "success", savedEnt);
|
|
2722
|
+
} catch (e) {
|
|
2723
|
+
if (e instanceof import_common2.HttpException) {
|
|
2724
|
+
throw e;
|
|
2725
|
+
}
|
|
2726
|
+
this.log.error(
|
|
2727
|
+
`Failed to upsert entity ${JSON.stringify(ent)}: ${e.toString()}`
|
|
2728
|
+
);
|
|
2729
|
+
throw new import_nesties8.BlankReturnMessageDto(500, "Internal error").toException();
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2562
2732
|
async delete(id, cond = {}) {
|
|
2563
2733
|
const bindingEnt = await this.getBindingPartialEntity();
|
|
2564
2734
|
let result;
|
|
@@ -2587,11 +2757,11 @@ var CrudBase = class {
|
|
|
2587
2757
|
const newEnt = new this.entityClass();
|
|
2588
2758
|
Object.assign(
|
|
2589
2759
|
newEnt,
|
|
2590
|
-
(0,
|
|
2760
|
+
(0, import_lodash4.omit)(ent, ...this._typeormRelations.map((r) => r.propertyName))
|
|
2591
2761
|
);
|
|
2592
2762
|
return newEnt;
|
|
2593
2763
|
});
|
|
2594
|
-
const invalidResults =
|
|
2764
|
+
const invalidResults = import_lodash4.default.compact(
|
|
2595
2765
|
await Promise.all(
|
|
2596
2766
|
ents.map(async (ent) => {
|
|
2597
2767
|
const reason = ent.isValidInCreate();
|
|
@@ -2764,10 +2934,10 @@ var CrudBase = class {
|
|
|
2764
2934
|
await flush();
|
|
2765
2935
|
return result;
|
|
2766
2936
|
};
|
|
2767
|
-
const
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
)
|
|
2937
|
+
const res = await this._mayBeTransaction(
|
|
2938
|
+
(tdb, repo) => op(repo),
|
|
2939
|
+
options.repo?.manager || this.repo.manager
|
|
2940
|
+
);
|
|
2771
2941
|
return res == null ? new import_nesties8.BlankReturnMessageDto(200, "success") : new import_nesties8.GenericReturnMessageDto(200, "success", res);
|
|
2772
2942
|
}
|
|
2773
2943
|
async _loadFullTextIndex() {
|
|
@@ -2821,7 +2991,7 @@ function CrudService(entityClass, crudOptions = {}) {
|
|
|
2821
2991
|
var import_common3 = require("@nestjs/common");
|
|
2822
2992
|
var import_nesties10 = require("nesties");
|
|
2823
2993
|
var import_swagger6 = require("@nestjs/swagger");
|
|
2824
|
-
var
|
|
2994
|
+
var import_lodash6 = __toESM(require("lodash"));
|
|
2825
2995
|
var import_nesties11 = require("nesties");
|
|
2826
2996
|
|
|
2827
2997
|
// src/bases/base-restful-controller.ts
|
|
@@ -2830,6 +3000,7 @@ var RestfulMethods = [
|
|
|
2830
3000
|
"findAll",
|
|
2831
3001
|
"create",
|
|
2832
3002
|
"update",
|
|
3003
|
+
"upsert",
|
|
2833
3004
|
"delete",
|
|
2834
3005
|
"import"
|
|
2835
3006
|
];
|
|
@@ -2864,6 +3035,9 @@ var BaseRestfulController = class {
|
|
|
2864
3035
|
update(id, dto) {
|
|
2865
3036
|
return this._service.update(id, dto);
|
|
2866
3037
|
}
|
|
3038
|
+
upsert(dto) {
|
|
3039
|
+
return this._service.upsert(dto);
|
|
3040
|
+
}
|
|
2867
3041
|
delete(id) {
|
|
2868
3042
|
return this._service.delete(id);
|
|
2869
3043
|
}
|
|
@@ -2895,7 +3069,7 @@ var PickTypeExpose = (cl, keys) => {
|
|
|
2895
3069
|
|
|
2896
3070
|
// src/utility/patch-column-in-get.ts
|
|
2897
3071
|
var import_nesties9 = require("nesties");
|
|
2898
|
-
var
|
|
3072
|
+
var import_lodash5 = __toESM(require("lodash"));
|
|
2899
3073
|
var import_constants = require("@nestjs/swagger/dist/constants");
|
|
2900
3074
|
var PatchColumnsInGet = (cl, originalCl = cl, fieldsToOmit = []) => {
|
|
2901
3075
|
const omit2 = new Set(fieldsToOmit);
|
|
@@ -2921,7 +3095,7 @@ var PatchColumnsInGet = (cl, originalCl = cl, fieldsToOmit = []) => {
|
|
|
2921
3095
|
field
|
|
2922
3096
|
);
|
|
2923
3097
|
}
|
|
2924
|
-
const queryableFieldsRemaining =
|
|
3098
|
+
const queryableFieldsRemaining = import_lodash5.default.difference(
|
|
2925
3099
|
getSpecificFields(useCl, "queryCondition"),
|
|
2926
3100
|
mutateFields
|
|
2927
3101
|
);
|
|
@@ -2982,7 +3156,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
2982
3156
|
this.entityClass,
|
|
2983
3157
|
{
|
|
2984
3158
|
...this.options,
|
|
2985
|
-
fieldsToOmit:
|
|
3159
|
+
fieldsToOmit: import_lodash6.default.uniq([...this.options.fieldsToOmit || [], ...fields])
|
|
2986
3160
|
},
|
|
2987
3161
|
this.__resolveVisited
|
|
2988
3162
|
);
|
|
@@ -2992,7 +3166,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
2992
3166
|
this.entityClass,
|
|
2993
3167
|
{
|
|
2994
3168
|
...this.options,
|
|
2995
|
-
writeFieldsToOmit:
|
|
3169
|
+
writeFieldsToOmit: import_lodash6.default.uniq([
|
|
2996
3170
|
...this.options.writeFieldsToOmit || [],
|
|
2997
3171
|
...fields
|
|
2998
3172
|
])
|
|
@@ -3005,7 +3179,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3005
3179
|
this.entityClass,
|
|
3006
3180
|
{
|
|
3007
3181
|
...this.options,
|
|
3008
|
-
createFieldsToOmit:
|
|
3182
|
+
createFieldsToOmit: import_lodash6.default.uniq([
|
|
3009
3183
|
...this.options.createFieldsToOmit || [],
|
|
3010
3184
|
...fields
|
|
3011
3185
|
])
|
|
@@ -3018,7 +3192,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3018
3192
|
this.entityClass,
|
|
3019
3193
|
{
|
|
3020
3194
|
...this.options,
|
|
3021
|
-
updateFieldsToOmit:
|
|
3195
|
+
updateFieldsToOmit: import_lodash6.default.uniq([
|
|
3022
3196
|
...this.options.updateFieldsToOmit || [],
|
|
3023
3197
|
...fields
|
|
3024
3198
|
])
|
|
@@ -3026,12 +3200,25 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3026
3200
|
this.__resolveVisited
|
|
3027
3201
|
);
|
|
3028
3202
|
}
|
|
3203
|
+
omitUpsert(...fields) {
|
|
3204
|
+
return new _RestfulFactory(
|
|
3205
|
+
this.entityClass,
|
|
3206
|
+
{
|
|
3207
|
+
...this.options,
|
|
3208
|
+
upsertFieldsToOmit: import_lodash6.default.uniq([
|
|
3209
|
+
...this.options.upsertFieldsToOmit || [],
|
|
3210
|
+
...fields
|
|
3211
|
+
])
|
|
3212
|
+
},
|
|
3213
|
+
this.__resolveVisited
|
|
3214
|
+
);
|
|
3215
|
+
}
|
|
3029
3216
|
omitFindAll(...fields) {
|
|
3030
3217
|
return new _RestfulFactory(
|
|
3031
3218
|
this.entityClass,
|
|
3032
3219
|
{
|
|
3033
3220
|
...this.options,
|
|
3034
|
-
findAllFieldsToOmit:
|
|
3221
|
+
findAllFieldsToOmit: import_lodash6.default.uniq([
|
|
3035
3222
|
...this.options.findAllFieldsToOmit || [],
|
|
3036
3223
|
...fields
|
|
3037
3224
|
])
|
|
@@ -3044,7 +3231,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3044
3231
|
this.entityClass,
|
|
3045
3232
|
{
|
|
3046
3233
|
...this.options,
|
|
3047
|
-
outputFieldsToOmit:
|
|
3234
|
+
outputFieldsToOmit: import_lodash6.default.uniq([
|
|
3048
3235
|
...this.options.outputFieldsToOmit || [],
|
|
3049
3236
|
...fields
|
|
3050
3237
|
])
|
|
@@ -3106,7 +3293,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3106
3293
|
return this.options.entityClassName || this.entityClass.name;
|
|
3107
3294
|
}
|
|
3108
3295
|
get fieldsToOmit() {
|
|
3109
|
-
return
|
|
3296
|
+
return import_lodash6.default.uniq([
|
|
3110
3297
|
...getSpecificFields(this.entityClass, "notColumn"),
|
|
3111
3298
|
...this.options.fieldsToOmit || [],
|
|
3112
3299
|
...getTypeormRelations(this.entityClass).map(
|
|
@@ -3115,7 +3302,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3115
3302
|
]);
|
|
3116
3303
|
}
|
|
3117
3304
|
get fieldsInCreateToOmit() {
|
|
3118
|
-
return
|
|
3305
|
+
return import_lodash6.default.uniq([
|
|
3119
3306
|
...this.fieldsToOmit,
|
|
3120
3307
|
...this.options.writeFieldsToOmit || [],
|
|
3121
3308
|
...this.options.createFieldsToOmit || [],
|
|
@@ -3130,7 +3317,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3130
3317
|
);
|
|
3131
3318
|
}
|
|
3132
3319
|
get fieldsInUpdateToOmit() {
|
|
3133
|
-
return
|
|
3320
|
+
return import_lodash6.default.uniq([
|
|
3134
3321
|
...this.fieldsToOmit,
|
|
3135
3322
|
...this.options.writeFieldsToOmit || [],
|
|
3136
3323
|
...this.options.updateFieldsToOmit || [],
|
|
@@ -3144,21 +3331,36 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3144
3331
|
`Update${this.entityClassName}Dto`
|
|
3145
3332
|
);
|
|
3146
3333
|
}
|
|
3334
|
+
get fieldsInUpsertToOmit() {
|
|
3335
|
+
return import_lodash6.default.uniq([
|
|
3336
|
+
...this.fieldsToOmit,
|
|
3337
|
+
...this.options.writeFieldsToOmit || [],
|
|
3338
|
+
...this.options.upsertFieldsToOmit || [],
|
|
3339
|
+
...getSpecificFields(this.entityClass, "notWritable"),
|
|
3340
|
+
...getSpecificFields(this.entityClass, "notUpsertable")
|
|
3341
|
+
]);
|
|
3342
|
+
}
|
|
3343
|
+
get upsertDto() {
|
|
3344
|
+
return (0, import_nesties11.RenameClass)(
|
|
3345
|
+
OmitTypeExclude(this.entityClass, this.fieldsInUpsertToOmit),
|
|
3346
|
+
`Upsert${this.entityClassName}Dto`
|
|
3347
|
+
);
|
|
3348
|
+
}
|
|
3147
3349
|
get importDto() {
|
|
3148
3350
|
return ImportDataDto(this.createDto);
|
|
3149
3351
|
}
|
|
3150
3352
|
get fieldsInGetToOmit() {
|
|
3151
|
-
return
|
|
3353
|
+
return import_lodash6.default.uniq([
|
|
3152
3354
|
...this.fieldsToOmit,
|
|
3153
3355
|
...getSpecificFields(this.entityClass, "notQueryable"),
|
|
3154
|
-
...
|
|
3356
|
+
...import_lodash6.default.difference(
|
|
3155
3357
|
getSpecificFields(this.entityClass, "requireGetMutator"),
|
|
3156
3358
|
getSpecificFields(this.entityClass, "getMutator")
|
|
3157
3359
|
)
|
|
3158
3360
|
]);
|
|
3159
3361
|
}
|
|
3160
3362
|
get queryableFields() {
|
|
3161
|
-
return
|
|
3363
|
+
return import_lodash6.default.difference(
|
|
3162
3364
|
[
|
|
3163
3365
|
...getSpecificFields(this.entityClass, "queryCondition"),
|
|
3164
3366
|
"pageCount",
|
|
@@ -3236,7 +3438,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3236
3438
|
const relationFactory = new _RestfulFactory(
|
|
3237
3439
|
relation.propertyClass,
|
|
3238
3440
|
{
|
|
3239
|
-
entityClassName: `${this.entityClassName}${this.options.relations ? (0,
|
|
3441
|
+
entityClassName: `${this.entityClassName}${this.options.relations ? (0, import_lodash6.upperFirst)(relation.propertyName) : relation.propertyClass.name}`,
|
|
3240
3442
|
relations: this.options.relations && getNextLevelRelations(
|
|
3241
3443
|
this.options.relations.map(extractRelationName),
|
|
3242
3444
|
relation.propertyName
|
|
@@ -3290,6 +3492,21 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3290
3492
|
`${this.entityClassName}CreateResultDto`
|
|
3291
3493
|
);
|
|
3292
3494
|
}
|
|
3495
|
+
get entityUpsertResultDto() {
|
|
3496
|
+
return (0, import_nesties11.RenameClass)(
|
|
3497
|
+
(0, import_swagger6.OmitType)(this.entityResultDto, [
|
|
3498
|
+
...this.options.upsertIncludeRelations ? [] : getTypeormRelations(this.entityClass).map(
|
|
3499
|
+
(r) => r.propertyName
|
|
3500
|
+
),
|
|
3501
|
+
...getSpecificFields(
|
|
3502
|
+
this.entityClass,
|
|
3503
|
+
"notColumn",
|
|
3504
|
+
(m) => m.hideInUpsert
|
|
3505
|
+
)
|
|
3506
|
+
]),
|
|
3507
|
+
`${this.entityClassName}UpsertResultDto`
|
|
3508
|
+
);
|
|
3509
|
+
}
|
|
3293
3510
|
get entityReturnMessageDto() {
|
|
3294
3511
|
return (0, import_nesties10.ReturnMessageDto)(this.entityResultDto);
|
|
3295
3512
|
}
|
|
@@ -3325,7 +3542,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3325
3542
|
(0, import_common3.HttpCode)(200),
|
|
3326
3543
|
(0, import_swagger6.ApiOperation)({
|
|
3327
3544
|
summary: `Create a new ${this.entityClassName}`,
|
|
3328
|
-
...
|
|
3545
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3329
3546
|
}),
|
|
3330
3547
|
(0, import_swagger6.ApiBody)({ type: this.createDto }),
|
|
3331
3548
|
(0, import_swagger6.ApiOkResponse)({ type: this.entityCreateReturnMessageDto }),
|
|
@@ -3335,12 +3552,36 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3335
3552
|
createParam() {
|
|
3336
3553
|
return (0, import_common3.Body)((0, import_nesties10.DataPipe)(), OmitPipe(this.fieldsInCreateToOmit));
|
|
3337
3554
|
}
|
|
3555
|
+
isUpsertable() {
|
|
3556
|
+
return !!reflector.get("upsertableEntity", this.entityClass);
|
|
3557
|
+
}
|
|
3558
|
+
upsert(extras = {}) {
|
|
3559
|
+
if (!this.isUpsertable()) {
|
|
3560
|
+
throw new Error(
|
|
3561
|
+
`Entity ${this.entityClass.name} is not upsertable. Please define at least one UpsertColumn or BindingColumn, and set @UpsertableEntity() decorator.`
|
|
3562
|
+
);
|
|
3563
|
+
}
|
|
3564
|
+
return (0, import_nesties10.MergeMethodDecorators)([
|
|
3565
|
+
this.usePrefix(import_common3.Put, extras.prefix),
|
|
3566
|
+
(0, import_common3.HttpCode)(200),
|
|
3567
|
+
(0, import_swagger6.ApiOperation)({
|
|
3568
|
+
summary: `Upsert a ${this.entityClassName}`,
|
|
3569
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3570
|
+
}),
|
|
3571
|
+
(0, import_swagger6.ApiBody)({ type: this.upsertDto }),
|
|
3572
|
+
(0, import_swagger6.ApiOkResponse)({ type: this.entityUpsertResultDto }),
|
|
3573
|
+
(0, import_nesties10.ApiError)(400, `The ${this.entityClassName} is not valid`)
|
|
3574
|
+
]);
|
|
3575
|
+
}
|
|
3576
|
+
upsertParam() {
|
|
3577
|
+
return (0, import_common3.Body)((0, import_nesties10.DataPipe)(), OmitPipe(this.fieldsInUpsertToOmit));
|
|
3578
|
+
}
|
|
3338
3579
|
findOne(extras = {}) {
|
|
3339
3580
|
return (0, import_nesties10.MergeMethodDecorators)([
|
|
3340
3581
|
this.usePrefix(import_common3.Get, extras.prefix, ":id"),
|
|
3341
3582
|
(0, import_swagger6.ApiOperation)({
|
|
3342
3583
|
summary: `Find a ${this.entityClassName} by id`,
|
|
3343
|
-
...
|
|
3584
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3344
3585
|
}),
|
|
3345
3586
|
(0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
|
|
3346
3587
|
(0, import_swagger6.ApiOkResponse)({ type: this.entityReturnMessageDto }),
|
|
@@ -3362,7 +3603,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3362
3603
|
this.usePrefix(import_common3.Get, extras.prefix),
|
|
3363
3604
|
(0, import_swagger6.ApiOperation)({
|
|
3364
3605
|
summary: `Find all ${this.entityClassName}`,
|
|
3365
|
-
...
|
|
3606
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3366
3607
|
}),
|
|
3367
3608
|
(0, import_swagger6.ApiOkResponse)({ type: this.entityArrayReturnMessageDto })
|
|
3368
3609
|
]);
|
|
@@ -3372,14 +3613,14 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3372
3613
|
this.usePrefix(import_common3.Get, extras.prefix),
|
|
3373
3614
|
(0, import_swagger6.ApiOperation)({
|
|
3374
3615
|
summary: `Find all ${this.entityClassName}`,
|
|
3375
|
-
...
|
|
3616
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3376
3617
|
}),
|
|
3377
3618
|
(0, import_swagger6.ApiOkResponse)({ type: this.entityCursorPaginationReturnMessageDto })
|
|
3378
3619
|
]);
|
|
3379
3620
|
}
|
|
3380
3621
|
getMutatorColumns() {
|
|
3381
3622
|
const mutatorColumns = getSpecificFields(this.entityClass, "getMutator");
|
|
3382
|
-
return
|
|
3623
|
+
return import_lodash6.default.difference(mutatorColumns, this.fieldsInGetToOmit);
|
|
3383
3624
|
}
|
|
3384
3625
|
findAllParam() {
|
|
3385
3626
|
const mutatorColumns = this.getMutatorColumns();
|
|
@@ -3399,7 +3640,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3399
3640
|
(0, import_common3.HttpCode)(200),
|
|
3400
3641
|
(0, import_swagger6.ApiOperation)({
|
|
3401
3642
|
summary: `Update a ${this.entityClassName} by id`,
|
|
3402
|
-
...
|
|
3643
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3403
3644
|
}),
|
|
3404
3645
|
(0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
|
|
3405
3646
|
(0, import_swagger6.ApiBody)({ type: this.updateDto }),
|
|
@@ -3421,7 +3662,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3421
3662
|
(0, import_common3.HttpCode)(200),
|
|
3422
3663
|
(0, import_swagger6.ApiOperation)({
|
|
3423
3664
|
summary: `Delete a ${this.entityClassName} by id`,
|
|
3424
|
-
...
|
|
3665
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3425
3666
|
}),
|
|
3426
3667
|
(0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
|
|
3427
3668
|
(0, import_nesties10.ApiBlankResponse)(),
|
|
@@ -3438,7 +3679,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3438
3679
|
(0, import_common3.HttpCode)(200),
|
|
3439
3680
|
(0, import_swagger6.ApiOperation)({
|
|
3440
3681
|
summary: `Import ${this.entityClassName}`,
|
|
3441
|
-
...
|
|
3682
|
+
...import_lodash6.default.omit(extras, "prefix")
|
|
3442
3683
|
}),
|
|
3443
3684
|
(0, import_swagger6.ApiBody)({ type: this.importDto }),
|
|
3444
3685
|
(0, import_swagger6.ApiOkResponse)({ type: this.importReturnMessageDto }),
|
|
@@ -3450,8 +3691,8 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3450
3691
|
this.usePrefix(import_common3.Post, options.prefix, ":id", operationName),
|
|
3451
3692
|
(0, import_common3.HttpCode)(200),
|
|
3452
3693
|
(0, import_swagger6.ApiOperation)({
|
|
3453
|
-
summary: `${(0,
|
|
3454
|
-
...
|
|
3694
|
+
summary: `${(0, import_lodash6.upperFirst)(operationName)} a ${this.entityClassName} by id`,
|
|
3695
|
+
...import_lodash6.default.omit(options, "prefix", "returnType")
|
|
3455
3696
|
}),
|
|
3456
3697
|
options.returnType ? (0, import_nesties10.ApiTypeResponse)(options.returnType) : (0, import_nesties10.ApiBlankResponse)(),
|
|
3457
3698
|
(0, import_nesties10.ApiError)(
|
|
@@ -3475,6 +3716,7 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3475
3716
|
(m) => routeOptions?.routes?.[m]?.enabled === true
|
|
3476
3717
|
);
|
|
3477
3718
|
const validMethods = RestfulMethods.filter((m) => {
|
|
3719
|
+
if (m === "upsert" && !this.isUpsertable()) return false;
|
|
3478
3720
|
const value = routeOptions?.routes?.[m]?.enabled;
|
|
3479
3721
|
if (value === false) return false;
|
|
3480
3722
|
if (value === true) return true;
|
|
@@ -3508,6 +3750,11 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3508
3750
|
paramDecorators: () => [this.idParam(), this.updateParam()],
|
|
3509
3751
|
methodDecorators: () => [this.update()]
|
|
3510
3752
|
},
|
|
3753
|
+
upsert: {
|
|
3754
|
+
paramTypes: [this.upsertDto],
|
|
3755
|
+
paramDecorators: () => [this.upsertParam()],
|
|
3756
|
+
methodDecorators: () => [this.upsert()]
|
|
3757
|
+
},
|
|
3511
3758
|
delete: {
|
|
3512
3759
|
paramTypes: [this.idType],
|
|
3513
3760
|
paramDecorators: () => [this.idParam()],
|
|
@@ -3574,9 +3821,14 @@ var _RestfulFactory = class _RestfulFactory {
|
|
|
3574
3821
|
return (0, import_nesties11.RenameClass)(cl, `${this.entityClassName}Controller`);
|
|
3575
3822
|
}
|
|
3576
3823
|
crudService(options = {}) {
|
|
3824
|
+
const keysToMigrate = [
|
|
3825
|
+
"relations",
|
|
3826
|
+
"outputFieldsToOmit",
|
|
3827
|
+
"upsertIncludeRelations",
|
|
3828
|
+
"keepEntityVersioningDates"
|
|
3829
|
+
];
|
|
3577
3830
|
return CrudService(this.entityClass, {
|
|
3578
|
-
|
|
3579
|
-
outputFieldsToOmit: this.options.outputFieldsToOmit,
|
|
3831
|
+
...import_lodash6.default.pick(this.options, keysToMigrate),
|
|
3580
3832
|
...options
|
|
3581
3833
|
});
|
|
3582
3834
|
}
|
|
@@ -3596,6 +3848,12 @@ __decorateClass([
|
|
|
3596
3848
|
__decorateClass([
|
|
3597
3849
|
(0, import_nfkit.Memorize)()
|
|
3598
3850
|
], _RestfulFactory.prototype, "updateDto", 1);
|
|
3851
|
+
__decorateClass([
|
|
3852
|
+
(0, import_nfkit.Memorize)()
|
|
3853
|
+
], _RestfulFactory.prototype, "fieldsInUpsertToOmit", 1);
|
|
3854
|
+
__decorateClass([
|
|
3855
|
+
(0, import_nfkit.Memorize)()
|
|
3856
|
+
], _RestfulFactory.prototype, "upsertDto", 1);
|
|
3599
3857
|
__decorateClass([
|
|
3600
3858
|
(0, import_nfkit.Memorize)()
|
|
3601
3859
|
], _RestfulFactory.prototype, "importDto", 1);
|
|
@@ -3617,6 +3875,9 @@ __decorateClass([
|
|
|
3617
3875
|
__decorateClass([
|
|
3618
3876
|
(0, import_nfkit.Memorize)()
|
|
3619
3877
|
], _RestfulFactory.prototype, "entityCreateResultDto", 1);
|
|
3878
|
+
__decorateClass([
|
|
3879
|
+
(0, import_nfkit.Memorize)()
|
|
3880
|
+
], _RestfulFactory.prototype, "entityUpsertResultDto", 1);
|
|
3620
3881
|
__decorateClass([
|
|
3621
3882
|
(0, import_nfkit.Memorize)()
|
|
3622
3883
|
], _RestfulFactory.prototype, "entityReturnMessageDto", 1);
|
|
@@ -3703,8 +3964,8 @@ var applyQueryMatchBooleanMySQL = createQueryCondition(
|
|
|
3703
3964
|
);
|
|
3704
3965
|
|
|
3705
3966
|
// src/transactional-typeorm.module.ts
|
|
3706
|
-
var
|
|
3707
|
-
var
|
|
3967
|
+
var import_typeorm9 = __toESM(require_typeorm());
|
|
3968
|
+
var import_typeorm10 = require("typeorm");
|
|
3708
3969
|
var import_common5 = require("@nestjs/common");
|
|
3709
3970
|
var import_nesties12 = require("nesties");
|
|
3710
3971
|
var import_core = require("@nestjs/core");
|
|
@@ -3756,7 +4017,7 @@ var import_rxjs = require("rxjs");
|
|
|
3756
4017
|
var requestWeakMap = /* @__PURE__ */ new WeakMap();
|
|
3757
4018
|
var normalizeDataSourceToken = (token) => typeof token === "string" ? token : token.name || token.toString();
|
|
3758
4019
|
var TransactionalTypeOrmInterceptor = (dataSource) => {
|
|
3759
|
-
const token = (0,
|
|
4020
|
+
const token = (0, import_typeorm9.getEntityManagerToken)(dataSource);
|
|
3760
4021
|
const interceptorClass = class SpecificTransactionalTypeOrmInterceptor {
|
|
3761
4022
|
constructor(entityManager) {
|
|
3762
4023
|
this.entityManager = entityManager;
|
|
@@ -3767,7 +4028,7 @@ var TransactionalTypeOrmInterceptor = (dataSource) => {
|
|
|
3767
4028
|
let innerSub = null;
|
|
3768
4029
|
let finished = false;
|
|
3769
4030
|
let abort;
|
|
3770
|
-
const aborted = new Promise((
|
|
4031
|
+
const aborted = new Promise((_7, reject) => {
|
|
3771
4032
|
abort = reject;
|
|
3772
4033
|
});
|
|
3773
4034
|
const run = this.entityManager.transaction(async (txEm) => {
|
|
@@ -3810,18 +4071,18 @@ var TransactionalTypeOrmInterceptor = (dataSource) => {
|
|
|
3810
4071
|
});
|
|
3811
4072
|
Reflect.defineMetadata(
|
|
3812
4073
|
"design:paramtypes",
|
|
3813
|
-
[
|
|
4074
|
+
[import_typeorm10.EntityManager],
|
|
3814
4075
|
interceptorClass
|
|
3815
4076
|
);
|
|
3816
4077
|
(0, import_common5.Inject)(token)(interceptorClass.prototype, void 0, 0);
|
|
3817
4078
|
(0, import_common5.Injectable)()(interceptorClass);
|
|
3818
4079
|
return interceptorClass;
|
|
3819
4080
|
};
|
|
3820
|
-
var getTransactionalEntityManagerToken = (dataSource) => `Transactional${normalizeDataSourceToken((0,
|
|
4081
|
+
var getTransactionalEntityManagerToken = (dataSource) => `Transactional${normalizeDataSourceToken((0, import_typeorm9.getEntityManagerToken)(dataSource))}`;
|
|
3821
4082
|
var getTransactionalEntityManagerProvider = (dataSource) => (0, import_nesties12.createProvider)(
|
|
3822
4083
|
{
|
|
3823
4084
|
provide: getTransactionalEntityManagerToken(dataSource),
|
|
3824
|
-
inject: [(0,
|
|
4085
|
+
inject: [(0, import_typeorm9.getEntityManagerToken)(dataSource), import_core.REQUEST],
|
|
3825
4086
|
scope: import_common5.Scope.REQUEST
|
|
3826
4087
|
},
|
|
3827
4088
|
(entityManager, request) => {
|
|
@@ -3838,12 +4099,12 @@ var InjectTransactionalEntityManager = createInjectFromTokenFactory(
|
|
|
3838
4099
|
getTransactionalEntityManagerToken
|
|
3839
4100
|
);
|
|
3840
4101
|
var getTransactionalRepositoryToken = (entity, dataSource) => `Transactional${normalizeDataSourceToken(
|
|
3841
|
-
(0,
|
|
4102
|
+
(0, import_typeorm9.getEntityManagerToken)(dataSource)
|
|
3842
4103
|
)}Repository_${entity.name || entity.toString()}`;
|
|
3843
4104
|
var getTransactionalRepositoryProvider = (entity, dataSource) => (0, import_nesties12.createProvider)(
|
|
3844
4105
|
{
|
|
3845
4106
|
provide: getTransactionalRepositoryToken(entity, dataSource),
|
|
3846
|
-
inject: [(0,
|
|
4107
|
+
inject: [(0, import_typeorm9.getEntityManagerToken)(dataSource), import_core.REQUEST],
|
|
3847
4108
|
scope: import_common5.Scope.REQUEST
|
|
3848
4109
|
},
|
|
3849
4110
|
(entityManager, req) => {
|
|
@@ -3872,7 +4133,7 @@ var TransactionalTypeOrmModule = class {
|
|
|
3872
4133
|
(entity) => getTransactionalRepositoryProvider(entity, dataSource)
|
|
3873
4134
|
)
|
|
3874
4135
|
];
|
|
3875
|
-
const moduleImports = entityArray.length ? [
|
|
4136
|
+
const moduleImports = entityArray.length ? [import_typeorm9.TypeOrmModule.forFeature(entityArray, dataSource)] : [];
|
|
3876
4137
|
const moduleExports = [...providers, ...moduleImports];
|
|
3877
4138
|
return {
|
|
3878
4139
|
module: TransactionalTypeOrmModule,
|
|
@@ -3925,6 +4186,7 @@ TransactionalTypeOrmModule = __decorateClass([
|
|
|
3925
4186
|
NotCreatable,
|
|
3926
4187
|
NotInResult,
|
|
3927
4188
|
NotQueryable,
|
|
4189
|
+
NotUpsertable,
|
|
3928
4190
|
NotWritable,
|
|
3929
4191
|
OmitPipe,
|
|
3930
4192
|
OptionalDataPipe,
|
|
@@ -3965,6 +4227,8 @@ TransactionalTypeOrmModule = __decorateClass([
|
|
|
3965
4227
|
TimeBase,
|
|
3966
4228
|
TransactionalTypeOrmInterceptor,
|
|
3967
4229
|
TransactionalTypeOrmModule,
|
|
4230
|
+
UpsertColumn,
|
|
4231
|
+
UpsertableEntity,
|
|
3968
4232
|
UuidColumn,
|
|
3969
4233
|
applyQueryMatchBoolean,
|
|
3970
4234
|
applyQueryMatchBooleanMySQL,
|