5htp-core 0.6.0-9 → 0.6.0-92

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.6.0-9",
4
+ "version": "0.6.0-92",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
File without changes
@@ -66,7 +66,7 @@ export default class AuthenticationRouterService<
66
66
  if (route.options.auth !== undefined) {
67
67
 
68
68
  // Basic auth check
69
- this.users.check(request, 'User', route.options.auth);
69
+ this.users.check(request, route.options.auth);
70
70
 
71
71
  // Redirect to logged page
72
72
  if (route.options.auth === false && request.user && route.options.redirectLogged)
@@ -1,21 +1,34 @@
1
-
2
- import type { Prisma } from '@models/types';
1
+ import type { Prisma, PrismaClient } from '@models/types';
3
2
  import * as runtime from '@/var/prisma/runtime/library.js';
4
3
 
4
+ export type TWithStats = {
5
+ $table: string,
6
+ $key: string
7
+ } & {
8
+ [key: string]: string // key => SQL
9
+ }
10
+
11
+ export type TSubset = (...a: any[]) => Prisma.ProspectContactLeadFindFirstArgs & {
12
+ withStats?: TWithStats
13
+ }
14
+
5
15
  export default class Facet<
6
16
  D extends {
7
17
  findMany(args?: any): Promise<any>
8
18
  findFirst(args?: any): Promise<any>
9
19
  },
10
- S extends (...a: any[]) => Prisma.ProspectContactLeadFindFirstArgs,
20
+ S extends TSubset,
11
21
  R
12
22
  > {
13
23
  constructor(
24
+
25
+ private readonly prisma: PrismaClient,
26
+
14
27
  private readonly delegate: D,
15
28
  private readonly subset: S,
16
29
 
17
30
  /* the **ONLY** line that changed ↓↓↓ */
18
- private readonly transform: (
31
+ private readonly transform?: (
19
32
  row: runtime.Types.Result.GetResult<
20
33
  Prisma.$ProspectContactLeadPayload,
21
34
  ReturnType<S>,
@@ -24,19 +37,82 @@ export default class Facet<
24
37
  ) => R,
25
38
  ) { }
26
39
 
27
- public findMany(
40
+ public async findMany(
28
41
  ...args: Parameters<S>
29
42
  ): Promise<R[]> {
30
- return this.delegate
31
- .findMany(this.subset(...args))
32
- .then(rows => rows.map(this.transform))
43
+
44
+ const { withStats, ...subset } = this.subset(...args);
45
+
46
+ const results = await this.delegate.findMany(subset);
47
+
48
+ // Load stats
49
+ const stats = withStats
50
+ ? await this.fetchStats( withStats, results )
51
+ : [];
52
+
53
+ return results.map(row => this.transformResult(row, stats, withStats));
33
54
  }
34
55
 
35
- public findFirst(
56
+ public async findFirst(
36
57
  ...args: Parameters<S>
37
58
  ): Promise<R | null> {
38
- return this.delegate
39
- .findFirst(this.subset(...args))
40
- .then(result => result ? this.transform(result) : null)
59
+
60
+ const { withStats, ...subset } = this.subset(...args);
61
+
62
+ const result = await this.delegate.findFirst(subset);
63
+
64
+ const stats = withStats
65
+ ? await this.fetchStats( withStats, [result] )
66
+ : [];
67
+
68
+ return result ? this.transformResult(result, stats, withStats) : null;
69
+ }
70
+
71
+ private async fetchStats(
72
+ { $table, $key, ...withStats }: TWithStats,
73
+ results: any[]
74
+ ): Promise<any[]> {
75
+
76
+ const select = Object.entries(withStats).map(([key, sql]) =>
77
+ `(COALESCE((
78
+ ${sql}
79
+ ), 0)) as ${key}`
80
+ );
81
+
82
+ const stats = await this.prisma.$queryRawUnsafe(`
83
+ SELECT ${$key}, ${select.join(', ')}
84
+ FROM ${$table}
85
+ WHERE ${$key} IN (
86
+ ${results.map(r => "'" + r[ $key ] + "'").join(',')}
87
+ )
88
+ `);
89
+
90
+ for (const stat of stats) {
91
+ for (const key in stat) {
92
+
93
+ if (key === $key)
94
+ continue;
95
+
96
+ stat[key] = stat[key] ? parseInt(stat[key]) : 0;
97
+ }
98
+ }
99
+
100
+ return stats;
101
+ }
102
+
103
+ private transformResult( result: any, stats: any[], withStats?: TWithStats ) {
104
+
105
+ // Transform stats
106
+ const resultStats = withStats
107
+ ? stats.find(stat => stat[withStats.$key] === result[withStats.$key]) || {}
108
+ : {};
109
+
110
+ if (this.transform)
111
+ result = this.transform(result);
112
+
113
+ return {
114
+ ...result,
115
+ ...resultStats
116
+ }
41
117
  }
42
118
  }
@@ -9,6 +9,9 @@ import { PrismaClient } from '@/var/prisma';
9
9
  import type { Application } from '@server/app';
10
10
  import Service from '@server/app/service';
11
11
 
12
+ // Specific
13
+ import Facet, { TSubset } from './Facet';
14
+
12
15
  /*----------------------------------
13
16
  - TYPES
14
17
  ----------------------------------*/
@@ -48,5 +51,20 @@ export default class ModelsManager extends Service<Config, Hooks, Application> {
48
51
  await this.client.$disconnect()
49
52
  }
50
53
 
54
+ public Facet<
55
+ D extends {
56
+ findMany(args?: any): Promise<any>
57
+ findFirst(args?: any): Promise<any>
58
+ },
59
+ S extends TSubset,
60
+ R
61
+ >(...args: [D, S, R]) {
62
+
63
+ return new Facet(
64
+ this.client,
65
+ ...args
66
+ );
67
+ }
68
+
51
69
 
52
70
  }
package/types/icons.d.ts CHANGED
@@ -1 +1 @@
1
- export type TIcones = "times"|"solid/spinner-third"|"long-arrow-right"|"check-circle"|"coins"|"building"|"at"|"phone"|"brands/linkedin"|"rocket"|"chart-bar"|"user-circle"|"arrow-right"|"crosshairs"|"user-shield"|"shield-alt"|"chart-line"|"money-bill-wave"|"star"|"link"|"file-alt"|"long-arrow-left"|"user-plus"|"mouse-pointer"|"thumbs-up"|"dollar-sign"|"plane-departure"|"plus-circle"|"comments-alt"|"calendar-alt"|"paper-plane"|"user"|"magnet"|"sun"|"brands/whatsapp"|"clock"|"cog"|"trash"|"ellipsis-h"|"plus"|"check"|"regular/shield-check"|"binoculars"|"angle-up"|"angle-down"|"search"|"lightbulb"|"envelope"|"solid/crown"|"eye"|"pen"|"file"|"download"|"info-circle"|"exclamation-circle"|"times-circle"|"arrow-left"|"meh-rolling-eyes"|"solid/star"|"solid/star-half-alt"|"regular/star"|"chevron-left"|"power-off"|"users"|"bug"|"bars"|"broom"|"brands/google"|"minus"|"exclamation-triangle"|"play"|"solid/check-circle"|"solid/exclamation-triangle"|"solid/times-circle"|"hourglass"|"sack-dollar"|"question-circle"|"wind"|"external-link"|"plug"|"minus-circle"|"coin"|"comment-alt"|"arrow-to-bottom"|"briefcase"|"map-marker"|"fire"|"map-marker-alt"|"solid/magic"|"industry"|"calendar"|"globe"|"magic"|"graduation-cap"|"unlink"|"bold"|"italic"|"underline"|"strikethrough"|"subscript"|"superscript"|"code"|"font"|"empty-set"|"horizontal-rule"|"page-break"|"image"|"table"|"poll"|"columns"|"sticky-note"|"caret-right"|"align-left"|"align-center"|"align-right"|"align-justify"|"indent"|"outdent"|"list-ul"|"check-square"|"h1"|"h2"|"h3"|"h4"|"list-ol"|"paragraph"|"quote-left"|"bolt"
1
+ export type TIcones = "times"|"solid/spinner-third"|"long-arrow-right"|"check-circle"|"coins"|"building"|"at"|"phone"|"bolt"|"brands/linkedin"|"rocket"|"chart-bar"|"user-circle"|"crosshairs"|"plane-departure"|"plus-circle"|"comments-alt"|"arrow-right"|"user-shield"|"shield-alt"|"chart-line"|"money-bill-wave"|"star"|"link"|"file-alt"|"long-arrow-left"|"user-plus"|"mouse-pointer"|"thumbs-up"|"dollar-sign"|"user"|"sun"|"brands/whatsapp"|"magnet"|"paper-plane"|"plus"|"angle-up"|"angle-down"|"clock"|"cog"|"trash"|"ellipsis-h"|"check"|"regular/shield-check"|"binoculars"|"calendar-alt"|"search"|"lightbulb"|"solid/crown"|"eye"|"pen"|"file"|"envelope"|"download"|"info-circle"|"exclamation-circle"|"times-circle"|"meh-rolling-eyes"|"arrow-left"|"bars"|"solid/star"|"solid/star-half-alt"|"regular/star"|"chevron-left"|"power-off"|"sack-dollar"|"question-circle"|"play"|"minus-circle"|"external-link"|"wind"|"broom"|"exclamation-triangle"|"brands/google"|"users"|"bug"|"solid/check-circle"|"solid/exclamation-triangle"|"solid/times-circle"|"hourglass"|"minus"|"comment-alt"|"plug"|"coin"|"map-marker-alt"|"arrow-to-bottom"|"solid/magic"|"briefcase"|"map-marker"|"fire"|"industry"|"calendar"|"globe"|"magic"|"graduation-cap"|"unlink"|"bold"|"italic"|"underline"|"strikethrough"|"subscript"|"superscript"|"code"|"font"|"empty-set"|"horizontal-rule"|"page-break"|"image"|"table"|"poll"|"columns"|"sticky-note"|"caret-right"|"align-left"|"align-center"|"align-right"|"align-justify"|"indent"|"outdent"|"list-ul"|"check-square"|"h1"|"h2"|"h3"|"h4"|"list-ol"|"paragraph"|"quote-left"