timestamp.cjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var timestamp_exports = {};
  20. __export(timestamp_exports, {
  21. PgTimestamp: () => PgTimestamp,
  22. PgTimestampBuilder: () => PgTimestampBuilder,
  23. PgTimestampString: () => PgTimestampString,
  24. PgTimestampStringBuilder: () => PgTimestampStringBuilder,
  25. timestamp: () => timestamp
  26. });
  27. module.exports = __toCommonJS(timestamp_exports);
  28. var import_entity = require("../../entity.cjs");
  29. var import_utils = require("../../utils.cjs");
  30. var import_common = require("./common.cjs");
  31. var import_date_common = require("./date.common.cjs");
  32. class PgTimestampBuilder extends import_date_common.PgDateColumnBaseBuilder {
  33. static [import_entity.entityKind] = "PgTimestampBuilder";
  34. constructor(name, withTimezone, precision) {
  35. super(name, "date", "PgTimestamp");
  36. this.config.withTimezone = withTimezone;
  37. this.config.precision = precision;
  38. }
  39. /** @internal */
  40. build(table) {
  41. return new PgTimestamp(table, this.config);
  42. }
  43. }
  44. class PgTimestamp extends import_common.PgColumn {
  45. static [import_entity.entityKind] = "PgTimestamp";
  46. withTimezone;
  47. precision;
  48. constructor(table, config) {
  49. super(table, config);
  50. this.withTimezone = config.withTimezone;
  51. this.precision = config.precision;
  52. }
  53. getSQLType() {
  54. const precision = this.precision === void 0 ? "" : ` (${this.precision})`;
  55. return `timestamp${precision}${this.withTimezone ? " with time zone" : ""}`;
  56. }
  57. mapFromDriverValue(value) {
  58. if (typeof value === "string") return new Date(this.withTimezone ? value : value + "+0000");
  59. return value;
  60. }
  61. mapToDriverValue = (value) => {
  62. return value.toISOString();
  63. };
  64. }
  65. class PgTimestampStringBuilder extends import_date_common.PgDateColumnBaseBuilder {
  66. static [import_entity.entityKind] = "PgTimestampStringBuilder";
  67. constructor(name, withTimezone, precision) {
  68. super(name, "string", "PgTimestampString");
  69. this.config.withTimezone = withTimezone;
  70. this.config.precision = precision;
  71. }
  72. /** @internal */
  73. build(table) {
  74. return new PgTimestampString(
  75. table,
  76. this.config
  77. );
  78. }
  79. }
  80. class PgTimestampString extends import_common.PgColumn {
  81. static [import_entity.entityKind] = "PgTimestampString";
  82. withTimezone;
  83. precision;
  84. constructor(table, config) {
  85. super(table, config);
  86. this.withTimezone = config.withTimezone;
  87. this.precision = config.precision;
  88. }
  89. getSQLType() {
  90. const precision = this.precision === void 0 ? "" : `(${this.precision})`;
  91. return `timestamp${precision}${this.withTimezone ? " with time zone" : ""}`;
  92. }
  93. mapFromDriverValue(value) {
  94. if (typeof value === "string") return value;
  95. const shortened = value.toISOString().slice(0, -1).replace("T", " ");
  96. if (this.withTimezone) {
  97. const offset = value.getTimezoneOffset();
  98. const sign = offset <= 0 ? "+" : "-";
  99. return `${shortened}${sign}${Math.floor(Math.abs(offset) / 60).toString().padStart(2, "0")}`;
  100. }
  101. return shortened;
  102. }
  103. }
  104. function timestamp(a, b = {}) {
  105. const { name, config } = (0, import_utils.getColumnNameAndConfig)(a, b);
  106. if (config?.mode === "string") {
  107. return new PgTimestampStringBuilder(name, config.withTimezone ?? false, config.precision);
  108. }
  109. return new PgTimestampBuilder(name, config?.withTimezone ?? false, config?.precision);
  110. }
  111. // Annotate the CommonJS export names for ESM import in node:
  112. 0 && (module.exports = {
  113. PgTimestamp,
  114. PgTimestampBuilder,
  115. PgTimestampString,
  116. PgTimestampStringBuilder,
  117. timestamp
  118. });
  119. //# sourceMappingURL=timestamp.cjs.map