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:
174
src/providers/mssql/queries/articlesupplier.js
Normal file
174
src/providers/mssql/queries/articlesupplier.js
Normal file
@@ -0,0 +1,174 @@
|
||||
export const LIST_BY_ARTICLE_ID = `
|
||||
SELECT
|
||||
asup.id,
|
||||
asup.article_id AS articleId,
|
||||
asup.entity_id AS entityId,
|
||||
e.name AS supplierName,
|
||||
asup.sku,
|
||||
asup.pu,
|
||||
asup.stock,
|
||||
asup.sort,
|
||||
asup.price_id AS priceId,
|
||||
p.net AS priceNet,
|
||||
p.tax_id AS priceTaxId,
|
||||
t.rate AS priceTaxRate
|
||||
FROM articlesupplier asup
|
||||
INNER JOIN entity e ON e.id = asup.entity_id
|
||||
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
|
||||
LEFT JOIN price p ON p.id = asup.price_id
|
||||
LEFT JOIN tax t ON t.id = p.tax_id
|
||||
WHERE asup.article_id = @articleId
|
||||
AND cg.company_id = @companyId
|
||||
ORDER BY asup.sort ASC, asup.id ASC;
|
||||
`;
|
||||
|
||||
export const GET_BY_ID_AND_ARTICLE_ID = `
|
||||
SELECT
|
||||
asup.id,
|
||||
asup.article_id AS articleId,
|
||||
asup.entity_id AS entityId,
|
||||
e.name AS supplierName,
|
||||
asup.sku,
|
||||
asup.pu,
|
||||
asup.stock,
|
||||
asup.sort,
|
||||
asup.price_id AS priceId,
|
||||
p.net AS priceNet,
|
||||
p.tax_id AS priceTaxId,
|
||||
t.rate AS priceTaxRate
|
||||
FROM articlesupplier asup
|
||||
INNER JOIN entity e ON e.id = asup.entity_id
|
||||
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
|
||||
LEFT JOIN price p ON p.id = asup.price_id
|
||||
LEFT JOIN tax t ON t.id = p.tax_id
|
||||
WHERE asup.id = @id
|
||||
AND asup.article_id = @articleId
|
||||
AND cg.company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const CHECK_EXISTS = `
|
||||
SELECT TOP 1 id
|
||||
FROM articlesupplier
|
||||
WHERE article_id = @articleId AND entity_id = @entityId;
|
||||
`;
|
||||
|
||||
export const GET_NEXT_SORT = `
|
||||
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
|
||||
FROM articlesupplier
|
||||
WHERE article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const CREATE = `
|
||||
INSERT INTO articlesupplier (article_id, entity_id, price_id, sku, stock, pu, sort)
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (@articleId, @entityId, @priceId, @sku, @stock, @pu, @sort);
|
||||
`;
|
||||
|
||||
export const UPDATE = `
|
||||
UPDATE articlesupplier
|
||||
SET
|
||||
sku = @sku,
|
||||
stock = @stock,
|
||||
pu = @pu,
|
||||
price_id = @priceId,
|
||||
sort = @sort
|
||||
WHERE id = @id AND article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const DELETE_BY_ID_AND_ARTICLE_ID = `
|
||||
DELETE FROM articlesupplier
|
||||
WHERE id = @id AND article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const LIST_IDS_BY_ARTICLE = `
|
||||
SELECT id
|
||||
FROM articlesupplier
|
||||
WHERE article_id = @articleId
|
||||
ORDER BY sort ASC, id ASC;
|
||||
`;
|
||||
|
||||
export const SET_TEMP_SORTS = `
|
||||
UPDATE articlesupplier
|
||||
SET sort = -id
|
||||
WHERE article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const UPDATE_SORT = `
|
||||
UPDATE articlesupplier
|
||||
SET sort = @sort
|
||||
WHERE id = @id AND article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const UPDATE_PRICE = `
|
||||
UPDATE price
|
||||
SET
|
||||
net = @net,
|
||||
tax_id = @taxId
|
||||
WHERE id = @id AND article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const DELETE_PRICE = `
|
||||
DELETE FROM price
|
||||
WHERE id = @id AND article_id = @articleId;
|
||||
`;
|
||||
|
||||
export const GET_COMPANY_DEFAULT_TAX = `
|
||||
SELECT taxrate
|
||||
FROM company
|
||||
WHERE id = @companyId;
|
||||
`;
|
||||
|
||||
export const LIST_BY_ENTITY_AND_SKUS = `
|
||||
SELECT
|
||||
asup.id,
|
||||
asup.article_id AS articleId,
|
||||
asup.entity_id AS entityId,
|
||||
e.name AS supplierName,
|
||||
asup.sku,
|
||||
asup.pu,
|
||||
asup.stock,
|
||||
asup.sort,
|
||||
asup.price_id AS priceId,
|
||||
p.net AS priceNet,
|
||||
p.tax_id AS priceTaxId,
|
||||
t.rate AS priceTaxRate,
|
||||
a.id AS a_id,
|
||||
a.sku AS a_sku,
|
||||
at.name AS a_name,
|
||||
at.slug AS a_slug,
|
||||
(
|
||||
SELECT
|
||||
ap.sort,
|
||||
p.hash,
|
||||
p.thumbhash,
|
||||
p.thumbw,
|
||||
p.thumbh
|
||||
FROM articlepic ap
|
||||
INNER JOIN pic p ON p.id = ap.pic_id
|
||||
WHERE ap.article_id = a.id
|
||||
ORDER BY ap.sort ASC, ap.id ASC
|
||||
FOR JSON PATH
|
||||
) AS photosJson,
|
||||
(
|
||||
SELECT ae.ean
|
||||
FROM articleean ae
|
||||
WHERE ae.article_id = a.id
|
||||
ORDER BY ae.ean
|
||||
FOR JSON PATH
|
||||
) AS eansJson
|
||||
FROM articlesupplier asup
|
||||
INNER JOIN entity e ON e.id = asup.entity_id
|
||||
INNER JOIN customergroup cg ON cg.id = e.customergroup_id
|
||||
INNER JOIN article a ON a.id = asup.article_id
|
||||
LEFT JOIN articletext at ON at.article_id = a.id AND at.language_id = @languageId
|
||||
LEFT JOIN price p ON p.id = asup.price_id
|
||||
LEFT JOIN tax t ON t.id = p.tax_id
|
||||
WHERE asup.entity_id = @entityId
|
||||
AND cg.company_id = @companyId
|
||||
AND (
|
||||
@skusJson IS NULL
|
||||
OR a.sku IN (SELECT value FROM OPENJSON(@skusJson))
|
||||
OR asup.sku IN (SELECT value FROM OPENJSON(@skusJson))
|
||||
)
|
||||
ORDER BY asup.sort ASC, asup.id ASC;
|
||||
`;
|
||||
Reference in New Issue
Block a user