@nestledjs/api 1.0.1 → 1.0.3
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,17 +1,21 @@
|
|
|
1
1
|
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
|
|
2
2
|
import { Prisma, PrismaClient } from '@<%= npmScope %>/api/prisma'
|
|
3
3
|
import { CorePagingInput } from './dto/core-paging.input'
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
|
|
6
6
|
@Injectable()
|
|
7
|
-
export class ApiCoreDataAccessService
|
|
7
|
+
export class ApiCoreDataAccessService
|
|
8
|
+
extends PrismaClient
|
|
9
|
+
implements OnModuleInit, OnModuleDestroy
|
|
10
|
+
{
|
|
8
11
|
constructor() {
|
|
9
12
|
const config: Prisma.PrismaClientOptions = {
|
|
10
13
|
datasources: {
|
|
11
14
|
db: { url: `${process.env['DATABASE_URL']}?connection_limit=30` },
|
|
12
15
|
},
|
|
13
16
|
log:
|
|
14
|
-
process.env['LOG_PRISMA_QUERIES'] === 'true' ||
|
|
17
|
+
process.env['LOG_PRISMA_QUERIES'] === 'true' ||
|
|
18
|
+
process.env['COUNT_PRISMA_QUERIES'] === 'true'
|
|
15
19
|
? [{ emit: 'event', level: 'query' }]
|
|
16
20
|
: [{ emit: 'event', level: 'warn' }],
|
|
17
21
|
}
|
|
@@ -25,9 +29,16 @@ export class ApiCoreDataAccessService extends PrismaClient implements OnModuleIn
|
|
|
25
29
|
console.warn('Not Running Prisma Optimize - No API Key Set')
|
|
26
30
|
}
|
|
27
31
|
|
|
32
|
+
const { withOptimize } = require('@prisma/extension-optimize')
|
|
28
33
|
const extendedClient = new PrismaClient(config).$extends(withOptimize({ apiKey }))
|
|
29
34
|
Object.assign(this, extendedClient)
|
|
30
|
-
|
|
35
|
+
try {
|
|
36
|
+
const { withOptimize } = require('@prisma/extension-optimize')
|
|
37
|
+
const extendedClient = new PrismaClient(config).$extends(withOptimize({ apiKey }))
|
|
38
|
+
Object.assign(this, extendedClient)
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.warn('Not Running Prisma Optimize - @prisma/extension-optimize not installed or failed to load:', err)
|
|
41
|
+
}
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
public queryCount: number
|
|
@@ -40,19 +51,21 @@ export class ApiCoreDataAccessService extends PrismaClient implements OnModuleIn
|
|
|
40
51
|
await this.$connect()
|
|
41
52
|
|
|
42
53
|
if (process.env['LOG_PRISMA_QUERIES'] == 'true') {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
this.$on('query' as never, async (e: Prisma.QueryEvent) => {
|
|
55
|
+
console.log(`QUERY: ${e.query} \n\nPARAMS: ${e.params}\n\n\n`)
|
|
56
|
+
})
|
|
46
57
|
}
|
|
47
58
|
|
|
48
59
|
if (process.env['COUNT_PRISMA_QUERIES'] == 'true') {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
this.$on('query' as never, async () => {
|
|
61
|
+
this.queryCount++
|
|
62
|
+
})
|
|
52
63
|
}
|
|
53
64
|
}
|
|
54
65
|
|
|
55
|
-
filter<T extends Record<string, unknown>>(
|
|
66
|
+
filter<T extends Record<string, unknown>>(
|
|
67
|
+
input: CorePagingInput = {},
|
|
68
|
+
): {
|
|
56
69
|
skip: number
|
|
57
70
|
take: number
|
|
58
71
|
where?: T
|
|
@@ -76,9 +89,11 @@ export class ApiCoreDataAccessService extends PrismaClient implements OnModuleIn
|
|
|
76
89
|
}
|
|
77
90
|
|
|
78
91
|
if (trimmedSearch && searchFields.length > 0) {
|
|
79
|
-
const terms = trimmedSearch.includes(' ')
|
|
80
|
-
|
|
81
|
-
|
|
92
|
+
const terms = trimmedSearch.includes(' ')
|
|
93
|
+
? trimmedSearch.split(' ')
|
|
94
|
+
: [trimmedSearch].filter(Boolean)
|
|
95
|
+
const searchFilters = terms.map(term => ({
|
|
96
|
+
OR: searchFields.map(field => ({
|
|
82
97
|
[field]: { contains: term, mode: Prisma.QueryMode.insensitive },
|
|
83
98
|
})),
|
|
84
99
|
}))
|
|
@@ -95,3 +110,4 @@ export class ApiCoreDataAccessService extends PrismaClient implements OnModuleIn
|
|
|
95
110
|
}
|
|
96
111
|
}
|
|
97
112
|
}
|
|
113
|
+
|