Skip to main content

Class: Attribute

Defined in: packages/client/src/core/attribute.ts:31

Represents a single attribute (field) within a Class schema.

Attributes define the structure of documents, including their type, validation rules (via Zod), and configuration options like mandatory, default values, primary keys, and foreign key references.

Use the static factory method Attribute.create to instantiate attributes with proper validation and persistence.

Example

// Create a string attribute
const titleAttr = await Attribute.create(taskClass, 'title', 'string', 'Task Title', {
mandatory: true,
maxLength: 200
});

// Create a foreign key reference
const assigneeAttr = await Attribute.create(taskClass, 'assigneeId', 'foreign_key', 'Assigned User', {
targetClass: 'User'
});

Extends

  • Attribute

Constructors

Constructor

new Attribute(
classObj,
name,
type,
description?,
config?): Attribute;

Defined in: packages/client/src/core/attribute.ts:55

Creates a new Attribute instance. For most use cases, prefer using Attribute.create which also persists the attribute.

Parameters

ParameterTypeDefault valueDescription
classObjClassnullThe parent Class for this attribute
namestringundefinedThe attribute name
type| "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference"undefinedThe attribute type (e.g., 'string', 'integer', 'boolean', 'enum')
description?stringundefinedOptional description
config?| AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfigundefinedType-specific configuration options

Returns

Attribute

Overrides

Attribute_.constructor

Properties

PropertyTypeDescriptionOverridesDefined in
classClassReference to the parent Class that owns this attribute.Attribute_.classpackages/client/src/core/attribute.ts:41
defaultValue?anyDefault value to use when the field is not provided.Attribute_.defaultValuepackages/client/src/core/attribute.ts:43
description?stringOptional description explaining the attribute's purpose.Attribute_.descriptionpackages/client/src/core/attribute.ts:35
fieldanyZod schema for runtime validation of this field.Attribute_.fieldpackages/client/src/core/attribute.ts:39
modelAttributeModelThe underlying AttributeModel containing type and configuration.Attribute_.modelpackages/client/src/core/attribute.ts:37
namestringThe attribute name used as the document field key.Attribute_.namepackages/client/src/core/attribute.ts:33

Methods

checkTypeValidity()

checkTypeValidity(type): boolean;

Defined in: packages/client/src/core/attribute.ts:441

Parameters

ParameterType
typestring

Returns

boolean

Overrides

Attribute_.checkTypeValidity

ensureReferenceConfigIsValid()

ensureReferenceConfigIsValid(): Promise<void>;

Defined in: packages/client/src/core/attribute.ts:74

Validates that reference-type attributes have proper domain configuration. Called automatically during Attribute.create.

Returns

Promise<void>

Throws

Error if the reference configuration is invalid

Overrides

Attribute_.ensureReferenceConfigIsValid

getClass()

getClass(): Class;

Defined in: packages/client/src/core/attribute.ts:360

Returns the parent Class for this attribute.

Returns

Class

The Class instance

Throws

Error if the attribute has no parent class

Overrides

Attribute_.getClass

getEmpty()

getEmpty(): object;

Defined in: packages/client/src/core/attribute.ts:424

Returns an empty/default value for this attribute. Uses the Zod schema's default value if configured.

Returns

object

Object with attribute name as key and default/null value

Overrides

Attribute_.getEmpty

getModel()

getModel(): AttributeModel;

Defined in: packages/client/src/core/attribute.ts:351

Returns the AttributeModel for this attribute.

Returns

AttributeModel

The underlying AttributeModel

Overrides

Attribute_.getModel

getName()

getName(): string;

Defined in: packages/client/src/core/attribute.ts:437

Returns the attribute name.

Returns

string

The attribute name string

Overrides

Attribute_.getName

getType()

getType(type):
| "string"
| "boolean"
| "object"
| "integer"
| "date"
| "decimal"
| "foreign_key"
| "enum"
| "reference";

Defined in: packages/client/src/core/attribute.ts:412

Parameters

ParameterType
type| "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference"

Returns

| "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference"

Overrides

Attribute_.getType

getTypeConf()

getTypeConf(type, config):
| AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig;

Defined in: packages/client/src/core/attribute.ts:454

Parameters

ParameterType
type| "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference"
config| AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig

Returns

| AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig

Overrides

Attribute_.getTypeConf

isMandatory()

isMandatory(): boolean;

Defined in: packages/client/src/core/attribute.ts:343

Checks if this attribute is mandatory (required).

Returns

boolean

true if the attribute is mandatory

Overrides

Attribute_.isMandatory

isPrimaryKey()

isPrimaryKey(): boolean;

Defined in: packages/client/src/core/attribute.ts:334

Checks if this attribute is marked as a primary key.

Returns

boolean

true if this is a primary key attribute

Overrides

Attribute_.isPrimaryKey

setField()

setField(): void;

Defined in: packages/client/src/core/attribute.ts:135

Builds the Zod validation schema based on attribute type and configuration. Called automatically during construction.

Returns

void

Overrides

Attribute_.setField

setModel()

setModel(model): void;

Defined in: packages/client/src/core/attribute.ts:404

Parameters

ParameterType
modelAttributeModel

Returns

void

Overrides

Attribute_.setModel

validate()

validate(data): Promise<ZodSafeParseResult<any>>;

Defined in: packages/client/src/core/attribute.ts:381

Validates a value against this attribute's Zod schema.

Parameters

ParameterTypeDescription
dataanyThe value to validate

Returns

Promise<ZodSafeParseResult<any>>

Zod safe parse result with success status and data/error

Example

const result = await priceAttr.validate(19.99);
if (result.success) {
console.log('Valid:', result.data);
} else {
console.log('Invalid:', result.error);
}

Overrides

Attribute_.validate

build()

static build(attributeObj): Promise<Attribute>;

Defined in: packages/client/src/core/attribute.ts:393

Adds an attribute to its parent class and persists to the database. Used internally by Attribute.create.

Parameters

ParameterTypeDescription
attributeObjAttributeThe Attribute instance to build

Returns

Promise<Attribute>

The Attribute instance

Throws

Error if the class has no stack connection

Overrides

Attribute_.build

create()

static create(
classObj,
name,
type,
description?,
config?): Promise<Attribute>;

Defined in: packages/client/src/core/attribute.ts:317

Creates a new Attribute and persists it to the parent Class. This is the primary factory method for creating attributes.

Parameters

ParameterTypeDescription
classObjClassThe parent Class to add the attribute to
namestringThe attribute name
type| "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference"The attribute type
description?stringOptional description
config?| AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfigType-specific configuration

Returns

Promise<Attribute>

The created Attribute instance

Example

const priceAttr = await Attribute.create(productClass, 'price', 'decimal', 'Product price', {
min: 0,
precision: 2,
mandatory: true
});

Overrides

Attribute_.create