This commit is contained in:
seb
2026-09-29 16:16:05 +02:00
parent 26d2f83d64
commit ae5d5cc6f8
40 changed files with 2139 additions and 0 deletions

View File

@@ -1,5 +1,10 @@
/** Drop order = reverse of schema create dependency order. */
export const TABLES = [
'articleattributevalue',
'articleproperty',
'attributevalue',
'attributename',
'propertyname',
'manifestattachment',
'labeltemplate',
'file',

View File

@@ -0,0 +1,41 @@
export const LIST_BY_ARTICLE_ID = `
SELECT
aav.id,
aav.article_id AS articleId,
an.id AS attributeId,
an.name AS attributeName,
an.exclusive,
av.id AS attributeValueId,
av.value,
aav.sort
FROM articleattributevalue aav
INNER JOIN attributevalue av ON av.id = aav.attributevalue_id
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE aav.article_id = @articleId
ORDER BY an.sort ASC, an.id ASC, aav.sort ASC, aav.id ASC
`;
export const DELETE_BY_ARTICLE_ID = `
DELETE FROM articleattributevalue
WHERE article_id = @articleId
`;
export const INSERT_ROW = `
INSERT INTO articleattributevalue (article_id, attributevalue_id, sort)
OUTPUT INSERTED.id, INSERTED.article_id AS articleId, INSERTED.attributevalue_id AS attributeValueId, INSERTED.sort
VALUES (@articleId, @attributeValueId, @sort)
`;
export const GET_VALUE_DETAIL = `
SELECT
av.id AS attributeValueId,
av.value,
av.sort,
an.id AS attributeId,
an.name AS attributeName,
an.exclusive,
an.company_id AS companyId
FROM attributevalue av
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE av.id = @attributeValueId
`;

View File

@@ -0,0 +1,29 @@
export const LIST_BY_ARTICLE_ID = `
SELECT
ap.id,
ap.article_id AS articleId,
ap.property_id AS propertyId,
pn.name AS name,
ap.value,
ap.sort
FROM articleproperty ap
INNER JOIN propertyname pn ON pn.id = ap.property_id
WHERE ap.article_id = @articleId
ORDER BY ap.sort ASC, ap.id ASC
`;
export const DELETE_BY_ARTICLE_ID = `
DELETE FROM articleproperty
WHERE article_id = @articleId
`;
export const DELETE_BY_ARTICLE_AND_PROPERTY_ID = `
DELETE FROM articleproperty
WHERE article_id = @articleId AND property_id = @propertyId
`;
export const INSERT_ROW = `
INSERT INTO articleproperty (article_id, property_id, value, sort)
OUTPUT INSERTED.id, INSERTED.article_id AS articleId, INSERTED.property_id AS propertyId, INSERTED.value, INSERTED.sort
VALUES (@articleId, @propertyId, @value, @sort)
`;

View File

@@ -0,0 +1,88 @@
export const LIST_BY_COMPANY = `
SELECT
an.id,
an.name,
an.sort,
an.exclusive,
(
SELECT
av.id,
av.value,
av.sort
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
FOR JSON PATH
) AS valuesJson
FROM attributename an
WHERE an.company_id = @companyId
ORDER BY an.sort ASC, an.id ASC
`;
export const LIST_IDS_BY_COMPANY = `
SELECT id
FROM attributename
WHERE company_id = @companyId
ORDER BY sort ASC, id ASC
`;
export const GET_BY_COMPANY_AND_ID = `
SELECT
an.id,
an.company_id,
an.name,
an.sort,
an.exclusive,
(
SELECT
av.id,
av.value,
av.sort
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
FOR JSON PATH
) AS valuesJson
FROM attributename an
WHERE an.id = @id AND an.company_id = @companyId
`;
export const GET_BY_COMPANY_AND_NAME = `
SELECT
id,
company_id,
name,
sort,
exclusive
FROM attributename
WHERE name = @name AND company_id = @companyId
`;
export const GET_NEXT_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM attributename
WHERE company_id = @companyId
`;
export const CREATE = `
INSERT INTO attributename (company_id, name, sort, exclusive)
OUTPUT INSERTED.id, INSERTED.name, INSERTED.sort, INSERTED.exclusive
VALUES (@companyId, @name, @sort, @exclusive)
`;
export const UPDATE_BY_ID = `
UPDATE attributename
SET name = @name, sort = @sort, exclusive = @exclusive
WHERE id = @id AND company_id = @companyId
`;
export const UPDATE_SORT = `
UPDATE attributename
SET sort = @sort
WHERE id = @id AND company_id = @companyId
`;
export const DELETE_BY_COMPANY_AND_ID = `
DELETE FROM attributename
WHERE id = @id AND company_id = @companyId
`;

View File

@@ -0,0 +1,70 @@
export const LIST_BY_ATTRIBUTE_ID = `
SELECT
av.id,
av.attribute_id AS attributeId,
av.value,
av.sort
FROM attributevalue av
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE av.attribute_id = @attributeId AND an.company_id = @companyId
ORDER BY av.sort ASC, av.id ASC
`;
export const LIST_IDS_BY_ATTRIBUTE_ID = `
SELECT av.id
FROM attributevalue av
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE av.attribute_id = @attributeId AND an.company_id = @companyId
ORDER BY av.sort ASC, av.id ASC
`;
export const GET_BY_ID_AND_COMPANY = `
SELECT
av.id,
av.attribute_id AS attributeId,
av.value,
av.sort,
an.company_id AS companyId
FROM attributevalue av
INNER JOIN attributename an ON an.id = av.attribute_id
WHERE av.id = @id AND an.company_id = @companyId
`;
export const GET_BY_ATTRIBUTE_AND_VALUE = `
SELECT
av.id,
av.attribute_id AS attributeId,
av.value,
av.sort
FROM attributevalue av
WHERE av.attribute_id = @attributeId AND av.value = @value
`;
export const GET_NEXT_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM attributevalue
WHERE attribute_id = @attributeId
`;
export const CREATE = `
INSERT INTO attributevalue (attribute_id, value, sort)
OUTPUT INSERTED.id, INSERTED.attribute_id AS attributeId, INSERTED.value, INSERTED.sort
VALUES (@attributeId, @value, @sort)
`;
export const UPDATE_BY_ID = `
UPDATE attributevalue
SET value = @value, sort = @sort
WHERE id = @id
`;
export const UPDATE_SORT = `
UPDATE attributevalue
SET sort = @sort
WHERE id = @id
`;
export const DELETE_BY_ID = `
DELETE FROM attributevalue
WHERE id = @id
`;

View File

@@ -0,0 +1,65 @@
export const LIST_BY_COMPANY = `
SELECT
id,
name,
sort
FROM propertyname
WHERE company_id = @companyId
ORDER BY sort ASC, id ASC
`;
export const LIST_IDS_BY_COMPANY = `
SELECT id
FROM propertyname
WHERE company_id = @companyId
ORDER BY sort ASC, id ASC
`;
export const GET_BY_COMPANY_AND_ID = `
SELECT
id,
company_id,
name,
sort
FROM propertyname
WHERE id = @id AND company_id = @companyId
`;
export const GET_BY_COMPANY_AND_NAME = `
SELECT
id,
company_id,
name,
sort
FROM propertyname
WHERE name = @name AND company_id = @companyId
`;
export const GET_NEXT_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM propertyname
WHERE company_id = @companyId
`;
export const CREATE = `
INSERT INTO propertyname (company_id, name, sort)
OUTPUT INSERTED.id, INSERTED.name, INSERTED.sort
VALUES (@companyId, @name, @sort)
`;
export const UPDATE_BY_ID = `
UPDATE propertyname
SET name = @name, sort = @sort
WHERE id = @id AND company_id = @companyId
`;
export const UPDATE_SORT = `
UPDATE propertyname
SET sort = @sort
WHERE id = @id AND company_id = @companyId
`;
export const DELETE_BY_COMPANY_AND_ID = `
DELETE FROM propertyname
WHERE id = @id AND company_id = @companyId
`;

View File

@@ -894,6 +894,80 @@ END
');
`;
export const INIT_PROPERTYNAME = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'propertyname')
BEGIN
CREATE TABLE propertyname (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
name NVARCHAR(255) NOT NULL,
sort INT NOT NULL CONSTRAINT DF_propertyname_sort DEFAULT 0,
CONSTRAINT FK_propertyname_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
CONSTRAINT UQ_propertyname_company_name UNIQUE (company_id, name)
);
END
`;
export const INIT_ARTICLEPROPERTY = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'articleproperty')
BEGIN
CREATE TABLE articleproperty (
id INT IDENTITY(1,1) PRIMARY KEY,
article_id INT NOT NULL,
property_id INT NOT NULL,
value NVARCHAR(MAX) NOT NULL,
sort INT NOT NULL CONSTRAINT DF_articleproperty_sort DEFAULT 0,
CONSTRAINT FK_articleproperty_article FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
CONSTRAINT FK_articleproperty_property FOREIGN KEY (property_id) REFERENCES propertyname(id) ON DELETE CASCADE,
CONSTRAINT UQ_articleproperty_article_property UNIQUE (article_id, property_id)
);
END
`;
export const INIT_ATTRIBUTENAME = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'attributename')
BEGIN
CREATE TABLE attributename (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
name NVARCHAR(255) NOT NULL,
sort INT NOT NULL CONSTRAINT DF_attributename_sort DEFAULT 0,
exclusive BIT NOT NULL CONSTRAINT DF_attributename_exclusive DEFAULT 0,
CONSTRAINT FK_attributename_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
CONSTRAINT UQ_attributename_company_name UNIQUE (company_id, name)
);
END
`;
export const INIT_ATTRIBUTEVALUE = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'attributevalue')
BEGIN
CREATE TABLE attributevalue (
id INT IDENTITY(1,1) PRIMARY KEY,
attribute_id INT NOT NULL,
value NVARCHAR(255) NOT NULL,
sort INT NOT NULL CONSTRAINT DF_attributevalue_sort DEFAULT 0,
CONSTRAINT FK_attributevalue_attribute FOREIGN KEY (attribute_id) REFERENCES attributename(id) ON DELETE CASCADE,
CONSTRAINT UQ_attributevalue_attribute_value UNIQUE (attribute_id, value)
);
END
`;
export const INIT_ARTICLEATTRIBUTEVALUE = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'articleattributevalue')
BEGIN
CREATE TABLE articleattributevalue (
id INT IDENTITY(1,1) PRIMARY KEY,
article_id INT NOT NULL,
attributevalue_id INT NOT NULL,
sort INT NOT NULL CONSTRAINT DF_articleattributevalue_sort DEFAULT 0,
CONSTRAINT FK_articleattributevalue_article FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
CONSTRAINT FK_articleattributevalue_attributevalue FOREIGN KEY (attributevalue_id) REFERENCES attributevalue(id) ON DELETE CASCADE,
CONSTRAINT UQ_articleattributevalue_article_attributevalue UNIQUE (article_id, attributevalue_id)
);
END
`;
/** DDL scripts in dependency order for schema init. */
export const SCHEMA_SCRIPTS = [
INIT_META,
@@ -926,6 +1000,11 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLENAME,
INIT_PRICE,
INIT_ARTICLE_SUPPLIER,
INIT_PROPERTYNAME,
INIT_ARTICLEPROPERTY,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLEATTRIBUTEVALUE,
INIT_MANIFEST,
INIT_MANIFESTARTICLE,
INIT_WAREHOUSE,

View File

@@ -0,0 +1,10 @@
import * as source from '../../mssql/queries/articleattributevalue.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'articleattributevalue');
export const {
LIST_BY_ARTICLE_ID,
DELETE_BY_ARTICLE_ID,
INSERT_ROW,
GET_VALUE_DETAIL,
} = queries;

View File

@@ -0,0 +1,10 @@
import * as source from '../../mssql/queries/articleproperty.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'articleproperty');
export const {
LIST_BY_ARTICLE_ID,
DELETE_BY_ARTICLE_ID,
DELETE_BY_ARTICLE_AND_PROPERTY_ID,
INSERT_ROW,
} = queries;

View File

@@ -0,0 +1,61 @@
import * as source from '../../mssql/queries/attributename.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'attributename');
export const {
LIST_IDS_BY_COMPANY,
GET_BY_COMPANY_AND_NAME,
GET_NEXT_SORT,
CREATE,
UPDATE_BY_ID,
UPDATE_SORT,
DELETE_BY_COMPANY_AND_ID,
} = queries;
export const LIST_BY_COMPANY = `
SELECT
an.id,
an.name,
an.sort,
an.exclusive,
(
SELECT COALESCE(json_group_array(json_object(
'id', id,
'value', value,
'sort', sort
)), '[]')
FROM (
SELECT av.id, av.value, av.sort
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
)
) AS valuesJson
FROM attributename an
WHERE an.company_id = @companyId
ORDER BY an.sort ASC, an.id ASC
`;
export const GET_BY_COMPANY_AND_ID = `
SELECT
an.id,
an.company_id,
an.name,
an.sort,
an.exclusive,
(
SELECT COALESCE(json_group_array(json_object(
'id', id,
'value', value,
'sort', sort
)), '[]')
FROM (
SELECT av.id, av.value, av.sort
FROM attributevalue av
WHERE av.attribute_id = an.id
ORDER BY av.sort ASC, av.id ASC
)
) AS valuesJson
FROM attributename an
WHERE an.id = @id AND an.company_id = @companyId
`;

View File

@@ -0,0 +1,15 @@
import * as source from '../../mssql/queries/attributevalue.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'attributevalue');
export const {
LIST_BY_ATTRIBUTE_ID,
LIST_IDS_BY_ATTRIBUTE_ID,
GET_BY_ID_AND_COMPANY,
GET_BY_ATTRIBUTE_AND_VALUE,
GET_NEXT_SORT,
CREATE,
UPDATE_BY_ID,
UPDATE_SORT,
DELETE_BY_ID,
} = queries;

View File

@@ -0,0 +1,15 @@
import * as source from '../../mssql/queries/propertyname.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'propertyname');
export const {
LIST_BY_COMPANY,
LIST_IDS_BY_COMPANY,
GET_BY_COMPANY_AND_ID,
GET_BY_COMPANY_AND_NAME,
GET_NEXT_SORT,
CREATE,
UPDATE_BY_ID,
UPDATE_SORT,
DELETE_BY_COMPANY_AND_ID,
} = queries;

View File

@@ -621,6 +621,65 @@ BEGIN
END;
`;
export const INIT_PROPERTYNAME = `
CREATE TABLE IF NOT EXISTS propertyname (
id INTEGER PRIMARY KEY,
company_id INTEGER NOT NULL,
name TEXT NOT NULL,
sort INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
UNIQUE (company_id, name)
);
`;
export const INIT_ARTICLEPROPERTY = `
CREATE TABLE IF NOT EXISTS articleproperty (
id INTEGER PRIMARY KEY,
article_id INTEGER NOT NULL,
property_id INTEGER NOT NULL,
value TEXT NOT NULL,
sort INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
FOREIGN KEY (property_id) REFERENCES propertyname(id) ON DELETE CASCADE,
UNIQUE (article_id, property_id)
);
`;
export const INIT_ATTRIBUTENAME = `
CREATE TABLE IF NOT EXISTS attributename (
id INTEGER PRIMARY KEY,
company_id INTEGER NOT NULL,
name TEXT NOT NULL,
sort INTEGER NOT NULL DEFAULT 0,
exclusive INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
UNIQUE (company_id, name)
);
`;
export const INIT_ATTRIBUTEVALUE = `
CREATE TABLE IF NOT EXISTS attributevalue (
id INTEGER PRIMARY KEY,
attribute_id INTEGER NOT NULL,
value TEXT NOT NULL,
sort INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (attribute_id) REFERENCES attributename(id) ON DELETE CASCADE,
UNIQUE (attribute_id, value)
);
`;
export const INIT_ARTICLEATTRIBUTEVALUE = `
CREATE TABLE IF NOT EXISTS articleattributevalue (
id INTEGER PRIMARY KEY,
article_id INTEGER NOT NULL,
attributevalue_id INTEGER NOT NULL,
sort INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (article_id) REFERENCES article(id) ON DELETE CASCADE,
FOREIGN KEY (attributevalue_id) REFERENCES attributevalue(id) ON DELETE CASCADE,
UNIQUE (article_id, attributevalue_id)
);
`;
/** DDL scripts in dependency order for SQLite schema initialization. */
export const SCHEMA_SCRIPTS = [
INIT_META,
@@ -652,6 +711,11 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLENAME,
INIT_PRICE,
INIT_ARTICLE_SUPPLIER,
INIT_PROPERTYNAME,
INIT_ARTICLEPROPERTY,
INIT_ATTRIBUTENAME,
INIT_ATTRIBUTEVALUE,
INIT_ARTICLEATTRIBUTEVALUE,
INIT_MANIFEST,
INIT_MANIFESTARTICLE,
INIT_WAREHOUSE,