Library

Braid

Declare what each detail source is and how it joins. Braid indexes every source once into a Map and does a single lookup per row, so adding a fourth source costs one more pass instead of another scan per row.

npm install @michaelrwalker/braid
channel_one shopify variations products 3 rows
{ sku: "TS-100", channel_one : {…} , shopify : {…} , variations : [2] }
9 lookups vs 33 scans nested .find()

// reference

One script, hover to learn it

Every highlighted token is real Braid API — hover or tab to it to watch the row type widen with each join.

catalog.ts hover the highlighted tokens

				
				import { Braid } from "@michaelrwalker/braid";
			
				
				 
			
				
				interface Listing {
			
				
				readonly sku: string;
			
				
				readonly productId: string;
			
				
				}
			
				
				 
			
				
				interface Product {
			
				
				readonly id: string;
			
				
				readonly title: string;
			
				
				readonly brandId: string;
			
				
				}
			
				
				 
			
				
				const catalog = new Braid()
			
				
				.main({
			
				
				name: "listing",
			
				
				source: listings,
			
				
				key: (listing) => listing.sku,
			
				
				})
			
				
				.join({
			
				
				name: "product",
			
				
				source: products,
			
				
				on: (product) => product.id,
			
				
				key: (listing) => listing.productId,
			
				
				type: "single",
			
				
				required: true,
			
				
				})
			
				
				.join({
			
				
				name: "brand",
			
				
				source: brands,
			
				
				on: (brand) => brand.id,
			
				
				from: (row) => row.product.brandId,
			
				
				type: "single",
			
				
				default: null,
			
				
				})
			
				
				.join({
			
				
				name: "reviews",
			
				
				source: reviews,
			
				
				on: (review) => review.sku,
			
				
				type: "many",
			
				
				})
			
				
				.run();
			
				
				 
			
				
				catalog[0].product;  // Product — required: true, never null
			
				
				catalog[0].brand;    // Brand | null — from() reads through product safely
			
				
				catalog[0].reviews;  // Review[] — [] when nothing matched, never undefined
			

Contents 16 pages