date.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 PgDateBuilder extends PgDateColumnBaseBuilder {
  6. static [entityKind] = "PgDateBuilder";
  7. constructor(name) {
  8. super(name, "date", "PgDate");
  9. }
  10. /** @internal */
  11. build(table) {
  12. return new PgDate(table, this.config);
  13. }
  14. }
  15. class PgDate extends PgColumn {
  16. static [entityKind] = "PgDate";
  17. getSQLType() {
  18. return "date";
  19. }
  20. mapFromDriverValue(value) {
  21. if (typeof value === "string") return new Date(value);
  22. return value;
  23. }
  24. mapToDriverValue(value) {
  25. return value.toISOString();
  26. }
  27. }
  28. class PgDateStringBuilder extends PgDateColumnBaseBuilder {
  29. static [entityKind] = "PgDateStringBuilder";
  30. constructor(name) {
  31. super(name, "string", "PgDateString");
  32. }
  33. /** @internal */
  34. build(table) {
  35. return new PgDateString(
  36. table,
  37. this.config
  38. );
  39. }
  40. }
  41. class PgDateString extends PgColumn {
  42. static [entityKind] = "PgDateString";
  43. getSQLType() {
  44. return "date";
  45. }
  46. mapFromDriverValue(value) {
  47. if (typeof value === "string") return value;
  48. return value.toISOString().slice(0, -14);
  49. }
  50. }
  51. function date(a, b) {
  52. const { name, config } = getColumnNameAndConfig(a, b);
  53. if (config?.mode === "date") {
  54. return new PgDateBuilder(name);
  55. }
  56. return new PgDateStringBuilder(name);
  57. }
  58. export {
  59. PgDate,
  60. PgDateBuilder,
  61. PgDateString,
  62. PgDateStringBuilder,
  63. date
  64. };
  65. //# sourceMappingURL=date.js.map