enum.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { entityKind } from "../../entity.js";
  2. import { PgColumn, PgColumnBuilder } from "./common.js";
  3. class PgEnumObjectColumnBuilder extends PgColumnBuilder {
  4. static [entityKind] = "PgEnumObjectColumnBuilder";
  5. constructor(name, enumInstance) {
  6. super(name, "string", "PgEnumObjectColumn");
  7. this.config.enum = enumInstance;
  8. }
  9. /** @internal */
  10. build(table) {
  11. return new PgEnumObjectColumn(
  12. table,
  13. this.config
  14. );
  15. }
  16. }
  17. class PgEnumObjectColumn extends PgColumn {
  18. static [entityKind] = "PgEnumObjectColumn";
  19. enum;
  20. enumValues = this.config.enum.enumValues;
  21. constructor(table, config) {
  22. super(table, config);
  23. this.enum = config.enum;
  24. }
  25. getSQLType() {
  26. return this.enum.enumName;
  27. }
  28. }
  29. const isPgEnumSym = Symbol.for("drizzle:isPgEnum");
  30. function isPgEnum(obj) {
  31. return !!obj && typeof obj === "function" && isPgEnumSym in obj && obj[isPgEnumSym] === true;
  32. }
  33. class PgEnumColumnBuilder extends PgColumnBuilder {
  34. static [entityKind] = "PgEnumColumnBuilder";
  35. constructor(name, enumInstance) {
  36. super(name, "string", "PgEnumColumn");
  37. this.config.enum = enumInstance;
  38. }
  39. /** @internal */
  40. build(table) {
  41. return new PgEnumColumn(
  42. table,
  43. this.config
  44. );
  45. }
  46. }
  47. class PgEnumColumn extends PgColumn {
  48. static [entityKind] = "PgEnumColumn";
  49. enum = this.config.enum;
  50. enumValues = this.config.enum.enumValues;
  51. constructor(table, config) {
  52. super(table, config);
  53. this.enum = config.enum;
  54. }
  55. getSQLType() {
  56. return this.enum.enumName;
  57. }
  58. }
  59. function pgEnum(enumName, input) {
  60. return Array.isArray(input) ? pgEnumWithSchema(enumName, [...input], void 0) : pgEnumObjectWithSchema(enumName, input, void 0);
  61. }
  62. function pgEnumWithSchema(enumName, values, schema) {
  63. const enumInstance = Object.assign(
  64. (name) => new PgEnumColumnBuilder(name ?? "", enumInstance),
  65. {
  66. enumName,
  67. enumValues: values,
  68. schema,
  69. [isPgEnumSym]: true
  70. }
  71. );
  72. return enumInstance;
  73. }
  74. function pgEnumObjectWithSchema(enumName, values, schema) {
  75. const enumInstance = Object.assign(
  76. (name) => new PgEnumObjectColumnBuilder(name ?? "", enumInstance),
  77. {
  78. enumName,
  79. enumValues: Object.values(values),
  80. schema,
  81. [isPgEnumSym]: true
  82. }
  83. );
  84. return enumInstance;
  85. }
  86. export {
  87. PgEnumColumn,
  88. PgEnumColumnBuilder,
  89. PgEnumObjectColumn,
  90. PgEnumObjectColumnBuilder,
  91. isPgEnum,
  92. pgEnum,
  93. pgEnumObjectWithSchema,
  94. pgEnumWithSchema
  95. };
  96. //# sourceMappingURL=enum.js.map