Files
dashlibstorage/src/store/supplier/ensureLieferantenGroup.js
seb 53a20c4353 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.
2026-09-20 13:51:01 +02:00

34 lines
924 B
JavaScript

import {
CREATE,
GET_BY_COMPANY_AND_NAME,
GET_NEXT_SORT,
} from '../../providers/mssql/queries/customergroup.js';
import { SUPPLIER_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
/** Ensure company has customergroup "Lieferanten"; return its id. */
export default async function ensureLieferantenGroup(pool, companyId, tx) {
const req = () => (tx ?? pool).request();
const existing = await req()
.input('companyId', companyId)
.input('name', SUPPLIER_GROUP_NAME)
.query(GET_BY_COMPANY_AND_NAME);
if (existing.recordset[0]) {
return existing.recordset[0].id;
}
const next = await req()
.input('companyId', companyId)
.query(GET_NEXT_SORT);
const sort = next.recordset[0].sort;
const created = await req()
.input('companyId', companyId)
.input('name', SUPPLIER_GROUP_NAME)
.input('sort', sort)
.query(CREATE);
return created.recordset[0].id;
}