import { ValueTransformer } from "typeorm";

// Convention used across the ERP schema (maeplu, maetie, maecli, etc.): boolean
// flags are stored as char(1) 'S'/'N', not as tinyint. Without this transformer,
// TypeORM/mysql2 return the raw string, and `Boolean("N")` evaluates to `true`
// because any non-empty string is truthy — every flag reads as true regardless
// of the stored value.
export const charBooleanTransformer: ValueTransformer = {
  to: (value?: boolean | null): string | null => {
    if (value === undefined || value === null) {
      return null;
    }
    return value ? "S" : "N";
  },
  from: (value?: string | null): boolean => value === "S",
};
