@koalarx/nest 1.18.14 → 1.18.16

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.
@@ -27,6 +27,7 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
27
27
  private orphanRemoval;
28
28
  private getIdPropName;
29
29
  private getInclude;
30
+ private getPropNameFromEntitySource;
30
31
  private persistRelations;
31
32
  protected context(transactionalClient?: TContext): TContext[TModelKey];
32
33
  protected findById(id: IComparableId): Promise<TEntity | null>;
@@ -6,6 +6,7 @@ const pagination_dto_1 = require("../dtos/pagination.dto");
6
6
  const koala_global_vars_1 = require("../koala-global-vars");
7
7
  const list_1 = require("../utils/list");
8
8
  const entity_base_1 = require("./entity.base");
9
+ const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
9
10
  class RepositoryBase {
10
11
  _context;
11
12
  _modelName;
@@ -43,12 +44,14 @@ class RepositoryBase {
43
44
  Object.keys(entity).forEach((key) => {
44
45
  if (entity[key] instanceof list_1.List) {
45
46
  const list = entity[key];
46
- const modelName = list.entityType?.name;
47
+ const entityInstance = list.entityType;
48
+ const modelName = entityInstance.name;
47
49
  const parentModelName = entity.constructor.name;
48
50
  if (modelName) {
49
51
  list.toArray('removed').forEach((item) => {
50
52
  relationDeletes.push({
51
53
  modelName: (0, KlString_1.toCamelCase)(modelName),
54
+ entityInstance,
52
55
  schema: { where: { id: item._id } },
53
56
  relations: [],
54
57
  });
@@ -56,6 +59,7 @@ class RepositoryBase {
56
59
  list.toArray('added').forEach((item) => {
57
60
  relationCreates.push({
58
61
  modelName: (0, KlString_1.toCamelCase)(modelName),
62
+ entityInstance,
59
63
  schema: {
60
64
  data: {
61
65
  ...this.entityToPrisma(item),
@@ -72,6 +76,7 @@ class RepositoryBase {
72
76
  list.toArray('updated').forEach((item) => {
73
77
  relationUpdates.push({
74
78
  modelName: (0, KlString_1.toCamelCase)(modelName),
79
+ entityInstance,
75
80
  schema: {
76
81
  where: { id: item._id },
77
82
  data: this.entityToPrisma(item),
@@ -129,8 +134,8 @@ class RepositoryBase {
129
134
  take: (pagination?.limit ?? 0) > 0 ? pagination?.limit : undefined,
130
135
  };
131
136
  }
132
- createEntity(data) {
133
- const entity = new this._modelName();
137
+ createEntity(data, entityClass) {
138
+ const entity = new (entityClass || this._modelName)();
134
139
  entity._action = entity_base_1.EntityActionType.update;
135
140
  entity.automap(data);
136
141
  return entity;
@@ -160,6 +165,21 @@ class RepositoryBase {
160
165
  });
161
166
  return result;
162
167
  }
168
+ getPropNameFromEntitySource(source, entity) {
169
+ return Object.keys(source).find((key) => {
170
+ const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(source, key);
171
+ if (propDefinitions) {
172
+ if (propDefinitions.type === entity.constructor.name) {
173
+ return true;
174
+ }
175
+ else if (source[key] instanceof list_1.List) {
176
+ const list = source[key];
177
+ return list.entityType?.name === entity.constructor.name;
178
+ }
179
+ }
180
+ return false;
181
+ });
182
+ }
163
183
  persistRelations(transaction, entity) {
164
184
  const { relationCreates, relationUpdates, relationDeletes } = this.listToRelationActionList(entity);
165
185
  return Promise.all([
@@ -167,10 +187,12 @@ class RepositoryBase {
167
187
  .create(relationCreate.schema)
168
188
  .then((response) => {
169
189
  return Promise.all(relationCreate.relations.map((relation) => {
170
- const relationEntity = entity[(0, KlString_1.toCamelCase)(relation.constructor.name)];
171
- relationEntity[this.getIdPropName(relationEntity)] =
172
- response[this.getIdPropName(relationEntity)];
173
- return this.persistRelations(transaction, relationEntity);
190
+ const relationPropName = this.getPropNameFromEntitySource(relation, relationCreate.entityInstance);
191
+ if (!relationPropName) {
192
+ throw new Error(`Propname not found for relation entity ${relation.constructor.name} on entity ${relationCreate.entityInstance.constructor.name}`);
193
+ }
194
+ relation[relationPropName] = this.createEntity(response, relationCreate.entityInstance);
195
+ return this.persistRelations(transaction, relation);
174
196
  }));
175
197
  })),
176
198
  ...relationUpdates.map((relation) => transaction[relation.modelName].update(relation.schema)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "1.18.14",
3
+ "version": "1.18.16",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",