binary.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { MySqlColumn, MySqlColumnBuilder } from "./common.js";
  4. class MySqlBinaryBuilder extends MySqlColumnBuilder {
  5. static [entityKind] = "MySqlBinaryBuilder";
  6. constructor(name, length) {
  7. super(name, "string", "MySqlBinary");
  8. this.config.length = length;
  9. }
  10. /** @internal */
  11. build(table) {
  12. return new MySqlBinary(table, this.config);
  13. }
  14. }
  15. class MySqlBinary extends MySqlColumn {
  16. static [entityKind] = "MySqlBinary";
  17. length = this.config.length;
  18. mapFromDriverValue(value) {
  19. if (typeof value === "string") return value;
  20. if (Buffer.isBuffer(value)) return value.toString();
  21. const str = [];
  22. for (const v of value) {
  23. str.push(v === 49 ? "1" : "0");
  24. }
  25. return str.join("");
  26. }
  27. getSQLType() {
  28. return this.length === void 0 ? `binary` : `binary(${this.length})`;
  29. }
  30. }
  31. function binary(a, b = {}) {
  32. const { name, config } = getColumnNameAndConfig(a, b);
  33. return new MySqlBinaryBuilder(name, config.length);
  34. }
  35. export {
  36. MySqlBinary,
  37. MySqlBinaryBuilder,
  38. binary
  39. };
  40. //# sourceMappingURL=binary.js.map