-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from join-com/alpha
fix: JOIN-36367 // add SchemaCache as separate class to pubsub lib to use in CFs
- Loading branch information
Showing
3 changed files
with
54 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { SchemaServiceClient } from '@google-cloud/pubsub/build/src/v1' | ||
import { Schema, Type } from 'avsc' | ||
import { DateType } from './logical-types/DateType' | ||
|
||
export class SchemaCache { | ||
|
||
private readonly topicTypeRevisionsCache: Record<string, Type> = {} | ||
|
||
constructor( | ||
private readonly schemaServiceClient: SchemaServiceClient, | ||
private readonly topicSchemaName: string | ||
) { } | ||
|
||
public async getTypeFromCacheOrRemote(schemaRevisionId: string): Promise<Type> { | ||
const typeFromCache = this.topicTypeRevisionsCache[schemaRevisionId] | ||
if (typeFromCache) { | ||
return typeFromCache | ||
} | ||
const projectName = process.env['GCLOUD_PROJECT'] | ||
if (!projectName) { | ||
throw new Error('Can\'t find GCLOUD_PROJECT env variable, please define it') | ||
} | ||
const revisionPath = `projects/${projectName}/schemas/${this.topicSchemaName}@${schemaRevisionId}` | ||
const [remoteSchema] = await this.schemaServiceClient.getSchema({ name: revisionPath }) | ||
|
||
if (!remoteSchema.definition) { | ||
throw new Error(`Can't process schema ${schemaRevisionId} without definition`) | ||
} | ||
const schema = JSON.parse(remoteSchema.definition) as Schema | ||
const type = Type.forSchema(schema, { logicalTypes: { 'timestamp-micros': DateType } }) | ||
this.topicTypeRevisionsCache[schemaRevisionId] = type | ||
|
||
return type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SchemaServiceClient } from '@google-cloud/pubsub/build/src/v1' | ||
import { SchemaCache } from './SchemaCache' | ||
|
||
export class SchemaCacheFactory { | ||
private readonly schemaServiceClient: SchemaServiceClient | ||
|
||
constructor() { | ||
this.schemaServiceClient = new SchemaServiceClient() | ||
} | ||
|
||
public getSchemaCache(topicSchemaName: string): SchemaCache { | ||
return new SchemaCache(this.schemaServiceClient, topicSchemaName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters