numeric.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { PgColumn, PgColumnBuilder } from "./common.js";
  4. class PgNumericBuilder extends PgColumnBuilder {
  5. static [entityKind] = "PgNumericBuilder";
  6. constructor(name, precision, scale) {
  7. super(name, "string", "PgNumeric");
  8. this.config.precision = precision;
  9. this.config.scale = scale;
  10. }
  11. /** @internal */
  12. build(table) {
  13. return new PgNumeric(table, this.config);
  14. }
  15. }
  16. class PgNumeric extends PgColumn {
  17. static [entityKind] = "PgNumeric";
  18. precision;
  19. scale;
  20. constructor(table, config) {
  21. super(table, config);
  22. this.precision = config.precision;
  23. this.scale = config.scale;
  24. }
  25. mapFromDriverValue(value) {
  26. if (typeof value === "string") return value;
  27. return String(value);
  28. }
  29. getSQLType() {
  30. if (this.precision !== void 0 && this.scale !== void 0) {
  31. return `numeric(${this.precision}, ${this.scale})`;
  32. } else if (this.precision === void 0) {
  33. return "numeric";
  34. } else {
  35. return `numeric(${this.precision})`;
  36. }
  37. }
  38. }
  39. class PgNumericNumberBuilder extends PgColumnBuilder {
  40. static [entityKind] = "PgNumericNumberBuilder";
  41. constructor(name, precision, scale) {
  42. super(name, "number", "PgNumericNumber");
  43. this.config.precision = precision;
  44. this.config.scale = scale;
  45. }
  46. /** @internal */
  47. build(table) {
  48. return new PgNumericNumber(
  49. table,
  50. this.config
  51. );
  52. }
  53. }
  54. class PgNumericNumber extends PgColumn {
  55. static [entityKind] = "PgNumericNumber";
  56. precision;
  57. scale;
  58. constructor(table, config) {
  59. super(table, config);
  60. this.precision = config.precision;
  61. this.scale = config.scale;
  62. }
  63. mapFromDriverValue(value) {
  64. if (typeof value === "number") return value;
  65. return Number(value);
  66. }
  67. mapToDriverValue = String;
  68. getSQLType() {
  69. if (this.precision !== void 0 && this.scale !== void 0) {
  70. return `numeric(${this.precision}, ${this.scale})`;
  71. } else if (this.precision === void 0) {
  72. return "numeric";
  73. } else {
  74. return `numeric(${this.precision})`;
  75. }
  76. }
  77. }
  78. class PgNumericBigIntBuilder extends PgColumnBuilder {
  79. static [entityKind] = "PgNumericBigIntBuilder";
  80. constructor(name, precision, scale) {
  81. super(name, "bigint", "PgNumericBigInt");
  82. this.config.precision = precision;
  83. this.config.scale = scale;
  84. }
  85. /** @internal */
  86. build(table) {
  87. return new PgNumericBigInt(
  88. table,
  89. this.config
  90. );
  91. }
  92. }
  93. class PgNumericBigInt extends PgColumn {
  94. static [entityKind] = "PgNumericBigInt";
  95. precision;
  96. scale;
  97. constructor(table, config) {
  98. super(table, config);
  99. this.precision = config.precision;
  100. this.scale = config.scale;
  101. }
  102. mapFromDriverValue = BigInt;
  103. mapToDriverValue = String;
  104. getSQLType() {
  105. if (this.precision !== void 0 && this.scale !== void 0) {
  106. return `numeric(${this.precision}, ${this.scale})`;
  107. } else if (this.precision === void 0) {
  108. return "numeric";
  109. } else {
  110. return `numeric(${this.precision})`;
  111. }
  112. }
  113. }
  114. function numeric(a, b) {
  115. const { name, config } = getColumnNameAndConfig(a, b);
  116. const mode = config?.mode;
  117. return mode === "number" ? new PgNumericNumberBuilder(name, config?.precision, config?.scale) : mode === "bigint" ? new PgNumericBigIntBuilder(name, config?.precision, config?.scale) : new PgNumericBuilder(name, config?.precision, config?.scale);
  118. }
  119. const decimal = numeric;
  120. export {
  121. PgNumeric,
  122. PgNumericBigInt,
  123. PgNumericBigIntBuilder,
  124. PgNumericBuilder,
  125. PgNumericNumber,
  126. PgNumericNumberBuilder,
  127. decimal,
  128. numeric
  129. };
  130. //# sourceMappingURL=numeric.js.map