column-builder.cjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 column_builder_exports = {};
  20. __export(column_builder_exports, {
  21. ColumnBuilder: () => ColumnBuilder
  22. });
  23. module.exports = __toCommonJS(column_builder_exports);
  24. var import_entity = require("./entity.cjs");
  25. class ColumnBuilder {
  26. static [import_entity.entityKind] = "ColumnBuilder";
  27. config;
  28. constructor(name, dataType, columnType) {
  29. this.config = {
  30. name,
  31. keyAsName: name === "",
  32. notNull: false,
  33. default: void 0,
  34. hasDefault: false,
  35. primaryKey: false,
  36. isUnique: false,
  37. uniqueName: void 0,
  38. uniqueType: void 0,
  39. dataType,
  40. columnType,
  41. generated: void 0
  42. };
  43. }
  44. /**
  45. * Changes the data type of the column. Commonly used with `json` columns. Also, useful for branded types.
  46. *
  47. * @example
  48. * ```ts
  49. * const users = pgTable('users', {
  50. * id: integer('id').$type<UserId>().primaryKey(),
  51. * details: json('details').$type<UserDetails>().notNull(),
  52. * });
  53. * ```
  54. */
  55. $type() {
  56. return this;
  57. }
  58. /**
  59. * Adds a `not null` clause to the column definition.
  60. *
  61. * Affects the `select` model of the table - columns *without* `not null` will be nullable on select.
  62. */
  63. notNull() {
  64. this.config.notNull = true;
  65. return this;
  66. }
  67. /**
  68. * Adds a `default <value>` clause to the column definition.
  69. *
  70. * Affects the `insert` model of the table - columns *with* `default` are optional on insert.
  71. *
  72. * If you need to set a dynamic default value, use {@link $defaultFn} instead.
  73. */
  74. default(value) {
  75. this.config.default = value;
  76. this.config.hasDefault = true;
  77. return this;
  78. }
  79. /**
  80. * Adds a dynamic default value to the column.
  81. * The function will be called when the row is inserted, and the returned value will be used as the column value.
  82. *
  83. * **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
  84. */
  85. $defaultFn(fn) {
  86. this.config.defaultFn = fn;
  87. this.config.hasDefault = true;
  88. return this;
  89. }
  90. /**
  91. * Alias for {@link $defaultFn}.
  92. */
  93. $default = this.$defaultFn;
  94. /**
  95. * Adds a dynamic update value to the column.
  96. * The function will be called when the row is updated, and the returned value will be used as the column value if none is provided.
  97. * If no `default` (or `$defaultFn`) value is provided, the function will be called when the row is inserted as well, and the returned value will be used as the column value.
  98. *
  99. * **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
  100. */
  101. $onUpdateFn(fn) {
  102. this.config.onUpdateFn = fn;
  103. this.config.hasDefault = true;
  104. return this;
  105. }
  106. /**
  107. * Alias for {@link $onUpdateFn}.
  108. */
  109. $onUpdate = this.$onUpdateFn;
  110. /**
  111. * Adds a `primary key` clause to the column definition. This implicitly makes the column `not null`.
  112. *
  113. * In SQLite, `integer primary key` implicitly makes the column auto-incrementing.
  114. */
  115. primaryKey() {
  116. this.config.primaryKey = true;
  117. this.config.notNull = true;
  118. return this;
  119. }
  120. /** @internal Sets the name of the column to the key within the table definition if a name was not given. */
  121. setName(name) {
  122. if (this.config.name !== "") return;
  123. this.config.name = name;
  124. }
  125. }
  126. // Annotate the CommonJS export names for ESM import in node:
  127. 0 && (module.exports = {
  128. ColumnBuilder
  129. });
  130. //# sourceMappingURL=column-builder.cjs.map