<?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 Liam Andrew Maddison on Medium]]></title>
        <description><![CDATA[Stories by Liam Andrew Maddison on Medium]]></description>
        <link>https://medium.com/@liamandrewmaddison?source=rss-401162d8a9c7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-wH0SVRq5tDHY119.jpg</url>
            <title>Stories by Liam Andrew Maddison on Medium</title>
            <link>https://medium.com/@liamandrewmaddison?source=rss-401162d8a9c7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:23:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@liamandrewmaddison/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[Unleashing the Power of Machine Learning within PostgreSQL: Introducing PostgresML]]></title>
            <link>https://medium.com/@liamandrewmaddison/unleashing-the-power-of-machine-learning-within-postgresql-introducing-postgresml-b9ddd6f6b0e9?source=rss-401162d8a9c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9ddd6f6b0e9</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[postgresml]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Liam Andrew Maddison]]></dc:creator>
            <pubDate>Mon, 29 Apr 2024 07:01:39 GMT</pubDate>
            <atom:updated>2024-04-29T09:02:13.608Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KmjBTIy-K2N6DYk_omzChg.jpeg" /></figure><p>All forms of data management and analysis are constantly shifting, and integrating machine learning (ML) capabilities with traditional database systems is almost table stakes. Many users want to be able to ‘drop’ ML algorithms into database workflows, and that’s becoming a more frequent desire. PostgreSQL, one of my preferred open-source relational database management systems, has a new and interesting extension that enables exactly that, called PostgresML. The ability to do this is really cool.</p><h3>Introduction to PostgresML</h3><p>PostgresML represents a paradigm shift in how we approach data analysis within PostgreSQL. Traditionally, performing machine learning tasks required exporting data from the database, processing it with external ML libraries or frameworks, and then importing the results back into the database. This process often involved significant overhead and complexity, leading to inefficiencies and potential data integrity issues.</p><p>With PostgresML, these barriers are dismantled, as it enables users to execute machine learning algorithms natively within the PostgreSQL environment. By seamlessly integrating ML functionalities into the database engine, PostgresML streamlines the entire ML pipeline, from data preparation to model training and inference, all within a familiar SQL interface.</p><h4>Features and Capabilities</h4><p>PostgresML offers a rich array of features and capabilities tailored to the needs of data scientists, analysts, and developers. Some key highlights include:</p><p><strong>1. Extensive Library of ML Algorithms:</strong> PostgresML comes equipped with a diverse selection of ML algorithms, ranging from classic techniques like linear regression and k-means clustering to cutting-edge models such as random forests and neural networks. This comprehensive library caters to a wide range of use cases, ensuring flexibility and versatility in model selection.</p><p><strong>2. SQL Integration:</strong> Leveraging the power of SQL, users can seamlessly incorporate ML operations into their database queries. This SQL-centric approach simplifies the development process and allows for seamless integration with existing database workflows.</p><p><strong>3. Scalability and Performance: </strong>Built upon the robust foundation of PostgreSQL, PostgresML inherits its scalability and performance capabilities. Whether handling small-scale analyses or large-scale predictive modelling tasks, PostgresML delivers optimal performance and efficiency, ensuring timely insights and actionable results.</p><p><strong>4. Ease of Deployment and Maintenance:</strong> Deploying PostgresML is a breeze, requiring minimal setup and configuration. Moreover, since it operates within the PostgreSQL environment, PostgresML seamlessly integrates with existing databases, eliminating the need for additional infrastructure or resource overhead.</p><h3>Case Study: Building a Recommendation System with PostgresML</h3><p>To illustrate the capabilities of PostgresML in action, let’s consider a practical scenario: building a recommendation system for an e-commerce platform. In this case study, we’ll demonstrate how PostgresML can be utilised to analyse user behaviour and generate personalised product recommendations.</p><h4>Step 1: Data Preparation</h4><p>In this step, we’ll prepare the data within the PostgreSQL database for building our recommendation system. The data typically consists of information about users, products, and their interactions, such as purchases, views, and ratings. We’ll aggregate and preprocess this data to create a structured dataset suitable for training our recommendation model.</p><p><strong>Example Scenario:</strong></p><p>Let’s assume we have three tables in our database:</p><p><strong>1. users:</strong> Contains information about users, including user IDs and demographic details.<br><strong>2. products:</strong> Stores information about products, including product IDs, names, and categories.<br><strong>3. interactions:</strong> Logs user interactions with products, such as views, purchases, and ratings.</p><p><strong>Data Preprocessing:</strong></p><p>We need to aggregate and preprocess the data from these tables to create a dataset suitable for training our recommendation model. This preprocessing step involves tasks such as:</p><ul><li>Joining relevant tables to get a comprehensive view of user-product interactions.</li><li>Cleaning and filtering out irrelevant data.</li><li>Transforming categorical variables into numerical representations if necessary.</li></ul><p><strong>SQL Code Example:</strong></p><pre>CREATE VIEW user_product_interactions AS<br>SELECT<br> u.user_id,<br> p.product_id,<br> i.interaction_type,<br> i.interaction_value<br>FROM<br> users u<br>JOIN<br> interactions i ON u.user_id = i.user_id<br>JOIN<br> products p ON i.product_id = p.product_id;<br>CREATE TABLE user_item_matrix AS<br>SELECT<br> user_id,<br> product_id,<br> SUM(CASE WHEN interaction_type = &#39;purchase&#39; THEN 1 ELSE 0 END) AS purchases,<br> SUM(CASE WHEN interaction_type = &#39;view&#39; THEN 1 ELSE 0 END) AS views,<br> AVG(CASE WHEN interaction_type = &#39;rating&#39; THEN interaction_value ELSE NULL END) AS average_rating<br>FROM<br> user_product_interactions<br>GROUP BY<br> user_id,<br> product_id;</pre><ul><li>We create a view `user_product_interactions` by joining the `users`, `interactions`, and `products` tables to get a comprehensive view of user-product interactions.</li><li>Then, we aggregate the interaction data in the view to create a user-item matrix stored in the `user_item_matrix` table. This matrix contains aggregated information such as the number of purchases and views for each user-item pair, as well as the average rating given by users (if applicable).</li></ul><p>By preprocessing the data in this manner, we have created a structured dataset that can be used for training our recommendation model in the subsequent steps. This dataset encapsulates the essential information about user-product interactions, laying the foundation for building an effective recommendation system using PostgresML.</p><h4>Step 2: Model Training</h4><p>Now that we have prepared our data within the PostgreSQL database, including aggregating and preprocessing user-product interaction data, we can proceed with training our recommendation model using PostgresML. In this step, we’ll leverage the structured dataset created in Step 1 to train a collaborative filtering model — a popular technique for recommendation systems.</p><p><strong>Collaborative Filtering:</strong></p><p>Collaborative filtering is a technique commonly used in recommendation systems to make predictions about the interests of a user by collecting preferences from many users (collaborating). The core idea behind collaborative filtering is to find similarities between users or items based on their interactions and use these similarities to make recommendations.</p><p><strong>Model Training with PostgresML:</strong></p><p>PostgresML provides a built-in `collaborative_filtering` algorithm that we can utilise for training our recommendation model directly within the PostgreSQL environment. This algorithm leverages the user-item interactions stored in our preprocessed dataset to learn patterns and preferences, enabling it to make personalized recommendations for users.</p><p><strong>SQL Code Example:</strong></p><pre>CREATE MODEL recommendation_model<br>USING collaborative_filtering<br>OPTIONS (<br> user_column = &#39;user_id&#39;,<br> item_column = &#39;product_id&#39;,<br> rating_column = &#39;rating&#39;<br>)<br>AS<br>SELECT<br> user_id,<br> product_id,<br> views, - We can use &#39;views&#39; as a proxy for user interest<br> purchases - We can use &#39;purchases&#39; as a signal for user preference<br>FROM<br> user_item_matrix;</pre><p>In the above SQL code:</p><ul><li>We use the `CREATE MODEL` statement to define and train our recommendation model named `recommendation_model`.</li><li>We specify the algorithm to use (`collaborative_filtering`) along with options such as the column names for users, items, and ratings. In this example, we use ‘user_id’ as the user column, ‘product_id’ as the item column, and ‘views’ (or ‘purchases’) as the rating column.</li><li>We select the relevant columns from our preprocessed dataset `user_item_matrix` to train the model. Here, we can use features such as the number of views and purchases as signals for user preferences.</li></ul><p>By executing this SQL code, PostgresML trains the collaborative filtering model using the specified user-item interactions. The trained model captures the underlying patterns in user behaviour and product preferences, laying the foundation for generating personalised recommendations in the next step.</p><p>With the model trained, we can now proceed to Step 3: Generating Recommendations, where we’ll utilise the trained model to generate personalised recommendations for individual users based on their historical interactions and preferences.</p><h4><strong>Step 3: Generating Recommendations</strong></h4><p>With our recommendation model trained using the collaborative filtering algorithm in Step 2 and our preprocessed dataset containing user-item interactions from Step 1, we can now proceed to generate personalized recommendations for individual users. In this step, we’ll leverage the trained model to predict user preferences and recommend items based on those predictions.</p><p><strong>Using the Trained Model:</strong></p><p>The collaborative filtering model we trained in Step 2 has learned patterns and similarities among users and items based on their interactions. By utilising this model, we can predict the likelihood of a user interacting with or showing interest in a particular item.</p><p><strong>SQL Code Example:</strong></p><pre>SELECT<br> product_id,<br> prediction AS predicted_interest<br>FROM<br> recommendation_model<br>WHERE<br> user_id = &#39;123&#39;<br>ORDER BY<br> prediction DESC<br>LIMIT<br> 10;</pre><p>In the above SQL code:</p><ul><li>We use the `SELECT` statement to retrieve recommendations for a specific user (`user_id = ‘123’`).</li><li>We select the `product_id` from the recommendation model along with the `prediction` column, which represents the model’s prediction of the user’s interest in each product.</li><li>We order the recommendations by the predicted interest (`prediction`) in descending order to prioritise items with higher predicted interest.</li><li>We limit the number of recommendations to 10 using the `LIMIT` clause to provide a manageable list of top recommendations for the user.</li></ul><p><strong>Interpreting the Recommendations:</strong></p><p>The generated recommendations represent a list of items that the model predicts the user is likely to be interested in based on their historical interactions and preferences. These recommendations can be presented to the user through various channels such as a website, mobile app, or email, to facilitate personalised and relevant user experiences.</p><p><strong>Customisation and Optimisation:</strong></p><p>While the collaborative filtering model provides a solid foundation for generating recommendations, it’s important to note that recommendations can be further customised and optimised based on additional factors such as user demographics, contextual information, and business rules. Organisations can experiment with different recommendation strategies and metrics to fine-tune the recommendation engine and enhance user satisfaction and engagement.</p><p>By following this approach, we can leverage the power of PostgresML to generate personalised recommendations for users, driving user engagement, and fostering a more satisfying user experience within our application or platform.</p><h4>Step 4: Evaluation and Refinement</h4><p>Finally, we evaluate the performance of our recommendation system using metrics such as precision, recall, and Mean Average Precision (MAP). Based on these evaluations, we can refine our model and iterate on the recommendation process to enhance its accuracy and effectiveness.</p><h4>Conclusion:</h4><p>PostgresML represents a groundbreaking advancement in the realm of database-driven machine learning, empowering users to seamlessly integrate ML capabilities within their PostgreSQL workflows. From predictive analytics to recommendation systems and beyond, PostgresML unlocks a world of possibilities for data-driven innovation, all within the familiar confines of SQL. As organisations continue to harness the power of data to drive insights and decision-making, PostgresML stands as a testament to the synergy between traditional database management and cutting-edge machine learning techniques.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9ddd6f6b0e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing an API in NodeJS, GraphQL, Apollo Server & TypeScript. (TypeGraphQL + TypeORM)]]></title>
            <link>https://medium.com/@liamandrewmaddison/writing-an-api-in-nodejs-graphql-apollo-server-typescript-typegraphql-typeorm-f545beee1e65?source=rss-401162d8a9c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/f545beee1e65</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Liam Andrew Maddison]]></dc:creator>
            <pubDate>Fri, 02 Dec 2022 09:01:35 GMT</pubDate>
            <atom:updated>2022-12-02T09:01:35.800Z</atom:updated>
            <content:encoded><![CDATA[<p>TLDR; I will be outlining how to create a simple API with Authentication, Authorisation &amp; User Permissions, with the tools outlined in the title.</p><p>Recently I have come up with an App idea and wanted, in my spare time, to start building it! So I went to the drawing board and started to research new tech I haven’t particularly had the opportunity to use before and most importantly have fun whilst doing it.</p><p>I knew I wanted a NodeJS API with TypeScript and I also wanted to strengthen my GraphQL skills…however…this became a pain very quickly as there are hundreds of different tools and frameworks to choose from within this stack alone!</p><p>So how exactly do we decide? And how do you know if these techs marry well with each other? Or if they will cause you pain down the road?</p><p>Well I researched for days upon days and tested a few things out until I found, in my opinion, the<strong><em>“perfect”</em></strong> stack for this solution. They are easy to use, easy to learn, fully supported with typescript and extremely fun to develop with, so without further ado here’s the stack I chose:</p><p><strong>The Backend Stack:</strong></p><ul><li><a href="https://nodejs.org/en/">NodeJS</a></li><li><a href="https://www.typescriptlang.org/">TypeScript</a></li><li><a href="https://graphql.org/">GraphQL</a></li><li><a href="https://typegraphql.com/">TypeGraphQL</a></li><li><a href="https://typeorm.io/">TypeORM</a></li><li><a href="https://www.postgresql.org/">Postgres</a> — you’ll need to know how to set this up locally</li></ul><p>I will be focusing on TypeGraphQL and TypeORM, as these are the key components to this API.</p><p>TypeGraphQL allows you to write your GraphQL types, queries, mutations and resolvers all in Typescript! Amazing right!? But that’s not all, TypeORM comes in and allows you to attach decorators to your TypeGraphQL types so that it can automatically synchronise/migrate your database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec074700aaf84474478c777fc0781be0/href">https://medium.com/media/ec074700aaf84474478c777fc0781be0/href</a></iframe><p>So this is what a typical entity class would look like.</p><p>As you can see, we are decorating a User class with TypeGraphQL and TypeORM.</p><p>Cool right? God I love cool tech!</p><p>So that’s the general outline, I guess you’re now wondering how do we get this up and running…</p><p>Let’s get to the real code!</p><p>Firstly we’re going to have to install the correct packages, here is my dependencies within package.json.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c551b8dcafcf97dd3afe469f1f2ca280/href">https://medium.com/media/c551b8dcafcf97dd3afe469f1f2ca280/href</a></iframe><p>Once you’ve installed them, we will need to create our index file, so that we can start our Apollo Server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f25887b1117f6230d52b64f495e6877/href">https://medium.com/media/7f25887b1117f6230d52b64f495e6877/href</a></iframe><p>We need to firstly create our connection to our Postgres DB, TypeORM allows us to do this pretty simply.</p><p>We then need to build our GraphQL schema and run the Apollo Server.</p><p>Then run the server on port 4000.</p><p>Now before we run this, we will need to create our TypeORM entities and have at least one resolver within our buildSchema method. For this we need to set up a Container, this will allow us to use dependency injection throughout our application. We require this as we will be injecting TypeORM repositories into our TypeGraphQL resolvers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bc365cc6f329d7e2b14579cd5f65174/href">https://medium.com/media/2bc365cc6f329d7e2b14579cd5f65174/href</a></iframe><p>Lets go and create our TypeGraphQL Type / TypeORM Entity, using the user.type.ts example above should work just fine.</p><p>After that we can create our createContext and authChecker methods:</p><p>Our createContext method allows us to retrieve the request token if available, then we can decode and add the user to our request if the token is valid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fc3604d744e84f4e79e214a10b0ee69/href">https://medium.com/media/2fc3604d744e84f4e79e214a10b0ee69/href</a></iframe><p>Our authChecker method will now allow us to use a decorator on our resolvers to guard them with a list of permissions for that resolver query, mutation and so on, allowing us to have a secure API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7455e4a5a56120cc8a3f6c16083960a0/href">https://medium.com/media/7455e4a5a56120cc8a3f6c16083960a0/href</a></iframe><p>Let’s now create our first resolver, here is where the magic starts coming together, and you start seeing the real power of our set up.</p><p>What we’re going to do, is actually create an abstract resolver, so we can leverage inheritance and code re-usage.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97812a3a421e369de6cfbad45dea482a/href">https://medium.com/media/97812a3a421e369de6cfbad45dea482a/href</a></iframe><p>The `@Authorized` decorator allows us to pass in a list of permissions that a user should have before being able to access this query.</p><p><strong>…PART 2 COMING SOON…</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f545beee1e65" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>