enum.cjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 enum_exports = {};
  20. __export(enum_exports, {
  21. PgEnumColumn: () => PgEnumColumn,
  22. PgEnumColumnBuilder: () => PgEnumColumnBuilder,
  23. PgEnumObjectColumn: () => PgEnumObjectColumn,
  24. PgEnumObjectColumnBuilder: () => PgEnumObjectColumnBuilder,
  25. isPgEnum: () => isPgEnum,
  26. pgEnum: () => pgEnum,
  27. pgEnumObjectWithSchema: () => pgEnumObjectWithSchema,
  28. pgEnumWithSchema: () => pgEnumWithSchema
  29. });
  30. module.exports = __toCommonJS(enum_exports);
  31. var import_entity = require("../../entity.cjs");
  32. var import_common = require("./common.cjs");
  33. class PgEnumObjectColumnBuilder extends import_common.PgColumnBuilder {
  34. static [import_entity.entityKind] = "PgEnumObjectColumnBuilder";
  35. constructor(name, enumInstance) {
  36. super(name, "string", "PgEnumObjectColumn");
  37. this.config.enum = enumInstance;
  38. }
  39. /** @internal */
  40. build(table) {
  41. return new PgEnumObjectColumn(
  42. table,
  43. this.config
  44. );
  45. }
  46. }
  47. class PgEnumObjectColumn extends import_common.PgColumn {
  48. static [import_entity.entityKind] = "PgEnumObjectColumn";
  49. 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. const isPgEnumSym = Symbol.for("drizzle:isPgEnum");
  60. function isPgEnum(obj) {
  61. return !!obj && typeof obj === "function" && isPgEnumSym in obj && obj[isPgEnumSym] === true;
  62. }
  63. class PgEnumColumnBuilder extends import_common.PgColumnBuilder {
  64. static [import_entity.entityKind] = "PgEnumColumnBuilder";
  65. constructor(name, enumInstance) {
  66. super(name, "string", "PgEnumColumn");
  67. this.config.enum = enumInstance;
  68. }
  69. /** @internal */
  70. build(table) {
  71. return new PgEnumColumn(
  72. table,
  73. this.config
  74. );
  75. }
  76. }
  77. class PgEnumColumn extends import_common.PgColumn {
  78. static [import_entity.entityKind] = "PgEnumColumn";
  79. enum = this.config.enum;
  80. enumValues = this.config.enum.enumValues;
  81. constructor(table, config) {
  82. super(table, config);
  83. this.enum = config.enum;
  84. }
  85. getSQLType() {
  86. return this.enum.enumName;
  87. }
  88. }
  89. function pgEnum(enumName, input) {
  90. return Array.isArray(input) ? pgEnumWithSchema(enumName, [...input], void 0) : pgEnumObjectWithSchema(enumName, input, void 0);
  91. }
  92. function pgEnumWithSchema(enumName, values, schema) {
  93. const enumInstance = Object.assign(
  94. (name) => new PgEnumColumnBuilder(name ?? "", enumInstance),
  95. {
  96. enumName,
  97. enumValues: values,
  98. schema,
  99. [isPgEnumSym]: true
  100. }
  101. );
  102. return enumInstance;
  103. }
  104. function pgEnumObjectWithSchema(enumName, values, schema) {
  105. const enumInstance = Object.assign(
  106. (name) => new PgEnumObjectColumnBuilder(name ?? "", enumInstance),
  107. {
  108. enumName,
  109. enumValues: Object.values(values),
  110. schema,
  111. [isPgEnumSym]: true
  112. }
  113. );
  114. return enumInstance;
  115. }
  116. // Annotate the CommonJS export names for ESM import in node:
  117. 0 && (module.exports = {
  118. PgEnumColumn,
  119. PgEnumColumnBuilder,
  120. PgEnumObjectColumn,
  121. PgEnumObjectColumnBuilder,
  122. isPgEnum,
  123. pgEnum,
  124. pgEnumObjectWithSchema,
  125. pgEnumWithSchema
  126. });
  127. //# sourceMappingURL=enum.cjs.map