Files
dashlibstorage/README.md
2026-07-31 07:42:19 +02:00

4.8 KiB

libdashstore

MSSQL schema + domain API for Dash. One class, methods mirror the file path under src/store/.

import sql from 'mssql';
import LibDashStore from 'libdashstore';

const pool = await sql.connect({ /* … */ });
const client = await new LibDashStore(pool); // runs initialize() (ensure schema, no purge)

SQL lives in src/sql/<table>.js. Schema DDL: src/schema.js / import 'libdashstore/schema'.

ID fields in the JS API use camelCase …Id (DB columns stay snake_case).


Schema

Method Description
initialize() Runs all SCHEMA_SCRIPTS (IF NOT EXISTS). Does not purge.
purge() Drops all known tables + number procedure.

company

Method Signature Returns
create ({ iso639, name, taxrate, reducedtaxrate, authId }) { id, rootCategoryId, languageId, taxrateId, reducedtaxrateId, taxfreerateId }
update (id, { iso639, name, taxrate, reducedtaxrate }) { id, rootCategoryId, languageId, taxrateId, reducedtaxrateId, taxfreerateId }
delete (companyId) void
list (authId) { id, name, rootCategoryId, languageId, taxrate, reducedtaxrate, taxfreerate }[]

create (transaction)

For POST /api/company:

  1. Fails if name is taken (Company name not available)
  2. Ensures language by iso639 (get or create)
  3. Ensures tax rows for taxrate, reducedtaxrate, and 0 → taxfreerateId
  4. Creates root category (parentId = null)
  5. Inserts company with all FKs set
  6. Links creator as companyuser (authId → company_id)

list

Returns only companies where authId has a companyuser row.

update (transaction)

For PATCH /api/company/:id:

  1. Fails if company missing / name taken by another company
  2. Ensures lang + tax rates
  3. Updates name, default_language_id, taxrate, reducedtaxrate
    (taxfreerateId / root category unchanged)

google_auth

Method Signature Returns
add (email) id
getByEmail (email) { id, email, createdAt, selectedCompanyId } | undefined
listAllowedEmails () { id, email, createdAt, selectedCompanyId }[]
removeAllowedEmail (email) void
setSelectedCompany (authId, companyId) void

Backend mapping:

  • Login: getByEmail(email)
  • CLI user manager: listAllowedEmails() / removeAllowedEmail(email) / add(email)
  • PUT /api/auth/selected-company: setSelectedCompany(authId, companyId)

companyuser

Method Signature Returns
add (companyId, authId) id
getByEmail (email) id[]
delete (id) void

lang

Method Signature Returns
create (iso639) id
getByIso639 (iso639) { id, iso639 } | undefined
ensure (iso639, tx?) id
list () { id, iso639 }[]
delete (id) void

tax

Method Signature Returns
create (rate) id
getByRate (rate) { id, rate } | undefined
ensure (rate, tx?) id
list () { id, rate }[]

category

Method Signature Returns
create ({ parentId, sort }) id
updateParentId (id, parentId) void
delete (id) void — blocked by FK if categoryarticle (or child categories) still reference it
getTree (rootId) { id, parentId, sort }[]
getTreeForCompany (authId, companyId) { id, parentId, sort }[] — membership check + company root

getTreeForCompany throws Not authorized if authId is not a companyuser of companyId, and Company not found if the company is missing or has no root_category_id. Reuses GET_TREE after access check.

getTree(rootId) stays for low-level use (tests, internal) when the root id is already known.


Backend route cheatsheet

Route Store call
POST /api/company company.create({ iso639, name, taxrate, reducedtaxrate, authId })
GET /api/company company.list(authId)
PATCH /api/company/:id company.update(id, { iso639, name, taxrate, reducedtaxrate })
DELETE /api/company/:id company.delete(id)
PUT /api/auth/selected-company google_auth.setSelectedCompany(authId, companyId)
GET /api/category/tree?companyId= category.getTreeForCompany(authId, companyId)

Scripts

npm run purge-db      # drop all tables + procedure (wipe data)
npm run reset-db      # purge + init-schema (empty DB, fresh schema)
npm run init-schema   # ensure schema only (does NOT delete data)
npm run test:api      # interactive API tester
npm run schema-graph  # regenerate schema-graph.svg

Env: MSSQL_SERVER, MSSQL_DATABASE, MSSQL_USER, MSSQL_PASSWORD.