column-builder.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { entityKind } from "./entity.js";
  2. class ColumnBuilder {
  3. static [entityKind] = "ColumnBuilder";
  4. config;
  5. constructor(name, dataType, columnType) {
  6. this.config = {
  7. name,
  8. keyAsName: name === "",
  9. notNull: false,
  10. default: void 0,
  11. hasDefault: false,
  12. primaryKey: false,
  13. isUnique: false,
  14. uniqueName: void 0,
  15. uniqueType: void 0,
  16. dataType,
  17. columnType,
  18. generated: void 0
  19. };
  20. }
  21. /**
  22. * Changes the data type of the column. Commonly used with `json` columns. Also, useful for branded types.
  23. *
  24. * @example
  25. * ```ts
  26. * const users = pgTable('users', {
  27. * id: integer('id').$type<UserId>().primaryKey(),
  28. * details: json('details').$type<UserDetails>().notNull(),
  29. * });
  30. * ```
  31. */
  32. $type() {
  33. return this;
  34. }
  35. /**
  36. * Adds a `not null` clause to the column definition.
  37. *
  38. * Affects the `select` model of the table - columns *without* `not null` will be nullable on select.
  39. */
  40. notNull() {
  41. this.config.notNull = true;
  42. return this;
  43. }
  44. /**
  45. * Adds a `default <value>` clause to the column definition.
  46. *
  47. * Affects the `insert` model of the table - columns *with* `default` are optional on insert.
  48. *
  49. * If you need to set a dynamic default value, use {@link $defaultFn} instead.
  50. */
  51. default(value) {
  52. this.config.default = value;
  53. this.config.hasDefault = true;
  54. return this;
  55. }
  56. /**
  57. * Adds a dynamic default value to the column.
  58. * The function will be called when the row is inserted, and the returned value will be used as the column value.
  59. *
  60. * **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
  61. */
  62. $defaultFn(fn) {
  63. this.config.defaultFn = fn;
  64. this.config.hasDefault = true;
  65. return this;
  66. }
  67. /**
  68. * Alias for {@link $defaultFn}.
  69. */
  70. $default = this.$defaultFn;
  71. /**
  72. * Adds a dynamic update value to the column.
  73. * 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.
  74. * 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.
  75. *
  76. * **Note:** This value does not affect the `drizzle-kit` behavior, it is only used at runtime in `drizzle-orm`.
  77. */
  78. $onUpdateFn(fn) {
  79. this.config.onUpdateFn = fn;
  80. this.config.hasDefault = true;
  81. return this;
  82. }
  83. /**
  84. * Alias for {@link $onUpdateFn}.
  85. */
  86. $onUpdate = this.$onUpdateFn;
  87. /**
  88. * Adds a `primary key` clause to the column definition. This implicitly makes the column `not null`.
  89. *
  90. * In SQLite, `integer primary key` implicitly makes the column auto-incrementing.
  91. */
  92. primaryKey() {
  93. this.config.primaryKey = true;
  94. this.config.notNull = true;
  95. return this;
  96. }
  97. /** @internal Sets the name of the column to the key within the table definition if a name was not given. */
  98. setName(name) {
  99. if (this.config.name !== "") return;
  100. this.config.name = name;
  101. }
  102. }
  103. export {
  104. ColumnBuilder
  105. };
  106. //# sourceMappingURL=column-builder.js.map