How to dynamically create gRPC proto schema in nodejs?

2.4k Views Asked by At

Loading .proto files can be done by providing file path (PROTO_PATH)

var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });

How to do this dynamically in node.js ? I want to construct proto schema (datatypes and functions) at run time.

1

There are 1 best solutions below

3
On BEST ANSWER

The @grpc/proto-loader library is specifically made to load .proto files, and does not support constructing protobuf message or service types dynamically at runtime.

However, Protobuf.js does support constructing protobuf reflection types at runtime (see its README for details), and it is possible to use that to construct a PackageDefinition object explicitly using that, and then load that into the grpc library. The type definitions in this document might be clearer.