@koalarx/nest 1.18.15 → 1.18.17

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.
@@ -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),
@@ -92,11 +97,11 @@ class RepositoryBase {
92
97
  .forEach((key) => {
93
98
  if (entity[key] instanceof entity_base_1.EntityBase) {
94
99
  if (entity[key]._action === entity_base_1.EntityActionType.create) {
95
- if (entity[key][this.getIdPropName()]) {
100
+ if (entity[key][this.getIdPropName(entity)]) {
96
101
  prismaSchema[key] = {
97
102
  connectOrCreate: {
98
103
  where: {
99
- [this.getIdPropName()]: entity[key][this.getIdPropName()],
104
+ [this.getIdPropName(entity)]: entity[key][this.getIdPropName(entity)],
100
105
  },
101
106
  create: this.entityToPrisma(entity[key]),
102
107
  },
@@ -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;
@@ -162,12 +167,15 @@ class RepositoryBase {
162
167
  }
163
168
  getPropNameFromEntitySource(source, entity) {
164
169
  return Object.keys(source).find((key) => {
165
- if (source[key] instanceof entity_base_1.EntityBase) {
166
- return source[key].constructor.name === entity.constructor.name;
167
- }
168
- else if (source[key] instanceof list_1.List) {
169
- const list = source[key];
170
- return list.entityType?.name === entity.constructor.name;
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
+ }
171
179
  }
172
180
  return false;
173
181
  });
@@ -179,13 +187,18 @@ class RepositoryBase {
179
187
  .create(relationCreate.schema)
180
188
  .then((response) => {
181
189
  return Promise.all(relationCreate.relations.map((relation) => {
182
- const relationPropName = this.getPropNameFromEntitySource(entity, relation);
190
+ const relationPropName = this.getPropNameFromEntitySource(relation, relationCreate.entityInstance);
183
191
  if (!relationPropName) {
184
- throw new Error(`Propname not found for relation entity ${relation.constructor.name} on entity ${entity.constructor.name}`);
192
+ throw new Error(`Propname not found for relation entity ${relation.constructor.name} on entity ${relationCreate.entityInstance.constructor.name}`);
185
193
  }
186
- entity.automap(response);
187
- relation[relationPropName] = entity;
188
- return this.persistRelations(transaction, relation);
194
+ relation[relationPropName] = this.createEntity(response, relationCreate.entityInstance);
195
+ return transaction[(0, KlString_1.toCamelCase)(relation.constructor.name)]
196
+ .create(this.entityToPrisma(relation))
197
+ .then((response) => {
198
+ entity[this.getIdPropName(relation)] =
199
+ response[this.getIdPropName(relation)];
200
+ return this.persistRelations(transaction, relation);
201
+ });
189
202
  }));
190
203
  })),
191
204
  ...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.15",
3
+ "version": "1.18.17",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",