<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by James Harrison on Medium]]></title>
        <description><![CDATA[Stories by James Harrison on Medium]]></description>
        <link>https://medium.com/@jharrisonio?source=rss-e719d063f99e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*1tJNXcliXwJQw5Ix3NMR8Q.jpeg</url>
            <title>Stories by James Harrison on Medium</title>
            <link>https://medium.com/@jharrisonio?source=rss-e719d063f99e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 16:39:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jharrisonio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Programmatically managing Prisma models]]></title>
            <link>https://jharrisonio.medium.com/programmatically-managing-prisma-models-1285cefebff3?source=rss-e719d063f99e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1285cefebff3</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[ast]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[schema]]></category>
            <dc:creator><![CDATA[James Harrison]]></dc:creator>
            <pubDate>Tue, 25 Oct 2022 19:49:14 GMT</pubDate>
            <atom:updated>2022-10-25T19:49:14.893Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Sq8-ba8JvWGmHjyz" /><figcaption>Photo by <a href="https://unsplash.com/@lazycreekimages?utm_source=medium&amp;utm_medium=referral">Michael Dziedzic</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I’ve woken up early this morning because I’m excited to try out something I found yesterday; a library for manipulating the <a href="https://www.prisma.io/">Prisma</a> schema file via code.</p><p>Why is this exciting? I’m working on a side project to automate the entire build of a web app, and that includes creating, modifying and deleting data models using Prisma. But it needs to be done programmatically. Usually I would use the <a href="https://medium.com/solutions/js-ts/ts-compiler">Typescript Compiler</a> to read the AST and manipulate it that way, but the Prisma schema file isn’t Typescript, or Javascript. It’s a proprietary format, which rules out this option.</p><p>Prisma has an <a href="https://github.com/prisma/prisma/tree/main/packages/internals">internals</a> library which has some functionality for this, but it’s undocumented and looks difficult to work with. There’s also a third party lib called <a href="https://github.com/MrLeebo/prisma-ast">prisma-ast</a>. Let’s explore both these options.</p><h4>Prisma Internals</h4><p>Digging through the Prisma NPM package I found the Internals library. It seems it has a whole lot of random utils in there, so it’s a bit hard to find the part that’s used for modifying the Schema file. Searching via the Github website is too hard so I’m going to clone the repo and open it up in VSCode.</p><p>I believe I found something at <a href="https://github.com/prisma/prisma/blob/main/packages/internals/src/engine-commands/getDmmf.ts">getDmmf.ts</a> but it&#39;s a little hard to tell. I remember there&#39;s a Prisma CLI command to pull a models from an existing database and add it your schema file. See <a href="https://www.prisma.io/docs/reference/api-reference/command-reference#db-pull">prisma db pull</a>. The prisma db push command would also need to read the schema file in order to convert it to SQL scripts. I&#39;ll look for the code behind these commands and work my way deeper.</p><p>This leads me to a file called <a href="https://github.com/prisma/prisma/blob/main/packages/migrate/src/commands/DbPull.ts">DbPull.ts</a>. It has a bunch of imports from that internals lib I mentioned before:</p><pre>import {<br>  arg,<br>  checkUnsupportedDataProxy,<br>  Command,<br>  createSpinner,<br>  drawBox,<br>  format,<br>  formatms,<br>  getCommandWithExecutor,<br>  getConfig,<br>  getSchema,<br>  getSchemaPath,<br>  HelpError,<br>  IntrospectionEngine,<br>  IntrospectionSchemaVersion,<br>  IntrospectionWarnings,<br>  link,<br>  loadEnvFile,<br>  protocolToConnectorType,<br>} from &quot;@prisma/internals&quot;;</pre><p>I can’t see anything in that list of imports that looks like it would generate a model though. Time to go deeper down the rabbit hole.</p><p>There’s a function called <a href="https://github.com/prisma/prisma/blob/d45a386158438a2fb8d8af9f8acfb6f35fc1b703/packages/migrate/src/commands/DbPull.ts#L87">parse</a> which seems to do a lot. Including calling one of those imported functions called <a href="https://github.com/prisma/prisma/blob/d45a386158438a2fb8d8af9f8acfb6f35fc1b703/packages/migrate/src/commands/DbPull.ts#L197">getConfig</a>. But on closer inspection that function doesn&#39;t return the right type of data to be useful. Let&#39;s keep going.</p><h4>Prisma Engines</h4><p>Here’s something interesting on <a href="https://github.com/prisma/prisma/blob/d45a386158438a2fb8d8af9f8acfb6f35fc1b703/packages/migrate/src/commands/DbPull.ts#L232">line 232</a>:</p><pre>const introspectionResult = await engine.introspect(<br>  schema,<br>  args[&quot;--force&quot;],<br>  args[&quot;--composite-type-depth&quot;]<br>);<br><br>introspectionSchema = introspectionResult.datamodel;<br>introspectionWarnings = introspectionResult.warnings;<br>introspectionSchemaVersion = introspectionResult.version;</pre><p>It appears the engine.introspect() function reads the datasource config from the schema file and returns a datamodel based on the database. This is what I&#39;m looking for. But the introspect() seems to just be a wrapper for another function:</p><pre>this.getRPCPayload(&quot;introspect&quot;, { schema, force, compositeTypeDepth });</pre><p>I’m not very familiar with RPC so I’l have to do some reading to figure out where this actually goes. I’ll do a whole-project search for “RPC” in all *.ts files to see if I can find the handler. No luck. But I did find some more specific RPC message names, like &quot;getDatabaseDescription&quot;. I&#39;ll try another search for that and see what I find.</p><p>OK I think I understand how this works. Looks like a separate process is spawned and the JSON data is piped into that process using stdin:</p><pre>if (this.child!.stdin!.destroyed) {<br>  throw new Error(<br>    `Can&#39;t execute ${JSON.stringify(<br>      request<br>    )} because introspection engine is destroyed.`<br>  );<br>}<br>debugRpc(&quot;SENDING RPC CALL&quot;, JSON.stringify(request));<br>this.child!.stdin!.write(JSON.stringify(request) + &quot;\n&quot;);</pre><p>Let’s see what this.child is doing. Bad news. It&#39;s running a binary file, like an exe or shell script. I can&#39;t see these binaries in the repo but they probably need to be built. I&#39;m going to try and build the project to see if they get generated. Then maybe I can figure out how they&#39;re generated.</p><p>I didn’t find how the engines are generated, but I did find some code that pointed me to <a href="https://www.prisma.io/docs/concepts/components/prisma-engines">useful documentation</a>. I’m getting a much better idea of how Prisma works now. There are engines (binary executables) for each type of database. They’re implemented in <a href="https://www.rust-lang.org/">Rust</a>. I’m going to give up on this avenue. I don’t need to see inside the binaries, I just need to read what they output. There must be a TS wrapper somewhere. I’ll go back to the engine.introspect() function.</p><h4>A dead end or a new perspective?</h4><p>After going back it looks like the Rust engine is responsible for converting the database structure into Prisma models. All it outputs is a string, which gets appended to schema.prisma.</p><p>Let me just recap my goal for my own sake…my head is still stuck in the deep dive.</p><blockquote>Programmatically read/write/update the schema.prisma file</blockquote><p>I assumed I would do that by directly manipulating the file, and <em>then</em> apply the changes to the DB. This isn’t possible with the path I’ve gone down. But flipping this on its head, I could manipulate the <em>database</em> (using simple SQL scripts) and <em>retrospectively</em> update the schema file.</p><p>To be honest I hadn’t even thought of doing that, and it might make sense. I’m out of time today, but I’m happy that I’ve still got 2 options:</p><ol><li>Modify the DB, then update the Schema</li><li>Use the <a href="https://github.com/MrLeebo/prisma-ast">prisma-ast</a> library</li></ol><p><em>This story was originally published on </em><a href="https://jharrison.io/blog/2022-10-25-programmatically-managing-prisma-models"><em>jharrison.io</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1285cefebff3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to apply React principles to OutSystems apps]]></title>
            <link>https://jharrisonio.medium.com/how-to-apply-react-principles-to-outsystems-apps-4979a1a8e168?source=rss-e719d063f99e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4979a1a8e168</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[outsystems]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[low-code]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[James Harrison]]></dc:creator>
            <pubDate>Wed, 13 May 2020 14:51:24 GMT</pubDate>
            <atom:updated>2020-05-13T14:51:24.665Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re a React developer, you’re working with an amazing framework that challenges the traditional, antiquated principles of web and mobile app development. It allows UI and logic to co-exist within neat, self-contained components. You’ll also be all-too-familiar with the difficulties of managing a project that can quickly exceed 50 dependencies, not including the back-end, testing, performance, security and scalability considerations.</p><p>If you’re an OutSystems developer, you may have the opposite experience. You don’t need to worry so much about front-end, back-end or non-functional requirements. But you may be familiar with projects that have UI, logic and data tied directly to screens, with little in the way of encapsulation or structure. As these screens grow in complexity, you may have asked yourself “how can I better structure my app?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VyvEjKpQPnEox1q1__GSHA.png" /><figcaption>Screenshots of the most common challenges in both React and OutSystems</figcaption></figure><blockquote>Under the hood OutSystems uses React as its front-end technology so the concepts are easily transferable.</blockquote><p>In this article, I’ll show you how we can get the best of both worlds. For those React developers out there, a common misconception of low-code platforms like OutSystems is that you will lose control over your code, or not be able to achieve certain functionality. I’ll show you how we can take the core elements from React (components, state, props, events, etc) and use them in OutSystems to achieve any functionality required. Under the hood OutSystems uses React as its front-end technology so the concepts are easily transferable.</p><p>For the OutSystems followers, we’ll borrow the core principles from React and apply them to our OutSystems apps. I’ll provide you with some rules to follow to help you decide when and where to load data, what should be converted to web blocks, and how to pass information up and down the component hierarchy.</p><p>There is a brilliant document on the React website called <a href="https://reactjs.org/docs/thinking-in-react.html">Thinking in React</a> that talks about the mental shift required to move from traditional HTML + JavaScript apps to a component-based model. Moving to OutSystems also requires a mental shift, so it makes sense to take this React article and dissect it in the context of OutSystems.</p><p><a href="https://reactjs.org/docs/thinking-in-react.html">Thinking in React - React</a></p><p>Before we begin, there are some overlaps between React and OutSystems that we need to address. Below is a list of the core React elements, and their OutSystems equivalents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*LD2qvKsd7Wqmv7Fv_Tf44A.png" /><figcaption>There are similar concepts in both React and OutSystems</figcaption></figure><p><strong>React</strong></p><ol><li>Components</li><li>Props</li><li>State</li><li>Events</li><li>Functions/logic</li></ol><p><strong>OutSystems</strong></p><ol><li>Web Blocks</li><li>Input Parameters</li><li>Local Variables</li><li>Events</li><li>Client Actions</li></ol><h3>Start With a Mock</h3><p>Just like the article, “Thinking in React”, let’s start with a mockup provided by a designer. We’ve been provided with a design for a simple app that allows users to search for products and filter based on stock.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*bG4sgSpw4P4BCkAofoCegA.png" /></figure><p>We’ll also use some mock data rather than connecting to a database or API straight away:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9516087c7dd90c7029147a6400f302c7/href">https://medium.com/media/9516087c7dd90c7029147a6400f302c7/href</a></iframe><h3>Step 1: Break The UI Into A Component Hierarchy</h3><p>This part of the process remains the same whether you’re working in React or OutSystems. A good principle to follow is that each component should have a single responsibility, and if components start to grow, split them into smaller sub-components.</p><p><a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">Single responsibility principle - Wikipedia</a></p><p>It may be useful to talk to your designer about how they logically separated the components in their design tool, as it may apply to your code too. Below is a screenshot of the mock design with coloured borders drawn around each component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*G1tR8_LZuO78PWTlakjKrg.png" /><figcaption>The mock design, split into multiple components</figcaption></figure><ol><li>FilterableProductTable (orange): contains the entirety of the example</li><li>SearchBar (blue): receives all user input</li><li>ProductTable (green): displays and filters the data collection based on user input</li><li>ProductCategoryRow (turquoise): displays a heading for each category</li><li>ProductRow (red): displays a row for each product</li></ol><p>In React, we would build these as Components. In OutSystems, these will become Web Blocks. Each will have different responsibilities; some will be load data and manage state and others will be purely for presentation purposes.</p><h3>Step 2: Build a static version</h3><p>To avoid getting lost in the detail and spiralling into self-destruction, we’re going to create a static version of our app. Why? It forces us to create a good skeleton for our app, and to build the components &amp; web blocks rather than lump everything onto a single screen. The static version will load data from a mock source, for example a JSON file. It also won’t be very interactive. It should render the UI, but not store any state.</p><p>This article isn’t intended to be a step-by-step tutorial, but I’ll try and provide enough information for you to follow along and actually build this app, if you so desire.</p><h4>Create The Mock Data Structure</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*RYlzfauRF9ISsAlN81C-lQ.gif" /></figure><p>Don’t worry about your database structure or your API endpoints just yet; nip over to the Data tab and create a simple Structure from the JSON mock data. It should contain fields for Category, Name, Price and Stocked.</p><p>Structures are more malleable than database entities, as no data is persisted. If we change fields or data types, the platform doesn’t need to handle the underlying SQL migration scripts.</p><p>Structures provide a useful sandbox until you lock down your database structure.</p><h4>Pick Your Starting Point</h4><p>You can either begin from the outside-in and build the higher level outer layers first, or you can start from the inside-out and create the lower level ProductRow and ProductCategoryRow components. This is entirely your choice. I prefer to start from the outside-in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*Iq8IssS8DqhaV4lNDaTjZQ.gif" /></figure><p>Mock data isn’t something that is often talked about in OutSystems because, compared to writing code, it is very easy to skip straight to the Database or APIs. You may be wondering, <em>“how do you even use mock data in OutSystems?”</em>.</p><p>The answer lies in the <em>JSON Deserialize </em>component. Create a web block and select <em>Fetch data from other sources, </em>being sure to set the output parameter to <em>Product List. </em>Inside this action, paste your mock JSON into a <em>JSON Deserialize </em>component<em>.</em></p><p>Now, the data is available in your top-level web block and can be passed into any child components. The finished structure should look like the screenshot below; 5 web blocks with appropriate input parameters, all of which receive data passed top-down from the <em>FilterableProductTable</em>.</p><p>React’s great performance and ease of debugging can be attributed to some secret sauce called <strong>one-way data flow </strong>(also called one-way bindings). This concept can be seen in OutSystems too. We always pass data <strong>down </strong>into child web blocks using <strong>input parameters. </strong>This means that if our data changes in the outer-most wrapper, all child components that use the same data will automatically be updated. Think about it…you can’t send data back up to parent components, can you?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rfcdT7XdBr8uTV5UAExcA.png" /></figure><h3>Step 3: Identify the minimal UI state</h3><p>At this point in time we have a static version of our app. It shows a list of products, but the <em>SearchBar </em>doesn’t work and we can’t filter based on stock. When you start building your next app, take a moment to work out the complete but minimal state. What does that actually mean?</p><p>Look at the <em>SearchBar </em>as an example. It has a text input, and it has a checkbox. We need somewhere to store the value of both these elements; the text the user has searched for and whether or not the checkbox is ticked. In OutSystems we would store these in Local Variables.</p><h4><strong>What is the difference between state, props, input variables and local variables?</strong></h4><p>React calls them props (short for properties) and state. OutSystems calls them Input Parameters and Local Variables. You can read more about the differences, but a good summary is that <strong><em>input parameters get passed to the web block whereas local variables are managed within the web block.</em></strong></p><p><a href="https://github.com/uberVU/react-guide/blob/master/props-vs-state.md">react-guide/props-vs-state.md at master · uberVU/react-guide</a></p><p>Start by building a list of all the data in your app. It might look something like this:</p><ul><li>The original list of products</li><li>The search text the user has entered</li><li>The value of the checkbox</li><li>The filtered list of products</li></ul><p>For each item in the list, ask these questions:</p><ol><li>Is it passed in from a parent via props? If so, it probably isn’t state.</li><li>Does it remain unchanged over time? If so, it probably isn’t state.</li><li>Can you compute it based on any other state or props in your component? If so, it isn’t state.</li></ol><p>This will narrow our list down to just two elements</p><ol><li>The search text the user has entered</li><li>The value of the checkbox</li></ol><h3>Step 4: Identify Where Your State Should Live</h3><p>This section is <strong>absolutely key </strong>to building well-structured applications<strong>.<br>If you only take away one thing from this article, THIS SHOULD BE IT!</strong></p><p>We’ve identified two elements of state that need to exist as Local Variables. Now we need to decide where to put them. Beginners will often make the mistake of putting them in the same web block as the UI element itself.</p><p>Take the screenshot below as an example of two different approaches. On the left, state is stored lower down in the <em>SearchBar </em>web block, alongside the UI itself. On the right, the state has been <strong>lifted up</strong> to the <em>FilterableProductTable </em>block and then <strong>passed down </strong>to the <em>SearchBar </em>through Input Parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*3CpilQeDul9NHkEcwpVEvA.png" /><figcaption>Left: State stored in the SearchBar. Right: State lifted up to the FilterProductTable block</figcaption></figure><p>Which one do you think is better? Consider the example on the left. How would you filter the data based on what the user types in the <em>SearchBar</em>? You would need to pass the search query <strong>back up </strong>the component hierarchy to the <em>FilterableProductTable </em>component. While technically you could achieve this using Events, it would involve duplication of state and unnecessary complexity.</p><h4>Interlude: Presentation vs Container Web Blocks</h4><p>When designing your web blocks and choosing the location of state, also consider which blocks are for presentation purposes and which ones are containers for data. Another way to conceptualise this is to think of them as “smart” vs “dumb” components.</p><p>A presentation web block simply renders whatever it’s told to (dumb). It may have Input Parameters, but it does not house state or load data.</p><p>A container web block usually doesn’t render much UI of its own, but is responsible for housing state and loading data (smart) before passing it onto other presentation web blocks. In the example in this article, we have inadvertently created one container web block, <em>FilterableProductTable. </em>All other web blocks receive data from this parent block, and are therefore presentation blocks.</p><p>You may choose to use a naming convention or grouping mechanism to easily identify different types of web blocks in your application.</p><h3>Step 5: Add Inverse data flow</h3><p>We’ve discussed React’s <strong>one-way data flow</strong>, but now it’s time to challenge that idea, and send information back the other way. In our app, we need to notify the <em>FilterableProductTable </em>in case the search input or stock filter changes. We achieve this using <strong>events.</strong></p><p>Take a look at the screenshot below to see how data flows through the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*-4bvHmbBgVSciYqz-_R-Ew.png" /></figure><ol><li><em>FilterableProductTable </em>stores the <em>Input </em>local variable (state), and sets a default value. It gets passed into the SearchBar as an input parameter.</li><li>When the user changes the search input, SearchBar triggers an event called <em>OnInputChange</em><strong><em>. </em></strong><em>FilterableProductTable </em>listens for this event with a handler called <em>SearchBarOnInputChange.</em></li><li><em>SearchBarOnInputChange </em>reads the new value from the event and updates the local variable to match, which in turn updates the <em>SearchBar </em>web block again.</li></ol><h3>Wrapping Up</h3><p>OutSystems is a low-code platform and contains a lot of drag-and-drop, but that isn’t reason to throw away best practice application architecture. Likewise, being visual in nature doesn’t equate to limited functionality. All the familiar programmatic constructs are there to be used, as we’ve seen in the parallels between React and OutSystems.</p><p>I hope this article helps you view your applications through a new lens, provides a process to follow when starting a project, and saves you from some of those common pitfalls.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4979a1a8e168" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>