custom.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { PgColumn, PgColumnBuilder } from "./common.js";
  4. class PgCustomColumnBuilder extends PgColumnBuilder {
  5. static [entityKind] = "PgCustomColumnBuilder";
  6. constructor(name, fieldConfig, customTypeParams) {
  7. super(name, "custom", "PgCustomColumn");
  8. this.config.fieldConfig = fieldConfig;
  9. this.config.customTypeParams = customTypeParams;
  10. }
  11. /** @internal */
  12. build(table) {
  13. return new PgCustomColumn(
  14. table,
  15. this.config
  16. );
  17. }
  18. }
  19. class PgCustomColumn extends PgColumn {
  20. static [entityKind] = "PgCustomColumn";
  21. sqlName;
  22. mapTo;
  23. mapFrom;
  24. constructor(table, config) {
  25. super(table, config);
  26. this.sqlName = config.customTypeParams.dataType(config.fieldConfig);
  27. this.mapTo = config.customTypeParams.toDriver;
  28. this.mapFrom = config.customTypeParams.fromDriver;
  29. }
  30. getSQLType() {
  31. return this.sqlName;
  32. }
  33. mapFromDriverValue(value) {
  34. return typeof this.mapFrom === "function" ? this.mapFrom(value) : value;
  35. }
  36. mapToDriverValue(value) {
  37. return typeof this.mapTo === "function" ? this.mapTo(value) : value;
  38. }
  39. }
  40. function customType(customTypeParams) {
  41. return (a, b) => {
  42. const { name, config } = getColumnNameAndConfig(a, b);
  43. return new PgCustomColumnBuilder(name, config, customTypeParams);
  44. };
  45. }
  46. export {
  47. PgCustomColumn,
  48. PgCustomColumnBuilder,
  49. customType
  50. };
  51. //# sourceMappingURL=custom.js.map