CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
column-builder.js106 linesDownload Raw Back to drizzle-orm
1import { entityKind } from "./entity.js";2class 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 019    };20  }21  /**22   * Changes the data type of the column. Commonly used with `json` columns. Also, useful for branded types.23   *24   * @example25   * ```ts26   * 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}103export {104  ColumnBuilder105};106//# sourceMappingURL=column-builder.js.map