This commit is contained in:
seb
2026-10-11 18:55:08 +02:00
parent 7f4092971d
commit 17cb4f4c72
11 changed files with 110 additions and 24 deletions

View File

@@ -4,11 +4,25 @@ SELECT
an.name,
an.sort,
an.exclusive,
(
SELECT COUNT(*)
FROM article a
WHERE a.parentattributename_id = an.id
AND a.is_parent = 1
AND a.company_id = an.company_id
) AS articleCount,
(
SELECT
av.id,
av.value,
av.sort
av.sort,
(
SELECT COUNT(DISTINCT aav.article_id)
FROM articleattributevalue aav
INNER JOIN article a ON a.id = aav.article_id
WHERE aav.attributevalue_id = av.id
AND a.company_id = an.company_id
) AS articleCount
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
@@ -33,11 +47,25 @@ SELECT
an.name,
an.sort,
an.exclusive,
(
SELECT COUNT(*)
FROM article a
WHERE a.parentattributename_id = an.id
AND a.is_parent = 1
AND a.company_id = an.company_id
) AS articleCount,
(
SELECT
av.id,
av.value,
av.sort
av.sort,
(
SELECT COUNT(DISTINCT aav.article_id)
FROM articleattributevalue aav
INNER JOIN article a ON a.id = aav.article_id
WHERE aav.attributevalue_id = av.id
AND a.company_id = an.company_id
) AS articleCount
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC

View File

@@ -3,7 +3,14 @@ SELECT
av.id,
av.attribute_id AS attributeId,
av.value,
av.sort
av.sort,
(
SELECT COUNT(DISTINCT aav.article_id)
FROM articleattributevalue aav
INNER JOIN article a ON a.id = aav.article_id
WHERE aav.attributevalue_id = av.id
AND a.company_id = an.company_id
) AS articleCount
FROM attributevalue av
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE av.attribute_id = @attributeId AND an.company_id = @companyId

View File

@@ -1,12 +1,19 @@
export const LIST_BY_COMPANY = `
SELECT
id,
name,
sort,
exclusive
FROM propertyname
WHERE company_id = @companyId
ORDER BY sort ASC, id ASC
pn.id,
pn.name,
pn.sort,
pn.exclusive,
(
SELECT COUNT(DISTINCT ap.article_id)
FROM articleproperty ap
INNER JOIN article a ON a.id = ap.article_id
WHERE ap.property_id = pn.id
AND a.company_id = pn.company_id
) AS articleCount
FROM propertyname pn
WHERE pn.company_id = @companyId
ORDER BY pn.sort ASC, pn.id ASC
`;
export const LIST_IDS_BY_COMPANY = `

View File

@@ -18,14 +18,32 @@ SELECT
an.name,
an.sort,
an.exclusive,
(
SELECT COUNT(*)
FROM article a
WHERE a.parentattributename_id = an.id
AND a.is_parent = 1
AND a.company_id = an.company_id
) AS articleCount,
(
SELECT COALESCE(json_group_array(json_object(
'id', id,
'value', value,
'sort', sort
'sort', sort,
'articleCount', articleCount
)), '[]')
FROM (
SELECT av.id, av.value, av.sort
SELECT
av.id,
av.value,
av.sort,
(
SELECT COUNT(DISTINCT aav.article_id)
FROM articleattributevalue aav
INNER JOIN article a ON a.id = aav.article_id
WHERE aav.attributevalue_id = av.id
AND a.company_id = an.company_id
) AS articleCount
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
@@ -43,14 +61,32 @@ SELECT
an.name,
an.sort,
an.exclusive,
(
SELECT COUNT(*)
FROM article a
WHERE a.parentattributename_id = an.id
AND a.is_parent = 1
AND a.company_id = an.company_id
) AS articleCount,
(
SELECT COALESCE(json_group_array(json_object(
'id', id,
'value', value,
'sort', sort
'sort', sort,
'articleCount', articleCount
)), '[]')
FROM (
SELECT av.id, av.value, av.sort
SELECT
av.id,
av.value,
av.sort,
(
SELECT COUNT(DISTINCT aav.article_id)
FROM articleattributevalue aav
INNER JOIN article a ON a.id = aav.article_id
WHERE aav.attributevalue_id = av.id
AND a.company_id = an.company_id
) AS articleCount
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC

View File

@@ -18,7 +18,7 @@ export default function list(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean, values: Array<{id: number, value: string, sort: number}>}>>}
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean, articleCount: number, values: Array<{id: number, value: string, sort: number, articleCount: number}>}>>}
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);
@@ -28,7 +28,13 @@ export default function list(provider) {
name: row.name,
sort: row.sort,
exclusive: Boolean(row.exclusive),
values: parseValues(row.valuesJson),
articleCount: Number(row.articleCount ?? 0),
values: parseValues(row.valuesJson).map((v) => ({
id: v.id,
value: v.value,
sort: v.sort,
articleCount: Number(v.articleCount ?? 0),
})),
}));
};
}

View File

@@ -13,7 +13,7 @@ export default function listByAttributeId(provider) {
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} attributeId - The attribute id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, attributeId: number, value: string, sort: number}>>}
* @returns {Promise<Array<{id: number, attributeId: number, value: string, sort: number, articleCount: number}>>}
*/
return async (googleAuthId, attributeId, tx) => {
if (!attributeId || !Number.isInteger(Number(attributeId))) {
@@ -29,6 +29,7 @@ export default function listByAttributeId(provider) {
attributeId: row.attributeId,
value: row.value,
sort: row.sort,
articleCount: Number(row.articleCount ?? 0),
}));
};
}

View File

@@ -12,7 +12,7 @@ export default function list(provider) {
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean}>>}
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean, articleCount: number}>>}
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);
@@ -22,6 +22,7 @@ export default function list(provider) {
name: row.name,
sort: row.sort,
exclusive: Boolean(row.exclusive),
articleCount: Number(row.articleCount ?? 0),
}));
};
}