@infrab4a/connect-nestjs 2.0.0-alpha.9 → 3.0.0-alpha.0

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.
Files changed (98) hide show
  1. package/eslint.config.js +17 -0
  2. package/package.json +4 -15
  3. package/project.json +49 -0
  4. package/rollup.config.js +17 -0
  5. package/src/consts/es-config.const.ts +1 -0
  6. package/src/consts/hasura-options.const.ts +1 -0
  7. package/src/consts/index.ts +4 -0
  8. package/src/consts/storage.const.ts +1 -0
  9. package/src/consts/vertex-config.const.ts +1 -0
  10. package/src/index.ts +7 -0
  11. package/src/infra/elasticsearch/adapters/index.ts +1 -0
  12. package/src/infra/elasticsearch/adapters/native-elasticsearch-adapter.ts +137 -0
  13. package/src/infra/elasticsearch/index.ts +1 -0
  14. package/src/infra/firebase/firestore/index.ts +2 -0
  15. package/src/infra/firebase/firestore/services/connect-collection-reference.ts +27 -0
  16. package/src/infra/firebase/firestore/services/connect-collection.service.ts +137 -0
  17. package/src/infra/firebase/firestore/services/connect-document-reference.ts +12 -0
  18. package/src/infra/firebase/firestore/services/connect-document.service.ts +51 -0
  19. package/src/infra/firebase/firestore/services/connect-firestore.service.ts +24 -0
  20. package/src/infra/firebase/firestore/services/connect-firestore.ts +9 -0
  21. package/src/infra/firebase/firestore/services/index.ts +6 -0
  22. package/src/infra/firebase/firestore/types/connect-query-snapshot.type.ts +6 -0
  23. package/src/infra/firebase/firestore/types/index.ts +1 -0
  24. package/src/infra/firebase/firestore/vo/connect-base-document-snapshot.vo.ts +27 -0
  25. package/src/infra/firebase/firestore/vo/index.ts +1 -0
  26. package/src/infra/firebase/index.ts +1 -0
  27. package/src/infra/index.ts +4 -0
  28. package/src/infra/pagarme/adapters/index.ts +3 -0
  29. package/src/infra/pagarme/adapters/pagarme-bank-slip-api.adapter.ts +96 -0
  30. package/src/infra/pagarme/adapters/pagarme-card-api.adapter.ts +226 -0
  31. package/src/infra/pagarme/adapters/pagarme-pix-api.adapter.ts +69 -0
  32. package/src/infra/pagarme/index.ts +1 -0
  33. package/src/infra/vertex-ai/adapters/discovery-engine-adapter.ts +148 -0
  34. package/src/infra/vertex-ai/adapters/index.ts +1 -0
  35. package/src/infra/vertex-ai/helpers/index.ts +1 -0
  36. package/src/infra/vertex-ai/helpers/product-vertex-ai.helper.ts +52 -0
  37. package/src/infra/vertex-ai/index.ts +3 -0
  38. package/src/infra/vertex-ai/types/index.ts +2 -0
  39. package/src/infra/vertex-ai/types/vertex-config.ts +23 -0
  40. package/src/infra/vertex-ai/types/vertex-search-result.ts +17 -0
  41. package/src/nest-connect.module.ts +36 -0
  42. package/src/nest-elastic-search.module.ts +26 -0
  43. package/src/nest-firestore.module.ts +363 -0
  44. package/src/nest-hasura-graphql.module.ts +227 -0
  45. package/src/nest-storage.module.ts +16 -0
  46. package/src/nest-vertex-ai-search.module.ts +26 -0
  47. package/src/types/pagarme.d.ts +25 -0
  48. package/tsconfig.json +15 -0
  49. package/tsconfig.lib.json +7 -0
  50. package/tsconfig.lib.prod.json +10 -0
  51. package/tsconfig.spec.json +17 -0
  52. package/index.cjs.d.ts +0 -1
  53. package/index.cjs.default.js +0 -1
  54. package/index.cjs.js +0 -1334
  55. package/index.cjs.mjs +0 -2
  56. package/index.esm.js +0 -1317
  57. package/src/consts/es-config.const.d.ts +0 -1
  58. package/src/consts/hasura-options.const.d.ts +0 -1
  59. package/src/consts/index.d.ts +0 -4
  60. package/src/consts/storage.const.d.ts +0 -1
  61. package/src/consts/vertex-config.const.d.ts +0 -1
  62. package/src/index.d.ts +0 -7
  63. package/src/infra/elasticsearch/adapters/index.d.ts +0 -1
  64. package/src/infra/elasticsearch/adapters/native-elasticsearch-adapter.d.ts +0 -19
  65. package/src/infra/elasticsearch/index.d.ts +0 -1
  66. package/src/infra/firebase/firestore/index.d.ts +0 -2
  67. package/src/infra/firebase/firestore/services/connect-collection-reference.d.ts +0 -19
  68. package/src/infra/firebase/firestore/services/connect-collection.service.d.ts +0 -32
  69. package/src/infra/firebase/firestore/services/connect-document-reference.d.ts +0 -11
  70. package/src/infra/firebase/firestore/services/connect-document.service.d.ts +0 -17
  71. package/src/infra/firebase/firestore/services/connect-firestore.d.ts +0 -7
  72. package/src/infra/firebase/firestore/services/connect-firestore.service.d.ts +0 -8
  73. package/src/infra/firebase/firestore/services/index.d.ts +0 -6
  74. package/src/infra/firebase/firestore/types/connect-query-snapshot.type.d.ts +0 -4
  75. package/src/infra/firebase/firestore/types/index.d.ts +0 -1
  76. package/src/infra/firebase/firestore/vo/connect-base-document-snapshot.vo.d.ts +0 -12
  77. package/src/infra/firebase/firestore/vo/index.d.ts +0 -1
  78. package/src/infra/firebase/index.d.ts +0 -1
  79. package/src/infra/index.d.ts +0 -4
  80. package/src/infra/pagarme/adapters/index.d.ts +0 -3
  81. package/src/infra/pagarme/adapters/pagarme-bank-slip-api.adapter.d.ts +0 -9
  82. package/src/infra/pagarme/adapters/pagarme-card-api.adapter.d.ts +0 -21
  83. package/src/infra/pagarme/adapters/pagarme-pix-api.adapter.d.ts +0 -8
  84. package/src/infra/pagarme/index.d.ts +0 -1
  85. package/src/infra/vertex-ai/adapters/discovery-engine-adapter.d.ts +0 -17
  86. package/src/infra/vertex-ai/adapters/index.d.ts +0 -1
  87. package/src/infra/vertex-ai/helpers/index.d.ts +0 -1
  88. package/src/infra/vertex-ai/helpers/product-vertex-ai.helper.d.ts +0 -7
  89. package/src/infra/vertex-ai/index.d.ts +0 -3
  90. package/src/infra/vertex-ai/types/index.d.ts +0 -2
  91. package/src/infra/vertex-ai/types/vertex-config.d.ts +0 -23
  92. package/src/infra/vertex-ai/types/vertex-search-result.d.ts +0 -18
  93. package/src/nest-connect.module.d.ts +0 -13
  94. package/src/nest-elastic-search.module.d.ts +0 -5
  95. package/src/nest-firestore.module.d.ts +0 -10
  96. package/src/nest-hasura-graphql.module.d.ts +0 -13
  97. package/src/nest-storage.module.d.ts +0 -2
  98. package/src/nest-vertex-ai-search.module.d.ts +0 -5
@@ -0,0 +1,17 @@
1
+ const baseConfig = require('../../eslint.config.js')
2
+
3
+ module.exports = [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.ts'],
7
+ languageOptions: {
8
+ parserOptions: {
9
+ project: './tsconfig.json',
10
+ },
11
+ },
12
+ rules: {
13
+ // Regras específicas para o projeto connect-nestjs
14
+ // Regras adaptadas para NestJS
15
+ },
16
+ },
17
+ ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infrab4a/connect-nestjs",
3
- "version": "2.0.0-alpha.9",
3
+ "version": "3.0.0-alpha.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org"
6
6
  },
@@ -9,26 +9,15 @@
9
9
  "url": "https://github.com/B4AGroup/b4a-firebase-libs"
10
10
  },
11
11
  "peerDependencies": {
12
- "@infrab4a/connect": "5.0.0-alpha.8",
12
+ "@infrab4a/connect": "6.0.0-alpha.0",
13
13
  "@nestjs/common": "^10.3.3",
14
14
  "@nestjs/core": "^10.3.3",
15
- "firebase-admin": "^12.0.0",
16
- "reflect-metadata": "^0.1.13"
15
+ "firebase-admin": "^12.0.0"
17
16
  },
18
17
  "dependencies": {
19
18
  "@elastic/elasticsearch": "^8.13.1",
20
19
  "@google-cloud/discoveryengine": "^1.14.0",
21
20
  "nestjs-firebase": "^10.4.0",
22
21
  "pagarme": "4.35.2"
23
- },
24
- "exports": {
25
- "./package.json": "./package.json",
26
- ".": {
27
- "module": "./index.esm.js",
28
- "import": "./index.cjs.mjs",
29
- "default": "./index.cjs.js"
30
- }
31
- },
32
- "module": "./index.esm.js",
33
- "main": "./index.cjs.js"
22
+ }
34
23
  }
package/project.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
3
+ "name": "connect-nestjs",
4
+ "projectType": "library",
5
+ "sourceRoot": "projects/connect-nestjs/src",
6
+ "prefix": "infrab4a",
7
+ "tags": ["type:lib", "scope:publishable", "framework:nestjs"],
8
+ "targets": {
9
+ "build": {
10
+ "executor": "@nx/rollup:rollup",
11
+ "dependsOn": ["connect:build"],
12
+ "options": {
13
+ "main": "projects/connect-nestjs/src/index.ts",
14
+ "tsConfig": "projects/connect-nestjs/tsconfig.lib.json",
15
+ "project": "projects/connect-nestjs/package.json",
16
+ "outputPath": "dist/connect-nestjs",
17
+ "publishable": true,
18
+ "format": ["cjs", "esm"],
19
+ "compiler": "tsc",
20
+ "generateExportsField": true,
21
+ "clean": true,
22
+ "external": ["@infrab4a/connect"],
23
+ "rollupConfig": "projects/connect-nestjs/rollup.config.js"
24
+ },
25
+ "configurations": {
26
+ "production": {
27
+ "tsConfig": "projects/connect-nestjs/tsconfig.lib.prod.json",
28
+ "optimization": true,
29
+ "external": ["@infrab4a/connect"]
30
+ }
31
+ },
32
+ "outputs": ["{workspaceRoot}/dist/connect-nestjs"]
33
+ },
34
+ "test": {
35
+ "executor": "@angular-devkit/build-angular:karma",
36
+ "options": {
37
+ "main": "projects/connect-nestjs/src/test.ts",
38
+ "tsConfig": "projects/connect-nestjs/tsconfig.spec.json",
39
+ "karmaConfig": "projects/connect-nestjs/karma.conf.js"
40
+ }
41
+ },
42
+ "lint": {
43
+ "executor": "@nx/eslint:lint",
44
+ "options": {
45
+ "lintFilePatterns": ["projects/connect-nestjs/**/*.ts"]
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,17 @@
1
+ const { join } = require('path')
2
+
3
+ module.exports = (config) => {
4
+ return {
5
+ ...config,
6
+ external: [
7
+ '@infrab4a/connect',
8
+ '@nestjs/common',
9
+ '@nestjs/core',
10
+ 'firebase-admin',
11
+ '@elastic/elasticsearch',
12
+ '@google-cloud/discoveryengine',
13
+ 'nestjs-firebase',
14
+ 'pagarme',
15
+ ],
16
+ }
17
+ }
@@ -0,0 +1 @@
1
+ export const ES_CONFIG = 'ES_CONFIG'
@@ -0,0 +1 @@
1
+ export const HASURA_OPTIONS = 'HASURA_OPTIONS'
@@ -0,0 +1,4 @@
1
+ export * from './es-config.const'
2
+ export * from './hasura-options.const'
3
+ export * from './storage.const'
4
+ export * from './vertex-config.const'
@@ -0,0 +1 @@
1
+ export const FIREBASE_STORAGE = 'FIREBASE_STORAGE'
@@ -0,0 +1 @@
1
+ export const VERTEX_CONFIG = 'VERTEX_CONFIG'
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './consts'
2
+ export * from './infra'
3
+ export * from './nest-connect.module'
4
+ export * from './nest-elastic-search.module'
5
+ export * from './nest-firestore.module'
6
+ export * from './nest-hasura-graphql.module'
7
+ export * from './nest-storage.module'
@@ -0,0 +1 @@
1
+ export * from './native-elasticsearch-adapter'
@@ -0,0 +1,137 @@
1
+ import { Client } from '@elastic/elasticsearch'
2
+ import {
3
+ BaseModel,
4
+ DebugHelper,
5
+ ElasticSearchAdapter,
6
+ ElasticSearchResult,
7
+ NotFoundError,
8
+ isEmpty,
9
+ } from '@infrab4a/connect'
10
+ import { Inject, Injectable } from '@nestjs/common'
11
+
12
+ import { ES_CONFIG } from '../../../consts'
13
+
14
+ export interface ESClientOptions {
15
+ cloud: { id: string }
16
+ auth: { apiKey: string }
17
+ }
18
+
19
+ @Injectable()
20
+ export class NativeElasticSearchAdapter<T extends BaseModel<T> = any> implements ElasticSearchAdapter<T> {
21
+ private client: Client
22
+ private logger: DebugHelper
23
+
24
+ constructor(@Inject(ES_CONFIG) config: ESClientOptions) {
25
+ this.logger = DebugHelper.from(this)
26
+
27
+ try {
28
+ this.client = !isEmpty(config.cloud.id) && !isEmpty(config.auth.apiKey) && new Client(config)
29
+ } catch (error) {
30
+ this.logger.error({ req: config, res: error })
31
+
32
+ throw error
33
+ }
34
+ }
35
+
36
+ public async get(index: string, id: string): Promise<T> {
37
+ const logger = this.logger.with('get')
38
+ const req = { index, id }
39
+
40
+ try {
41
+ const data = await this.client.get({ index, id })
42
+
43
+ logger.log({ req, res: data })
44
+
45
+ return data._source as T
46
+ } catch (error) {
47
+ const message = error instanceof Error ? error.message : `${index}/${id} not found`
48
+
49
+ logger.log({ req, res: error })
50
+
51
+ throw new NotFoundError(message)
52
+ }
53
+ }
54
+
55
+ public async query(index: string, query: any): Promise<ElasticSearchResult<T>> {
56
+ const logger = this.logger.with('query')
57
+ const params = {
58
+ index,
59
+ query,
60
+ }
61
+
62
+ try {
63
+ const result = await this.client.search(params)
64
+ const res = {
65
+ total: result.hits.total as number,
66
+ hits: result.hits.hits as ElasticSearchResult<T>['hits'],
67
+ }
68
+
69
+ logger.log({ req: params, res })
70
+
71
+ return res
72
+ } catch (error) {
73
+ logger.error({ req: params, res: error })
74
+
75
+ throw error
76
+ }
77
+ }
78
+
79
+ public async save(index: string, data: T): Promise<void> {
80
+ const logger = this.logger.with('save')
81
+ const req = {
82
+ index,
83
+ id:
84
+ data.identifiersFields.length > 1
85
+ ? JSON.stringify(data.identifier)
86
+ : Object.values(data.identifier).shift().toString(),
87
+ document: data.toPlain(),
88
+ }
89
+
90
+ try {
91
+ const res = await this.client.index(req)
92
+
93
+ logger.log({ req, res })
94
+ } catch (error) {
95
+ logger.error({ req, res: error })
96
+
97
+ throw error
98
+ }
99
+ }
100
+
101
+ public async update(index: string, id: string, data: Partial<T>): Promise<void> {
102
+ const logger = this.logger.with('update')
103
+ const req = {
104
+ index,
105
+ id,
106
+ doc: data,
107
+ }
108
+
109
+ try {
110
+ await this.client.update(req)
111
+
112
+ logger.log({ req, res: undefined })
113
+ } catch (error) {
114
+ logger.error({ req, res: error })
115
+
116
+ throw error
117
+ }
118
+ }
119
+
120
+ public async delete(index: string, id: string): Promise<void> {
121
+ const logger = this.logger.with('delete')
122
+ const req = {
123
+ index,
124
+ id,
125
+ }
126
+
127
+ try {
128
+ await this.client.delete(req)
129
+
130
+ logger.log({ req, res: undefined })
131
+ } catch (error) {
132
+ logger.error({ req, res: error })
133
+
134
+ throw error
135
+ }
136
+ }
137
+ }
@@ -0,0 +1 @@
1
+ export * from './adapters'
@@ -0,0 +1,2 @@
1
+ export * from './services'
2
+ export * from './types'
@@ -0,0 +1,27 @@
1
+ import {
2
+ ConnectDocumentData,
3
+ ConnectDocumentSnapshot,
4
+ ConnectQuerySnapshot,
5
+ ConnectSnapshotOptions,
6
+ ConnectWhereOption,
7
+ } from '@infrab4a/connect'
8
+
9
+ import { ConnectDocumentReference } from './connect-document-reference'
10
+
11
+ export interface ConnectCollectionReference<T extends ConnectDocumentData> {
12
+ add(data: T, id?: string): Promise<ConnectDocumentReference<T>>
13
+ where(attribute: string, operator: ConnectWhereOption, value: any): ConnectCollectionReference<T>
14
+ order(attribute: string, order: 'asc' | 'desc'): ConnectCollectionReference<T>
15
+ limit(quantity: number): ConnectCollectionReference<T>
16
+ offset(offset: number): ConnectCollectionReference<T>
17
+ fromStartAt(startAt: number): ConnectCollectionReference<T>
18
+ fromStartAt(startAt: ConnectDocumentReference<T>): ConnectCollectionReference<T>
19
+ fromStartAfter(startAt: number): ConnectCollectionReference<T>
20
+ fromStartAfter(startAt: ConnectDocumentReference<T>): ConnectCollectionReference<T>
21
+ withConverter(converter: {
22
+ toFirestore: (data: T) => ConnectDocumentData<T>
23
+ fromFirestore: (snapshot: ConnectDocumentSnapshot<T>, options?: ConnectSnapshotOptions) => T
24
+ }): ConnectCollectionReference<T>
25
+ getDoc(id?: string): ConnectDocumentReference<T>
26
+ getDocs(): Promise<ConnectQuerySnapshot<T>>
27
+ }
@@ -0,0 +1,137 @@
1
+ import {
2
+ ConnectDocumentData,
3
+ ConnectDocumentSnapshot,
4
+ ConnectQuerySnapshot,
5
+ ConnectSnapshotOptions,
6
+ ConnectWhereOption,
7
+ isEmpty,
8
+ } from '@infrab4a/connect'
9
+ import {
10
+ CollectionReference,
11
+ DocumentReference,
12
+ Firestore,
13
+ Query,
14
+ QueryDocumentSnapshot,
15
+ } from 'firebase-admin/firestore'
16
+
17
+ import { ConnectBaseDocumentSnapshot } from '../vo'
18
+
19
+ import { ConnectCollectionReference } from './connect-collection-reference'
20
+ import { ConnectDocumentReference } from './connect-document-reference'
21
+ import { ConnectDocumentService } from './connect-document.service'
22
+
23
+ export class ConnectCollectionService<T extends ConnectDocumentData> implements ConnectCollectionReference<T> {
24
+ private reference: CollectionReference<T>
25
+ private query: Query
26
+ private orderBy: { fieldPath: string; directionStr: 'asc' | 'desc' }
27
+ private limitBy: number
28
+ private offsetBy: number
29
+ private startingAt: number | unknown
30
+ private startingAfter: number | unknown
31
+ private converter: {
32
+ toFirestore: (data: T) => ConnectDocumentData<T>
33
+ fromFirestore: (snapshot: ConnectDocumentSnapshot<T>, options?: ConnectSnapshotOptions) => T
34
+ }
35
+
36
+ constructor(path: string, private readonly firestore: Firestore) {
37
+ this.reference = firestore.collection(path).withConverter({
38
+ toFirestore: (data) => data,
39
+ fromFirestore: (snapshot) => {
40
+ return { id: snapshot.id, ...snapshot.data() } as T
41
+ },
42
+ })
43
+ }
44
+
45
+ async add(data: T, id?: string): Promise<ConnectDocumentReference<T>> {
46
+ const newDoc = await this.save(data, id)
47
+
48
+ return new ConnectDocumentService<T>(newDoc.path, this.firestore).withConverter(this.converter)
49
+ }
50
+
51
+ async getDocs(): Promise<ConnectQuerySnapshot<T>> {
52
+ if (this.startingAt) this.limitBy += this.startingAt as number
53
+
54
+ let query = this.query ? this.query : this.reference
55
+
56
+ query = this.orderBy ? query.orderBy(this.orderBy.fieldPath, this.orderBy.directionStr) : query
57
+ query = this.limitBy ? query.limit(this.limitBy) : query
58
+
59
+ return query.get().then((docs) => {
60
+ const docsPaginated = this.startingAt ? docs.docs.slice(this.startingAt as number, this.limitBy) : docs.docs
61
+
62
+ return {
63
+ empty: Boolean(docsPaginated.length),
64
+ size: docsPaginated.length,
65
+ docs: docsPaginated.map((doc) => new ConnectBaseDocumentSnapshot(doc)),
66
+ } as any
67
+ })
68
+ }
69
+
70
+ getDoc(id?: string): ConnectDocumentReference<T> {
71
+ return new ConnectDocumentService<T>(`${this.reference.path}/${id}`, this.firestore).withConverter(this.converter)
72
+ }
73
+
74
+ where(fieldPath: string, opStr: ConnectWhereOption, value: any): ConnectCollectionReference<T> {
75
+ this.query = this.query ? this.query.where(fieldPath, opStr, value) : this.reference.where(fieldPath, opStr, value)
76
+
77
+ return this
78
+ }
79
+
80
+ order(attribute: string, order: 'asc' | 'desc'): ConnectCollectionReference<T> {
81
+ this.orderBy = { fieldPath: attribute, directionStr: order }
82
+
83
+ return this
84
+ }
85
+
86
+ limit(quantity: number): ConnectCollectionReference<T> {
87
+ this.limitBy = quantity
88
+
89
+ return this
90
+ }
91
+
92
+ offset(offsetBy: number): ConnectCollectionReference<T> {
93
+ this.offsetBy = offsetBy
94
+
95
+ return this
96
+ }
97
+
98
+ fromStartAt(startingAt: number): ConnectCollectionReference<T>
99
+ fromStartAt(startingAt: ConnectDocumentReference<T>): ConnectCollectionReference<T>
100
+ fromStartAt(startingAt: unknown): ConnectCollectionReference<T> {
101
+ this.startingAt = startingAt
102
+
103
+ return this
104
+ }
105
+
106
+ fromStartAfter(startingAt: number): ConnectCollectionReference<T>
107
+ fromStartAfter(startingAt: ConnectDocumentReference<T>): ConnectCollectionReference<T>
108
+ fromStartAfter(startingAt: unknown): ConnectCollectionReference<T> {
109
+ this.startingAfter = startingAt
110
+
111
+ return this
112
+ }
113
+
114
+ withConverter(params: {
115
+ toFirestore: (data: T) => ConnectDocumentData<T>
116
+ fromFirestore: (snapshot: ConnectDocumentSnapshot<T>, options?: ConnectSnapshotOptions) => T
117
+ }): ConnectCollectionReference<T> {
118
+ this.converter = params
119
+ this.reference = this.reference.withConverter({
120
+ toFirestore: (data: T) => params.toFirestore(data),
121
+ fromFirestore: (snapshot: QueryDocumentSnapshot<T>, options?: ConnectSnapshotOptions) =>
122
+ params.fromFirestore(new ConnectBaseDocumentSnapshot(snapshot), options),
123
+ })
124
+
125
+ return this
126
+ }
127
+
128
+ private async save(data: T, id?: string): Promise<DocumentReference<T>> {
129
+ if (isEmpty(id)) return this.reference.add(data)
130
+
131
+ const docRef = this.reference.doc(id)
132
+
133
+ await docRef.set(data)
134
+
135
+ return docRef
136
+ }
137
+ }
@@ -0,0 +1,12 @@
1
+ import { ConnectDocumentData, ConnectDocumentSnapshot, ConnectSnapshotOptions } from '@infrab4a/connect'
2
+
3
+ export interface ConnectDocumentReference<T extends ConnectDocumentData> {
4
+ get(): Promise<ConnectDocumentSnapshot<T>>
5
+ getId(): string
6
+ save(data: T): Promise<ConnectDocumentReference<T>>
7
+ delete(): Promise<void>
8
+ withConverter(converter: {
9
+ toFirestore: (data: T) => ConnectDocumentData<T>
10
+ fromFirestore: (snapshot: ConnectDocumentSnapshot<T>, options?: ConnectSnapshotOptions) => T
11
+ }): ConnectDocumentReference<T>
12
+ }
@@ -0,0 +1,51 @@
1
+ import { ConnectDocumentData, ConnectDocumentSnapshot, ConnectSnapshotOptions } from '@infrab4a/connect'
2
+ import { DocumentReference, Firestore, QueryDocumentSnapshot } from 'firebase-admin/firestore'
3
+
4
+ import { ConnectBaseDocumentSnapshot } from '../vo'
5
+
6
+ import { ConnectDocumentReference } from './connect-document-reference'
7
+
8
+ export class ConnectDocumentService<T extends ConnectDocumentData> implements ConnectDocumentReference<T> {
9
+ private reference: DocumentReference<T>
10
+
11
+ constructor(private path: string, private firestore: Firestore) {
12
+ this.reference = this.firestore.doc(this.path).withConverter({
13
+ toFirestore: (data) => data as any,
14
+ fromFirestore: (snapshot) => {
15
+ return { id: snapshot.id, ...snapshot.data() } as T
16
+ },
17
+ })
18
+ }
19
+
20
+ async get(): Promise<ConnectDocumentSnapshot<T>> {
21
+ return new ConnectBaseDocumentSnapshot(await this.reference.get())
22
+ }
23
+
24
+ getId(): string {
25
+ return this.reference.id
26
+ }
27
+
28
+ async save(data: T): Promise<ConnectDocumentReference<T>> {
29
+ if (this.reference) this.reference.update(data)
30
+ else await this.reference.set(data)
31
+
32
+ return this
33
+ }
34
+
35
+ async delete(): Promise<void> {
36
+ await this.reference.delete()
37
+ }
38
+
39
+ withConverter(params: {
40
+ toFirestore: (data: T) => ConnectDocumentData<T>
41
+ fromFirestore: (snapshot: ConnectDocumentSnapshot<T>, options?: ConnectSnapshotOptions) => T
42
+ }): ConnectDocumentReference<T> {
43
+ this.reference = this.reference.withConverter({
44
+ toFirestore: (data: T) => params.toFirestore(data),
45
+ fromFirestore: (snapshot: QueryDocumentSnapshot<T>, options?: ConnectSnapshotOptions) =>
46
+ params.fromFirestore(new ConnectBaseDocumentSnapshot(snapshot), options),
47
+ })
48
+
49
+ return this
50
+ }
51
+ }
@@ -0,0 +1,24 @@
1
+ import {
2
+ ConnectCollectionReference,
3
+ ConnectDocumentData,
4
+ ConnectDocumentReference,
5
+ ConnectFirestore,
6
+ } from '@infrab4a/connect'
7
+ import { Injectable } from '@nestjs/common'
8
+ import { Firestore } from 'firebase-admin/firestore'
9
+
10
+ import { ConnectCollectionService } from './connect-collection.service'
11
+ import { ConnectDocumentService } from './connect-document.service'
12
+
13
+ @Injectable()
14
+ export class ConnectFirestoreService implements ConnectFirestore {
15
+ constructor(private readonly firestore: Firestore) {}
16
+
17
+ public getCollection<T extends ConnectDocumentData>(path: string): ConnectCollectionReference<T> {
18
+ return new ConnectCollectionService<T>(path, this.firestore)
19
+ }
20
+
21
+ public getDocument<T extends ConnectDocumentData>(path: string): ConnectDocumentReference<T> {
22
+ return new ConnectDocumentService<T>(path, this.firestore)
23
+ }
24
+ }
@@ -0,0 +1,9 @@
1
+ import { ConnectDocumentData } from '@infrab4a/connect'
2
+
3
+ import { ConnectCollectionReference } from './connect-collection-reference'
4
+ import { ConnectDocumentReference } from './connect-document-reference'
5
+
6
+ export interface ConnectFirestore {
7
+ getCollection<T extends ConnectDocumentData>(path: string): ConnectCollectionReference<T>
8
+ getDocument<T extends ConnectDocumentData>(path: string): ConnectDocumentReference<T>
9
+ }
@@ -0,0 +1,6 @@
1
+ export * from './connect-collection-reference'
2
+ export * from './connect-collection.service'
3
+ export * from './connect-document-reference'
4
+ export * from './connect-document.service'
5
+ export * from './connect-firestore'
6
+ export * from './connect-firestore.service'
@@ -0,0 +1,6 @@
1
+ import { ConnectDocumentData, ConnectDocumentSnapshot as ConnectDocumentSnapshotBase } from '@infrab4a/connect'
2
+
3
+ export type ConnectDocumentSnapshot<T extends ConnectDocumentData = ConnectDocumentData> =
4
+ ConnectDocumentSnapshotBase<T> & {
5
+ exists: boolean
6
+ }
@@ -0,0 +1 @@
1
+ export * from './connect-query-snapshot.type'
@@ -0,0 +1,27 @@
1
+ import { ConnectDocumentData, ConnectSnapshotOptions } from '@infrab4a/connect'
2
+ import { DocumentSnapshot } from 'firebase-admin/firestore'
3
+
4
+ import { ConnectDocumentSnapshot } from '../types'
5
+
6
+ export class ConnectBaseDocumentSnapshot<T extends ConnectDocumentData = ConnectDocumentData>
7
+ implements ConnectDocumentSnapshot<T>
8
+ {
9
+ public id: string
10
+ public exists: boolean
11
+
12
+ constructor(private readonly connectDocumentSnapshot: DocumentSnapshot<T>) {
13
+ this.id = connectDocumentSnapshot.id
14
+ }
15
+
16
+ public isNotEmpty(): boolean {
17
+ return this.connectDocumentSnapshot.exists
18
+ }
19
+
20
+ public data(_options?: ConnectSnapshotOptions): T {
21
+ return this.connectDocumentSnapshot.data()
22
+ }
23
+
24
+ public get(fieldPath: string) {
25
+ return this.connectDocumentSnapshot.get(fieldPath)
26
+ }
27
+ }
@@ -0,0 +1 @@
1
+ export * from './connect-base-document-snapshot.vo'
@@ -0,0 +1 @@
1
+ export * from './firestore'
@@ -0,0 +1,4 @@
1
+ export * from './elasticsearch'
2
+ export * from './firebase'
3
+ export * from './pagarme'
4
+ export * from './vertex-ai'
@@ -0,0 +1,3 @@
1
+ export * from './pagarme-bank-slip-api.adapter'
2
+ export * from './pagarme-card-api.adapter'
3
+ export * from './pagarme-pix-api.adapter'