diff --git a/API.brand.md b/API.brand.md index 4955933..1339ef0 100644 --- a/API.brand.md +++ b/API.brand.md @@ -99,13 +99,13 @@ Lists the brands of the user's selected company, sorted by name. **Returns** -`Promise<{brands: Array<{id: number, name: string, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>` — The brands plus the total unpaged count. Small-thumbnail fields are present only when `includeSmallThumbs` is true; the binary is base64-encoded AVIF data. +`Promise<{brands: Array<{id: number, name: string, articleCount: number, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>` — The brands plus the total unpaged count. Small-thumbnail fields are present only when `includeSmallThumbs` is true; the binary is base64-encoded AVIF data. **Example** ```js const result = await store.brand.list(42, { offset: 0, limit: 10 }); -// => { brands: [{ id, name, gsprpinfo, pics: [...] }], total: 25, offset: 0, limit: 10 } +// => { brands: [{ id, name, articleCount, gsprpinfo, pics: [...] }], total: 25, offset: 0, limit: 10 } ``` ## `brand.update` diff --git a/API.md b/API.md index c1bcfe3..bc13072 100644 --- a/API.md +++ b/API.md @@ -132,7 +132,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met - `addImage(googleAuthId: number, brandId: number, image: Buffer, tx?: object): Promise<{id: number, fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW: number, smallThumbH: number, pics: Array}>` - `create(googleAuthId: number, input: {name: string, gsprpinfo?: string}, tx?: object): Promise<{id: number, name: string, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string}>}>` - `delete(googleAuthId: number, id: number, tx?: object): Promise` - - `list(googleAuthId: number, opts?: {offset?: number, limit?: number, includeSmallThumbs?: boolean}, tx?: object): Promise<{brands: Array<{id: number, name: string, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>` + - `list(googleAuthId: number, opts?: {offset?: number, limit?: number, includeSmallThumbs?: boolean}, tx?: object): Promise<{brands: Array<{id: number, name: string, articleCount: number, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>` - `update(googleAuthId: number, id: number, input: {name?: string, gsprpinfo?: string}, tx?: object): Promise<{id: number, name: string, gsprpinfo: string|null, touchedArticleCount: number}>` - `upsert(googleAuthId: number, input: {name: string, gsprpinfo?: string}, tx?: object): Promise<{id: number, name: string, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string}>}>` - [customergroup](API.customergroup.md) diff --git a/src/providers/mssql/queries/brand.js b/src/providers/mssql/queries/brand.js index 54e5ca7..ccd45b2 100644 --- a/src/providers/mssql/queries/brand.js +++ b/src/providers/mssql/queries/brand.js @@ -3,6 +3,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT p.hash AS fullHash, @@ -31,6 +42,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT p.hash AS fullHash, @@ -54,6 +76,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT p.hash AS fullHash, diff --git a/src/providers/sqlite3/queries/brand.js b/src/providers/sqlite3/queries/brand.js index b2ac20e..c17df26 100644 --- a/src/providers/sqlite3/queries/brand.js +++ b/src/providers/sqlite3/queries/brand.js @@ -12,6 +12,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT COALESCE(json_group_array(json_object( 'fullHash', hash, @@ -37,6 +48,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT COALESCE(json_group_array(json_object( 'fullHash', hash, @@ -63,6 +85,17 @@ SELECT b.id, b.name, b.gsprpinfo, + ( + SELECT COUNT(*) + FROM article a + LEFT JOIN article parent ON parent.id = a.parentarticle_id + WHERE a.company_id = b.company_id + AND a.is_parent = 0 + AND NOT EXISTS ( + SELECT 1 FROM article child WHERE child.parentarticle_id = a.id + ) + AND COALESCE(parent.brand_id, a.brand_id) = b.id + ) AS articleCount, ( SELECT COALESCE(json_group_array(json_object( 'fullHash', hash, diff --git a/src/store/brand/list.js b/src/store/brand/list.js index 1620648..4678acb 100644 --- a/src/store/brand/list.js +++ b/src/store/brand/list.js @@ -48,11 +48,11 @@ export default function list(provider) { * @param {number} [opts.limit=10] - Number of brands to return (max 100). * @param {boolean} [opts.includeSmallThumbs=false] - Include 32px-edge small thumbnail dimensions and base64 AVIF data. * @param {object} [tx] - Optional transaction to run within. - * @returns {Promise<{brands: Array<{id: number, name: string, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>} The brands plus the total unpaged count. Small-thumbnail fields are present only when `includeSmallThumbs` is true; the binary is base64-encoded AVIF data. + * @returns {Promise<{brands: Array<{id: number, name: string, articleCount: number, gsprpinfo: string|null, pics: Array<{fullHash: string, thumbW: number, thumbH: number, thumbHash: string, smallThumbW?: number|null, smallThumbH?: number|null, smallThumbBinary?: string|null}>}>, total: number, offset: number, limit: number}>} The brands plus the total unpaged count. Small-thumbnail fields are present only when `includeSmallThumbs` is true; the binary is base64-encoded AVIF data. * * @example * const result = await store.brand.list(42, { offset: 0, limit: 10 }); - * // => { brands: [{ id, name, gsprpinfo, pics: [...] }], total: 25, offset: 0, limit: 10 } + * // => { brands: [{ id, name, articleCount, gsprpinfo, pics: [...] }], total: 25, offset: 0, limit: 10 } */ return async (googleAuthId, opts, tx) => { let offset = 0; @@ -91,6 +91,7 @@ export default function list(provider) { brands: listResult.rows.map((row) => ({ id: row.id, name: row.name, + articleCount: Number(row.articleCount ?? 0), gsprpinfo: row.gsprpinfo, pics: parsePics(row.picsJson), })),