protobuf-ts-generatorVscode extension in marketplace
Generate Typescript code from protobuf message
Table of ContentsGetting startedYou can install this awesome theme through the Visual Studio Code Marketplace. IntroductionAfter multiple implementation of services we kind of always need to translater our ideas in class to be used on code. When tou work on oriented object tou have to define each entity of your domain in class and do a kind of monkey job at the begining of your project. This monkey job can be reduced by generating theses class from a low level language input. the aim here is to generate the basic code that you need to start written your business code from a simple input. Having already your class with some getters / setters and more can be cool when you need to go fast. the service is here only to help you, its dump so sometimes you will need to review it for example for importing the nested classes. Code generated is Typescripted so we can have object that we can rely on. Wen can have than more that just classes, tests, mappers, schéma and more. But first lets define some roules on the input that we need and also define what we want as result. InputTo define simple message there is the proto definitions, wich is pretty low level language and super simple to understand and use. Here is more informations about protobuf definitions. So lets define for example a message that we are going tontranslate into Typescript code. ServicesSome of these services generates also tests so that you have coverages on generated code. All services has a command that you can run separetly but also a general command to have them all. Notice that you have always a second command to it for ES6 supports. PropsFrom properties it can generate props. The props are the entry definitions used when you want to create your object , update and more. Basically they are not the domain object, it’s more a plenty object that you map after to your object private properties. FixtureFor tests we always want to create a fake object with fake data. This function returns you fake data as props so that you can fill your object from it. GettersFrom properties it can generate getter for them. The getters are there to acces to your data when it’s private. SettersFrom properties it can generate setter for them. The setters are there to update your data when it’s private. EqualsFunction that check every property to define if the object received as input is the same as the actual object. ConstructorEach class has a constructor function to initialize your object so here we use props input to initialize the properties defined on the class. UpdateThis function is used to update properties of the object. Takes properties for the input and update the property if it’s defined. InterfaceFrom properties it can generate the interface that you want to export to your domain. Helps you in your typescript application. So that you don’t use your object as a type. Mongo schemaFrom properties it can generate the interface that you want to export to your repository to express object coming from mongodb. Helps you in your typescript application. Because they have a mongoose id but the rest of properies are the same as your interface. ClassCreate a class from your protobuf message after processing the properties listed in it. It contains everything listed above and export it. Generate also a test file link to it. MappersFor objects sometimes you need to change the format of your object to have a plenty object. You can have multiple format for the return type following the purpose that you need. We have different type of return such as props or protobuf type.
Nestjs module ( only crud )It generates you a simple module for your nestjs application to be directly used. A module contains multiple type of files. For simple usage it creates you only CRUD service for your entity.
Release
|