decimal.js 4.2 KB

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