@koalarx/nest 3.0.8 → 3.0.10

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.
@@ -19,6 +19,9 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
19
19
  private readonly _modelName;
20
20
  private readonly _include?;
21
21
  constructor({ context, modelName, include, }: RepositoryInitProps<TEntity, TContext>);
22
+ private getConnectPrismaSchemaForRelation;
23
+ private getSelectRootPrismaSchema;
24
+ private getPropNameFromEntitySource;
22
25
  private listRelationEntities;
23
26
  private listToRelationActionList;
24
27
  private entityToPrisma;
@@ -27,7 +30,6 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
27
30
  private orphanRemoval;
28
31
  private getIdPropName;
29
32
  private getInclude;
30
- private getPropNameFromEntitySource;
31
33
  private persistRelations;
32
34
  protected context(transactionalClient?: TContext): TContext[TModelKey];
33
35
  protected findById(id: IComparableId): Promise<TEntity | null>;
@@ -16,6 +16,46 @@ class RepositoryBase {
16
16
  this._modelName = modelName;
17
17
  this._include = include;
18
18
  }
19
+ getConnectPrismaSchemaForRelation(entity, data) {
20
+ const propIdName = this.getIdPropName(entity);
21
+ return {
22
+ connect: {
23
+ [propIdName]: (data ?? entity)[propIdName],
24
+ },
25
+ };
26
+ }
27
+ getSelectRootPrismaSchema(entity) {
28
+ const selectSchema = {};
29
+ Object.keys(entity)
30
+ .filter((key) => !['id', '_id', '_action'].includes(key))
31
+ .filter((key) => !(entity[key] instanceof Function || entity[key] instanceof list_1.List))
32
+ .forEach((key) => {
33
+ if (entity[key] instanceof entity_base_1.EntityBase) {
34
+ selectSchema[key] = {
35
+ select: this.getSelectRootPrismaSchema(entity[key]),
36
+ };
37
+ }
38
+ else {
39
+ selectSchema[key] = true;
40
+ }
41
+ });
42
+ return selectSchema;
43
+ }
44
+ getPropNameFromEntitySource(source, entity) {
45
+ return Object.keys(source).find((key) => {
46
+ const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(source.constructor, key);
47
+ if (propDefinitions) {
48
+ if (propDefinitions.type === entity.name) {
49
+ return true;
50
+ }
51
+ else if (source[key] instanceof list_1.List) {
52
+ const list = source[key];
53
+ return list.entityType?.name === entity.name;
54
+ }
55
+ }
56
+ return false;
57
+ });
58
+ }
19
59
  listRelationEntities(entity) {
20
60
  const relationEntities = [];
21
61
  Object.keys(entity).forEach((key) => {
@@ -23,16 +63,13 @@ class RepositoryBase {
23
63
  const list = entity[key];
24
64
  list.toArray('added').forEach((item) => {
25
65
  relationEntities.push(item);
26
- relationEntities.push(...this.listRelationEntities(item));
27
66
  });
28
67
  list.toArray('updated').forEach((item) => {
29
68
  relationEntities.push(item);
30
- relationEntities.push(...this.listRelationEntities(item));
31
69
  });
32
70
  }
33
71
  else if (entity[key] instanceof entity_base_1.EntityBase) {
34
72
  relationEntities.push(entity[key]);
35
- relationEntities.push(...this.listRelationEntities(entity[key]));
36
73
  }
37
74
  });
38
75
  return relationEntities;
@@ -47,6 +84,7 @@ class RepositoryBase {
47
84
  const entityInstance = list.entityType;
48
85
  const modelName = entityInstance.name;
49
86
  const parentModelName = entity.constructor.name;
87
+ const parentPropName = this.getPropNameFromEntitySource(new entityInstance(), entity.constructor) ?? parentModelName;
50
88
  if (modelName) {
51
89
  list.toArray('removed').forEach((item) => {
52
90
  relationDeletes.push({
@@ -63,12 +101,9 @@ class RepositoryBase {
63
101
  schema: {
64
102
  data: {
65
103
  ...this.entityToPrisma(item),
66
- [(0, KlString_1.toCamelCase)(parentModelName)]: {
67
- connect: {
68
- [this.getIdPropName(entity)]: entity[this.getIdPropName(entity)],
69
- },
70
- },
104
+ [parentPropName]: this.getConnectPrismaSchemaForRelation(entity),
71
105
  },
106
+ select: this.getSelectRootPrismaSchema(item),
72
107
  },
73
108
  relations: this.listRelationEntities(item),
74
109
  });
@@ -80,6 +115,7 @@ class RepositoryBase {
80
115
  schema: {
81
116
  where: { id: item._id },
82
117
  data: this.entityToPrisma(item),
118
+ select: this.getSelectRootPrismaSchema(item),
83
119
  },
84
120
  relations: this.listRelationEntities(item),
85
121
  });
@@ -119,7 +155,7 @@ class RepositoryBase {
119
155
  };
120
156
  }
121
157
  }
122
- else {
158
+ else if (!Array.isArray(entity[key])) {
123
159
  prismaSchema[key] = entity[key];
124
160
  }
125
161
  });
@@ -165,21 +201,6 @@ class RepositoryBase {
165
201
  });
166
202
  return result;
167
203
  }
168
- getPropNameFromEntitySource(source, entity) {
169
- return Object.keys(source).find((key) => {
170
- const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(source.constructor, key);
171
- if (propDefinitions) {
172
- if (propDefinitions.type === entity.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.name;
178
- }
179
- }
180
- return false;
181
- });
182
- }
183
204
  persistRelations(transaction, entity) {
184
205
  const { relationCreates, relationUpdates, relationDeletes } = this.listToRelationActionList(entity);
185
206
  return Promise.all([
@@ -188,18 +209,17 @@ class RepositoryBase {
188
209
  .then((response) => {
189
210
  return Promise.all(relationCreate.relations.map((relation) => {
190
211
  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}`);
212
+ if (relationPropName) {
213
+ relation[relationPropName] =
214
+ this.getConnectPrismaSchemaForRelation(relationCreate.entityInstance, response);
193
215
  }
194
- const relationEntity = this.createEntity(response, relationCreate.entityInstance);
195
- relationEntity._action = entity_base_1.EntityActionType.create;
196
- relation[relationPropName] = relationEntity;
197
216
  return transaction[(0, KlString_1.toCamelCase)(relation.constructor.name)]
198
217
  .create({
199
218
  data: this.entityToPrisma(relation),
219
+ select: this.getSelectRootPrismaSchema(relation),
200
220
  })
201
221
  .then((response) => {
202
- entity[this.getIdPropName(relation)] =
222
+ relation[this.getIdPropName(relation)] =
203
223
  response[this.getIdPropName(relation)];
204
224
  return this.persistRelations(transaction, relation);
205
225
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.0.8",
3
+ "version": "3.0.10",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",