refactor: introduce database provider abstraction, isolate MSSQL layer
- Add provider contract (src/providers/provider.js): query() -> {rows,
rowsAffected, insertId}, callback transaction(fn), exec() for stored
procs, initialize/purge lifecycle
- Add MssqlProvider (src/providers/mssql/index.js) wrapping an mssql
ConnectionPool with normalized results, tx commit/rollback and
constraint-error classifiers (unique 2627/2601, FK 547)
- LibDashStore constructor now takes a provider instead of a pool
(breaking); adds LibDashStore.create() async factory; keeps pool as
transitional bridge until store modules are converted (see TODO.md)
- Move MSSQL-specific code into the provider: src/sql/* ->
src/providers/mssql/queries/*, src/schema.js ->
src/providers/mssql/schema.js, purge SQL -> purgeSql.js
- Update scripts, generate-schema-graph.js and package.json exports
(./providers/mssql, ./providers/provider)
- Add TODO.md with the remaining provider/SQLite migration plan
No SQL or behavioral changes.
This commit is contained in:
144
src/providers/mssql/queries/category.js
Normal file
144
src/providers/mssql/queries/category.js
Normal file
@@ -0,0 +1,144 @@
|
||||
export const GET_TREE_FOR_COMPANY = `
|
||||
WITH tree AS (
|
||||
SELECT id, parent_id, sort
|
||||
FROM category
|
||||
WHERE id = @rootId
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT c.id, c.parent_id, c.sort
|
||||
FROM category c
|
||||
INNER JOIN tree t ON c.parent_id = t.id
|
||||
)
|
||||
SELECT
|
||||
t.id,
|
||||
t.parent_id AS parentId,
|
||||
t.sort,
|
||||
ct.name,
|
||||
ct.slug,
|
||||
p.hash AS fullHash,
|
||||
p.thumbw AS thumbW,
|
||||
p.thumbh AS thumbH,
|
||||
p.thumbhash AS thumbHash
|
||||
FROM tree t
|
||||
LEFT JOIN categorytext ct
|
||||
ON ct.category_id = t.id AND ct.language_id = @languageId
|
||||
OUTER APPLY (
|
||||
SELECT TOP 1 cp.pic_id
|
||||
FROM categorypic cp
|
||||
WHERE cp.category_id = t.id
|
||||
ORDER BY cp.sort ASC, cp.id ASC
|
||||
) first_pic
|
||||
LEFT JOIN pic p ON p.id = first_pic.pic_id
|
||||
ORDER BY t.sort, t.id
|
||||
`;
|
||||
|
||||
import {
|
||||
ARTICLE_LIST_COLUMNS,
|
||||
ARTICLE_LIST_JOINS,
|
||||
} from './article.js';
|
||||
|
||||
export const GET_CATEGORY_ARTICLES = `
|
||||
SELECT
|
||||
${ARTICLE_LIST_COLUMNS}
|
||||
FROM categoryarticle ca
|
||||
INNER JOIN article a ON a.id = ca.article_id
|
||||
${ARTICLE_LIST_JOINS}
|
||||
WHERE ca.category_id = @categoryId
|
||||
ORDER BY a.id
|
||||
`;
|
||||
|
||||
export const CREATE = `
|
||||
INSERT INTO category (parent_id, sort)
|
||||
OUTPUT INSERTED.id, INSERTED.parent_id AS parentId, INSERTED.sort
|
||||
VALUES (@parentId, @sort)
|
||||
`;
|
||||
|
||||
export const RESOLVE_CATEGORY_ACCESS = `
|
||||
WITH ancestors AS (
|
||||
SELECT id, parent_id
|
||||
FROM category
|
||||
WHERE id = @categoryId
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT c.id, c.parent_id
|
||||
FROM category c
|
||||
INNER JOIN ancestors a ON c.id = a.parent_id
|
||||
),
|
||||
root AS (
|
||||
SELECT TOP 1 id AS rootCategoryId
|
||||
FROM ancestors
|
||||
WHERE parent_id IS NULL
|
||||
)
|
||||
SELECT
|
||||
p.id AS categoryId,
|
||||
r.rootCategoryId,
|
||||
co.id AS companyId,
|
||||
co.default_language_id AS languageId,
|
||||
co.poscustomergroup_id AS posCustomerGroupId,
|
||||
cu.id AS companyuserId
|
||||
FROM category p
|
||||
LEFT JOIN root r ON 1 = 1
|
||||
LEFT JOIN company co ON co.root_category_id = r.rootCategoryId
|
||||
LEFT JOIN companyuser cu
|
||||
ON cu.company_id = co.id AND cu.google_auth_id = @googleAuthId
|
||||
WHERE p.id = @categoryId
|
||||
`;
|
||||
|
||||
export const GET_NEXT_SIBLING_SORT = `
|
||||
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
|
||||
FROM category
|
||||
WHERE parent_id = @parentId
|
||||
`;
|
||||
|
||||
/** Resolve a child category by its parent id and name (language-sensitive). */
|
||||
export const GET_BY_PARENT_AND_NAME = `
|
||||
SELECT c.id
|
||||
FROM category c
|
||||
INNER JOIN categorytext ct
|
||||
ON ct.category_id = c.id AND ct.language_id = @languageId
|
||||
WHERE c.parent_id = @parentId
|
||||
AND ct.name = @name
|
||||
`;
|
||||
|
||||
export const LIST_CHILD_IDS = `
|
||||
SELECT id
|
||||
FROM category
|
||||
WHERE parent_id = @parentId
|
||||
`;
|
||||
|
||||
export const UPDATE_SORT = `
|
||||
UPDATE category
|
||||
SET sort = @sort
|
||||
WHERE id = @id AND parent_id = @parentId
|
||||
`;
|
||||
|
||||
/** True if @id is an ancestor of @parentId (would create a cycle when moving). */
|
||||
export const IS_ANCESTOR = `
|
||||
WITH ancestors AS (
|
||||
SELECT id, parent_id
|
||||
FROM category
|
||||
WHERE id = @parentId
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT c.id, c.parent_id
|
||||
FROM category c
|
||||
INNER JOIN ancestors a ON c.id = a.parent_id
|
||||
)
|
||||
SELECT id
|
||||
FROM ancestors
|
||||
WHERE id = @id
|
||||
`;
|
||||
|
||||
export const MOVE = `
|
||||
UPDATE category
|
||||
SET parent_id = @parentId, sort = @sort
|
||||
WHERE id = @id
|
||||
`;
|
||||
|
||||
export const DELETE = `
|
||||
DELETE FROM category
|
||||
WHERE id = @id
|
||||
`;
|
||||
Reference in New Issue
Block a user