Add embryo to products
This commit is contained in:
@@ -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' },
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './products-sql';
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user