Skip to content

Commit

Permalink
Support ForSchemaOption to call avro.Type.forSchema() (#47)
Browse files Browse the repository at this point in the history
So that we can use @ovotech/avro-logical-types with @kafkajs/confluent-schema-registry.
  • Loading branch information
Dieken authored Mar 7, 2020
1 parent c0b0b66 commit bb1286d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
Empty file modified Dockerfile
100755 → 100644
Empty file.
6 changes: 3 additions & 3 deletions src/SchemaRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encode, MAGIC_BYTE } from './encoder'
import decode from './decoder'
import { COMPATIBILITY, DEFAULT_SEPERATOR } from './constants'
import API, { SchemaRegistryAPIClientArgs, SchemaRegistryAPIClient } from './api'
import API, { SchemaRegistryAPIClientArgs, SchemaRegistryAPIClientOptions, SchemaRegistryAPIClient } from './api'
import Cache from './cache'
import {
ConfluentSchemaRegistryArgumentError,
Expand All @@ -28,9 +28,9 @@ export default class SchemaRegistry {
private api: SchemaRegistryAPIClient
public cache: Cache

constructor({ auth, clientId, host, retry }: SchemaRegistryAPIClientArgs) {
constructor({ auth, clientId, host, retry }: SchemaRegistryAPIClientArgs, options?: SchemaRegistryAPIClientOptions) {
this.api = API({ auth, clientId, host, retry })
this.cache = new Cache()
this.cache = new Cache(options?.forSchemaOptions)
}

public async register(schema: Schema, userOpts?: Opts): Promise<RegisteredSchema> {
Expand Down
5 changes: 5 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import forge, { Client, Authorization } from 'mappersmith'
import RetryMiddleware, { RetryMiddlewareOptions } from 'mappersmith/middleware/retry/v2'
import BasicAuthMiddleware from 'mappersmith/middleware/basic-auth'
import { ForSchemaOptions } from 'avsc'

import { DEFAULT_API_CLIENT_ID } from '../constants'
import errorMiddleware from './middleware/errorMiddleware'
Expand All @@ -21,6 +22,10 @@ export interface SchemaRegistryAPIClientArgs {
retry?: Partial<RetryMiddlewareOptions>
}

export interface SchemaRegistryAPIClientOptions {
forSchemaOptions?: Partial<ForSchemaOptions>
}

// TODO: Improve typings
export type SchemaRegistryAPIClient = Client<{
Schema: {
Expand Down
8 changes: 5 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import avro from 'avsc'
import avro, { ForSchemaOptions } from 'avsc'

import { Schema } from './@types'

export default class Cache {
registryIdBySubject: { [key: string]: number }
schemasByRegistryId: { [key: string]: Schema }
forSchemaOptions?: Partial<ForSchemaOptions>

constructor() {
constructor(forSchemaOptions?: Partial<ForSchemaOptions>) {
this.registryIdBySubject = {}
this.schemasByRegistryId = {}
this.forSchemaOptions = forSchemaOptions
}

getLatestRegistryId = (subject: string): number | undefined => this.registryIdBySubject[subject]
Expand All @@ -23,7 +25,7 @@ export default class Cache {

setSchema = (registryId: number, schema: Schema) => {
// @ts-ignore TODO: Fix typings for Schema...
this.schemasByRegistryId[registryId] = avro.Type.forSchema(schema)
this.schemasByRegistryId[registryId] = avro.Type.forSchema(schema, this.forSchemaOptions)

return this.schemasByRegistryId[registryId]
}
Expand Down

0 comments on commit bb1286d

Please sign in to comment.