@koalarx/nest 3.0.9 → 3.0.11

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.
@@ -13,21 +13,25 @@ interface RepositoryInitProps<TEntity extends EntityBase<TEntity>, TContext exte
13
13
  modelName: Type<TEntity>;
14
14
  transactionContext?: Type<TContext>;
15
15
  include?: RepositoryInclude<TEntity>;
16
+ includeFindMany?: RepositoryInclude<TEntity>;
16
17
  }
17
18
  export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient = PrismaTransactionalClient, TModelKey extends keyof TContext = keyof TContext> {
18
19
  protected _context: TContext;
19
20
  private readonly _modelName;
20
21
  private readonly _include?;
21
- constructor({ context, modelName, include, }: RepositoryInitProps<TEntity, TContext>);
22
+ private readonly _includeFindMany?;
23
+ constructor({ context, modelName, include, includeFindMany, }: RepositoryInitProps<TEntity, TContext>);
24
+ private getConnectPrismaSchemaForRelation;
25
+ private getSelectRootPrismaSchema;
22
26
  private getPropNameFromEntitySource;
23
27
  private listRelationEntities;
24
28
  private listToRelationActionList;
25
29
  private entityToPrisma;
30
+ private getInclude;
26
31
  private findManySchema;
27
32
  private createEntity;
28
33
  private orphanRemoval;
29
34
  private getIdPropName;
30
- private getInclude;
31
35
  private persistRelations;
32
36
  protected context(transactionalClient?: TContext): TContext[TModelKey];
33
37
  protected findById(id: IComparableId): Promise<TEntity | null>;
@@ -11,10 +11,38 @@ class RepositoryBase {
11
11
  _context;
12
12
  _modelName;
13
13
  _include;
14
- constructor({ context, modelName, include, }) {
14
+ _includeFindMany;
15
+ constructor({ context, modelName, include, includeFindMany, }) {
15
16
  this._context = context;
16
17
  this._modelName = modelName;
17
18
  this._include = include;
19
+ this._includeFindMany =
20
+ includeFindMany ?? this.getSelectRootPrismaSchema(new modelName());
21
+ }
22
+ getConnectPrismaSchemaForRelation(entity, data) {
23
+ const propIdName = this.getIdPropName(entity);
24
+ return {
25
+ connect: {
26
+ [propIdName]: (data ?? entity)[propIdName],
27
+ },
28
+ };
29
+ }
30
+ getSelectRootPrismaSchema(entity) {
31
+ const selectSchema = {};
32
+ Object.keys(entity)
33
+ .filter((key) => !['_id', '_action'].includes(key))
34
+ .filter((key) => !(entity[key] instanceof Function || entity[key] instanceof list_1.List))
35
+ .forEach((key) => {
36
+ if (entity[key] instanceof entity_base_1.EntityBase) {
37
+ selectSchema[key] = {
38
+ select: this.getSelectRootPrismaSchema(entity[key]),
39
+ };
40
+ }
41
+ else {
42
+ selectSchema[key] = true;
43
+ }
44
+ });
45
+ return selectSchema;
18
46
  }
19
47
  getPropNameFromEntitySource(source, entity) {
20
48
  return Object.keys(source).find((key) => {
@@ -76,12 +104,9 @@ class RepositoryBase {
76
104
  schema: {
77
105
  data: {
78
106
  ...this.entityToPrisma(item),
79
- [parentPropName]: {
80
- connect: {
81
- [this.getIdPropName(entity)]: entity[this.getIdPropName(entity)],
82
- },
83
- },
107
+ [parentPropName]: this.getConnectPrismaSchemaForRelation(entity),
84
108
  },
109
+ select: this.getSelectRootPrismaSchema(item),
85
110
  },
86
111
  relations: this.listRelationEntities(item),
87
112
  });
@@ -93,6 +118,7 @@ class RepositoryBase {
93
118
  schema: {
94
119
  where: { id: item._id },
95
120
  data: this.entityToPrisma(item),
121
+ select: this.getSelectRootPrismaSchema(item),
96
122
  },
97
123
  relations: this.listRelationEntities(item),
98
124
  });
@@ -138,9 +164,24 @@ class RepositoryBase {
138
164
  });
139
165
  return prismaSchema;
140
166
  }
167
+ getInclude(include) {
168
+ include = include ?? this._include ?? {};
169
+ const result = {};
170
+ Object.keys(include).forEach((key) => {
171
+ if (typeof include[key] === 'boolean') {
172
+ result[key] = include[key];
173
+ }
174
+ else {
175
+ result[key] = {
176
+ include: this.getInclude(include[key]),
177
+ };
178
+ }
179
+ });
180
+ return result;
181
+ }
141
182
  findManySchema(where, pagination) {
142
183
  return {
143
- include: this.getInclude(),
184
+ include: this.getInclude(this._includeFindMany),
144
185
  where,
145
186
  orderBy: pagination?.generateOrderBy(),
146
187
  skip: pagination?.skip(),
@@ -163,21 +204,6 @@ class RepositoryBase {
163
204
  getIdPropName(entity) {
164
205
  return (Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype) ?? 'id');
165
206
  }
166
- getInclude(include) {
167
- include = include ?? this._include ?? {};
168
- const result = {};
169
- Object.keys(include).forEach((key) => {
170
- if (typeof include[key] === 'boolean') {
171
- result[key] = include[key];
172
- }
173
- else {
174
- result[key] = {
175
- include: this.getInclude(include[key]),
176
- };
177
- }
178
- });
179
- return result;
180
- }
181
207
  persistRelations(transaction, entity) {
182
208
  const { relationCreates, relationUpdates, relationDeletes } = this.listToRelationActionList(entity);
183
209
  return Promise.all([
@@ -186,15 +212,15 @@ class RepositoryBase {
186
212
  .then((response) => {
187
213
  return Promise.all(relationCreate.relations.map((relation) => {
188
214
  const relationPropName = this.getPropNameFromEntitySource(relation, relationCreate.entityInstance);
189
- if (relationPropName) {
190
- const relationEntity = this.createEntity(response, relationCreate.entityInstance);
191
- relationEntity._action = entity_base_1.EntityActionType.create;
192
- relation[relationPropName] = relationEntity;
215
+ if (relationPropName &&
216
+ !(relation[relationPropName] instanceof list_1.List)) {
217
+ relation[relationPropName] =
218
+ this.getConnectPrismaSchemaForRelation(relationCreate.entityInstance, response);
193
219
  }
194
220
  return transaction[(0, KlString_1.toCamelCase)(relation.constructor.name)]
195
221
  .create({
196
222
  data: this.entityToPrisma(relation),
197
- select: { [this.getIdPropName(relation)]: true },
223
+ select: this.getSelectRootPrismaSchema(relation),
198
224
  })
199
225
  .then((response) => {
200
226
  relation[this.getIdPropName(relation)] =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.0.9",
3
+ "version": "3.0.11",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",