<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Phạm Trần Gia Hưng</title>
    <description>The latest articles on DEV Community by Phạm Trần Gia Hưng (@hung1510).</description>
    <link>https://dev.to/hung1510</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4046865%2F8e542f20-5561-43e1-9531-48b140875531.jpg</url>
      <title>DEV Community: Phạm Trần Gia Hưng</title>
      <link>https://dev.to/hung1510</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hung1510"/>
    <language>en</language>
    <item>
      <title>How to Generate Realistic E-Commerce Test Data in 2 Lines of Code</title>
      <dc:creator>Phạm Trần Gia Hưng</dc:creator>
      <pubDate>Sat, 25 Jul 2026 13:24:58 +0000</pubDate>
      <link>https://dev.to/hung1510/how-to-generate-realistic-e-commerce-test-data-in-2-lines-of-code-3dj4</link>
      <guid>https://dev.to/hung1510/how-to-generate-realistic-e-commerce-test-data-in-2-lines-of-code-3dj4</guid>
      <description>&lt;h1&gt;
  
  
  Generate Realistic E-Commerce Test Data
&lt;/h1&gt;

&lt;p&gt;Some "fake data" libraries give a independent random objects. Call &lt;code&gt;faker.commerce.product()&lt;/code&gt; a hundred times and you get a hundred products but nothing connects them. If you need a realistic &lt;em&gt;dataset&lt;/em&gt; (carts that reference real users, orders that reference real carts, shipments that reference real orders, total that add up), you end up hand-wiring all of that yourself in a seed script.&lt;/p&gt;

&lt;p&gt;My project &lt;a href="https://github.com/Hung1510/Eco-Faker" rel="noopener noreferrer"&gt;eco-faker&lt;/a&gt; does the wiring for you. Its a stateful, relationally-consistent fake-data generator built specifically for e-commerce: every &lt;code&gt;Cart&lt;/code&gt;, &lt;code&gt;Order&lt;/code&gt;, &lt;code&gt;Shipment&lt;/code&gt;, and &lt;code&gt;ReturnRequest&lt;/code&gt; comes out of the same underlying state machine, so the dataset reads like a real store's history instead of unrelated fixtures.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 2 lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; eco-faker
my-eco-gen generate &lt;span class="nt"&gt;--users&lt;/span&gt; 100 &lt;span class="nt"&gt;--format&lt;/span&gt; sql &lt;span class="nt"&gt;--output&lt;/span&gt; ./seed.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That a SQL seed file with 100 users and everything downstream of them: carts, abandoned checkouts, orders, shipments, returns, all internally consistent. If you prefer code over CLI&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serialize&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eco-faker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;scaleFactor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sql&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// or "csv", or just use the JSON directly&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same seed, same output, every time &lt;code&gt;generate()&lt;/code&gt; is fully deterministic given &lt;code&gt;(config, referenceNow)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "relationally consistent" actually means
&lt;/h2&gt;

&lt;p&gt;It's not just that the foreign keys line up. The financials balance (&lt;code&gt;subtotal + tax + shipping === total&lt;/code&gt;, to the cent), the timeline makes sense (a shipment's tracking events can't happen before the order that created it), and every line item's &lt;code&gt;productId&lt;/code&gt; resolves to a real product that's genuinely reused across orders — not independently invented per line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users → Carts → (AbandonedCheckouts | Orders → Shipments → ReturnRequests)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A cart either gets abandoned or converts into an order. An order gets shipped and tracked through a real carrier-style event sequence. Some shipments get returned, with a return reason and a refund that's grounded in the order's real total.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenarios
&lt;/h2&gt;

&lt;p&gt;Realistic e-commerce data isn't just "N users, N orders", it's &lt;em&gt;shaped&lt;/em&gt; data. eco-faker ships five built-in scenario presets that tune the underlying rates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my-eco-gen generate &lt;span class="nt"&gt;--scenario&lt;/span&gt; black-friday &lt;span class="nt"&gt;--users&lt;/span&gt; 500
my-eco-gen generate &lt;span class="nt"&gt;--scenario&lt;/span&gt; post-holiday-returns &lt;span class="nt"&gt;--users&lt;/span&gt; 500
my-eco-gen generate &lt;span class="nt"&gt;--scenario&lt;/span&gt; supply-chain-crisis &lt;span class="nt"&gt;--users&lt;/span&gt; 500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;black-friday&lt;/code&gt; cranks cart volume and abandonment. &lt;code&gt;post-holiday-returns&lt;/code&gt; spikes the return rate. &lt;code&gt;supply-chain-crisis&lt;/code&gt; introduces real stockouts and delayed shipments. Want a custom mix? Write your own scenario file (YAML or JSON) that inherits from a built-in preset and overrides just the knobs you care about validated against the same schema &lt;code&gt;generate()&lt;/code&gt; itself uses, so a typo surfaces immediately instead of silently generating nonsense.&lt;/p&gt;

&lt;h2&gt;
  
  
  It doesn't stop at "generate"
&lt;/h2&gt;

&lt;p&gt;Once you have a dataset, eco-faker gives you tools to actually &lt;em&gt;use&lt;/em&gt; it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;my-eco-gen serve&lt;/code&gt;: turns any dataset into a live REST API in one command, with pagination, filtering, and even chaos-mode (simulated 500s/429s/latency) for testing how your frontend handles a flaky backend.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;my-eco-gen lint&lt;/code&gt;: checks referential integrity, financial consistency, and temporal ordering before you insert anything into a real database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;my-eco-gen fuzz&lt;/code&gt;: deliberately mutates the dataset with schema-valid-but-logically-impossible data (inverted prices, oversell quantities) to find bugs your validation layer doesn't catch.&lt;/li&gt;
&lt;li&gt;MSW, tRPC, GraphQL, Apollo Client, and React Query adapters, so the exact same dataset can back your frontend tests without a second mocking setup.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; eco-faker
my-eco-gen generate &lt;span class="nt"&gt;--scenario&lt;/span&gt; black-friday &lt;span class="nt"&gt;--users&lt;/span&gt; 100 &lt;span class="nt"&gt;--format&lt;/span&gt; sql &lt;span class="nt"&gt;--output&lt;/span&gt; ./seed.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you dont have Node then theres a 1-command Docker path that seeds a real Postgres:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose up &lt;span class="nt"&gt;--build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Live browser demo (no install): &lt;a href="https://hung1510.github.io/Eco-Faker/" rel="noopener noreferrer"&gt;https://hung1510.github.io/Eco-Faker/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/Hung1510/Eco-Faker" rel="noopener noreferrer"&gt;https://github.com/Hung1510/Eco-Faker&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;npm: &lt;a href="https://www.npmjs.com/package/eco-faker" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/eco-faker&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>testing</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
