@appxdigital/appx-core 0.1.98 → 0.1.100

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.
@@ -14,6 +14,7 @@ export declare class PrismaService {
14
14
  prismaClient: PrismaClient;
15
15
  constructor(prismaClient: PrismaClient, permissionsConfig: PermissionsConfigType);
16
16
  debugQueries(enable: boolean): void;
17
+ withExposedModels(models: string[], callback: () => Promise<void>): Promise<void>;
17
18
  private debug;
18
19
  $transaction<T>(fn: (prisma: Prisma.TransactionClient) => Promise<T>): any;
19
20
  proxyModels(): void;
@@ -100,6 +101,7 @@ export declare class PrismaService {
100
101
  relation: string;
101
102
  foreignKey?: string;
102
103
  referencingColumn?: string;
104
+ isRequired: boolean;
103
105
  };
104
106
  selectPermission(permissions: any, action: string, modelName: string, userRole: string): any;
105
107
  }
@@ -1 +1 @@
1
- {"version":3,"file":"prisma.service.d.ts","sourceRoot":"","sources":["../../src/prisma/prisma.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAE,YAAY,EAAC,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAC,qBAAqB,EAAC,MAAM,yCAAyC,CAAC;AAE9E,OAAO,KAAK,EAAC,YAAY,IAAI,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAGlE,KAAK,QAAQ,GAAG,MAAM,aAAa,CAAC;AAEpC,oDAAoD;AACpD,MAAM,MAAM,iBAAiB,GAAG;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAGF,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,QAAQ,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,YAAY,GAAG,mBAAmB,GAAG,QAAQ,GAAG,QAAQ,CAAC,CAAC;AAEtI,qBACa,aAAa;IAMY,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IALpE,OAAO,CAAC,YAAY,C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CAUzB;IAED,IAAI,IAAI,IAAI,eAAe,CAAC,MAAM,CAAC,CAElC;IAED,IAAI,OAAO,IAAI,eAAe,CAAC,SAAS,CAAC,CAExC;IAED,IAAI,gBAAgB,QAEnB;IAED;;;OAGG;IACH,WAAW;IAqDX;;;;;;;;OAQG;IACH,OAAO,CAAC,kBAAkB;IAqC1B,IAAI,2BAA2B,QAM9B;IAED;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,oBAAoB;IA6K5B;;;;;;;OAOG;IACH,OAAO,CAAC,eAAe;IAgBvB;;;;;;;OAOG;IACH,OAAO,CAAC,mBAAmB;IA2B3B;;;;;;;OAOG;IACH,OAAO,CAAC,eAAe;IAUvB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,iBAAiB;IA8EzB,WAAW,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,IAAI,GAAG;QAC5E,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;QACjB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,UAAU,EAAE,OAAO,CAAA;KACtB;IAmBD,gBAAgB,CAAC,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAgBzF"}
@@ -27,6 +27,16 @@ let PrismaService = class PrismaService {
27
27
  debugQueries(enable) {
28
28
  nestjs_request_context_1.RequestContext.currentContext.req.corePrismaDebug = enable;
29
29
  }
30
+ /*
31
+ * Exposes the specified models for the duration of the callback execution.
32
+ */
33
+ withExposedModels(models, callback) {
34
+ const previous = nestjs_request_context_1.RequestContext.currentContext?.req.prismaExposedModels || [];
35
+ nestjs_request_context_1.RequestContext.currentContext.req.prismaExposedModels = [...new Set([...previous, ...models.map(m => m.toLowerCase())])];
36
+ return callback().finally(() => {
37
+ nestjs_request_context_1.RequestContext.currentContext.req.prismaExposedModels = previous;
38
+ });
39
+ }
30
40
  debug(msg, type = 'log') {
31
41
  if (nestjs_request_context_1.RequestContext.currentContext?.req.corePrismaDebug) {
32
42
  // Default for log, yellow for warn, red for error, blue for info
@@ -156,6 +166,7 @@ let PrismaService = class PrismaService {
156
166
  relationFields[field_name] = {
157
167
  model: field.type,
158
168
  relation: field.isList ? 'hasMany' : 'belongsTo',
169
+ isRequired: field.isRequired,
159
170
  };
160
171
  if (!field.isList && field.relationToFields && field.relationFromFields) {
161
172
  relationFields[field_name].foreignKey = field.relationToFields[0];
@@ -306,17 +317,42 @@ let PrismaService = class PrismaService {
306
317
  this.debug(`Merging belongsTo relation conditions into main ${modelName}.`, 'warn');
307
318
  }
308
319
  for (const entry of belongsToQueue) {
309
- const { relatedPermissions, field } = entry;
320
+ const { relatedPermissions, field, relation } = entry;
310
321
  this.debug(`Merging conditions for belongsTo relation field '${field}': ${JSON.stringify(relatedPermissions?.conditions)}`, 'info');
311
- args.where = {
312
- ...(args.where || {}),
313
- [field]: {
314
- AND: [
315
- args?.where?.[field] ?? {},
316
- this.buildConditions(relatedPermissions?.conditions, user)
322
+ let belongsToConditions = this.buildConditions(relatedPermissions?.conditions, user);
323
+ if (!args.where)
324
+ args.where = {};
325
+ // If relation is optional allow for non-matching records
326
+ if (relation.isRequired === false) {
327
+ args.where = {
328
+ OR: [
329
+ {
330
+ ...args.where,
331
+ [field]: {
332
+ AND: [
333
+ args?.where?.[field] ?? {},
334
+ belongsToConditions
335
+ ]
336
+ }
337
+ },
338
+ {
339
+ ...args.where,
340
+ [field]: { is: null }
341
+ }
317
342
  ]
318
- }
319
- };
343
+ };
344
+ }
345
+ else {
346
+ args.where = {
347
+ ...(args.where || {}),
348
+ [field]: {
349
+ AND: [
350
+ args?.where?.[field] ?? {},
351
+ belongsToConditions
352
+ ]
353
+ }
354
+ };
355
+ }
320
356
  }
321
357
  // If model is exposed, do not apply conditions
322
358
  if (nestjs_request_context_1.RequestContext.currentContext?.req.prismaExposedModels?.map((m) => m.toLowerCase()).includes(modelName.toLowerCase())) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appxdigital/appx-core",
3
- "version": "0.1.98",
3
+ "version": "0.1.100",
4
4
  "description": "Appx Core is a library that provides a set of tools to help you build your application faster.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",