Files
graphql-api-js/src/datasources/sql/products-sql.ts
T

202 lines
5.0 KiB
TypeScript

import { ProductsFilterInput } from '../../types/product-types';
// Get syntax highlighting with vscode by installing "Comment tagged templates2
const baseQuery = /* sql */ `
SELECT products.productid AS id,
stock.id AS stockid,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
products.publishing_date,
products.ref1,
products.ref2,
products.ref3,
(
SELECT json_agg(
json_build_object(
'printId',
"product-printproducts".printid,
'productId',
"product-printproducts".productid,
'groupId',
"product-printproducts".groupid,
'inserted',
"product-printproducts".inserted,
'updated',
"product-printproducts".updated
)
)
FROM "product-printproducts"
WHERE "product-printproducts".productid = products.productid
) AS printproducts,
(
SELECT json_agg(
json_build_object(
'id',
product_blacklist.id,
'productId',
product_blacklist.product_id,
'marketId',
product_blacklist.market_id,
'groupId',
product_blacklist.group_id,
'inserted',
product_blacklist.created_at,
'updated',
product_blacklist.updated_at
)
)
FROM product_blacklist
WHERE product_blacklist.product_id = products.productid
) AS blacklisting,
(
SELECT json_object_agg(fields.field, pf.value)
FROM "product-products_fields" pf
JOIN "product-fields" fields ON fields.fieldid = pf.fieldid
WHERE pf.productid = products.productid
) AS fields,
(
SELECT json_agg(keywords.value)
FROM product_keyword pk
JOIN keywords ON keywords.id = pk.keyword_id
WHERE keywords.id IN (1103, 1104)
AND pk.product_id = products.productid
) AS types,
(
SELECT json_agg(wt.wallpapertype_id)
FROM product_wallpapertypes wt
WHERE wt.product_id = products.productid
) AS wallpapertypes
FROM "product-products" products
LEFT JOIN stockproducts stock ON stock.product_id = products.productid
`;
export function products(input: ProductsFilterInput) {
const limit = input.pagination.limit ?? 100;
const offset = input.pagination.offset ?? 0;
const filter = input.filter ?? null;
return (
baseQuery +
/* sql */ `
${
filter?.visible != null
? /* sql */ `
WHERE products.visible = ${filter?.visible ? '1' : '0'}
AND products.browsable = ${filter?.browsable ? '1' : '0'}`
: ``
}
ORDER BY products.publishing_date DESC, products.productid DESC
LIMIT ${limit} OFFSET ${offset}
`
);
}
export function productsTotal(input: ProductsFilterInput) {
const filter = input.filter;
return /*sql */ `
SELECT count(*) FROM "product-products" products
${
filter?.visible != null
? /* sql */ `
WHERE products.visible = ${filter?.visible ? '1' : '0'}
AND products.browsable = ${filter?.browsable ? '1' : '0'}`
: ``
}
`;
}
export function product(id: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid = ${id}
`
);
}
export function productByPath(path: string) {
return (
baseQuery +
/* sql */ `
WHERE products.path = '${path}'
`
);
}
export function categoryProducts(categoryId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT product_id FROM product_category WHERE category_id = ${categoryId})
ORDER BY products.inserted DESC
`
);
}
export function categoryWithAllSubCategoriesProducts(categoryId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (
SELECT product_id
FROM product_category
WHERE category_id IN (
SELECT cat.id
FROM categories cat
JOIN (
SELECT id, lft, rgt
FROM categories
WHERE id = ${categoryId}
) tree
ON cat.lft >= tree.lft AND cat.rgt <= tree.rgt
)
ORDER BY product_id
)
ORDER BY products.inserted DESC
`
);
}
export function designerProducts() {
return (
baseQuery +
/* sql */ `
WHERE products.designerid = ?
ORDER BY products.inserted DESC
`
);
}
export function referenceProducts() {
return (
baseQuery +
/* sql */ `
WHERE LOWER(products.ref1) LIKE ? OR LOWER(products.ref2) LIKE ? OR LOWER(products.ref3) LIKE ? ORDER BY products.inserted DESC`
);
}
export function keywordProducts(keywordId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT product_id FROM product_keyword WHERE keyword_id = ${keywordId})
ORDER BY products.inserted DESC
`
);
}
export function searchByFieldValue(fieldid: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT productid FROM "product-products_fields" WHERE fieldid = ${fieldid} AND LOWER(value) LIKE ? LIMIT 5000) -- limit to 5000 results
ORDER BY products.inserted DESC
`
);
}