Managed Code Pattern
If you want see how MCP
works in @leight
, you can read more in SDK workbench guide.
Prologue
A lot of time, I've worked on projects without proper man power, so creating a lot of boilerplate code was quite a pain, also when there was need for refactoring.
Ever since I've started (professional) programming (~2011), I've done some kind of code generation which simplified a lot of things for me.
And here we are.
Concept
Cool name, I like it same as what it does: you have some existing piece of code and you're writing layer(s) of your own code on top of it. Many times it's repeated. So you wrap it into hooks or functions or whatever. But... what if there is some tool which can do this for you?
That's the reason why I've come with this name: Managed Code Pattern means you have small input definition on top of which is generated some bigger piece of code which is not directly under your control.
For example, Prisma does this. Zod schema generator for Prisma does this. Leight does this for generating Sources and a lot of other stuff.
Reason
Read-only generated code managed by some library. You're isolated of internal changes, internal implementation, but you cas still alter behavior of the stuff. With this it's much simpler to add new features, extends current things or fix bugs. Because you cannot alter the code, generator (thus managed piece of code) can do whatever it wants - it will not break anything of yours.
Because of generator presence, things are made in standard, predicable way, so you know what to expect. All callbacks retrieving data accepts Query;; all Sources provides same methods for counting, paging, sorting and other stuff. So you do not have a way how to do things differently, so it's much simpler to use your app and focus on business logic.
Implementation
Leight currently uses a few generators - Prisma (opens in a new tab), Zod schema generator (opens in a new tab) for Prisma and maybe something else not mentioned yet.
Also, Leight itself implements generators for its own parts based on Prisma, so for example Forms, Tables, tRPC procedures and some other things which you definitively do by hand are generated.
There is SDK package which covers all parts of Leight Viv and it's quite simple to integrate it into your project, see Workbench for some tips.
What's generated
This is dynamic topic, so I'll put only a few of things I know now:
- Sources - this wraps Prisma "repository", typings and makes a "standard" way how to get data
- SourceProvider for types data (isolates from where you get data)
- QueryProvider for managing SourceProviders, so you can simply manage Source state (filtering, ordering, paging, ...)
- Schemas for validating inputs/outputs
- ...and some other things I don't know yet or I'm lazy to write now