Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/schema references #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mattwahner
Copy link

Hello!

I am looking to add in functionality for schema references in this pull request. Specifically, I noticed a PR has already been created for this, PR#92, but seems to have lost traction for getting merged in. Although there are still a few changes I plan to make to this pull request before considering it ready to be pulled in, I wanted to get a pulse check into the likelihood of this PR getting merged, especially as compared to PR#92. Things I still am looking to change include:

  • Caching some of the API calls I have added into the register function.
  • Write integration tests.
  • Overall code cleanup.

This PR is intended to only add schema references to AVRO. Protobuf namely seems to be giving me issues while encoding nested structures, and seems to not be in a state of feature parity with AVRO. Likely, more fixes will be need to be addressed before including schema reference functionality to other schema types.

Here is an example of how client code would look like while interfacing with these changes:

const { SchemaRegistry, SchemaType } = require('@kafkajs/confluent-schema-registry');
const dotenv = require('dotenv');
dotenv.config();

const schemaRegistry = new SchemaRegistry({ host: process.env.REGISTRY_HOST });

const cookieSchema = JSON.stringify({
    type: "record",
    name: "Cookie",
    namespace: "com.food",
    fields: [
        { name: "name", type: "string" },
        { name: "calories", type: "int" }
    ]
});

const cakeSchema = JSON.stringify({
    type: "record",
    name: "Cake",
    namespace: "com.food",
    fields: [
        { name: "name", type: "string" },
        { name: "size", type: "int" }
    ]
});

const genericFoodSchema = JSON.stringify({
    type: 'record',
    name: 'GenericFood',
    namespace: 'com.food',
    fields: [
        { name: 'food', type: ['Cookie', 'Cake'] }
    ]
});

const main = async () => {
    const { id: cookieId } = await schemaRegistry.register({
        type: SchemaType.AVRO,
        schema: cookieSchema
    });

    const { id: cakeId } = await schemaRegistry.register({
        type: SchemaType.AVRO,
        schema: cakeSchema
    });

    const { id: genericFoodId } = await schemaRegistry.register({
        type: SchemaType.AVRO,
        schema: genericFoodSchema
    }, {
        referenceSchemaIds: [
            cookieId,
            cakeId
        ]
    });

    const encodedPayload = await schemaRegistry.encode(genericFoodId, {
        food: {
            'com.food.Cookie': {
                name :'BigOlCookie',
                calories: 1000
            }
        }
    });

    const decodedPayload = await schemaRegistry.decode(encodedPayload);
    console.log('calories', decodedPayload.food['com.food.Cookie'].calories);
};

main();

Matthew Wahner and others added 2 commits May 27, 2021 11:24
@brinchj
Copy link

brinchj commented Jul 30, 2021

I've started a discussion around references in the related issue here: #82

I think we are a bunch who are interested in this and I think we could team up and produce the changes needed together in uniform 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants