jsonb.js 913 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { entityKind } from "../../entity.js";
  2. import { PgColumn, PgColumnBuilder } from "./common.js";
  3. class PgJsonbBuilder extends PgColumnBuilder {
  4. static [entityKind] = "PgJsonbBuilder";
  5. constructor(name) {
  6. super(name, "json", "PgJsonb");
  7. }
  8. /** @internal */
  9. build(table) {
  10. return new PgJsonb(table, this.config);
  11. }
  12. }
  13. class PgJsonb extends PgColumn {
  14. static [entityKind] = "PgJsonb";
  15. constructor(table, config) {
  16. super(table, config);
  17. }
  18. getSQLType() {
  19. return "jsonb";
  20. }
  21. mapToDriverValue(value) {
  22. return JSON.stringify(value);
  23. }
  24. mapFromDriverValue(value) {
  25. if (typeof value === "string") {
  26. try {
  27. return JSON.parse(value);
  28. } catch {
  29. return value;
  30. }
  31. }
  32. return value;
  33. }
  34. }
  35. function jsonb(name) {
  36. return new PgJsonbBuilder(name ?? "");
  37. }
  38. export {
  39. PgJsonb,
  40. PgJsonbBuilder,
  41. jsonb
  42. };
  43. //# sourceMappingURL=jsonb.js.map