Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 131x 57x 131x 57x 131x 131x 131x 5x 131x 2x 131x 3x 131x 131x 7x 7x | import type { IndexOptions } from '../ddl/ddl-generator.ts';
import type { AttributeOptions, UniqueOptions } from '../model/attribute-options.ts';
import type { DataTypeInput } from '../data-types/data-types.ts';
import { decorate } from './decorator-factory.ts';
import type { ModelPropertyDecorator } from './decorator-types.ts';
/** Argument of `@Column`: a data type or column options. */
export type ColumnInput = DataTypeInput | AttributeOptions;
const isOptions = (input: ColumnInput): input is AttributeOptions =>
typeof input === 'object' && 'type' in input && !('sqlType' in input);
/**
* Declares a class field as a model attribute (a column).
*
* The type is not inferred from the TypeScript type (there is no `reflect-metadata`): pass a
* `DataTypes` value or an options object with `type`. Combine with the shortcut decorators.
*
* @param input - A data type (`DataTypes.STRING(64)`) or column options (`{ type, allowNull, … }`).
* @returns An attribute decorator.
* Accepts the TC39 and the legacy `experimentalDecorators` call signature.
*
* @example
* ```ts
* class User extends Model {
* @Column({ type: DataTypes.STRING, allowNull: false })
* name!: string;
* }
* ```
*/
export const Column = (input: ColumnInput): ModelPropertyDecorator =>
decorate.column(isOptions(input) ? input : { type: input });
/**
* Makes the attribute part of the primary key (`primaryKey: true`).
*
* @example
* ```ts
* class User extends Model {
* @PrimaryKey
* @Column(DataTypes.INTEGER)
* id!: number;
* }
* ```
*/
export const PrimaryKey: ModelPropertyDecorator = decorate.column({ primaryKey: true });
/**
* Generates the attribute from a sequence (`autoIncrement: true`).
*
* @example
* ```ts
* class User extends Model {
* @PrimaryKey
* @AutoIncrement
* @Column(DataTypes.INTEGER)
* id!: number;
* }
* ```
*/
export const AutoIncrement: ModelPropertyDecorator = decorate.column({ autoIncrement: true });
/**
* Sets `allowNull`. Usable bare (`@AllowNull`, meaning `true`) or called.
*
* @param allowNull - Whether `NULL` is allowed.
* @defaultValue true
* @returns An attribute decorator.
* @example
* ```ts
* class User extends Model {
* @AllowNull(false)
* @Column(DataTypes.TEXT)
* name!: string;
* }
* ```
*/
export const AllowNull = decorate.optionalProperty<boolean, ModelPropertyDecorator>(
(allowNull = true) => decorate.column({ allowNull }),
);
/**
* Sets the default value of the attribute (`defaultValue`).
*
* @param value - A value, an expression, or `DataTypes.NOW` / `DataTypes.UUIDV4`.
* @returns An attribute decorator.
* @example
* ```ts
* class User extends Model {
* @Default(false)
* @Column(DataTypes.BOOLEAN)
* active!: boolean;
* }
* ```
*/
export const Default = (value: unknown): ModelPropertyDecorator =>
decorate.column({ defaultValue: value });
/**
* Adds a unique constraint (`unique`). Usable bare or with a constraint name or options.
*
* @param unique - Constraint name or `{ name, msg, nullsNotDistinct }`. Attributes sharing a name form a composite key.
* @returns An attribute decorator.
* @example
* ```ts
* class User extends Model {
* @Unique
* @Column(DataTypes.STRING)
* email!: string;
* }
* ```
*/
export const Unique = decorate.optionalProperty<string | UniqueOptions, ModelPropertyDecorator>(
(unique = true as never) => decorate.column({ unique }),
);
/**
* Sets the column comment (`comment`).
*
* @param text - Comment text.
* @returns An attribute decorator.
* @example
* ```ts
* class User extends Model {
* @Comment('Login name')
* @Column(DataTypes.STRING)
* login!: string;
* }
* ```
*/
export const Comment = (text: string): ModelPropertyDecorator => decorate.column({ comment: text });
/**
* Adds the attribute to an index of the table (`indexes`). Usable bare, with an index name or
* with index options; attributes sharing a name form one composite index.
*
* Hidden from the generated API reference on purpose: VitePress treats a page named `Index.md` as a
* directory index (case-insensitive), so it is documented in the Decorators guide instead.
*
* @param index - Index name or index options (without `fields`).
* @returns An attribute decorator.
* @hidden
* @example
* ```ts
* class User extends Model {
* @Index({ name: 'users_name_idx', using: 'btree' })
* @Column(DataTypes.STRING)
* name!: string;
* }
* ```
*/
export const Index = decorate.optionalProperty<
string | Omit<IndexOptions, 'fields'>,
ModelPropertyDecorator
>((index = {}) =>
decorate.forProperty((meta, key) => {
meta.indexes.push({ key, options: typeof index === 'string' ? { name: index } : index });
}),
);
|