(FRONT) FRONT (2023)

<< Back <<&nbps;&nbps; Brief of real workable NestJs application.&nbps;&nbps;<< Back <<

This is brief of real workable project.

1. CRON_JOBS service.

For me, this is most interesting service in NestJs, because I many time configured by various way this service:



2. ORM (TypeOrmModule)

NestJs Documentation example use MySQL SQL (TypeORM), but my real project use PostgreSQL. This is screen with Module declaration, where autoLoadEntities: true and synchronize: true mean loading User table from database into Repository(of User).



Ts Omit mean delete one key from result object Omit, and each property decorator has description.



UsersController, UsersService, UserEntity, UsersController combined to UsersModule



This allow us to create API controller



And refer to User entity on any place of project.



exactly with the same fields as User entity defined on database.



You can inject Repository to any controllers with @InjectRepository and than use GraphQL query to this repository or check something like this




Repository can dynamically created and preload



If you want read more about this ORM - please read this book TypeOrm Git book.

3. Controller.

For working with with Controllers we also need to define DTO object and interfaces



And than bind input controller parameters by many ways - Request object, in this case with @Body decorator



Also you can define own decorator



and use own decorator to bind for any request parameters.



Also on screen above you can see how to @Param and @Query parameters. This is exactly what I describe on my post The ways to receive complex parameters in ASP.NET and this in update for NET.Core 8 Parameter Binding in Minimal API apps

Any decorator, of course, pass to JS runtime



You can inject reference to service into controller with Angular forwardRef and than use that reference.



4. Validators

We always use various validators in any API projects, for example:

  • VB.NET ASP.NET Core 3.1 Audit by custom attribute (Project Template for VS2019) - Download from: [MyServer], [Github], [VisualStudio Marketplace]
  • Use jQuery Unobtrusive Validation, custom attributes for validation and validation service/controller.
  • Use CustomAttribute to store metadata about model field.
  • All model has custom validator with attributes and own function.
  • NestJs also has a lot of opportunity to validate DTO objects with various decorators, include own validators and any custom transformation supported by class-validator



    This is exactly the same Data Annotations what we have on .NET on server side Data Annotations or support jQuery Unobtrusive Validation, 3.11.4 jquery.validate.unobtrusive.js

    Some custom validator can use DataSource from TypedOrm, in this case Validator must declare as Provider



    Provider is a factory function that creates a new instance of the service each time it is requested. Other provider in this project was Liquid engine.

    5. Liquid engine - generate Html on server side.

    Liquid JS allow reading HTML template from local folder, insert data with syntax {{ }} like this

    const engine = new Liquid()
    const tpl = engine.parse('Welcome to {{v}}!')
    engine.render(tpl, {v: "Liquid"}).then(console.log)
    


    and write result to browser.



    6. NestJs Auth module

    NestJs authentication module allow to combine to one place all feature related to Authentication include Built-in Passport Guards



    with simple configure local and remote strategy and inject Auth guard without no one string of code



    and than use @UseGuards decorator in controllers



    Personally for me was interesting to see how to create custom Guard protector.



    To be honest I don't understand why some guard used Observable, but another Guard used Promise.



    7. Logging Database changing, GraphQL

    For me most simple and natural way to loggin database changing is DB Triggers - look for example on this page - MySQL Kvm hosting management Db with tracking changing by triggers., however this project used opposite strategy, any logging made on program layer by ORM opportunity.

    This is Poco class (DTO), memory buffer used ORM.



    Controller just call service



    And Service use GraphQL to require data.



    but without Apollo client and without implicit declaration @Query - look to this page GraphQL learning

    New log entity added with @JoinColumn

    This is, of course, more sophisticated solution, that ordinary solution with Database trigger, at least need to right configure ORM.



    8. Winston Logger

    Project used Winston Logger, I like this logger and also used it to my projects. Need to configure it, than inject to any service, factory, provider or controller.



    Result looking fine.



    9. Jest testing

    Project included all test of all services, factories, providers, controllers, decorators and any separate functions.



    This is hard and sophisticated job, for me even more hard than writing this software. I don't know how to program this, therefore I prefer more fast and simple way, like this.



    Double difficulties for me to create Jest test with Observable.



    10. Azure storage

    Project used JS Azure Storage Blob client to store data to Ms Azure Azure Blob Storage.



    For me, this is most idiotic parts of this project, because this is most expensive storage service on the world, if I need similar feature, I use Backblaze - faster than Amazon, cheaper than Amazon

    11. Embed Xml into Pdf

    There is fantastic and very rare future - PDF file usually a kind of container and we can embed any links or XML data to PDF, this is Adobe tutorial how to embed Links to PDF, this is documentation for DevExpress DevExpress PdfDocumentProcessor.AttachFile. The same future support pdfjs-dist npm, this is example and source mozilla/pdf.js

    Project use this future, parse XML included inside PDF (with ast-xml-parser



    12. Transaction with QueryRunner

    All this software has been build with transaction



    Code written asynchronously and used Promise.all to wait finishing all operation on PostgreSQL

    13. Handlebars helpers

    Project use Handlebars-helpers packages, to simplify custom generate Html (Handlebars) from *.hbs files.



    14. Mailing subsystem

    Project used nestJs mailer, this implementation of mailing subsystem working exactly with HandleBars helper - NestJS - Mailer.



    Controllers calls mailService (third parameters is replacement in mail template)



    mailService create Promise from NodeMailer https://www.nodemailer.com/ (SentMessageInfo is simply an alias for any).



    And call Brevo service inside promise. Brevo service is the same as Twilio, but without some future like Elastic SIP Trunking, however with free month plan up to 300 mail. Call with this parameters.



    Brevo service read API key from config, compile Handlebars and create new Promise with call Brevo post server.



    For me comparing this solution with VB.NET is very interesting, in Visual Basic we never speaking about handleBat, because this is just one code line. And VB programmers type this line with disconnected mind, absolutely automatically.



    All mailing process usually also need just one code line.



    If only VB.NET programmers create sophisticated complex mail system with as lot of combination of mailing (for example, Agent receive link only, Customer receive link to Agent, of Customer receive full Special Offer for individually constructed PDF, or Agent receive copy of Customer Email and so no)



    Only in this case VB.NET programmer need to write something more, than just one code line.



    This is even including various marker like "Test mode" and various additional identifiers for test mode only.



    15. Time calculations

    For any time calculation project use Moment.js, also project use nanoid.js to generate random password.



    In Visual Basic this is just one code line, for example Робота з байтами у VB.NET - ChrW, BitConverter.ToInt32, Convert.ToInt32, Byte.Parse, ToString("X2") / AllowHexSpecifier, GetBytes/GetString, New Byte(N) {}, UInt32 = &H33

    16. WebRequest

    Project use nestjs/axios module to create request to 3-rd party resources, moreover it call 3-td party resources with Observable inside Promise.



    This is hard code for me. Because for VB.NET programmer any web Request (with Get/Post parameters and with/without JWT AU) is just one VB code line, this is reason why I use VB for testing this project.



    Look for example to this API library what I have write in 2002 year and want to modify in this year, but have no time. This API also contains GET/POST client with anon or AU WebRequest in WebRequest section Rebuilding my ancient API factory on modern way (Cryptor, Windows services, IIS management, Windows Task Scheduler, TcpListener, WCF, gRPC, Port Sharing service)

    17. Exception filter

    When application started, it created exceptionFactory



    And we can see always exception from TypeOrm for example.



    however application has number of additional exception catcher.



    18. Class transformation

    Project used class-transformer package to as decorator to POCO class and for various separately transformation



    19. Other useful feature

    I'm not an high-end expert on NextJs application (sorry, I can make so many types of applications, look for example for my title page and NextJs is only one from 1000 type application I can doing)and for me this project is cool example of using this packages and example of a lot of usable functions





    Comments ( )
    <00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
    Link to this page: http://www.vb-net.com/NestJs/Devspire.htm
    <SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>