Add embryo to products

This commit is contained in:
Niklas Fondberg
2021-04-06 15:49:41 +02:00
parent 813f4c8e47
commit 949c641410
10 changed files with 110 additions and 4 deletions
+23
View File
@@ -0,0 +1,23 @@
import { SQLDataSource } from 'datasource-sql';
import * as sql from './sql';
const MINUTE = 60;
export class ProductAPI extends SQLDataSource {
constructor(config) {
super(config);
}
async getProducts() {
const query = sql.products(true, true, 2);
const data = await this.knex.raw(query);
console.log(JSON.stringify(data.rows[0], null, 2));
return [
{ id: 1, artNo: 'SE' },
{ id: 2, artNo: 'DK' },
];
}
}
+1
View File
@@ -0,0 +1 @@
export * from './products-sql';
+62
View File
@@ -0,0 +1,62 @@
// Get syntax highlighting with vscode by installing "Comment tagged templates2
export function products(
visible: boolean | null,
browsable: boolean | null,
limit: number = 100,
) {
const query = /* sql */ `
SELECT products.productid,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
json_agg(
json_build_object(
'printid',
printproducts.printid,
'productid',
printproducts.productid,
'groupid',
printproducts.groupid,
'inserted',
printproducts.inserted,
'updated',
printproducts.updated
)
) AS print_products,
json_agg(
json_build_object(
'id',
blacklist.id,
'product_id',
blacklist.product_id,
'market_id',
blacklist.market_id,
'group_id',
blacklist.group_id,
'created_at',
blacklist.created_at,
'updated_at',
blacklist.updated_at
)
) AS blacklisted
FROM "product-products" products
JOIN "product-printproducts" printproducts ON products.productid = printproducts.productid
JOIN product_blacklist blacklist ON products.productid = blacklist.product_id
${
visible != null
? /* sql */ `
WHERE products.visible = ${visible ? '1' : '0'}
AND products.browsable = ${browsable ? '1' : '0'}`
: ``
}
GROUP BY products.productid
LIMIT ${limit}
`;
return query;
}