Skip to main content

getProductsBaseQuery

Get a base SelectQuery for querying products with descriptions, inventory, and main images joined.

Import

import { getProductsBaseQuery } from "@evershop/evershop/catalog/services";

Syntax

getProductsBaseQuery(): SelectQuery

Parameters

None.

Return Value

Returns a SelectQuery object with:

  • Base table: product
  • Left join: product_description on product_description_product_id = product_id
  • Inner join: product_inventory on product_inventory_product_id = product_id
  • Left join: product_image (only main image where is_main = true)

Examples

Basic Usage

import { getProductsBaseQuery } from "@evershop/evershop/catalog/services";
import { pool } from "@evershop/evershop/lib/postgres";

const query = getProductsBaseQuery();

// Load all visible products.
// Only the FIRST condition uses `.where()`. It returns a `Where`, which offers
// `.andWhere()`, `.orWhere()`, `.and()`, `.or()` and `.execute()` — but not a
// second `.where()`. Calling `query.where()` again would also RESET the builder's
// existing predicates rather than add to them.
const products = await query
.where('visibility', '=', true)
.andWhere('status', '=', true)
.execute(pool);

Get Single Product

import { getProductsBaseQuery } from "@evershop/evershop/catalog/services";
import { pool } from "@evershop/evershop/lib/postgres";

const query = getProductsBaseQuery();

// Load product by SKU
const product = await query
.where('sku', '=', 'LAPTOP-123')
.load(pool);

console.log(product.name);
console.log(product.price);
console.log(product.qty);

See Also