<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Database on David Lang</title>
    <link>https://www.davidlang.tech/tags/database/</link>
    <description>Recent content in Database on David Lang</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 16 Nov 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://www.davidlang.tech/tags/database/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Using dynamic SQL query in Redshift database</title>
      <link>https://www.davidlang.tech/posts/using-sql-dynamic-queries-in-redshift-database/</link>
      <pubDate>Thu, 16 Nov 2023 00:00:00 +0000</pubDate>
      <guid>https://www.davidlang.tech/posts/using-sql-dynamic-queries-in-redshift-database/</guid>
      <description>&lt;p&gt;While working with a client, we had a requirement to perform bulk insert/update using &lt;strong&gt;&lt;a href=&#34;https://docs.retool.com/docs/working-with-tables&#34;&gt;Retool table&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href=&#34;https://aws.amazon.com/redshift/&#34;&gt;Redshift database&lt;/a&gt;&lt;/strong&gt;. For this situation, We had to loop through the table records and execute a dynamic SQL query in Redshift.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Redshift database&lt;/strong&gt; supports execution of &lt;strong&gt;dynamic SQL&lt;/strong&gt; with the help of &lt;strong&gt;Prepared Statements&lt;/strong&gt; or &lt;strong&gt;Stored Procedures&lt;/strong&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;prepared-statements&#34;&gt;Prepared Statements&lt;/h2&gt;&#xA;&lt;p&gt;We use prepared statements when we want to execute dynamic SQL queries directly without a stored procedure. When a prepared statement is executed, the SQL statement is parsed, rewritten, and planned. We then &lt;strong&gt;EXECUTE&lt;/strong&gt; the prepared statement.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Prisma ORM: Modern Database Access in Node.js</title>
      <link>https://www.davidlang.tech/posts/prisma-orm-modern-database-access-in-nodejs/</link>
      <pubDate>Tue, 07 Dec 2021 00:00:00 +0000</pubDate>
      <guid>https://www.davidlang.tech/posts/prisma-orm-modern-database-access-in-nodejs/</guid>
      <description>&lt;p&gt;Prisma provides type-safe database access, migrations, and a visual data browser-popular in TypeScript full stack projects.&lt;/p&gt;&#xA;&lt;h2 id=&#34;schema-definition&#34;&gt;Schema Definition&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model User {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  id        String   @id @default(cuid())&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  email     String   @unique&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  posts     Post[]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  createdAt DateTime @default(now())&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model Post {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  id       String @id @default(cuid())&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  title    String&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  author   User   @relation(fields: [authorId], references: [id])&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  authorId String&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;queries&#34;&gt;Queries&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-typescript&#34; data-lang=&#34;typescript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#268bd2&#34;&gt;const&lt;/span&gt; users &lt;span style=&#34;color:#719e07&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#719e07&#34;&gt;await&lt;/span&gt; prisma.user.findMany({&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  include&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; { posts: &lt;span style=&#34;color:#dc322f&#34;&gt;true&lt;/span&gt; },&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  where&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; { email&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; { contains&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#2aa198&#34;&gt;&amp;#39;@company.com&amp;#39;&lt;/span&gt; } },&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;});&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;migrations&#34;&gt;Migrations&lt;/h2&gt;&#xA;&lt;p&gt;&lt;code&gt;npx prisma migrate dev&lt;/code&gt; applies schema changes in development. Use &lt;code&gt;prisma generate&lt;/code&gt; after schema edits to refresh the client.&lt;/p&gt;</description>
    </item>
    <item>
      <title>MongoDB Best Practices: Schema Design and Indexing</title>
      <link>https://www.davidlang.tech/posts/mongodb-best-practices-schema-design-and-indexing/</link>
      <pubDate>Mon, 25 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.davidlang.tech/posts/mongodb-best-practices-schema-design-and-indexing/</guid>
      <description>&lt;p&gt;MongoDB&amp;rsquo;s flexible documents are powerful-but schema design still matters for performance and maintainability.&lt;/p&gt;&#xA;&lt;h2 id=&#34;embedding-vs-referencing&#34;&gt;Embedding vs Referencing&lt;/h2&gt;&#xA;&lt;p&gt;Embed when data is read together and has a one-to-few relationship (user preferences inside a user doc). Reference when data grows unbounded or is shared (comments on posts).&lt;/p&gt;&#xA;&lt;h2 id=&#34;indexing&#34;&gt;Indexing&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;db.orders.createIndex({ customerId&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#2aa198&#34;&gt;1&lt;/span&gt;, createdAt&lt;span style=&#34;color:#719e07&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#719e07&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#2aa198&#34;&gt;1&lt;/span&gt; });&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Compound indexes support queries that filter and sort on the same fields. Use &lt;code&gt;.explain()&lt;/code&gt; to verify index usage.&lt;/p&gt;&#xA;&lt;h2 id=&#34;validation-and-migrations&#34;&gt;Validation and Migrations&lt;/h2&gt;&#xA;&lt;p&gt;Use JSON Schema validation at the collection level. Version documents with a &lt;code&gt;schemaVersion&lt;/code&gt; field for gradual migrations.&lt;/p&gt;</description>
    </item>
    <item>
      <title>PostgreSQL vs MySQL: Choosing the Right Database</title>
      <link>https://www.davidlang.tech/posts/postgresql-vs-mysql-choosing-the-right-database/</link>
      <pubDate>Mon, 10 Aug 2020 00:00:00 +0000</pubDate>
      <guid>https://www.davidlang.tech/posts/postgresql-vs-mysql-choosing-the-right-database/</guid>
      <description>&lt;p&gt;Relational databases remain the backbone of most full stack applications. PostgreSQL and MySQL are the two most common choices-each excels in different scenarios.&lt;/p&gt;&#xA;&lt;h2 id=&#34;postgresql-strengths&#34;&gt;PostgreSQL Strengths&lt;/h2&gt;&#xA;&lt;p&gt;PostgreSQL offers advanced types (JSONB, arrays), full-text search, robust concurrency (MVCC), and extensions like PostGIS. It is the default choice for complex queries, analytics-heavy apps, and strict data integrity.&lt;/p&gt;&#xA;&lt;h2 id=&#34;mysql-strengths&#34;&gt;MySQL Strengths&lt;/h2&gt;&#xA;&lt;p&gt;MySQL (and MariaDB) are widely hosted, familiar to many teams, and perform well for read-heavy web workloads. Managed offerings are plentiful and often slightly cheaper at small scale.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
