import _ from 'lodash'; // Abbildung snake_case (PG) <-> camelCase (Repository), eine Funktion je Tabelle (ADR 0002 §3). const BREW_COLUMNS = { shareId : 'share_id', editId : 'edit_id', googleId : 'google_id', title : 'title', text : 'text', pageCount : 'page_count', description : 'description', tags : 'tags', systems : 'systems', lang : 'lang', renderer : 'renderer', authors : 'authors', invitedAuthors : 'invited_authors', published : 'published', thumbnail : 'thumbnail', createdAt : 'created_at', updatedAt : 'updated_at', lastViewed : 'last_viewed', views : 'views', version : 'version', lock : 'lock' }; const ARRAY_FIELDS = ['tags', 'systems', 'authors', 'invitedAuthors']; const INT_FIELDS = ['pageCount', 'views', 'version']; const DATE_FIELDS = ['createdAt', 'updatedAt', 'lastViewed']; // Spalten ohne NOT NULL: explizit `undefined` heißt "auf NULL setzen" (Mongo: $unset) const NULLABLE = ['googleId', 'systems', 'lock']; // NOT NULL mit Default: explizit `undefined` setzt den Default zurück (Stub-Brews: text) const RESET_TO_DEFAULT = { text: '' }; const invert = (map)=>Object.fromEntries(Object.entries(map).map(([k, v])=>[v, k])); const BREW_FIELDS_BY_COLUMN = invert(BREW_COLUMNS); const toArray = (v)=>{ if(Array.isArray(v)) return v; return (v === undefined || v === null || v === '') ? [] : [v]; }; const coerce = (field, value)=>{ if(ARRAY_FIELDS.includes(field)) return toArray(value); if(INT_FIELDS.includes(field)) return Number.isFinite(Number(value)) ? Math.trunc(Number(value)) : undefined; if(DATE_FIELDS.includes(field)) return value instanceof Date ? value : new Date(value); if(field === 'published') return !!value; return value; }; // mode 'insert': nur gesetzte Felder (Defaults greifen), 'update': undefined → NULL/Default export const brewToRow = (brew, mode = 'insert')=>{ const row = {}; for (const [field, column] of Object.entries(BREW_COLUMNS)) { if(!(field in brew)) continue; const value = brew[field]; if(value === undefined || value === null) { if(mode !== 'update') continue; if(NULLABLE.includes(field)) row[column] = null; else if(field in RESET_TO_DEFAULT) row[column] = RESET_TO_DEFAULT[field]; continue; } const coerced = coerce(field, value); if(coerced !== undefined) row[column] = coerced; } return row; }; export const rowToBrew = (row, { fields } = {})=>{ if(!row) return null; const brew = { id: row.id }; for (const [column, value] of Object.entries(row)) { const field = BREW_FIELDS_BY_COLUMN[column]; if(!field) continue; if(NULLABLE.includes(field) && value === null) continue; // wie Mongo: Feld fehlt brew[field] = value; } return fields ? _.pick(brew, ['id', ...fields]) : brew; }; export const BREW_SELECT_COLUMNS = Object.values(BREW_COLUMNS); export const BREW_LIST_COLUMNS = BREW_SELECT_COLUMNS.filter((c)=>c !== 'text'); export const columnFor = (field)=>BREW_COLUMNS[field]; // --- notifications --- export const notificationToRow = (n)=>{ const row = { dismiss_key: n.dismissKey }; for (const [field, column] of [['title', 'title'], ['text', 'text'], ['startAt', 'start_at'], ['stopAt', 'stop_at'], ['createdAt', 'created_at']]) if(n[field] !== undefined && n[field] !== null) row[column] = n[field]; return row; }; export const rowToNotification = (r)=>(!r ? null : { id : r.id, dismissKey : r.dismiss_key, title : r.title, text : r.text, createdAt : r.created_at, startAt : r.start_at, stopAt : r.stop_at }); // --- users --- export const rowToUser = (r)=>(!r ? null : { id : r.id, username : r.username, oidcIssuer : r.oidc_issuer ?? null, oidcSub : r.oidc_sub ?? null, email : r.email ?? null, emailVerified : !!r.email_verified, displayName : r.display_name ?? null, createdAt : r.created_at, lastLoginAt : r.last_login_at ?? null });