timestamp.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { PgColumn } from "./common.js";
  4. import { PgDateColumnBaseBuilder } from "./date.common.js";
  5. class PgTimestampBuilder extends PgDateColumnBaseBuilder {
  6. static [entityKind] = "PgTimestampBuilder";
  7. constructor(name, withTimezone, precision) {
  8. super(name, "date", "PgTimestamp");
  9. this.config.withTimezone = withTimezone;
  10. this.config.precision = precision;
  11. }
  12. /** @internal */
  13. build(table) {
  14. return new PgTimestamp(table, this.config);
  15. }
  16. }
  17. class PgTimestamp extends PgColumn {
  18. static [entityKind] = "PgTimestamp";
  19. withTimezone;
  20. precision;
  21. constructor(table, config) {
  22. super(table, config);
  23. this.withTimezone = config.withTimezone;
  24. this.precision = config.precision;
  25. }
  26. getSQLType() {
  27. const precision = this.precision === void 0 ? "" : ` (${this.precision})`;
  28. return `timestamp${precision}${this.withTimezone ? " with time zone" : ""}`;
  29. }
  30. mapFromDriverValue(value) {
  31. if (typeof value === "string") return new Date(this.withTimezone ? value : value + "+0000");
  32. return value;
  33. }
  34. mapToDriverValue = (value) => {
  35. return value.toISOString();
  36. };
  37. }
  38. class PgTimestampStringBuilder extends PgDateColumnBaseBuilder {
  39. static [entityKind] = "PgTimestampStringBuilder";
  40. constructor(name, withTimezone, precision) {
  41. super(name, "string", "PgTimestampString");
  42. this.config.withTimezone = withTimezone;
  43. this.config.precision = precision;
  44. }
  45. /** @internal */
  46. build(table) {
  47. return new PgTimestampString(
  48. table,
  49. this.config
  50. );
  51. }
  52. }
  53. class PgTimestampString extends PgColumn {
  54. static [entityKind] = "PgTimestampString";
  55. withTimezone;
  56. precision;
  57. constructor(table, config) {
  58. super(table, config);
  59. this.withTimezone = config.withTimezone;
  60. this.precision = config.precision;
  61. }
  62. getSQLType() {
  63. const precision = this.precision === void 0 ? "" : `(${this.precision})`;
  64. return `timestamp${precision}${this.withTimezone ? " with time zone" : ""}`;
  65. }
  66. mapFromDriverValue(value) {
  67. if (typeof value === "string") return value;
  68. const shortened = value.toISOString().slice(0, -1).replace("T", " ");
  69. if (this.withTimezone) {
  70. const offset = value.getTimezoneOffset();
  71. const sign = offset <= 0 ? "+" : "-";
  72. return `${shortened}${sign}${Math.floor(Math.abs(offset) / 60).toString().padStart(2, "0")}`;
  73. }
  74. return shortened;
  75. }
  76. }
  77. function timestamp(a, b = {}) {
  78. const { name, config } = getColumnNameAndConfig(a, b);
  79. if (config?.mode === "string") {
  80. return new PgTimestampStringBuilder(name, config.withTimezone ?? false, config.precision);
  81. }
  82. return new PgTimestampBuilder(name, config?.withTimezone ?? false, config?.precision);
  83. }
  84. export {
  85. PgTimestamp,
  86. PgTimestampBuilder,
  87. PgTimestampString,
  88. PgTimestampStringBuilder,
  89. timestamp
  90. };
  91. //# sourceMappingURL=timestamp.js.map