|
| 1 | +import { BigQuery } from '@google-cloud/bigquery'; |
| 2 | +import { EnrichmentAdapter } from '../EnrichmentAdapter'; |
| 3 | +import { Logger } from '@nestjs/common'; |
| 4 | + |
| 5 | +function chunk<T>(arr: T[], size: number): T[][] { |
| 6 | + const chunks: T[][] = []; |
| 7 | + for (let i = 0; i < arr.length; i += size) { |
| 8 | + chunks.push(arr.slice(i, i + size)); |
| 9 | + } |
| 10 | + return chunks; |
| 11 | +} |
| 12 | + |
| 13 | +export class BqEnrichmentAdapter implements EnrichmentAdapter { |
| 14 | + private client: BigQuery; |
| 15 | + private project: string; |
| 16 | + private dataset: string; |
| 17 | + private table: string; |
| 18 | + private allowlist: string[]; |
| 19 | + private cachedFields?: string[]; |
| 20 | + |
| 21 | + private logger: Logger; |
| 22 | + constructor() { |
| 23 | + |
| 24 | + this.logger = new Logger('BqEnrichmentAdapter'); |
| 25 | + |
| 26 | + this.project = process.env.ENRICHMENT_BQ_PROJECT || ''; |
| 27 | + this.dataset = process.env.ENRICHMENT_BQ_DATASET || ''; |
| 28 | + this.table = process.env.ENRICHMENT_BQ_TABLE || ''; |
| 29 | + this.allowlist = (process.env.ENRICHMENT_FIELDS_ALLOWLIST || '') |
| 30 | + .split(',') |
| 31 | + .map((s) => s.trim()) |
| 32 | + .filter((s) => s.length > 0); |
| 33 | + this.logger.log( |
| 34 | + `Enrichment fields allowlist: ${this.allowlist.join(', ')}` |
| 35 | + ); |
| 36 | + const config: any = { |
| 37 | + projectId: this.project, |
| 38 | + }; |
| 39 | + |
| 40 | + // Only add keyFilename if GOOGLE_APPLICATION_CREDENTIALS is set |
| 41 | + if (process.env.GOOGLE_APPLICATION_CREDENTIALS) { |
| 42 | + config.keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; |
| 43 | + } |
| 44 | + |
| 45 | + this.client = new BigQuery(config); |
| 46 | + } |
| 47 | + |
| 48 | + async supportedFields(): Promise<string[]> { |
| 49 | + if (this.cachedFields) return this.cachedFields; |
| 50 | + const [metadata] = await this.client |
| 51 | + .dataset(this.dataset) |
| 52 | + .table(this.table) |
| 53 | + .getMetadata(); |
| 54 | + let fields = (metadata.schema?.fields || []) |
| 55 | + .map((f: any) => f.name) |
| 56 | + .filter((name: string) => name !== 'id'); |
| 57 | + if (this.allowlist.length) { |
| 58 | + fields = fields.filter((f) => this.allowlist.includes(f)); |
| 59 | + } |
| 60 | + this.cachedFields = fields; |
| 61 | + return fields; |
| 62 | + } |
| 63 | + |
| 64 | + async fetchEnrichmentByIds( |
| 65 | + ids: string[], |
| 66 | + options?: { fields?: string[] }, |
| 67 | + ): Promise<Record<string, Record<string, unknown>>> { |
| 68 | + if (!ids.length) return {}; |
| 69 | + const allowed = await this.supportedFields(); |
| 70 | + if (!allowed.length) { |
| 71 | + return {}; |
| 72 | + } |
| 73 | + const requested = options?.fields?.length |
| 74 | + ? options.fields.filter((f) => allowed.includes(f)) |
| 75 | + : allowed; |
| 76 | + |
| 77 | + const projection = requested.length ? requested.join(', ') : ''; |
| 78 | + const results: Record<string, Record<string, unknown>> = {}; |
| 79 | + for (const batch of chunk(ids, 5000)) { |
| 80 | + const query = `SELECT id${projection ? ', ' + projection : ''} FROM \`${this.project}.${this.dataset}.${this.table}\` WHERE id IN UNNEST(@ids)`; |
| 81 | + const options = { query, params: { ids: batch } } as any; |
| 82 | + const [rows] = await this.client.query(options); |
| 83 | + for (const row of rows as any[]) { |
| 84 | + const { id, ...rest } = row; |
| 85 | + results[id] = rest; |
| 86 | + } |
| 87 | + } |
| 88 | + return results; |
| 89 | + } |
| 90 | +} |
0 commit comments